[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;
}
ui.painter().add(PaintCmd::Path {
points,
closed: true,
fill: Default::default(),
stroke,
});
ui.painter().add(PaintCmd::closed_line(points, stroke));
}
/// 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(),
vec2(rect.width() * 0.7, rect.height() * 0.45),
);
painter.add(PaintCmd::Path {
points: vec![rect.left_top(), rect.right_top(), rect.center_bottom()],
closed: true,
fill: Default::default(),
stroke: visuals.fg_stroke,
});
painter.add(PaintCmd::closed_line(
vec![rect.left_top(), rect.right_top(), rect.center_bottom()],
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, max.y - cr));
}
ui.painter().add(PaintCmd::Path {
points,
closed: false,
fill: Default::default(),
stroke: visuals.bg_stroke,
});
ui.painter().add(PaintCmd::line(points, visuals.bg_stroke));
}
// ----------------------------------------------------------------------------

View file

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

View file

@ -46,6 +46,7 @@ pub enum PaintCmd {
Triangles(Triangles),
}
/// ## Constructors
impl PaintCmd {
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self {
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 {
Self::Circle {
center,
@ -108,7 +136,10 @@ impl PaintCmd {
color,
}
}
}
/// ## Operations
impl PaintCmd {
pub fn triangles(triangles: Triangles) -> Self {
debug_assert!(triangles.is_valid());
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 r = rect.height() / 4.0;
let picked_color = color_at(*value);
ui.painter().add(PaintCmd::Path {
points: vec![
ui.painter().add(PaintCmd::polygon(
vec![
pos2(x - r, rect.bottom()),
pos2(x + r, rect.bottom()),
pos2(x, rect.center().y),
],
closed: true,
fill: picked_color,
stroke: Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)),
});
picked_color,
Stroke::new(visuals.fg_stroke.width, contrast_color(picked_color)),
));
}
response

View file

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