Direct Selection: a marquee picks the points of a selected blend's spine

A Direct Selection marquee now picks the spine points inside it on the
topmost selected blend, and the picked points drag together as
Shift-clicked ones do. With Shift the marquee adds points or takes
picked ones away, and the blend stays selected. A plain marquee around
none of them drops the picks, and Escape during the marquee keeps them.
Unselected, locked and hidden blends are skipped.
This commit is contained in:
dotCooCoo
2026-11-15 22:51:59 -08:00
parent ed6f22a84c
commit b4e45fa93c
4 changed files with 172 additions and 5 deletions
+39 -4
View File
@@ -25,8 +25,9 @@
//! Both pick the key objects of a blend, and click or drag ruler guides ([`crate::rulerguide`]).
//! A Cmd/Ctrl+Shift double-click with either one releases the ruler guide or guide path under the
//! pointer ([`release_guide_at`]).
//! Direct Selection also edits a blend's spine: drag its points (Shift-click picks several, which
//! drag together; a key object on a point moves with it) and, once a point is clicked, its
//! Direct Selection also edits a blend's spine: drag its points (Shift-click and, on a selected
//! blend, the marquee pick several, which drag together; a key object on a point moves with it)
//! and, once a point is clicked, its
//! handles; and the points of selected gradient meshes
//! and of mesh and warp envelopes and their handles ([`MeshEdit`]): Shift-clicks and the marquee pick several,
//! a click on a gradient mesh's line picks its two ends and one inside a patch the four points
@@ -192,7 +193,8 @@ pub struct DirectSelectionTool {
moved: Option<Vec2>,
/// The spine point last clicked (blend, anchor): its handles show and can be dragged.
spine: Option<(NodeId, usize)>,
/// Other points of that spine picked with it (Shift-click adds or takes one away); dragging
/// Other points of that spine picked with it (Shift-click adds or takes one away, and a
/// marquee picks those inside it); dragging
/// a picked point moves them all (#5285).
spine_more: Vec<usize>,
mesh: MeshEdit,
@@ -915,6 +917,10 @@ impl Tool for DirectSelectionTool {
if let Some(out) = self.press_hidden(cx, ev, p) {
return out;
}
// The spine points picked before stay picked while the marquee is drawn: its
// release replaces them (with Shift, toggles them), and Escape keeps them.
self.spine = held;
self.spine_more = if held.is_some() { more } else { vec![] };
self.state = State::Marquee { start: p, cur: p, toggle: ev.mods.shift, enclosed: false, whole: ev.mods.alt };
vec![]
}
@@ -1641,12 +1647,14 @@ impl DirectSelectionTool {
return vec![];
}
self.frame.clear();
(self.spine, self.spine_more) = (None, vec![]);
return vec![Action::Exec("select.none".into(), json!({}))];
}
// With Group Selection's enclosed marquee, the tool selects the objects inside it
// (members of groups too), each whole, as a click selects one; with an Alt-marquee, every
// object it touches, each whole.
if enclosed || whole {
(self.spine, self.spine_more) = (None, vec![]);
let mut touched = vectorcraft_doc::hit::marquee_in_view(cx.doc, r, cx.isolation, true, cx.outline);
let editable = cx.doc.editable();
touched.retain(|id| editable(*id));
@@ -1702,6 +1710,33 @@ impl DirectSelectionTool {
}
}
}
// Direct Selection: the points of a selected blend's spine inside the rect are picked, on
// the topmost such blend; with Shift they are toggled. The blend stays selected. A plain
// marquee that holds none of them drops the points picked before.
let mut spined: Option<NodeId> = None;
if !self.group {
let found = spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)).find_map(|(id, path)| {
let inside: BTreeSet<usize> = path.anchors().filter(|(s, _, a)| *s == 0 && r.contains(a.p)).map(|(_, i, _)| i).collect();
(!inside.is_empty()).then_some((id, inside))
});
match found {
Some((id, inside)) => {
let held = self.spine.filter(|(h, _)| toggle && *h == id);
let before: BTreeSet<usize> = held.map(|(_, a)| self.spine_more.iter().copied().chain([a]).collect()).unwrap_or_default();
let picked: BTreeSet<usize> = if toggle { before.symmetric_difference(&inside).copied().collect() } else { inside };
let focus = held.map(|(_, a)| a).filter(|a| picked.contains(a)).or_else(|| picked.last().copied());
self.spine = focus.map(|a| (id, a));
self.spine_more = picked.into_iter().filter(|a| Some(*a) != focus).collect();
spined = Some(id);
if !toggle {
sel.push((id, vec![]));
}
}
None if !toggle => (self.spine, self.spine_more) = (None, vec![]),
// With Shift, the blend holding the picked points stays selected with them.
None => spined = self.spine.map(|(id, _)| id),
}
}
let mut marked: BTreeMap<NodeId, BTreeSet<AnchorRef>> =
if toggle { self.frame_anchors(cx).into_iter().map(|(id, v)| (id, v.into_iter().collect())).collect() } else { BTreeMap::new() };
for (id, inside) in &framed {
@@ -1721,7 +1756,7 @@ impl DirectSelectionTool {
// the marquee touches them, as with the Selection tool (#927).
for id in vectorcraft_doc::hit::marquee_in_view(cx.doc, r, cx.isolation, true, cx.outline) {
let whole = cx.doc.node(id).is_some_and(|n| !matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }));
if whole && !sel.iter().any(|(s, _)| *s == id) && !framed.iter().any(|(f, _)| *f == id) && !meshed.contains(&id) {
if whole && !sel.iter().any(|(s, _)| *s == id) && !framed.iter().any(|(f, _)| *f == id) && !meshed.contains(&id) && spined != Some(id) {
sel.push((id, vec![]));
}
}
+131
View File
@@ -246,6 +246,137 @@ fn shift_click_picks_several_spine_points_which_drag_together() {
assert!(matches!(a.last(), Some(Action::Preview(_, v)) if v["anchor"] == 0 && v.get("with").is_none()), "{a:?}");
}
/// [`blend_doc`] with a spine through (110, 310), (160, 310) and (210, 310).
fn blend_doc_with_three_spine_points() -> (vectorcraft_doc::Document, NodeId) {
let (mut d, g, _) = blend_doc();
let sp = vectorcraft_geom::SubPath::polyline(&[Point::new(110.0, 310.0), Point::new(160.0, 310.0), Point::new(210.0, 310.0)], false);
let Some(NodeKind::Blend { spec, .. }) = d.node_mut(g).map(|n| &mut n.kind) else { panic!("blend") };
(spec.spine, spec.key_anchors) = (Some(PathData::single(sp)), vec![0, 2]);
(d, g)
}
/// A marquee drawn with Direct Selection from `from` to `to`, released.
fn spine_marquee(t: &mut DirectSelectionTool, cx: &ToolContext, from: (f64, f64), to: (f64, f64), mods: Mods) -> Vec<Action> {
assert_eq!(t.pointer(cx, &PointerEvent::new(PointerKind::Down, from.0, from.1).with_mods(mods)), vec![]);
t.pointer(cx, &PointerEvent::new(PointerKind::Drag, to.0, to.1).with_mods(mods));
assert!(t.overlays(cx).iter().any(|o| matches!(o, Overlay::Marquee(_))), "a marquee");
t.pointer(cx, &PointerEvent::new(PointerKind::Up, to.0, to.1).with_mods(mods))
}
/// A Direct Selection marquee picks the points of a selected blend's spine inside it, which then
/// drag together, and keeps the blend selected; a marquee around none of them drops the picks.
#[test]
fn marquee_picks_selected_blend_spine_points_which_drag_together() {
let (d, g) = blend_doc_with_three_spine_points();
let mut s = Selection::default();
s.set([g]);
let p = paint();
let cx = cx(&d, &s, &p);
let filled = |t: &DirectSelectionTool| t.overlays(&cx).iter().filter(|o| matches!(o, Overlay::Anchor { filled: true, .. })).count();
let mut t = DirectSelectionTool::new(false);
// Around (160, 310) and (210, 310), the second on the rect's edge.
let a = spine_marquee(&mut t, &cx, (230.0, 330.0), (160.0, 290.0), Mods::default());
assert!(
matches!(a.first(), Some(Action::Exec(c, v)) if c == "select.anchorsMany" && v["mode"] == "set" && v["items"].as_array().is_some_and(|i| i.iter().any(|i| i["id"] == g.0))),
"the blend stays selected: {a:?}"
);
assert_eq!(filled(&t), 2, "both picked points show");
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 210.0, 310.0));
let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, 210.0, 330.0));
assert_eq!(
a.last(),
Some(&Action::Preview("object.blend.spine.moveAnchor".into(), json!({"id": g.0, "anchor": 2, "x": 210.0, "y": 330.0, "with": [1]})))
);
assert_eq!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 210.0, 330.0)), vec![Action::Commit]);
// A marquee around none of the points drops them.
spine_marquee(&mut t, &cx, (300.0, 330.0), (240.0, 290.0), Mods::default());
assert_eq!((t.spine, filled(&t)), (None, 0));
}
/// With Shift, a Direct Selection marquee toggles the spine points inside it: a picked one leaves,
/// another joins, and those outside it keep their state; the blend is not toggled away.
#[test]
fn shift_marquee_toggles_selected_blend_spine_points() {
let (d, g) = blend_doc_with_three_spine_points();
let mut s = Selection::default();
s.set([g]);
let p = paint();
let cx = cx(&d, &s, &p);
let shift = Mods { shift: true, ..Mods::default() };
let mut t = DirectSelectionTool::new(false);
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 160.0, 310.0));
t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 160.0, 310.0));
let a = spine_marquee(&mut t, &cx, (230.0, 330.0), (150.0, 290.0), shift);
assert!(
matches!(a.first(), Some(Action::Exec(c, v)) if c == "select.anchorsMany" && v["items"].as_array().is_some_and(|i| !i.iter().any(|i| i["id"] == g.0))),
"the blend is not toggled: {a:?}"
);
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 2)), vec![]));
spine_marquee(&mut t, &cx, (130.0, 330.0), (90.0, 290.0), shift);
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 2)), vec![0]));
// A Shift-marquee around none of them keeps them, and keeps the blend selected when it
// touches the blend's art.
spine_marquee(&mut t, &cx, (300.0, 330.0), (240.0, 290.0), shift);
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 2)), vec![0]));
let a = spine_marquee(&mut t, &cx, (230.0, 330.0), (205.0, 315.0), shift);
assert!(
matches!(a.first(), Some(Action::Exec(c, v)) if c == "select.anchorsMany" && v["items"].as_array().is_some_and(|i| !i.iter().any(|i| i["id"] == g.0))),
"the blend is not toggled: {a:?}"
);
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 2)), vec![0]));
}
/// A Direct Selection marquee does not pick the spine points of a blend that is not selected, or
/// of a selected blend that is locked or hidden.
#[test]
fn marquee_skips_unselected_locked_and_hidden_blend_spines() {
let (d, g) = blend_doc_with_three_spine_points();
let p = paint();
let mut s = Selection::default();
let none = cx(&d, &s, &p);
let mut t = DirectSelectionTool::new(false);
spine_marquee(&mut t, &none, (230.0, 330.0), (150.0, 290.0), Mods::default());
assert_eq!(t.spine, None, "not selected");
s.set([g]);
for lock in [true, false] {
let mut d = d.clone();
let Some(n) = d.node_mut(g) else { panic!("blend") };
if lock {
n.locked = true;
} else {
n.visible = false;
}
let c = cx(&d, &s, &p);
let mut t = DirectSelectionTool::new(false);
spine_marquee(&mut t, &c, (230.0, 330.0), (150.0, 290.0), Mods::default());
assert_eq!(t.spine, None, "locked: {lock}");
}
// The same marquee on the selected blend picks them.
let c = cx(&d, &s, &p);
let mut t = DirectSelectionTool::new(false);
spine_marquee(&mut t, &c, (230.0, 330.0), (150.0, 290.0), Mods::default());
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 2)), vec![1]));
}
/// Escape during a Direct Selection marquee cancels it and keeps the spine points picked before.
#[test]
fn escape_cancels_a_marquee_and_keeps_the_picked_spine_points() {
let (d, g) = blend_doc_with_three_spine_points();
let mut s = Selection::default();
s.set([g]);
let p = paint();
let cx = cx(&d, &s, &p);
let mut t = DirectSelectionTool::new(false);
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 160.0, 310.0));
t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 160.0, 310.0));
t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 300.0, 330.0));
t.pointer(&cx, &PointerEvent::new(PointerKind::Drag, 90.0, 290.0));
assert_eq!(t.key(&cx, ToolKey::Escape, Mods::default()), vec![Action::Cancel]);
assert_eq!(t.pointer(&cx, &PointerEvent::new(PointerKind::Up, 90.0, 290.0)), vec![]);
assert_eq!((t.spine, t.spine_more.clone()), (Some((g, 1)), vec![]));
assert!(t.overlays(&cx).iter().any(|o| matches!(o, Overlay::Anchor { filled: true, .. })), "the picked point shows");
}
/// #1111: the Anchor Point tool bends a selected blend's straight spine; Direct Selection
/// reshapes a curved spine segment and leaves a straight one alone, as it does a path's.
#[test]