mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 13:50:04 -04:00
epaint: Add EllipseShape (#4122)
Adds an Ellipse shape draw-able with the Painter, brings egui closer to the SVG Specification. I've done some optimization towards using less vertices and doing less calculations. ~~Currently the vertices are evenly distributed. It's possible this could be optimized further taking into account the gradient and increasing the concentration of vertices where the change in gradient is larger.~~  
This commit is contained in:
@@ -30,6 +30,9 @@ pub enum Shape {
|
||||
/// Circle with optional outline and fill.
|
||||
Circle(CircleShape),
|
||||
|
||||
/// Ellipse with optional outline and fill.
|
||||
Ellipse(EllipseShape),
|
||||
|
||||
/// A line between two points.
|
||||
LineSegment { points: [Pos2; 2], stroke: Stroke },
|
||||
|
||||
@@ -236,6 +239,16 @@ impl Shape {
|
||||
Self::Circle(CircleShape::stroke(center, radius, stroke))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ellipse_filled(center: Pos2, radius: Vec2, fill_color: impl Into<Color32>) -> Self {
|
||||
Self::Ellipse(EllipseShape::filled(center, radius, fill_color))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ellipse_stroke(center: Pos2, radius: Vec2, stroke: impl Into<Stroke>) -> Self {
|
||||
Self::Ellipse(EllipseShape::stroke(center, radius, stroke))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rect_filled(
|
||||
rect: Rect,
|
||||
@@ -324,6 +337,7 @@ impl Shape {
|
||||
rect
|
||||
}
|
||||
Self::Circle(circle_shape) => circle_shape.visual_bounding_rect(),
|
||||
Self::Ellipse(ellipse_shape) => ellipse_shape.visual_bounding_rect(),
|
||||
Self::LineSegment { points, stroke } => {
|
||||
if stroke.is_empty() {
|
||||
Rect::NOTHING
|
||||
@@ -388,6 +402,11 @@ impl Shape {
|
||||
circle_shape.radius *= transform.scaling;
|
||||
circle_shape.stroke.width *= transform.scaling;
|
||||
}
|
||||
Self::Ellipse(ellipse_shape) => {
|
||||
ellipse_shape.center = transform * ellipse_shape.center;
|
||||
ellipse_shape.radius *= transform.scaling;
|
||||
ellipse_shape.stroke.width *= transform.scaling;
|
||||
}
|
||||
Self::LineSegment { points, stroke } => {
|
||||
for p in points {
|
||||
*p = transform * *p;
|
||||
@@ -497,6 +516,61 @@ impl From<CircleShape> for Shape {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// How to paint an ellipse.
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
pub struct EllipseShape {
|
||||
pub center: Pos2,
|
||||
|
||||
/// Radius is the vector (a, b) where the width of the Ellipse is 2a and the height is 2b
|
||||
pub radius: Vec2,
|
||||
pub fill: Color32,
|
||||
pub stroke: Stroke,
|
||||
}
|
||||
|
||||
impl EllipseShape {
|
||||
#[inline]
|
||||
pub fn filled(center: Pos2, radius: Vec2, fill_color: impl Into<Color32>) -> Self {
|
||||
Self {
|
||||
center,
|
||||
radius,
|
||||
fill: fill_color.into(),
|
||||
stroke: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn stroke(center: Pos2, radius: Vec2, stroke: impl Into<Stroke>) -> Self {
|
||||
Self {
|
||||
center,
|
||||
radius,
|
||||
fill: Default::default(),
|
||||
stroke: stroke.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The visual bounding rectangle (includes stroke width)
|
||||
pub fn visual_bounding_rect(&self) -> Rect {
|
||||
if self.fill == Color32::TRANSPARENT && self.stroke.is_empty() {
|
||||
Rect::NOTHING
|
||||
} else {
|
||||
Rect::from_center_size(
|
||||
self.center,
|
||||
self.radius * 2.0 + Vec2::splat(self.stroke.width),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EllipseShape> for Shape {
|
||||
#[inline(always)]
|
||||
fn from(shape: EllipseShape) -> Self {
|
||||
Self::Ellipse(shape)
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// A path which can be stroked and/or filled (if closed).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
|
||||
Reference in New Issue
Block a user