mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-23 10:53:35 +01:00
Dimension tool: a geometry effect on any part keeps a dimension in place
A press on one part of a dimension now also checks its other parts for a geometry effect, so an effect on an extension line keeps the drag from moving the dimension (and from drawing the parts again without the effect). In isolation mode, only art inside the isolated object is compared with the dimension when the press decides between them.
This commit is contained in:
1 parent
1be942d7fc
commit
49fd8652b1
2 files changed
+70
-6
No files matched your search
@@ -2352,6 +2352,50 @@ fn a_dimension_is_dragged_only_where_the_canvas_hits_it() {
|
||||
assert!(!boxes.iter().any(|r| r.x1 > 399.0), "the rectangle is not outlined: {boxes:?}");
|
||||
}
|
||||
|
||||
/// A geometry effect on a part of a dimension other than the one pressed keeps the Dimension tool
|
||||
/// from dragging it, and the effect stays on that part.
|
||||
#[test]
|
||||
fn a_geometry_effect_on_another_part_keeps_the_dimension_in_place() {
|
||||
let plain = ViewInfo::default();
|
||||
let pt = vectorcraft_geom::Point::new;
|
||||
let (mut s, first) = level_start(plain);
|
||||
let extension = {
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let parts = doc.node(NodeId(first)).unwrap().children().unwrap();
|
||||
// The extension line at x = 100, which the press below on the dimension line misses.
|
||||
let line = parts.iter().find(|c| c.geometric_bounds().is_some_and(|r| r.x1 < 101.0 && r.height() > 10.0));
|
||||
line.unwrap().id.0
|
||||
};
|
||||
run(&mut s, "effect.apply", json!({"effect":"distort.transform","ids":[extension],"params":{"moveH":-60}}));
|
||||
run(&mut s, "select.none", json!({}));
|
||||
for (kind, y) in [(PointerKind::Move, 170.0), (PointerKind::Down, 170.0), (PointerKind::Drag, 130.0), (PointerKind::Up, 130.0)] {
|
||||
s.pointer(&PointerEvent::new(kind, 150.0, y), plain).unwrap();
|
||||
}
|
||||
assert_eq!(recipe(&s, first).line_ends(), (pt(100.0, 170.0), pt(200.0, 170.0)));
|
||||
let kept = s.doc().unwrap().doc.node(NodeId(extension)).is_some_and(|n| !n.appearance.effects.is_empty());
|
||||
assert!(kept, "the extension line keeps its effect");
|
||||
}
|
||||
|
||||
/// In isolation mode, art outside the isolated group does not take a press on a dimension inside
|
||||
/// it: the drag moves the dimension.
|
||||
#[test]
|
||||
fn art_outside_isolation_does_not_take_the_press_on_a_dimension() {
|
||||
let plain = ViewInfo::default();
|
||||
let pt = vectorcraft_geom::Point::new;
|
||||
let (mut s, first) = level_start(plain);
|
||||
// A line across the dimension line, outside the group isolated below. The press, a point below
|
||||
// the dimension line, is nearer to it than to the dimension line.
|
||||
run(&mut s, "path.create", json!({"d": "M150 100 L150 250"}));
|
||||
run(&mut s, "select.set", json!({"ids":[first]}));
|
||||
let group = run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap();
|
||||
run(&mut s, "object.isolate", json!({"id": group}));
|
||||
run(&mut s, "select.none", json!({}));
|
||||
for (kind, y) in [(PointerKind::Move, 171.0), (PointerKind::Down, 171.0), (PointerKind::Drag, 131.0), (PointerKind::Up, 131.0)] {
|
||||
s.pointer(&PointerEvent::new(kind, 150.0, y), plain).unwrap();
|
||||
}
|
||||
assert_eq!(recipe(&s, first).line_ends(), (pt(100.0, 130.0), pt(200.0, 130.0)));
|
||||
}
|
||||
|
||||
/// A dimension pasted into a document that had none is dragged with the Dimension tool like one
|
||||
/// drawn there.
|
||||
#[test]
|
||||
|
||||
@@ -157,6 +157,11 @@ fn constrained(cx: &ToolContext, last: Point, at: Point) -> Point {
|
||||
|
||||
/// Nearest editable segment, with bounded work and no snapping to dimension artwork itself.
|
||||
pub fn pick(cx: &ToolContext, p: Point) -> Option<SegmentRef> {
|
||||
pick_among(cx, p, cx.doc.layers.iter().rev().map(|n| n.as_ref()))
|
||||
}
|
||||
|
||||
/// [`pick`] among `roots`, topmost first, and what they hold.
|
||||
fn pick_among<'a>(cx: &ToolContext, p: Point, roots: impl Iterator<Item = &'a Node>) -> Option<SegmentRef> {
|
||||
let mut best = None;
|
||||
let mut dist = cx.tol(8.0);
|
||||
let mut budget = 20_000usize;
|
||||
@@ -186,7 +191,7 @@ pub fn pick(cx: &ToolContext, p: Point) -> Option<SegmentRef> {
|
||||
}
|
||||
}
|
||||
}
|
||||
for n in cx.doc.layers.iter().rev() {
|
||||
for n in roots {
|
||||
visit(n, p, &mut budget, &mut dist, &mut best);
|
||||
}
|
||||
best
|
||||
@@ -601,11 +606,21 @@ fn dimension_under(cx: &ToolContext, p: Point) -> Option<(NodeId, f64)> {
|
||||
let nodes: Vec<&Node> = chain.iter().filter_map(|id| cx.doc.node(*id)).collect();
|
||||
let below = scope_at.map_or(0, |i| i + 1);
|
||||
let outline = opt.outline || nodes.iter().skip(below).any(|n| matches!(n.kind, NodeKind::Layer { preview: false, .. }));
|
||||
let displaced = nodes.iter().enumerate().any(|(i, n)| {
|
||||
vectorcraft_effects::has_geometry(&n.appearance.effects)
|
||||
|| (i >= at && n.appearance.items.iter().any(|item| item.visible() && vectorcraft_effects::has_geometry(item.effects())))
|
||||
let item_geometry = |n: &Node| n.appearance.items.iter().any(|item| item.visible() && vectorcraft_effects::has_geometry(item.effects()));
|
||||
let displaced = nodes.iter().enumerate().any(|(i, n)| vectorcraft_effects::has_geometry(&n.appearance.effects) || (i >= at && item_geometry(n)));
|
||||
// The parts of the dimension the press did not land on count too: a geometry effect on any of
|
||||
// them is drawn elsewhere as well.
|
||||
let parts_displaced = cx.doc.node(id).is_some_and(|d| {
|
||||
d.try_walk(&mut |n| {
|
||||
if vectorcraft_effects::has_geometry(&n.appearance.effects) || item_geometry(n) {
|
||||
std::ops::ControlFlow::Break(())
|
||||
} else {
|
||||
std::ops::ControlFlow::Continue(())
|
||||
}
|
||||
})
|
||||
.is_break()
|
||||
});
|
||||
if !outline && displaced {
|
||||
if !outline && (displaced || parts_displaced) {
|
||||
return None;
|
||||
}
|
||||
let n = cx.doc.node(id).filter(|_| cx.doc.is_editable(id))?;
|
||||
@@ -1199,7 +1214,12 @@ impl Tool for DimensionTool {
|
||||
&& self.lines.is_empty()
|
||||
&& let Some((id, d)) = dimension_under(cx, ev.pos)
|
||||
{
|
||||
let art = pick(cx, ev.pos).and_then(|e| nearest_on(cx, &e, ev.pos)).map(|q| q.distance(ev.pos));
|
||||
// In isolation mode only art inside the isolated object competes for the press.
|
||||
let edge = match cx.hit_options().scope.and_then(|s| cx.doc.node(s)) {
|
||||
Some(isolated) => pick_among(cx, ev.pos, std::iter::once(isolated)),
|
||||
None => pick(cx, ev.pos),
|
||||
};
|
||||
let art = edge.and_then(|e| nearest_on(cx, &e, ev.pos)).map(|q| q.distance(ev.pos));
|
||||
if art.is_none_or(|a| d < a) {
|
||||
self.grab = Some(id);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user