diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs index b6421c882b..6151e3affb 100644 --- a/crates/ui-egui/src/canvas/selection.rs +++ b/crates/ui-egui/src/canvas/selection.rs @@ -699,12 +699,12 @@ const PIXEL_SLACK: f64 = 1e-6; impl KeptMesh { /// Moves the mesh by `by` in the document when tessellating the moved marks would give the same mesh moved, and - /// returns whether it did. `was` is the box of the marks' points before the move ([`knots_box`]). The mesh moves - /// when the marks' whole move since they were tessellated is a whole number of physical pixels on screen, so the - /// tessellator's rounding to pixels lands the same, and when every point is inside the canvas the marks are drawn - /// near and inside the clip both before and after the move, so no anchor, handle or outline is left out or culled - /// on either side. - fn translate(&mut self, by: Vec2, was: Option) -> bool { + /// returns whether it did. `points` are the marks' anchors and direction handles before the move ([`knot_points`]). + /// The mesh moves when the marks' whole move since they were tessellated is a whole number of physical pixels on + /// screen, and when every point's `f32` screen position moves by this move's whole pixels too, so the tessellator's + /// rounding to pixels lands the same. It also needs every point inside the canvas before and after the move, so the + /// marks are drawn near and inside the clip on both sides and no anchor, handle or outline is left out or culled. + fn translate(&mut self, by: Vec2, points: impl IntoIterator) -> bool { let key = &self.key; let ppp = f64::from(key.pixels_per_point); let view = key.view.affine(); @@ -713,26 +713,31 @@ impl KeptMesh { if moved.iter().any(|p| !p.is_finite() || (p - p.round()).abs() > PIXEL_SLACK) { return false; } - if let Some(was) = was { - let area = key.rect.expand(key.look.reach).intersect(key.clip); - let inside = |r: vectorcraft_geom::Rect| { - [(r.x0, r.y0), (r.x1, r.y0), (r.x1, r.y1), (r.x0, r.y1)].into_iter().all(|(x, y)| area.contains(key.view.to_screen(Point::new(x, y)))) - }; - if !(inside(was) && inside(was + by)) { + // This move in whole physical pixels. + let pixels = [moved[0].round() - self.moved[0].round(), moved[1].round() - self.moved[1].round()]; + let area = key.rect.expand(key.look.reach).intersect(key.clip); + // `CanvasView::to_screen` with the affine computed once. + let screen = |p: Point| { + let s = view * p; + egui::pos2(s.x as f32, s.y as f32) + }; + let whole = |from: f32, to: f32, px: f64| ((f64::from(to) - f64::from(from)) * ppp - px).abs() <= PIXEL_SLACK; + for p in points { + let (from, to) = (screen(p), screen(p + by)); + if !(area.contains(from) && area.contains(to) && whole(from.x, to.x, pixels[0]) && whole(from.y, to.y, pixels[1])) { return false; } } - let step = |axis: usize| ((moved[axis].round() - self.moved[axis].round()) / ppp) as f32; - let offset = vec2(step(0), step(1)); + let offset = vec2((pixels[0] / ppp) as f32, (pixels[1] / ppp) as f32); self.moved = moved; Arc::make_mut(&mut self.mesh).translate(offset); true } } -/// The box of every anchor and direction handle of `anchors`, in document units, or `None` without any. The outlines -/// lie inside it too, as a curve lies inside the hull of its points. -fn knots_box(anchors: &Anchors) -> Option { +/// Every anchor and direction handle of `anchors`, in document units. The outlines lie inside the hull of these +/// points, as a curve lies inside the hull of its points. +fn knot_points(anchors: &Anchors) -> impl Iterator + '_ { anchors .paths .iter() @@ -740,7 +745,6 @@ fn knots_box(anchors: &Anchors) -> Option { .flat_map(|p| &p.subpaths) .flat_map(|sp| &sp.knots) .flat_map(|knot| [knot.anchor, knot.in_ctrl, knot.out_ctrl]) - .fold(None, |r: Option, p| Some(r.map_or(vectorcraft_geom::Rect::from_points(p, p), |r| r.union_pt(p)))) } /// One set of anchors as `BezierOverlay` took it: the paths, which anchors are selected and how they show. @@ -1263,9 +1267,8 @@ impl SelectionMarks { /// Moves every mark by `by`, and the kept mesh of the anchor marks with them when it can move as it is /// ([`KeptMesh::translate`]); otherwise the next paint tessellates the marks again. fn translate(&mut self, by: Vec2) { - let was = knots_box(&self.anchors); let mesh = self.mesh.get_mut(); - if mesh.as_mut().is_some_and(|kept| !kept.translate(by, was)) { + if mesh.as_mut().is_some_and(|kept| !kept.translate(by, knot_points(&self.anchors))) { *mesh = None; } let shift = vectorcraft_geom::Affine::translate(by); diff --git a/crates/ui-egui/src/canvas/selection/tests.rs b/crates/ui-egui/src/canvas/selection/tests.rs index f23e4087aa..3b979256c5 100644 --- a/crates/ui-egui/src/canvas/selection/tests.rs +++ b/crates/ui-egui/src/canvas/selection/tests.rs @@ -1360,3 +1360,40 @@ fn a_drag_of_the_selection_moves_the_anchor_marks_mesh_without_tessellating_it() assert_eq!(frames(&mut app, 2), 1, "a step past the canvas's edge"); app.session.commit_interaction().unwrap(); } + +// A move of whole pixels keeps the mesh only when every point's `f32` screen position moves by those whole pixels: +// at x = 255.999991 the screen position before a one-pixel move is 255.99998 and after it 257.0, so the marks' +// squares would round to other pixels than the moved mesh's. +#[test] +fn a_whole_pixel_move_keeps_the_mesh_only_when_the_screen_positions_move_by_whole_pixels() { + let screen = Rect::from_min_size(egui::pos2(0.0, 0.0), vec2(1000.0, 1000.0)); + let look = MarkLook { + line: Color32::BLACK, + fill: Color32::WHITE, + edge: Color32::BLACK, + anchor: 5.0, + large: 7.0, + thin: 1.0, + medium: 2.0, + thick: 3.0, + reach: 20.0, + }; + let kept = || KeptMesh { + key: MeshKey { + marks: 0, + view: CanvasView::new(screen, Point::new(500.0, 500.0), 1.0), + rect: screen, + clip: screen, + pixels_per_point: 1.0, + options: egui::epaint::TessellationOptions::default(), + look, + }, + mesh: Arc::new(egui::Mesh::default()), + moved: [0.0; 2], + }; + let by = Vec2::new(1.0, 0.0); + assert!(kept().translate(by, [Point::new(255.5, 10.0)]), "a point whose screen position moves one whole pixel"); + let mut off = kept(); + assert!(!off.translate(by, [Point::new(255.5, 10.0), Point::new(255.999_991, 10.0)]), "a point that moves 1.0000153 pixels"); + assert_eq!(off.moved, [0.0; 2], "the refused move leaves the mesh where it was"); +}