mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Canvas: a drag moves the kept anchor marks mesh instead of tessellating it
Each step of a drag moved the kept selection marks, but the anchor marks' mesh was keyed on the marks key, which changes with every revision, so every step tessellated the anchors, handles and outlines again and flattened every outline. With about 2,000 paths selected that was the largest part of each step. The kept mesh now moves with the marks when that gives what a new tessellation gives: the move since the mesh was tessellated is a whole number of physical pixels on screen, so pixel rounding lands the same, and every anchor and handle is inside the canvas and the clip before and after the move, so no mark is left out or culled. Otherwise the next paint tessellates the marks as before. A new revision of the same document and selection also keeps the mesh.
This commit is contained in:
1 parent
f93c802a79
commit
e009722fe8
2 files changed
+143
-2
No files matched your search
@@ -688,6 +688,59 @@ struct MeshKey {
|
||||
pub(crate) struct KeptMesh {
|
||||
key: MeshKey,
|
||||
mesh: Arc<egui::Mesh>,
|
||||
/// How far the marks moved on screen since they were tessellated, in physical pixels, unrounded
|
||||
/// ([`KeptMesh::translate`]). The mesh moved by this distance rounded.
|
||||
moved: [f64; 2],
|
||||
}
|
||||
|
||||
/// How far from a whole number of physical pixels the marks' move since they were tessellated may be on screen, and
|
||||
/// still count as whole: rounding noise of the screen position, well under what the tessellator's `f32` positions hold.
|
||||
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<vectorcraft_geom::Rect>) -> bool {
|
||||
let key = &self.key;
|
||||
let ppp = f64::from(key.pixels_per_point);
|
||||
let view = key.view.affine();
|
||||
let shift = (view * (Point::ZERO + by)) - (view * Point::ZERO);
|
||||
let moved = [self.moved[0] + shift.x * ppp, self.moved[1] + shift.y * ppp];
|
||||
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)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let step = |axis: usize| ((moved[axis].round() - self.moved[axis].round()) / ppp) as f32;
|
||||
let offset = vec2(step(0), step(1));
|
||||
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<vectorcraft_geom::Rect> {
|
||||
anchors
|
||||
.paths
|
||||
.iter()
|
||||
.chain(&anchors.plain)
|
||||
.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<vectorcraft_geom::Rect>, 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.
|
||||
@@ -809,7 +862,7 @@ impl Component for AnchorMarks<'_> {
|
||||
None => {
|
||||
let shapes = shapes();
|
||||
let mesh = Arc::new(tessellate(shapes, &key));
|
||||
*kept = Some(KeptMesh { key, mesh: Arc::clone(&mesh) });
|
||||
*kept = Some(KeptMesh { key, mesh: Arc::clone(&mesh), moved: [0.0; 2] });
|
||||
mesh
|
||||
}
|
||||
};
|
||||
@@ -1198,12 +1251,23 @@ impl SelectionMarks {
|
||||
self.from = (moved.from.clone(), moved.by);
|
||||
self.doc = moved.to.clone();
|
||||
}
|
||||
// The kept mesh shows these marks, so it stays theirs under the new key.
|
||||
let was = self.key;
|
||||
if let Some(kept) = self.mesh.get_mut().as_mut().filter(|kept| kept.key.marks == was) {
|
||||
kept.key.marks = key;
|
||||
}
|
||||
self.key = key;
|
||||
Some(self)
|
||||
}
|
||||
|
||||
/// Moves every mark by `by`.
|
||||
/// 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)) {
|
||||
*mesh = None;
|
||||
}
|
||||
let shift = vectorcraft_geom::Affine::translate(by);
|
||||
for o in &mut self.outlines {
|
||||
o.paths.iter_mut().chain(o.key.as_mut()).for_each(|path| path.apply_affine(shift));
|
||||
|
||||
@@ -1283,3 +1283,80 @@ fn a_move_of_the_selection_moves_its_marks_without_building_them() {
|
||||
assert_eq!(frames(&mut app, 3), 1, "an undo");
|
||||
assert!(kept_marks_are_current(&app), "after an undo");
|
||||
}
|
||||
|
||||
/// The mesh of the anchor marks the canvas keeps.
|
||||
fn kept_mesh(app: &VectorcraftApp) -> Arc<egui::Mesh> {
|
||||
let marks = app.canvas.selection_marks.as_ref().expect("the canvas drew the selection's marks");
|
||||
let kept = marks.mesh.borrow();
|
||||
Arc::clone(&kept.as_ref().expect("the canvas kept the anchor marks' mesh").mesh)
|
||||
}
|
||||
|
||||
// A drag of the selection moves the kept mesh of its anchor marks instead of tessellating them again: steps of whole
|
||||
// pixels tessellate nothing and leave the mesh a new tessellation gives, a new revision of the same marks keeps it, and
|
||||
// a step of part of a pixel or one that takes the marks past the canvas's edge tessellates them once.
|
||||
#[test]
|
||||
fn a_drag_of_the_selection_moves_the_anchor_marks_mesh_without_tessellating_it() {
|
||||
let (mut app, curve) = canvas_app();
|
||||
app.select_tool("selection");
|
||||
app.run("select.set", json!({"ids": [curve.0]})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
let frames = |app: &mut VectorcraftApp, n: usize| {
|
||||
let before = MESHES_BUILT.with(std::cell::Cell::get);
|
||||
for _ in 0..n {
|
||||
canvas_frame(app, &ctx, vec![]);
|
||||
}
|
||||
MESHES_BUILT.with(std::cell::Cell::get) - before
|
||||
};
|
||||
assert_eq!(frames(&mut app, 2), 1, "the first frame tessellates the marks");
|
||||
// A document distance that is `px` physical pixels across on screen.
|
||||
let pixels = |app: &VectorcraftApp, px: f64| px / (app.canvas_view().unwrap().zoom * f64::from(ctx.pixels_per_point()));
|
||||
let step = |app: &mut VectorcraftApp, dx: f64| {
|
||||
app.session.preview("object.transform", &json!({"matrix": [1, 0, 0, 1, dx, 0], "copy": false})).unwrap();
|
||||
};
|
||||
|
||||
let layer = app.session.active().unwrap().doc.layers[0].id;
|
||||
app.run("layer.setCurrent", json!({"id": layer.0})).unwrap();
|
||||
assert_eq!(frames(&mut app, 2), 0, "a new revision of the same document and selection");
|
||||
|
||||
// The kept mesh matches the one marks built now tessellate; the kept marks go back afterwards, so the drag
|
||||
// goes on moving them.
|
||||
let matches_new = |app: &mut VectorcraftApp, case: &str| {
|
||||
let moved = kept_mesh(app);
|
||||
let kept = app.canvas.selection_marks.take();
|
||||
canvas_frame(app, &ctx, vec![]);
|
||||
let fresh = kept_mesh(app);
|
||||
app.canvas.selection_marks = kept;
|
||||
assert!(!fresh.vertices.is_empty(), "{case}: the marks drew something");
|
||||
assert_eq!(moved.indices, fresh.indices, "{case}: the moved mesh has the triangles of a new one");
|
||||
assert_eq!(moved.vertices.len(), fresh.vertices.len(), "{case}");
|
||||
for (a, b) in moved.vertices.iter().zip(&fresh.vertices) {
|
||||
assert_eq!(a.color, b.color, "{case}");
|
||||
assert!((a.pos - b.pos).length() < 1e-3, "{case}: a moved vertex at {:?}, a new one at {:?}", a.pos, b.pos);
|
||||
}
|
||||
};
|
||||
|
||||
app.session.begin_interaction("Move").unwrap();
|
||||
let built = MARKS_BUILT.with(std::cell::Cell::get);
|
||||
for px in [3.0, 10.0, -4.0] {
|
||||
let dx = pixels(&app, px);
|
||||
step(&mut app, dx);
|
||||
assert_eq!(frames(&mut app, 2), 0, "a drag's step to {px} pixels");
|
||||
}
|
||||
assert_eq!(MARKS_BUILT.with(std::cell::Cell::get), built, "the drag moved the kept marks");
|
||||
matches_new(&mut app, "after whole pixels");
|
||||
|
||||
let built = MARKS_BUILT.with(std::cell::Cell::get);
|
||||
let dx = pixels(&app, -4.5);
|
||||
step(&mut app, dx);
|
||||
assert_eq!(frames(&mut app, 2), 1, "a step to part of a pixel");
|
||||
let dx = pixels(&app, -3.5);
|
||||
step(&mut app, dx);
|
||||
assert_eq!(frames(&mut app, 2), 0, "a whole pixel from the marks tessellated at part of one");
|
||||
assert_eq!(MARKS_BUILT.with(std::cell::Cell::get), built, "the drag moved the kept marks");
|
||||
matches_new(&mut app, "after part of a pixel and a whole one");
|
||||
// Whole pixels from the last step.
|
||||
let dx = pixels(&app, 1996.5);
|
||||
step(&mut app, dx);
|
||||
assert_eq!(frames(&mut app, 2), 1, "a step past the canvas's edge");
|
||||
app.session.commit_interaction().unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user