diff --git a/crates/ui-egui/src/canvas/overlays.rs b/crates/ui-egui/src/canvas/overlays.rs index b4d2d44aac..b94a739997 100644 --- a/crates/ui-egui/src/canvas/overlays.rs +++ b/crates/ui-egui/src/canvas/overlays.rs @@ -8,7 +8,7 @@ use super::artboards::names_an_artboard; use super::input::marquee_through; use super::outline::{KEY_OUTLINE, c32, node_outline, selection_hit_at, stroke_path, to_screen_path, too_many_anchors}; use super::selection_marks::{MarkOptions, SelectionMarks}; -use super::{BezPath, Color32, CornerRadius, NodeKind, Overlay, Pos2, Shape, Stroke, StrokeKind, Tokens, VectorcraftApp, Xf, vec2}; +use super::{BezPath, Color32, CornerRadius, NodeKind, Overlay, Point, Pos2, Shape, Stroke, StrokeKind, Tokens, VectorcraftApp, Xf, vec2}; pub(super) fn hover_highlight(app: &VectorcraftApp, p: &egui::Painter, xf: &Xf) { let Some(h) = app.hover_doc else { return }; @@ -452,6 +452,42 @@ pub(super) fn dashes_from(line: &[Pos2], s: Stroke, from: f64, out: &mut Vec) { + if !(a.is_finite() && b.is_finite()) { + return; + } + let r = clip.expand(s.width.max(1.0)); + let (left, right, top, bottom) = (f64::from(r.left()), f64::from(r.right()), f64::from(r.top()), f64::from(r.bottom())); + let d = b - a; + let (mut t0, mut t1) = (0.0_f64, 1.0_f64); + // Each edge as (the run toward it, the room before it), as `grid::clip_segment` does in `f32`. + for (run, room) in [(-d.x, a.x - left), (d.x, right - a.x), (-d.y, a.y - top), (d.y, bottom - a.y)] { + if run == 0.0 { + if room < 0.0 { + return; + } + } else if run < 0.0 { + t0 = t0.max(room / run); + } else { + t1 = t1.min(room / run); + } + } + if t0 > t1 { + return; + } + let at = |t: f64| { + let p = a + d * t; + egui::pos2(p.x as f32, p.y as f32) + }; + // How far along the whole line the part on the canvas starts. + dashes_from(&[at(t0), at(t1)], s, d.hypot() * t0, out); +} + /// Add to `out` each line of `line` that has a length as its own [`Shape::LineSegment`], as /// [`Shape::dashed_line`] draws a dash. fn segments(line: &[Pos2], s: Stroke, out: &mut Vec) { @@ -562,11 +598,12 @@ pub(super) fn draw_overlays(p: &egui::Painter, xf: &Xf, overlays: &[Overlay], t: } Overlay::Line { a, b, color, dashed } => { let s = Stroke::new(1.0, c32(*color)); - let (a, b) = (xf.to_screen(*a), xf.to_screen(*b)); if *dashed { - p.extend(Shape::dashed_line(&[a, b], s, 4.0, 3.0)); + let mut dashes = vec![]; + dashed_line_in(p.clip_rect(), xf.affine() * *a, xf.affine() * *b, s, &mut dashes); + p.extend(dashes); } else { - p.line_segment([a, b], s); + p.line_segment([xf.to_screen(*a), xf.to_screen(*b)], s); } } Overlay::Anchor { p: pt, color, filled, size } => anchor_square(p, xf.to_screen(*pt), c32(*color), *filled, *size), diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index d5244aa234..c040e31d35 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -211,6 +211,70 @@ fn a_line_dashed_in_pieces_dashes_as_the_whole_line_does() { } } +/// A dashed [`Overlay::Line`] much longer than the canvas draws only the dashes on the canvas, +/// where the whole line dashed in one go has them, and a line off the canvas or with an end at +/// infinity draws none. +#[test] +fn a_long_dashed_overlay_line_draws_only_the_dashes_on_the_canvas() { + let s = Stroke::new(1.0, Color32::WHITE); + let clip = egui::Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + // The stretches of x the dashes cover inside `lo..hi`, each dash as one stretch. + let spans = |shapes: &[Shape], lo: f32, hi: f32| -> Vec<(f32, f32)> { + let mut out: Vec<(f32, f32)> = shapes + .iter() + .map(|s| match s { + Shape::LineSegment { points: [a, b], .. } => (a.x.min(b.x).max(lo), a.x.max(b.x).min(hi)), + other => panic!("{other:?}"), + }) + .filter(|(a, b)| b > a) + .collect(); + out.sort_by(|a, b| a.0.total_cmp(&b.0)); + out + }; + // A line from 1,000 px left of the canvas to 1,500 px right of it, along y = 150. + let (a, b) = (pos2(-1000.0, 150.0), pos2(1900.0, 150.0)); + let mut drawn = vec![]; + overlays::dashed_line_in(clip, Point::new(-1000.0, 150.0), Point::new(1900.0, 150.0), s, &mut drawn); + let (lo, hi) = (clip.left() - 1.0, clip.right() + 1.0); + let (want, got) = (spans(&Shape::dashed_line(&[a, b], s, 4.0, 3.0), lo, hi), spans(&drawn, lo, hi)); + assert_eq!(want.len(), got.len(), "{want:?} {got:?}"); + assert!(want.iter().zip(&got).all(|(w, g)| (w.0 - g.0).abs() < 1e-2 && (w.1 - g.1).abs() < 1e-2), "{want:?} {got:?}"); + assert!(drawn.len() <= 60, "only the dashes on the canvas: {}", drawn.len()); + // Ends 10,000,000,000 px away on both sides still keep the part on the canvas. + let mut far = vec![]; + overlays::dashed_line_in(clip, Point::new(-1e10, 150.0), Point::new(1e10, 150.0), s, &mut far); + let far = spans(&far, lo, hi); + assert!(far.len() >= 50 && far.len() <= 60, "{far:?}"); + assert!(far.iter().all(|(a, b)| b - a <= 4.0 + 1e-3), "{far:?}"); + // Drawn through the overlays at a view where the line is 2,000,000 px long. + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + crate::theme::apply(&ctx, crate::theme::Brightness::MediumDark); + let prefs = vectorcraft_engine::Prefs::default(); + let segments = |line: Overlay| { + let raw = egui::RawInput { screen_rect: Some(clip), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + let t = Tokens::get(ui.ctx()); + let xf = Xf { rect: ui.max_rect(), zoom: 1.0, center: Point::new(200.0, 150.0), rot: 0.3 }; + overlays::draw_overlays( + ui.painter(), + &xf, + std::slice::from_ref(&line), + &t, + overlays::HandleLook::of(&prefs), + &mut overlays::SharedPaths::default(), + ); + }); + out.textures_delta.clear(); + out.shapes.iter().filter(|c| matches!(c.shape, Shape::LineSegment { .. })).count() + }; + let line = |a: Point, b: Point| Overlay::Line { a, b, color: [0xff, 0, 0], dashed: true }; + let n = segments(line(Point::new(-1e6, 150.0), Point::new(1e6, 150.0))); + assert!(n > 0 && n <= 80, "{n} dashes"); + assert_eq!(segments(line(Point::new(-1e6, 5000.0), Point::new(1e6, 5000.0))), 0, "a line off the canvas"); + assert_eq!(segments(line(Point::new(0.0, 150.0), Point::new(f64::INFINITY, 150.0))), 0, "an end at infinity"); +} + #[test] fn subpixel_dock_layout_does_not_change_pointer_document_coordinates() { let mut observed = Vec::new();