use egui::epaint::{Mesh, PathShape, PathStroke, TextShape};
use egui::{Align2, Color32, FontId, Painter, Pos2, Rect, Shape, Stroke, StrokeKind, pos2};
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Dash {
Solid,
Dashed(f32, f32),
Dotted(f32),
Marching {
dash: f32,
gap: f32,
offset: f32,
},
}
#[derive(Clone, Debug, PartialEq)]
pub(super) enum Prim {
Line {
points: Vec<Pos2>,
width: f32,
color: Color32,
dash: Dash,
},
Area {
points: Vec<Pos2>,
base_y: f32,
color: Color32,
},
Polygon { points: Vec<Pos2>, color: Color32 },
Quad { points: [Pos2; 4], color: Color32 },
Fan {
center: Pos2,
points: Vec<Pos2>,
color: Color32,
},
Rect {
rect: Rect,
radius: f32,
fill: Color32,
stroke: Option<(f32, Color32)>,
},
Circle {
center: Pos2,
radius: f32,
fill: Color32,
stroke: Option<(f32, Color32)>,
},
Text {
pos: Pos2,
text: String,
size: f32,
color: Color32,
baseline: f32,
angle: f32,
},
}
#[derive(Clone, Debug)]
pub(super) struct Scene {
pub size: egui::Vec2,
pub frame: Rect,
pub under: Vec<Prim>,
pub data: Vec<Prim>,
pub over: Vec<Prim>,
pub drawn: Vec<Option<Vec<usize>>>,
}
pub(super) fn paint(painter: &Painter, clipped: &Painter, scene: Scene) {
let layer = |painter: &Painter, prims: Vec<Prim>| {
let mut shapes = Vec::with_capacity(prims.len());
for prim in prims {
push_shapes(painter, prim, &mut shapes);
}
painter.extend(shapes);
};
layer(painter, scene.under);
layer(clipped, scene.data);
layer(painter, scene.over);
}
fn push_shapes(painter: &Painter, prim: Prim, out: &mut Vec<Shape>) {
match prim {
Prim::Line {
points,
width,
color,
dash,
} => {
let stroke = Stroke::new(width, color);
match dash {
Dash::Solid if points.len() == 2 => out.push(Shape::line_segment([points[0], points[1]], stroke)),
Dash::Solid => out.push(Shape::Path(PathShape::line(points, PathStroke::from(stroke)))),
Dash::Dashed(dash, gap) => out.extend(Shape::dashed_line(&points, stroke, dash, gap)),
Dash::Dotted(spacing) => out.extend(Shape::dotted_line(&points, color, spacing, width * 0.5)),
Dash::Marching { dash, gap, offset } => {
out.extend(marching_line(&points, stroke, dash, gap, offset));
}
}
}
Prim::Area { points, base_y, color } => out.push(Shape::mesh(area_mesh(&points, base_y, color))),
Prim::Polygon { points, color } => out.push(Shape::convex_polygon(points, color, Stroke::NONE)),
Prim::Quad { points, color } => {
let mut mesh = Mesh::default();
for point in points {
mesh.colored_vertex(point, color);
}
mesh.add_triangle(0, 1, 2);
mesh.add_triangle(0, 2, 3);
out.push(Shape::mesh(mesh));
}
Prim::Fan { center, points, color } => {
out.push(Shape::mesh(fan_mesh(center, &points, color)));
}
Prim::Rect {
rect,
radius,
fill,
stroke,
} => {
let stroke = stroke.map_or(Stroke::NONE, |(width, color)| Stroke::new(width, color));
out.push(Shape::rect_filled(rect, radius, fill));
if stroke.width > 0.0 {
out.push(Shape::rect_stroke(rect, radius, stroke, StrokeKind::Inside));
}
}
Prim::Circle {
center,
radius,
fill,
stroke,
} => {
let stroke = stroke.map_or(Stroke::NONE, |(width, color)| Stroke::new(width, color));
out.push(Shape::circle_filled(center, radius, fill));
if stroke.width > 0.0 {
out.push(Shape::circle_stroke(center, radius, stroke));
}
}
Prim::Text {
pos,
text,
size,
color,
angle,
..
} => {
let galley = painter.layout_no_wrap(text, FontId::proportional(size), color);
out.push(Shape::Text(TextShape::new(pos, galley, color).with_angle(angle)));
}
}
}
fn area_mesh(points: &[Pos2], base_y: f32, color: Color32) -> Mesh {
let mut mesh = Mesh::default();
for pair in points.windows(2) {
let index = mesh.vertices.len() as u32;
for position in [pair[0], pos2(pair[0].x, base_y), pair[1], pos2(pair[1].x, base_y)] {
mesh.colored_vertex(position, color);
}
mesh.add_triangle(index, index + 1, index + 2);
mesh.add_triangle(index + 2, index + 1, index + 3);
}
mesh
}
fn fan_mesh(center: Pos2, points: &[Pos2], color: Color32) -> Mesh {
let mut mesh = Mesh::default();
if points.len() < 2 {
return mesh;
}
mesh.colored_vertex(center, color);
for point in points {
mesh.colored_vertex(*point, color);
}
for index in 1..points.len() as u32 {
mesh.add_triangle(0, index, index + 1);
}
mesh
}
fn marching_line(points: &[Pos2], stroke: Stroke, dash: f32, gap: f32, offset: f32) -> Vec<Shape> {
let pattern = (dash + gap).max(0.5);
let mut out = Vec::new();
let mut drawing = offset.rem_euclid(pattern) < dash;
let mut remaining = if drawing {
dash - offset.rem_euclid(pattern)
} else {
pattern - offset.rem_euclid(pattern)
}
.max(0.01);
for pair in points.windows(2) {
if !(pair[0].is_finite() && pair[1].is_finite()) {
continue;
}
let mut start = pair[0];
let mut left = pair[0].distance(pair[1]);
while left > 0.01 {
let step = left.min(remaining);
let direction = (pair[1] - start) / start.distance(pair[1]).max(0.001);
let end = start + direction * step;
if drawing {
out.push(Shape::line_segment([start, end], stroke));
}
start = end;
left -= step;
remaining -= step;
if remaining <= 0.01 {
drawing = !drawing;
remaining = if drawing { dash } else { gap };
}
}
}
out
}
pub(super) struct Measure {
pub ctx: egui::Context,
pub pixels_per_point: f32,
}
impl Measure {
pub fn text(&self, text: &str, size: f32) -> (f32, f32, f32) {
let galley = self
.ctx
.fonts_mut(|fonts| fonts.layout_no_wrap(text.to_owned(), FontId::proportional(size), Color32::WHITE));
let baseline = galley
.rows
.first()
.and_then(|row| row.row.glyphs.first().map(|glyph| row.pos.y + glyph.pos.y))
.unwrap_or(size * 0.9);
(galley.size().x, galley.size().y, baseline)
}
pub fn snap(&self, value: f32) -> f32 {
(value * self.pixels_per_point).round() / self.pixels_per_point
}
pub fn center(&self, value: f32) -> f32 {
((value * self.pixels_per_point - 0.5).round() + 0.5) / self.pixels_per_point
}
pub fn label(&self, at: Pos2, anchor: Align2, text: &str, size: f32, color: Color32) -> Prim {
let (width, height, baseline) = self.text(text, size);
let rect = anchor.anchor_size(at, egui::vec2(width, height));
Prim::Text {
pos: pos2(self.snap(rect.left()), self.snap(rect.top())),
text: text.to_owned(),
size,
color,
baseline,
angle: 0.0,
}
}
}