Pen: the rubber band over the first anchor or another path's end is the segment a click adds

Over the path's first anchor, a click closes the path on that anchor; over
an end of another open path, it joins the two, and the segment arrives
along that end's own handle. The preview ended at the pointer and arrived
without a handle, so the segment moved by up to the pick tolerance at the
click and changed shape when the other end was smooth. The preview now
ends on the anchor the click uses, with that anchor's handle, and picks
the anchor at the pointer before snapping, in the order a press does.

Part of #8296.
This commit is contained in:
dotCooCoo
2026-11-18 06:28:21 -08:00
parent dce8a0c05e
commit 4f89536718
4 changed files with 177 additions and 5 deletions
+1
View File
@@ -27,6 +27,7 @@ mod param_keys;
mod paste_many;
mod pen_click_after_smooth;
mod pen_endpoint_picking;
mod pen_rubber_band;
mod ruler_guide_nodes;
mod ruler_guide_perf;
mod ruler_origin;
+136
View File
@@ -0,0 +1,136 @@
//! The Pen's rubber band after a smooth anchor is the segment the next click adds: a new corner,
//! a close on the first anchor, or a join with an end of another open path.
use serde_json::json;
use vectorcraft_doc::NodeKind;
use vectorcraft_engine::{Session, ViewInfo};
use vectorcraft_geom::{CubicBez, PathData, PathEl, Point};
use vectorcraft_tools::{Overlay, PointerEvent, PointerKind};
fn session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
s
}
fn view() -> ViewInfo {
ViewInfo { zoom: 1.0, smart_guides: false, snap_to_grid: false, snap_to_pixel: false, snap_to_point: false, ..Default::default() }
}
fn at(s: &mut Session, v: ViewInfo, kind: PointerKind, p: Point) {
s.pointer(&PointerEvent::new(kind, p.x, p.y), v).unwrap();
}
/// A press at `p` dragged to `out`: a smooth anchor with its outgoing handle at `out`.
fn smooth(s: &mut Session, v: ViewInfo, p: Point, out: Point) {
at(s, v, PointerKind::Down, p);
at(s, v, PointerKind::Drag, out);
at(s, v, PointerKind::Up, out);
}
fn click(s: &mut Session, v: ViewInfo, p: Point) {
at(s, v, PointerKind::Down, p);
at(s, v, PointerKind::Up, p);
}
/// The rubber band shown with the pointer hovering at `h`, as one cubic segment.
fn band(s: &mut Session, v: ViewInfo, h: Point) -> CubicBez {
at(s, v, PointerKind::Move, h);
let paths: Vec<Vec<PathEl>> =
s.overlays(v).into_iter().filter_map(|o| if let Overlay::Path { path, .. } = o { Some(path.elements().to_vec()) } else { None }).collect();
match paths.as_slice() {
[els] => match els.as_slice() {
[PathEl::MoveTo(p0), PathEl::CurveTo(p1, p2, p3)] => CubicBez::new(*p0, *p1, *p2, *p3),
other => panic!("the rubber band is one curve, got {other:?}"),
},
other => panic!("one rubber band, got {other:?}"),
}
}
fn paths(s: &Session) -> Vec<PathData> {
let mut v = vec![];
s.doc().unwrap().doc.walk(|n| {
if let NodeKind::Path { path, .. } = &n.kind {
v.push(path.clone());
}
});
v
}
/// An open path whose first anchor, at `end`, is smooth with its incoming handle at `h_in`.
fn smooth_ended_path(s: &mut Session, end: Point, h_in: Point, other: Point) {
s.execute(
"path.create",
&json!({"anchors": [{"x": end.x, "y": end.y, "in": [h_in.x, h_in.y], "out": [2.0 * end.x - h_in.x, 2.0 * end.y - h_in.y]}, {"x": other.x, "y": other.y}]}),
)
.unwrap();
s.execute("select.none", &json!({})).unwrap();
}
#[test]
fn rubber_band_is_the_corner_a_click_adds() {
let (mut s, v) = (session(), view());
s.select_tool("pen", v).unwrap();
smooth(&mut s, v, Point::new(100.0, 100.0), Point::new(150.0, 60.0));
let h = Point::new(300.0, 200.0);
let preview = band(&mut s, v, h);
click(&mut s, v, h);
let [path] = paths(&s).try_into().unwrap();
assert_eq!(path.subpaths[0].segment(0), preview);
}
#[test]
fn rubber_band_on_the_first_anchor_is_the_closing_segment() {
let (mut s, v) = (session(), view());
s.select_tool("pen", v).unwrap();
let first = Point::new(100.0, 100.0);
smooth(&mut s, v, first, Point::new(150.0, 60.0));
click(&mut s, v, Point::new(300.0, 100.0));
smooth(&mut s, v, Point::new(300.0, 300.0), Point::new(350.0, 340.0));
// Within the pick tolerance of the first anchor: the band ends on the anchor, not the pointer.
let preview = band(&mut s, v, Point::new(102.0, 101.0));
assert_eq!(preview.p3, first);
click(&mut s, v, Point::new(102.0, 101.0));
let [path] = paths(&s).try_into().unwrap();
let sp = &path.subpaths[0];
assert!(sp.closed);
assert_eq!(sp.segment(2), preview);
}
#[test]
fn rubber_band_on_another_paths_end_is_the_joining_segment() {
let (mut s, v) = (session(), view());
let end = Point::new(400.0, 300.0);
smooth_ended_path(&mut s, end, Point::new(430.0, 250.0), Point::new(500.0, 300.0));
s.select_tool("pen", v).unwrap();
smooth(&mut s, v, Point::new(100.0, 100.0), Point::new(150.0, 60.0));
let preview = band(&mut s, v, Point::new(402.0, 301.0));
assert_eq!((preview.p2, preview.p3), (Point::new(430.0, 250.0), end));
click(&mut s, v, Point::new(402.0, 301.0));
let [path] = paths(&s).try_into().unwrap();
assert_eq!(path.subpaths[0].anchors.len(), 3);
assert_eq!(path.subpaths[0].segment(0), preview);
}
#[test]
fn rubber_band_picks_the_end_at_the_pointer_before_grid_snapping() {
let mut s = session();
let mut prefs = s.prefs.clone();
prefs.gridline_every = 100.0;
prefs.grid_subdivisions = 1;
s.apply_prefs(prefs);
let v = ViewInfo { snap_to_grid: true, ..view() };
// The end is off the grid: the pointer next to it snaps to (100, 300), out of its pick tolerance,
// and a press there still joins at the pointer.
let end = Point::new(63.0, 303.0);
smooth_ended_path(&mut s, end, Point::new(90.0, 250.0), Point::new(13.0, 303.0));
s.select_tool("pen", v).unwrap();
smooth(&mut s, v, Point::new(300.0, 500.0), Point::new(350.0, 450.0));
let pointer = Point::new(64.0, 303.0);
let preview = band(&mut s, v, pointer);
assert_eq!((preview.p2, preview.p3), (Point::new(90.0, 250.0), end));
click(&mut s, v, pointer);
let [path] = paths(&s).try_into().unwrap();
assert_eq!(path.subpaths[0].anchors.len(), 3);
assert_eq!(path.subpaths[0].segment(0), preview);
}
+39 -5
View File
@@ -51,6 +51,8 @@ pub struct PenTool {
/// The anchor a press placed, while the drag shapes its handles.
drag: Option<Place>,
hover: Option<Point>,
/// The pointer at the last hover before snapping: a press picks anchors there first.
hover_raw: Option<Point>,
/// The incoming handle of the anchor being dragged out, as last previewed.
in_h: Point,
/// The first anchor, as it was when a press on it began closing the path, and its outgoing
@@ -297,6 +299,7 @@ impl Tool for PenTool {
match ev.kind {
PointerKind::Move => {
self.hover = Some(p);
self.hover_raw = Some(ev.pos);
vec![]
}
PointerKind::Down => {
@@ -522,17 +525,19 @@ impl Tool for PenTool {
// The rubber band preference hides only the preview segment; Measurement Labels decides the readout.
if self.drag.is_none()
&& self.handle.is_none()
&& let (Some((id, _, _, last, out)), Some(h)) = (self.active(cx), self.hover)
&& let (Some((id, si, first, last, out)), Some(h)) = (self.active(cx), self.hover)
{
if cx.pen_rubber_band {
// The segment a click at `h` adds: it leaves the last anchor along its outgoing
// handle and arrives at the new corner anchor without a handle.
// handle and arrives at the anchor the click ends it on, with that anchor's handle.
let raw = self.hover_raw.unwrap_or(h);
let (end, end_h) = click_end(cx, id, si, first, last, raw, h);
let mut bp = BezPath::new();
bp.move_to(last);
if out.distance(last) > 1e-9 {
bp.curve_to(out, h, h);
if out.distance(last) > 1e-9 || end_h.distance(end) > 1e-9 {
bp.curve_to(out, end_h, end);
} else {
bp.line_to(h);
bp.line_to(end);
}
o.push(Overlay::Path { path: bp, color: cx.doc.layer_color(id), width: 1.0, dashed: false });
}
@@ -641,6 +646,35 @@ impl PenTool {
}
}
/// Where a click ends the next segment of subpath `si` of path `id` (first anchor `first`, last
/// anchor `last`), and the incoming handle it arrives with, for a pointer at `raw` that snaps to
/// `h`. The anchors are picked in the order a press picks them: at `raw` first, then at `h`. On the
/// first anchor the path closes, with that anchor's incoming handle retracted
/// ([`PenTool::begin_close`]). On an end of another open path the two join, and the segment arrives
/// with that end's own handle on the joining side. Anywhere else the click adds a corner anchor at `h`.
fn click_end(cx: &ToolContext, id: NodeId, si: usize, first: Point, last: Point, raw: Point, h: Point) -> (Point, Point) {
let tol = cx.tol(5.0).max(cx.point_tol());
let closing_end = last_anchor(cx, id, Some(si)).filter(|(_, ai)| *ai > 0).map(|_| first);
match pick_endpoint(closing_end, last, raw, tol, Endpoint::First) {
Some(Endpoint::First) => return (first, first),
Some(Endpoint::Last) => return (h, h),
None => {}
}
let raw_other = open_end_at(cx, raw, tol, Some((id, si)));
if raw_other.is_none() && h != raw {
match pick_endpoint(closing_end, last, h, tol, Endpoint::First) {
Some(Endpoint::First) => return (first, first),
Some(Endpoint::Last) => return (h, h),
None => {}
}
}
let joined = raw_other.or_else(|| open_end_at(cx, h, tol, Some((id, si)))).and_then(|(other, osi, at_first)| {
let sp = cx.doc.node(other)?.path_data()?.subpaths.get(osi)?;
if at_first { sp.anchors.first().map(|a| (a.p, a.h_in)) } else { sp.anchors.last().map(|a| (a.p, a.h_out)) }
});
joined.unwrap_or((h, h))
}
/// The first anchor of subpath `si` of path `id`.
pub(crate) fn first_anchor(cx: &ToolContext, id: NodeId, si: usize) -> Option<Anchor> {
cx.doc.node(id)?.path_data()?.subpaths.get(si)?.anchors.first().copied()
File diff suppressed because one or more lines are too long