diff --git a/crates/behavior/src/hit.rs b/crates/behavior/src/hit.rs index 1a77ccf26..0ca496cdd 100644 --- a/crates/behavior/src/hit.rs +++ b/crates/behavior/src/hit.rs @@ -30,13 +30,15 @@ pub enum HitShape { /// The other end. b: Pos2, }, - /// A chain of segments, hit within [`LINE_REACH`]. + /// A chain of segments, hit within `reach` ([`LINE_REACH`] unless [`HitShape::polyline_within`] says otherwise). #[non_exhaustive] Polyline { /// The corners in order. points: Vec, /// Whether the last corner joins the first. closed: bool, + /// How far from the chain a point still hits it, in points. + reach: f32, }, /// A filled polygon. Polygon(Vec), @@ -62,9 +64,16 @@ impl HitShape { HitShape::Segment { a, b } } - /// A chain of segments, closed when `closed`. + /// A chain of segments, closed when `closed`, hit within [`LINE_REACH`]. pub fn polyline(points: Vec, closed: bool) -> Self { - HitShape::Polyline { points, closed } + HitShape::Polyline { points, closed, reach: LINE_REACH } + } + + /// A chain of segments, closed when `closed`, hit within `reach` points: a tool whose measured reach differs from + /// a line's. A negative or non-finite `reach` is [`LINE_REACH`]. + pub fn polyline_within(points: Vec, closed: bool, reach: f32) -> Self { + let reach = if reach.is_finite() && reach >= 0.0 { reach } else { LINE_REACH }; + HitShape::Polyline { points, closed, reach } } /// The part of `within` outside `polygon`. @@ -95,7 +104,8 @@ impl HitShape { } HitShape::Circle { center, radius } => center.distance(point) <= radius.max(across / 2.0), HitShape::Segment { a, b } => segment_distance(point, *a, *b) <= reach, - HitShape::Polyline { points, closed } => { + HitShape::Polyline { points, closed, reach: own } => { + let reach = own.max(across / 2.0); let closing = closed.then(|| points.first().zip(points.last())).flatten(); points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= reach) || closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= reach) @@ -116,10 +126,10 @@ impl HitShape { HitShape::Rect(rect) => rect.contains(point), HitShape::Circle { center, radius } => center.distance(point) <= *radius, HitShape::Segment { a, b } => segment_distance(point, *a, *b) <= LINE_REACH, - HitShape::Polyline { points, closed } => { + HitShape::Polyline { points, closed, reach } => { let closing = closed.then(|| points.first().zip(points.last())).flatten(); - points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= LINE_REACH) - || closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= LINE_REACH) + points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= *reach) + || closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= *reach) } HitShape::Polygon(points) => inside_polygon(point, points), HitShape::OutsidePolygon { within, polygon } => within.contains(point) && !inside_polygon(point, polygon), @@ -330,4 +340,18 @@ mod tests { let tie = [shape(big(), Sense::CLICK, 1, 0, HitLevel::Body, 0), shape(big(), Sense::CLICK, 2, 0, HitLevel::Body, 0)]; assert_eq!(owner(&tie), Some(2)); } + + #[test] + fn a_polyline_hits_within_its_own_reach_and_a_bad_reach_is_a_lines() { + let points = vec![pos2(0.0, 0.0), pos2(100.0, 0.0)]; + let line = HitShape::polyline(points.clone(), false); + assert!(line.hit(pos2(50.0, LINE_REACH)) && !line.hit(pos2(50.0, LINE_REACH + 0.5))); + let wide = HitShape::polyline_within(points.clone(), false, 5.0); + assert!(wide.hit(pos2(50.0, 5.0)) && wide.hit(pos2(50.0, -5.0)) && !wide.hit(pos2(50.0, 5.5))); + let hits = |shape: HitShape, y: f32| Shape::new(shape, Sense::CLICK, 0_u8, None, HitPriority::BODY, 0).min_across(8.0).hits(pos2(50.0, y)); + assert!(hits(wide.clone(), 5.0) && !hits(wide, 5.5), "a target size under twice the reach keeps the reach"); + for bad in [f32::NAN, f32::INFINITY, -1.0] { + assert_eq!(HitShape::polyline_within(points.clone(), false, bad), line); + } + } } diff --git a/crates/draw/src/ui/pen/bezier/mod.rs b/crates/draw/src/ui/pen/bezier/mod.rs index d99b541df..f5554222b 100644 --- a/crates/draw/src/ui/pen/bezier/mod.rs +++ b/crates/draw/src/ui/pen/bezier/mod.rs @@ -47,6 +47,11 @@ use super::{PathOptionsSettings, PathTool}; /// Insert on a picked anchor adds one halfway along the segment leaving it. const INSERT: Chord = Chord::plain(Key::Insert); +/// How far from a segment, in screen points, an Add Anchor press or hover still takes it: the reference application +/// adds an anchor 5 px from a segment at 100 % and none at 6 px. The other tools keep a line's reach +/// ([`LINE_REACH`](craft_behavior::hit::LINE_REACH)). +const ADD_ANCHOR_REACH: f32 = 5.0; + /// Above this many anchors in the paths shown, the overlay draws outlines only and declares no part (no anchor in /// the AccessKit tree either) (VectorCraft's `OVERLAY_MAX_ANCHORS`). pub const MAX_ANCHORS: usize = 100_000; @@ -66,14 +71,14 @@ pub enum BezierTool { Convert, /// 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. 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. With ⌥ a click on a segment selects its whole subpath and a drag moves a - /// copy of it ([`PathEdit::DuplicateSubpath`]; the subpath itself on a path that takes no copies), and a press on - /// an anchor does nothing. Hover: the segment mark with the anchor a click adds ([`HitMark`]) and the pen with "+"; - /// over an anchor the arrow; with ⌥ no mark and the arrow with "+"; the crosshair under the canvas's precise cursor - /// ([`CanvasSpace::precise`]). + /// Add Anchor Point: a press on a segment, within 5 screen points of it, inserts an anchor at the hit's `t` + /// ([`PathEdit::InsertAnchor`]); a drag 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. With ⌥ a click on a segment selects + /// its whole subpath and a drag moves a copy of it ([`PathEdit::DuplicateSubpath`]; the subpath itself on a path that + /// takes no copies), and a press on an anchor does nothing. Hover: the segment mark with the anchor a click adds + /// ([`HitMark`]) and the pen with "+"; over an anchor the arrow; with ⌥ no mark and the arrow with "+"; the + /// crosshair under the canvas's precise cursor ([`CanvasSpace::precise`]). AddAnchor, /// Delete Anchor Point: a click on an anchor reports [`delete`](BezierOutput::delete) of that anchor; ⌥ makes it /// Add Anchor Point, except that a ⌥ click on an anchor selects its whole subpath. Hover: the anchor mark. @@ -508,13 +513,18 @@ impl<'a> BezierOverlay<'a> { let start = if ends && i == 0 { reach } else { 0.0 }; let end = if ends && i + 1 == sub.segment_count() { reach } else { 0.0 }; let Some(points) = trim_polyline(points, start, end) else { continue }; + let shape = if self.tool == BezierTool::AddAnchor { + HitShape::polyline_within(points, false, ADD_ANCHOR_REACH) + } else { + HitShape::polyline(points, false) + }; items.push(Item { kind: Kind::Segment, path: at, key: path.key, knot: KnotRef::new(s, i), at: corners[0], - shape: HitShape::polyline(points, false), + shape, interior: true, straight: sub.is_straight(i), shown: true, diff --git a/crates/draw/tests/it/bezier.rs b/crates/draw/tests/it/bezier.rs index 440c9188c..beeb41ff6 100644 --- a/crates/draw/tests/it/bezier.rs +++ b/crates/draw/tests/it/bezier.rs @@ -919,6 +919,63 @@ fn add_anchor_selects_and_edits_existing_anchors_and_handles() { } } +// Add Anchor reaches a segment within 5 screen points, as the reference application does (PhotoCraft #12732): a click +// 2 to 5 points above or below a straight segment adds an anchor and shows the pen with "+", one 5.5 or 6 points away +// adds nothing; Delete Anchor and Direct Selection keep a line's reach. +#[trial] +fn add_anchor_reaches_a_segment_within_5_points() { + use craft_behavior::{Cursor, CursorBadge, PaintedCursor}; + let segment = p(110.0, 60.0); + let at_tool = |tool: BezierTool| { + let mut h = two_paths(); + h.update(|st| st.tool = tool); + h + }; + let click = |h: &mut Harness, at: Pos2| { + h.press_at(at); + h.release_at(at); + }; + let pen_plus = |h: &Harness| { + matches!( + craft_ui::runtime::cursor::current(h.fixture.ctx()), + Some(Cursor::Painted(PaintedCursor::Tool { badge: Some(CursorBadge::Add), .. })) + ) + }; + for off in [2.0_f32, 3.0, 4.5, 5.0, -2.0, -5.0] { + let mut h = at_tool(BezierTool::AddAnchor); + let at = h.screen(segment) + vec2(0.0, off); + h.move_to(at); + h.fixture.frame(Vec::new()); + assert!(pen_plus(&h), "{off}: the pen with + shows where a click adds"); + click(&mut h, at); + let st = h.state.borrow(); + assert!(matches!(st.changes.last(), Some(PathEdit::InsertAnchor { path: 1, segment, .. }) if *segment == kr(0)), "{off}: {:?}", st.changes); + assert_eq!(st.paths[0].subpaths[0].knots.len(), 5, "{off}"); + } + for off in [5.5_f32, 6.0, -6.0] { + let mut h = at_tool(BezierTool::AddAnchor); + let at = h.screen(segment) + vec2(0.0, off); + h.move_to(at); + h.fixture.frame(Vec::new()); + assert!(!pen_plus(&h), "{off}: no + past the reach"); + click(&mut h, at); + let st = h.state.borrow(); + assert!(st.changes.is_empty() && st.seen.is_empty(), "{off}: {:?} {:?}", st.changes, st.seen); + assert_eq!(st.paths, [path_a(), path_b()], "{off}"); + } + // The other tools keep a line's reach: 4.5 points from the segment misses it, 3 points hits it. + for tool in [BezierTool::DeleteAnchor, BezierTool::Direct] { + let mut h = at_tool(tool); + let near = h.screen(segment) + vec2(0.0, 3.0); + h.drag(near, vec2(0.0, FAR)); + assert!(matches!(h.last_change(), Some(PathEdit::BendSegment { path: 1, .. })), "{tool:?}: {:?}", h.last_change()); + let mut h = at_tool(tool); + let past = h.screen(segment) + vec2(0.0, 4.5); + h.drag(past, vec2(0.0, FAR)); + assert!(!matches!(h.last_change(), Some(PathEdit::BendSegment { .. })), "{tool:?}: {:?}", h.last_change()); + } +} + // 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 (with "+" under ⌥, PhotoCraft #12709), the pen // with "-" for Delete Anchor, the Convert Point icon, the arrow for Direct Selection and under ⌘, the crosshair when