[refactor] make use of LineStyle in more places

This commit is contained in:
Emil Ernerfeldt 2020-05-23 14:21:08 +02:00
parent d214574c97
commit a8e5676a09
3 changed files with 20 additions and 28 deletions

View file

@ -16,8 +16,7 @@ pub enum PaintCmd {
}, },
LineSegment { LineSegment {
points: [Pos2; 2], points: [Pos2; 2],
color: Color, style: LineStyle,
width: f32,
}, },
Path { Path {
path: Path, path: Path,
@ -46,8 +45,7 @@ impl PaintCmd {
pub fn line_segment(points: [Pos2; 2], color: Color, width: f32) -> Self { pub fn line_segment(points: [Pos2; 2], color: Color, width: f32) -> Self {
Self::LineSegment { Self::LineSegment {
points, points,
color, style: LineStyle::new(width, color),
width,
} }
} }
} }

View file

@ -368,10 +368,9 @@ pub fn paint_path_outline(
options: PaintOptions, options: PaintOptions,
path_type: PathType, path_type: PathType,
path: &[PathPoint], path: &[PathPoint],
color: Color, style: &LineStyle,
width: f32,
) { ) {
if color == color::TRANSPARENT { if style.color == color::TRANSPARENT {
return; return;
} }
@ -385,10 +384,10 @@ pub fn paint_path_outline(
}; };
if options.anti_alias { if options.anti_alias {
let color_inner = color; let color_inner = style.color;
let color_outer = color::TRANSPARENT; let color_outer = color::TRANSPARENT;
let thin_line = width <= options.aa_size; let thin_line = style.width <= options.aa_size;
if thin_line { if thin_line {
/* /*
We paint the line using three edges: outer, inner, outer. We paint the line using three edges: outer, inner, outer.
@ -398,7 +397,7 @@ pub fn paint_path_outline(
*/ */
// Fade out as it gets thinner: // Fade out as it gets thinner:
let color_inner = mul_color(color_inner, width / options.aa_size); let color_inner = mul_color(color_inner, style.width / options.aa_size);
if color_inner == color::TRANSPARENT { if color_inner == color::TRANSPARENT {
return; return;
} }
@ -448,8 +447,8 @@ pub fn paint_path_outline(
let mut i0 = n - 1; let mut i0 = n - 1;
for i1 in 0..n { for i1 in 0..n {
let connect_with_previous = path_type == PathType::Closed || i1 > 0; let connect_with_previous = path_type == PathType::Closed || i1 > 0;
let inner_rad = 0.5 * (width - options.aa_size); let inner_rad = 0.5 * (style.width - options.aa_size);
let outer_rad = 0.5 * (width + options.aa_size); let outer_rad = 0.5 * (style.width + options.aa_size);
let p1 = &path[i1 as usize]; let p1 = &path[i1 as usize];
let p = p1.pos; let p = p1.pos;
let n = p1.normal; let n = p1.normal;
@ -497,11 +496,11 @@ pub fn paint_path_outline(
); );
} }
let thin_line = width <= options.aa_size; let thin_line = style.width <= options.aa_size;
if thin_line { if thin_line {
// Fade out thin lines rather than making them thinner // Fade out thin lines rather than making them thinner
let radius = options.aa_size / 2.0; let radius = options.aa_size / 2.0;
let color = mul_color(color, width / options.aa_size); let color = mul_color(style.color, style.width / options.aa_size);
if color == color::TRANSPARENT { if color == color::TRANSPARENT {
return; return;
} }
@ -514,14 +513,14 @@ pub fn paint_path_outline(
.push(vert(p.pos - radius * p.normal, color)); .push(vert(p.pos - radius * p.normal, color));
} }
} else { } else {
let radius = width / 2.0; let radius = style.width / 2.0;
for p in path { for p in path {
triangles triangles
.vertices .vertices
.push(vert(p.pos + radius * p.normal, color)); .push(vert(p.pos + radius * p.normal, style.color));
triangles triangles
.vertices .vertices
.push(vert(p.pos - radius * p.normal, color)); .push(vert(p.pos - radius * p.normal, style.color));
} }
} }
} }
@ -563,19 +562,15 @@ pub fn paint_command_into_triangles(
fill_closed_path(out, options, &path.0, fill); fill_closed_path(out, options, &path.0, fill);
} }
if let Some(outline) = outline { if let Some(outline) = outline {
paint_path_outline(out, options, Closed, &path.0, outline.color, outline.width); paint_path_outline(out, options, Closed, &path.0, &outline);
} }
} }
PaintCmd::Triangles(triangles) => { PaintCmd::Triangles(triangles) => {
out.append(&triangles); out.append(&triangles);
} }
PaintCmd::LineSegment { PaintCmd::LineSegment { points, style } => {
points,
color,
width,
} => {
path.add_line_segment(points); path.add_line_segment(points);
paint_path_outline(out, options, Open, &path.0, color, width); paint_path_outline(out, options, Open, &path.0, &style);
} }
PaintCmd::Path { PaintCmd::Path {
path, path,
@ -593,7 +588,7 @@ pub fn paint_command_into_triangles(
} }
if let Some(outline) = outline { if let Some(outline) = outline {
let typ = if closed { Closed } else { Open }; let typ = if closed { Closed } else { Open };
paint_path_outline(out, options, typ, &path.0, outline.color, outline.width); paint_path_outline(out, options, typ, &path.0, &outline);
} }
} }
} }
@ -613,7 +608,7 @@ pub fn paint_command_into_triangles(
fill_closed_path(out, options, &path.0, fill); fill_closed_path(out, options, &path.0, fill);
} }
if let Some(outline) = outline { if let Some(outline) = outline {
paint_path_outline(out, options, Closed, &path.0, outline.color, outline.width); paint_path_outline(out, options, Closed, &path.0, &outline);
} }
} }
PaintCmd::Text { PaintCmd::Text {

View file

@ -486,8 +486,7 @@ impl Widget for Separator {
}; };
ui.add_paint_cmd(PaintCmd::LineSegment { ui.add_paint_cmd(PaintCmd::LineSegment {
points, points,
color: color, style: LineStyle::new(line_width, color),
width: line_width,
}); });
ui.interact_hover(rect) ui.interact_hover(rect)
} }