mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 18:02:45 +00:00
Add Anchor: select and edit existing anchors and handles
In the reference application the Add Anchor Point tool edits the anchors and handles a path already has as Direct Selection does; only a press on a segment adds an anchor. The overlay took a press on an anchor and did nothing with it, and never took a handle. Under Add Anchor a press on an anchor now selects and drags it as Direct does (Shift adds to the selection), a press on a shown handle drags it (Alt moves it alone), and a click on empty canvas deselects the anchors and keeps the path. Alt on an anchor is still Delete Anchor's click, and a click on a segment still adds an anchor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
18ffc07519
commit
0274c3e5a0
@@ -67,7 +67,9 @@ pub enum BezierTool {
|
||||
/// Anchors drawn, not edited (PhotoCraft's Path Selection and Pen tools).
|
||||
Anchors,
|
||||
/// Add Anchor Point: a press on a segment inserts an anchor at the hit's `t` ([`PathEdit::InsertAnchor`]); a drag
|
||||
/// moves the new anchor, which is selected; ⌥ makes it Delete Anchor Point. Hover: the segment mark with the
|
||||
/// moves the new anchor, which is selected. A press on an anchor or a shown handle selects and drags it as
|
||||
/// [`BezierTool::Direct`] does (⇧ adds to the selection, ⌥ on a handle moves it alone), and a click on empty canvas
|
||||
/// deselects the anchors and keeps the path. ⌥ makes it Delete Anchor Point on segments and anchors. Hover: the segment mark with the
|
||||
/// anchor a click adds ([`HitMark`]) and the pen with "+"; over an anchor the arrow; the crosshair under the
|
||||
/// canvas's precise cursor ([`CanvasSpace::precise`]).
|
||||
AddAnchor,
|
||||
@@ -572,7 +574,8 @@ impl<'a> BezierOverlay<'a> {
|
||||
}
|
||||
|
||||
/// The target a press drags: for the tools that act on the press ([`BezierTool::clicks`]) an insert into a
|
||||
/// segment they add to, a segment Delete Anchor drags, or the ⌥ pick of a subpath, else nothing; for the others [`target`](Self::target).
|
||||
/// segment they add to, a segment Delete Anchor drags, an anchor or handle Add Anchor edits, or the ⌥ pick of a
|
||||
/// subpath, else nothing; for the others [`target`](Self::target).
|
||||
fn press_target(&mut self, hit: BezierHit, start: PressStart, mods: Mods, paths: &[EditPath]) -> Target {
|
||||
if !self.tool.clicks() {
|
||||
return self.target(hit, start, mods, paths);
|
||||
@@ -583,6 +586,10 @@ impl<'a> BezierOverlay<'a> {
|
||||
}
|
||||
// Delete Anchor drags a segment as Direct does; only a press on an anchor is its click.
|
||||
BezierHit::Segment { .. } if self.tool == BezierTool::DeleteAnchor => self.target(hit, start, mods, paths),
|
||||
// Add Anchor selects and edits the anchors and handles a path has, as Direct does (PhotoCraft #12708); ⌥
|
||||
// on an anchor is Delete Anchor's click.
|
||||
BezierHit::Handle { .. } if self.tool == BezierTool::AddAnchor => self.target(hit, start, mods, paths),
|
||||
BezierHit::Anchor { .. } if self.tool == BezierTool::AddAnchor && !mods.alt => self.target(hit, start, mods, paths),
|
||||
BezierHit::Anchor { path, knot } if self.tool.picks_subpath(mods) => Target::Pick { path, knot },
|
||||
_ => Target::Nothing,
|
||||
}
|
||||
@@ -841,6 +848,10 @@ impl<'a> BezierOverlay<'a> {
|
||||
}
|
||||
if matches!(press.phase, PressPhase::Down { start, .. } if start.button == PointerButton::Primary) {
|
||||
self.report.pressed_empty = true;
|
||||
// Add Anchor's click off the paths deselects the anchors; the path stays the one being edited.
|
||||
if self.tool == BezierTool::AddAnchor && (!self.selection.anchors.is_empty() || self.selection.segment.is_some()) {
|
||||
self.report.select = Some(self.deselected());
|
||||
}
|
||||
}
|
||||
}
|
||||
// A press none of the parts carries any more (its part went with the paths) is over for the overlay.
|
||||
@@ -1127,7 +1138,8 @@ pub struct BezierOutput {
|
||||
/// on an anchor reports that one anchor with `segments` false.
|
||||
pub delete: Option<AnchorDelete>,
|
||||
/// A press landed on empty canvas, outside every path, under Direct Selection (the press of its marquee), Add
|
||||
/// Anchor or Delete Anchor. Under the other tools the canvas's tool takes such a press.
|
||||
/// Anchor (which also deselects the anchors, [`select`](Self::select)) or Delete Anchor. Under the other tools the
|
||||
/// canvas's tool takes such a press.
|
||||
pub pressed_empty: bool,
|
||||
/// A secondary click on a part of a path, with its screen position, for the host's path menu.
|
||||
pub context: Option<(BezierHit, Pos2)>,
|
||||
|
||||
@@ -27,8 +27,8 @@ impl BezierTool {
|
||||
///
|
||||
/// - Direct: every part. Convert: anchors and handles, and a straight segment (a drag bends it, or moves a fully
|
||||
/// selected path); never a curved one.
|
||||
/// - Add Anchor: a segment, or an anchor (the press is taken so it starts no path; it reports nothing). Delete
|
||||
/// Anchor: an anchor, or a segment (taken, nothing reported). Handles are never taken.
|
||||
/// - Add Anchor: every part: a segment adds an anchor, an anchor or a shown handle is selected and dragged as
|
||||
/// under Direct. Delete Anchor: an anchor, or a segment (a drag bends it). Delete Anchor never takes a handle.
|
||||
/// - Auto Add/Delete: a segment, or an anchor that is not the first or last of an open subpath (those are the
|
||||
/// pen draft's: the first closes, the last retracts). Nothing else, so the pen draft starts a path on empty
|
||||
/// canvas.
|
||||
@@ -42,7 +42,8 @@ impl BezierTool {
|
||||
BezierTool::Convert => straight || !matches!(hit, BezierHit::Segment { .. }),
|
||||
BezierTool::Anchors => false,
|
||||
// The swap with ⌥ changes what a press reports, not what it takes.
|
||||
BezierTool::AddAnchor | BezierTool::DeleteAnchor | BezierTool::Curvature => !matches!(hit, BezierHit::Handle { .. }),
|
||||
BezierTool::AddAnchor => true,
|
||||
BezierTool::DeleteAnchor | BezierTool::Curvature => !matches!(hit, BezierHit::Handle { .. }),
|
||||
BezierTool::AutoAddDelete => match hit {
|
||||
BezierHit::Handle { .. } => false,
|
||||
BezierHit::Segment { .. } => true,
|
||||
@@ -106,9 +107,9 @@ pub(super) fn anchor_click(tool: BezierTool, hit: BezierHit, mods: Mods, selecti
|
||||
}
|
||||
}
|
||||
|
||||
/// The part the hover mark shows for `hit` with `mods` held: what a press would edit. Under Add an anchor and under
|
||||
/// Delete a segment are taken and do nothing (PhotoCraft draws no mark there), nor is a hidden anchor under Delete,
|
||||
/// which a click only shows. Curvature marks every part it takes.
|
||||
/// The part the hover mark shows for `hit` with `mods` held: what a press would add or delete. Under Add an anchor or
|
||||
/// a handle (which a press selects and drags as Direct does) and under Delete a segment get no mark (PhotoCraft draws
|
||||
/// none there), nor does a hidden anchor under Delete, which a click only shows. Curvature marks every part it takes.
|
||||
pub(super) fn hover_mark(tool: BezierTool, hit: BezierHit, mods: Mods, selection: &PathSelection) -> Option<BezierHit> {
|
||||
// pc: crates/ui-egui/src/tools/anchor_tools.rs:152-169 `draw_hover`, `draw_pen_hover`;
|
||||
// crates/ui-egui/src/tools/direct_select.rs:378-389 `curvature_hover`.
|
||||
|
||||
@@ -744,6 +744,112 @@ fn add_anchor_shows_the_pen_with_plus_over_a_segment_and_the_arrow_over_an_ancho
|
||||
}
|
||||
}
|
||||
|
||||
// Add Anchor selects and edits the anchors and handles a path has, as Direct Selection does and as the reference
|
||||
// application measures (PhotoCraft #12708): a click on an anchor selects it and ⇧ adds one, with no edit; a drag moves
|
||||
// the anchor in one gesture; dragging a smooth anchor's handle turns the other one, ⌥ moves it alone; a click on empty
|
||||
// canvas deselects the anchors and keeps the path; a click on a segment still adds an anchor; ⌥ on an anchor is still
|
||||
// Delete Anchor's click.
|
||||
#[trial]
|
||||
fn add_anchor_selects_and_edits_existing_anchors_and_handles() {
|
||||
let add_anchor = || {
|
||||
let mut h = two_paths();
|
||||
h.update(|st| st.tool = BezierTool::AddAnchor);
|
||||
h
|
||||
};
|
||||
let click = |h: &mut Harness, at: Pos2| {
|
||||
h.press_at(at);
|
||||
h.release_at(at);
|
||||
};
|
||||
let (corner, smooth) = (p(160.0, 60.0), p(260.0, 60.0));
|
||||
|
||||
let mut h = add_anchor();
|
||||
let at = h.screen(corner);
|
||||
click(&mut h, at);
|
||||
assert_eq!(h.state.borrow().selection, sel(1, &[(0, 1)]));
|
||||
let at = h.screen(smooth);
|
||||
h.fixture.hold(egui::Modifiers::SHIFT);
|
||||
click(&mut h, at);
|
||||
h.fixture.hold(egui::Modifiers::NONE);
|
||||
{
|
||||
let st = h.state.borrow();
|
||||
assert_eq!(st.selection, sel(1, &[(0, 1), (0, 2)]));
|
||||
assert!(st.changes.is_empty() && st.seen.is_empty(), "a click edits nothing: {:?} {:?}", st.changes, st.seen);
|
||||
assert_eq!(st.paths, [path_a(), path_b()]);
|
||||
}
|
||||
|
||||
// A drag on an anchor moves it with its handles, in one gesture, and adds nothing.
|
||||
let mut h = add_anchor();
|
||||
let at = h.screen(corner);
|
||||
h.drag(at, vec2(30.0, -20.0));
|
||||
{
|
||||
let st = h.state.borrow();
|
||||
assert!(
|
||||
matches!(st.changes.last(), Some(PathEdit::MoveAnchors { path: 1, anchors, by }) if anchors == &[kr(1)] && near(Point::ZERO + *by, p(30.0, -20.0))),
|
||||
"{:?}",
|
||||
st.changes
|
||||
);
|
||||
assert_eq!((st.seen.first(), st.seen.last()), (Some(&Seen::Begin), Some(&Seen::Commit)), "{:?}", st.seen);
|
||||
assert_eq!(st.paths[0].subpaths[0].knots.len(), 4, "no anchor added");
|
||||
assert!(near(st.paths[0].subpaths[0].knots[1].anchor, p(190.0, 40.0)));
|
||||
}
|
||||
|
||||
// The smooth anchor's out handle, shown once the anchor is selected: a drag turns the in handle opposite, with its
|
||||
// length; ⌥ moves the dragged handle alone.
|
||||
for alt in [false, true] {
|
||||
let mut h = add_anchor();
|
||||
let at = h.screen(smooth);
|
||||
click(&mut h, at);
|
||||
let handle = h.screen(p(290.0, 100.0));
|
||||
h.fixture.hold(if alt { egui::Modifiers::ALT } else { egui::Modifiers::NONE });
|
||||
h.drag(handle, vec2(0.0, 40.0));
|
||||
h.fixture.hold(egui::Modifiers::NONE);
|
||||
let st = h.state.borrow();
|
||||
assert!(
|
||||
matches!(st.changes.last(), Some(PathEdit::MoveHandle { path: 1, knot, side: HandleSide::Out, alone, .. }) if *knot == kr(2) && *alone == alt),
|
||||
"{alt}: {:?}",
|
||||
st.changes
|
||||
);
|
||||
let k = st.paths[0].subpaths[0].knots[2];
|
||||
assert!(near(k.out_ctrl, p(290.0, 140.0)), "{alt}: {k:?}");
|
||||
if alt {
|
||||
assert!(near(k.in_ctrl, p(230.0, 20.0)), "the in handle stays: {k:?}");
|
||||
} else {
|
||||
let (was, is) = (p(230.0, 20.0).distance(p(260.0, 60.0)), k.in_ctrl.distance(k.anchor));
|
||||
assert!((was - is).abs() < 1e-6, "the in handle keeps its length: {was} {is}");
|
||||
let (o, i) = (k.out_ctrl - k.anchor, k.in_ctrl - k.anchor);
|
||||
assert!((o.x * i.y - o.y * i.x).abs() < 1e-6 && o.dot(i) < 0.0, "the in handle turns opposite: {k:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// A click on empty canvas deselects the anchors and keeps the path, with no edit.
|
||||
let mut h = add_anchor();
|
||||
h.update(|st| st.selection = sel(1, &[(0, 1)]));
|
||||
let empty = h.screen(p(320.0, 250.0));
|
||||
click(&mut h, empty);
|
||||
{
|
||||
let st = h.state.borrow();
|
||||
assert_eq!(st.selection, sel(1, &[]));
|
||||
assert!(st.changes.is_empty() && st.seen.is_empty(), "{:?} {:?}", st.changes, st.seen);
|
||||
}
|
||||
|
||||
// A click on a segment still adds an anchor, the only one selected.
|
||||
let at = h.screen(p(110.0, 60.0));
|
||||
click(&mut h, at);
|
||||
{
|
||||
let st = h.state.borrow();
|
||||
assert!(matches!(st.changes.last(), Some(PathEdit::InsertAnchor { path: 1, .. })), "{:?}", st.changes);
|
||||
assert_eq!(st.selection, sel(1, &[(0, 1)]));
|
||||
}
|
||||
|
||||
// ⌥ on an anchor is still Delete Anchor's click.
|
||||
let mut h = add_anchor();
|
||||
let at = h.screen(corner);
|
||||
h.fixture.hold(egui::Modifiers::ALT);
|
||||
click(&mut h, at);
|
||||
h.fixture.hold(egui::Modifiers::NONE);
|
||||
assert_eq!(h.state.borrow().deletes, [AnchorDelete::new(1, vec![kr(1)], false)]);
|
||||
}
|
||||
|
||||
// The anchor tools' canvas tool: a press off the paths begins nothing (it never starts a path), and off the paths the
|
||||
// cursor says what a click does: the arrow for Add Anchor (the pen with "-" under ⌥, where a click deletes), the pen
|
||||
// with "-" for Delete Anchor, the Convert Point icon, the arrow for Direct Selection and under ⌘, the crosshair when
|
||||
|
||||
Reference in New Issue
Block a user