[refactor] Add helpful constructors to PaintCmd

This commit is contained in:
Emil Ernerfeldt 2020-11-02 17:35:16 +01:00
parent ca96172552
commit 63c0379082
7 changed files with 48 additions and 36 deletions

View file

@ -122,12 +122,7 @@ pub fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
*p = rect.center() + v; *p = rect.center() + v;
} }
ui.painter().add(PaintCmd::Path { ui.painter().add(PaintCmd::closed_line(points, stroke));
points,
closed: true,
fill: Default::default(),
stroke,
});
} }
/// A header which can be collapsed/expanded, revealing a contained `Ui` region. /// A header which can be collapsed/expanded, revealing a contained `Ui` region.

View file

@ -116,10 +116,8 @@ fn paint_icon(painter: &Painter, rect: Rect, visuals: &WidgetVisuals) {
rect.center(), rect.center(),
vec2(rect.width() * 0.7, rect.height() * 0.45), vec2(rect.width() * 0.7, rect.height() * 0.45),
); );
painter.add(PaintCmd::Path { painter.add(PaintCmd::closed_line(
points: vec![rect.left_top(), rect.right_top(), rect.center_bottom()], vec![rect.left_top(), rect.right_top(), rect.center_bottom()],
closed: true, visuals.fg_stroke,
fill: Default::default(), ));
stroke: visuals.fg_stroke,
});
} }

View file

@ -583,12 +583,7 @@ fn paint_frame_interaction(
points.push(pos2(max.x, min.y + cr)); points.push(pos2(max.x, min.y + cr));
points.push(pos2(max.x, max.y - cr)); points.push(pos2(max.x, max.y - cr));
} }
ui.painter().add(PaintCmd::Path { ui.painter().add(PaintCmd::line(points, visuals.bg_stroke));
points,
closed: false,
fill: Default::default(),
stroke: visuals.bg_stroke,
});
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View file

@ -318,12 +318,7 @@ impl Painting {
for line in &self.lines { for line in &self.lines {
if line.len() >= 2 { if line.len() >= 2 {
let points: Vec<Pos2> = line.iter().map(|p| rect.min + *p).collect(); let points: Vec<Pos2> = line.iter().map(|p| rect.min + *p).collect();
painter.add(paint::PaintCmd::Path { painter.add(paint::PaintCmd::line(points, self.stroke));
points,
closed: false,
stroke: self.stroke,
fill: Default::default(),
});
} }
} }
} }

View file

@ -46,6 +46,7 @@ pub enum PaintCmd {
Triangles(Triangles), Triangles(Triangles),
} }
/// ## Constructors
impl PaintCmd { impl PaintCmd {
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self { pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self {
Self::LineSegment { Self::LineSegment {
@ -54,6 +55,33 @@ impl PaintCmd {
} }
} }
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: false,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: true,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn polygon(points: Vec<Pos2>, fill: impl Into<Srgba>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: true,
fill: fill.into(),
stroke: stroke.into(),
}
}
pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into<Srgba>) -> Self { pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into<Srgba>) -> Self {
Self::Circle { Self::Circle {
center, center,
@ -108,7 +136,10 @@ impl PaintCmd {
color, color,
} }
} }
}
/// ## Operations
impl PaintCmd {
pub fn triangles(triangles: Triangles) -> Self { pub fn triangles(triangles: Triangles) -> Self {
debug_assert!(triangles.is_valid()); debug_assert!(triangles.is_valid());
Self::Triangles(triangles) Self::Triangles(triangles)

View file

@ -115,16 +115,15 @@ fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Srgba
let x = lerp(rect.left()..=rect.right(), *value); let x = lerp(rect.left()..=rect.right(), *value);
let r = rect.height() / 4.0; let r = rect.height() / 4.0;
let picked_color = color_at(*value); let picked_color = color_at(*value);
ui.painter().add(PaintCmd::Path { ui.painter().add(PaintCmd::polygon(
points: vec![ vec![
pos2(x - r, rect.bottom()), pos2(x - r, rect.bottom()),
pos2(x + r, rect.bottom()), pos2(x + r, rect.bottom()),
pos2(x, rect.center().y), pos2(x, rect.center().y),
], ],
closed: true, picked_color,
fill: picked_color, Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)),
stroke: Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)), ));
});
} }
response response

View file

@ -411,16 +411,15 @@ impl<'a> Widget for Checkbox<'a> {
}); });
if *checked { if *checked {
ui.painter().add(PaintCmd::Path { // Check mark:
points: vec![ ui.painter().add(PaintCmd::line(
vec![
pos2(small_icon_rect.left(), small_icon_rect.center().y), pos2(small_icon_rect.left(), small_icon_rect.center().y),
pos2(small_icon_rect.center().x, small_icon_rect.bottom()), pos2(small_icon_rect.center().x, small_icon_rect.bottom()),
pos2(small_icon_rect.right(), small_icon_rect.top()), pos2(small_icon_rect.right(), small_icon_rect.top()),
], ],
closed: false, visuals.fg_stroke,
fill: Default::default(), ));
stroke: visuals.fg_stroke,
});
} }
let text_color = text_color let text_color = text_color