mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 20:19:44 +00:00
Dimension tool: levels come from the document the next preview applies to
While a linear dimension is placed, each preview starts again from the document as the placement began. The tool now reads the lines of the other dimensions from that document (the new ToolContext field interaction_doc), not from the document a command changed in between, so a dimension that a command moves or hides and that the next preview puts back does not leave the placed dimension on a level the committed document does not have. The engine test now drags a dimension out, runs dimension.update or object.hide on the other dimension, releases, and checks the committed line against that dimension as the document has it. The tool test that checks which dimensions read lines also covers radius and diameter dimensions.
This commit is contained in:
@@ -476,6 +476,7 @@ impl Session {
|
||||
plane_targets: self.prefs.perspective_widget.targets(),
|
||||
precise_cursors: self.precise_cursors(),
|
||||
global_edit: st.global_edit.as_ref().map_or(&[], |g| g.members.as_slice()),
|
||||
interaction_doc: st.interaction.as_ref().map(|it| &*it.doc),
|
||||
};
|
||||
let tool = &mut self.tool;
|
||||
match crate::guard::catch_panic(|| f(tool.as_mut(), &cx)) {
|
||||
|
||||
@@ -2080,19 +2080,36 @@ fn a_dimension_of_the_same_points_lines_up_and_the_line_follows_the_pointer() {
|
||||
assert_eq!(recipe(&s, id).line_ends(), (pt(200.0, 186.0), pt(300.0, 186.0)));
|
||||
}
|
||||
|
||||
/// With Real-time Drawing off, a dimension hidden, locked or deleted by a command while another is
|
||||
/// placed is no longer a level: no guide runs along its level.
|
||||
/// A command that moves or hides a dimension while another is placed (Real-time Drawing on) does
|
||||
/// not leave the placed dimension on a level the committed document does not have: committed with
|
||||
/// its line near the other dimension's line as the document has it then, it is on that level, and
|
||||
/// otherwise at the pointer.
|
||||
#[test]
|
||||
fn a_dimension_hidden_locked_or_deleted_while_another_is_placed_is_no_longer_a_level() {
|
||||
fn a_dimension_placed_after_a_command_lines_up_with_the_committed_document() {
|
||||
let plain = ViewInfo::default();
|
||||
for cmd in ["object.hide", "object.lock", "edit.clear"] {
|
||||
for (cmd, params, y) in [("dimension.update", json!({"offset": -10.0}), 192.0), ("object.hide", json!({}), 172.0)] {
|
||||
let (mut s, first) = level_start(plain);
|
||||
run(&mut s, "prefs.set", json!({"key": "realTimeDrawing", "value": false}));
|
||||
linear_click(&mut s, plain, 200.0, 200.0);
|
||||
linear_click(&mut s, plain, 300.0, 200.0);
|
||||
assert!(!guide_lines(&mut s, plain, 250.0, 172.5).is_empty(), "{cmd}: on the level before");
|
||||
run(&mut s, cmd, json!({"ids": [first]}));
|
||||
assert!(guide_lines(&mut s, plain, 250.0, 172.5).is_empty(), "{cmd}: no level after");
|
||||
run(&mut s, "prefs.set", json!({"key": "realTimeDrawing", "value": true}));
|
||||
// The bottom edge dragged out onto the level of the first line.
|
||||
for (kind, y) in [(PointerKind::Move, 300.0), (PointerKind::Down, 300.0), (PointerKind::Drag, 240.0), (PointerKind::Drag, 172.0)] {
|
||||
s.pointer(&PointerEvent::new(kind, 160.0, y), plain).unwrap();
|
||||
}
|
||||
let mut params = params;
|
||||
params["ids"] = json!([first]);
|
||||
run(&mut s, cmd, params);
|
||||
if cmd == "dimension.update" {
|
||||
assert_eq!(recipe(&s, first).line_ends().0.y, 190.0, "moved before the release");
|
||||
}
|
||||
// Released without a drag in between: the release places the dimension.
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 160.0, y), plain).unwrap();
|
||||
let placed = s.doc().unwrap().selection.objects[0].0;
|
||||
assert_ne!(placed, first, "{cmd}: a new dimension");
|
||||
let reference = s.doc().unwrap().doc.node(NodeId(first)).map(|n| (n.visible, recipe(&s, first).line_ends().0.y));
|
||||
let expected = match reference {
|
||||
Some((true, level)) if (level - y).abs() <= 4.0 => level,
|
||||
_ => y,
|
||||
};
|
||||
assert_eq!(recipe(&s, placed).line_ends().0.y, expected, "{cmd}: the other dimension is {reference:?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -350,6 +350,10 @@ pub struct ToolContext<'a> {
|
||||
/// The objects of the Global Edit in progress (Select › Start Global Edit), the one it started
|
||||
/// from first, each with its transform; empty while Global Edit is off.
|
||||
pub global_edit: &'a [(NodeId, vectorcraft_geom::Affine)],
|
||||
/// While an interaction is in progress: the document as it was when the interaction began,
|
||||
/// which each preview of the interaction replaces the document with before it applies. None
|
||||
/// outside an interaction.
|
||||
pub interaction_doc: Option<&'a Document>,
|
||||
}
|
||||
|
||||
impl ToolContext<'_> {
|
||||
|
||||
@@ -484,10 +484,10 @@ fn on_dimensions(cx: &ToolContext, p: Point) -> Option<(Point, bool)> {
|
||||
/// The line of a linear dimension already drawn: its ends and its direction.
|
||||
type DimensionLine = ((Point, Point), Vec2);
|
||||
|
||||
/// The lines of the visible, unlocked linear dimensions in the document, except the preview of the
|
||||
/// The lines of the visible, unlocked linear dimensions in `doc`, except the preview of the
|
||||
/// dimension being placed: a dimension with the id `preview.0` that measures the points
|
||||
/// `preview.1`. Bounded work, as in [`on_dimensions`].
|
||||
fn dimension_lines(cx: &ToolContext, preview: (u64, [Point; 2])) -> Vec<DimensionLine> {
|
||||
fn dimension_lines(doc: &vectorcraft_doc::Document, preview: (u64, [Point; 2])) -> Vec<DimensionLine> {
|
||||
fn visit(n: &Node, preview: (u64, [Point; 2]), budget: &mut usize, out: &mut Vec<DimensionLine>) {
|
||||
let is_preview = n.id.0 == preview.0
|
||||
&& n.extras().dimension.as_ref().is_some_and(|d| d.a.distance(preview.1[0]) < 1e-9 && d.b.distance(preview.1[1]) < 1e-9);
|
||||
@@ -509,7 +509,7 @@ fn dimension_lines(cx: &ToolContext, preview: (u64, [Point; 2])) -> Vec<Dimensio
|
||||
}
|
||||
}
|
||||
let (mut budget, mut out) = (20_000usize, vec![]);
|
||||
for n in cx.doc.layers.iter().rev() {
|
||||
for n in doc.layers.iter().rev() {
|
||||
if budget == 0 {
|
||||
break;
|
||||
}
|
||||
@@ -750,14 +750,17 @@ impl DimensionTool {
|
||||
// With Smart Guides on, a linear dimension's line lands on the level of a parallel
|
||||
// dimension line already drawn within the Smart Guides snapping tolerance. The lines are
|
||||
// read at the first preview and, with Smart Guides on, again when the document has changed
|
||||
// since (a command hid, locked or deleted a dimension). The preview of this dimension is
|
||||
// not one of them.
|
||||
// since. They are read from the document the next preview applies to: while the
|
||||
// interaction is in progress, the document as it began, which each preview starts again
|
||||
// from, so a dimension that a command moves or hides in between, and that the next
|
||||
// preview puts back, stays a level where it was. The preview of this dimension is not one
|
||||
// of them.
|
||||
if d.mode == DimensionMode::Linear {
|
||||
if !self.began {
|
||||
self.preview_id = cx.doc.peek_next_id();
|
||||
}
|
||||
if !self.began || (cx.smart_guides && self.levels_at != cx.revision) {
|
||||
self.levels = dimension_lines(cx, (self.preview_id, [d.a, d.b]));
|
||||
self.levels = dimension_lines(cx.interaction_doc.unwrap_or(cx.doc), (self.preview_id, [d.a, d.b]));
|
||||
self.levels_at = cx.revision;
|
||||
}
|
||||
}
|
||||
@@ -1156,6 +1159,21 @@ mod tests {
|
||||
t.pointer(&c, &ev(PointerKind::Down, 100.0, 100.0));
|
||||
assert!(matches!(t.pointer(&c, &ev(PointerKind::Drag, 130.0, 140.0)).first(), Some(Action::Begin(_))));
|
||||
assert!(t.levels.is_empty(), "an angular dimension reads no lines");
|
||||
// A radius (dragged outwards from a circle) and a diameter (inwards) read none either.
|
||||
let circle = d.alloc_id();
|
||||
let round = vectorcraft_geom::shapes::ellipse(Rect::new(400.0, 150.0, 500.0, 250.0));
|
||||
d.insert(Some(l), usize::MAX, Node::path(circle, round, vectorcraft_doc::Appearance::default_art())).unwrap();
|
||||
let c = cx(&d, &s, &p);
|
||||
for (to, mode) in [(530.0, "radius"), (470.0, "diameter")] {
|
||||
let mut t = DimensionTool::default();
|
||||
t.set_option("mode", &json!("radial"));
|
||||
t.pointer(&c, &ev(PointerKind::Down, 500.0, 200.0));
|
||||
let acts = t.pointer(&c, &ev(PointerKind::Drag, to, 200.0));
|
||||
assert!(matches!(acts.first(), Some(Action::Begin(_))), "{mode}: {acts:?}");
|
||||
let Some(Action::Preview(_, params)) = acts.last() else { panic!("{mode}: {acts:?}") };
|
||||
assert_eq!(params["mode"], json!(mode));
|
||||
assert!(t.levels.is_empty(), "a {mode} dimension reads no lines");
|
||||
}
|
||||
}
|
||||
|
||||
/// A dimension hidden, locked or deleted while another is placed is no longer a level, and one
|
||||
@@ -1233,6 +1251,46 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// While the interaction is in progress, the lines are read from the document it began with,
|
||||
/// which the next preview starts from, not from the document a command changed in between: a
|
||||
/// dimension moved or hidden there stays a level where it was.
|
||||
#[test]
|
||||
fn dimension_lines_are_read_from_the_document_the_preview_applies_to() {
|
||||
use vectorcraft_doc::{Node, NodeKind};
|
||||
let (mut d, _) = doc_with_rect();
|
||||
let pt = Point::new;
|
||||
let line = |y: f64| Some(Box::new(DimensionTool::default().recipe(DimensionMode::Linear, pt(300.0, y), pt(400.0, y), pt(350.0, y), None)));
|
||||
let id = d.alloc_id();
|
||||
let mut drawn = Node::new(id, NodeKind::Group { children: vec![], clip: false });
|
||||
drawn.extras_mut().dimension = line(300.0);
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), usize::MAX, drawn).unwrap();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let ev = |kind, x: f64, y: f64| PointerEvent::new(kind, x, y);
|
||||
let offset = |t: &mut DimensionTool, c: &ToolContext, y: f64| {
|
||||
let acts = t.pointer(c, &ev(PointerKind::Move, 420.0, y));
|
||||
let Some(Action::Preview(_, params)) = acts.last() else { panic!("{acts:?}") };
|
||||
params["offset"].clone()
|
||||
};
|
||||
let mut t = DimensionTool::default();
|
||||
let c = cx(&d, &s, &p);
|
||||
for (x, y) in [(400.0, 400.0), (450.0, 400.0)] {
|
||||
t.pointer(&c, &ev(PointerKind::Down, x, y));
|
||||
t.pointer(&c, &ev(PointerKind::Up, x, y));
|
||||
}
|
||||
assert!(matches!(t.pointer(&c, &ev(PointerKind::Move, 420.0, 450.0)).first(), Some(Action::Begin(_))));
|
||||
for (rev, edit) in [(1, "moved"), (2, "hidden")] {
|
||||
let mut doc = d.clone();
|
||||
match edit {
|
||||
"moved" => doc.node_mut(id).unwrap().extras_mut().dimension = line(320.0),
|
||||
_ => doc.node_mut(id).unwrap().visible = false,
|
||||
}
|
||||
let c = ToolContext { revision: (0, rev), interaction_doc: Some(&d), ..cx(&doc, &s, &p) };
|
||||
assert_eq!(offset(&mut t, &c, 318.0), json!(-82.0), "{edit}: not on the level at y = 320");
|
||||
assert_eq!(offset(&mut t, &c, 302.0), json!(-100.0), "{edit}: on the level at y = 300");
|
||||
}
|
||||
}
|
||||
|
||||
/// The preview of the dimension itself does not count toward the 20,000 objects read: a
|
||||
/// dimension that was the last one read stays a level once the preview is in front of it.
|
||||
#[test]
|
||||
|
||||
@@ -677,6 +677,7 @@ pub(crate) mod testutil {
|
||||
plane_targets: distort::perspective::widget::PlaneTargets::ALL,
|
||||
precise_cursors: false,
|
||||
global_edit: &[],
|
||||
interaction_doc: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user