Skip to content

Commit

Permalink
Change the ArcPieShape stroke to PathStroke
Browse files Browse the repository at this point in the history
  • Loading branch information
varphone committed Apr 28, 2024
1 parent 216e782 commit 3cb39f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl Painter {
radius: f32,
start_angle: f32,
end_angle: f32,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> ShapeIdx {
self.add(Shape::arc(center, radius, start_angle, end_angle, stroke))
}
Expand All @@ -317,7 +317,7 @@ impl Painter {
start_angle: f32,
end_angle: f32,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> ShapeIdx {
self.add(Shape::pie(
center,
Expand Down
12 changes: 6 additions & 6 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Shape {
radius: f32,
start_angle: f32,
end_angle: f32,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self::ArcPie(ArcPieShape::arc(
center,
Expand Down Expand Up @@ -298,7 +298,7 @@ impl Shape {
start_angle: f32,
end_angle: f32,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self::ArcPie(ArcPieShape::pie(
center,
Expand Down Expand Up @@ -559,7 +559,7 @@ pub struct ArcPieShape {
pub end_angle: f32,
pub closed: bool,
pub fill: Color32,
pub stroke: Stroke,
pub stroke: PathStroke,
}

impl ArcPieShape {
Expand All @@ -581,7 +581,7 @@ impl ArcPieShape {
end_angle: f32,
closed: bool,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self {
center,
Expand All @@ -608,7 +608,7 @@ impl ArcPieShape {
radius: f32,
start_angle: f32,
end_angle: f32,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self::new(
center,
Expand Down Expand Up @@ -637,7 +637,7 @@ impl ArcPieShape {
start_angle: f32,
end_angle: f32,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self::new(center, radius, start_angle, end_angle, true, fill, stroke)
}
Expand Down
13 changes: 11 additions & 2 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,17 @@ pub fn adjust_colors(
}) => {
if *closed {
adjust_color(fill);
} else {
adjust_color(&mut stroke.color);
}
match &stroke.color {
color::ColorMode::Solid(mut col) => adjust_color(&mut col),
color::ColorMode::UV(callback) => {
let callback = callback.clone();
stroke.color = color::ColorMode::UV(Arc::new(Box::new(move |rect, pos| {
let mut col = callback(rect, pos);
adjust_color(&mut col);
col
})));
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ impl Tessellator {
}
}
Shape::ArcPie(arc_pie_shape) => {
self.tessellate_arc_pie(&arc_pie_shape, out);
self.tessellate_arc_pie(arc_pie_shape, out);
}
Shape::Circle(circle) => {
self.tessellate_circle(circle, out);
Expand Down Expand Up @@ -1411,7 +1411,7 @@ impl Tessellator {
///
/// * `arc_pie_shape`: the arc or pie to tessellate.
/// * `out`: triangles are appended to this.
pub fn tessellate_arc_pie(&mut self, arc_pie_shape: &ArcPieShape, out: &mut Mesh) {
pub fn tessellate_arc_pie(&mut self, arc_pie_shape: ArcPieShape, out: &mut Mesh) {
let ArcPieShape {
center,
radius,
Expand All @@ -1420,7 +1420,7 @@ impl Tessellator {
closed,
fill,
stroke,
} = *arc_pie_shape;
} = arc_pie_shape;

if radius <= 0.0
|| start_angle == end_angle
Expand All @@ -1440,6 +1440,14 @@ impl Tessellator {

// If the arc is a full circle, we can just use the circle function.
if (end_angle - start_angle).abs() >= std::f32::consts::TAU {
let stroke_color = match stroke.color {
ColorMode::Solid(color) => color,
ColorMode::UV(callback) => {
// We can't use the callback here, so we just use the center of the circle.
callback(Rect::from_center_size(center, Vec2::splat(radius)), center)
}
};
let stroke = Stroke::new(stroke.width, stroke_color);
let circle = CircleShape {
center,
radius,
Expand All @@ -1456,12 +1464,12 @@ impl Tessellator {
.add_pie(center, radius, start_angle, end_angle);
self.scratchpad_path.fill(self.feathering, fill, out);
self.scratchpad_path
.stroke_closed(self.feathering, &PathStroke::from(stroke), out);
.stroke_closed(self.feathering, &stroke, out);
} else {
self.scratchpad_path
.add_arc(center, radius, start_angle, end_angle);
self.scratchpad_path
.stroke_open(self.feathering, &PathStroke::from(stroke), out);
.stroke_open(self.feathering, &stroke, out);
}
}

Expand Down

0 comments on commit 3cb39f1

Please sign in to comment.