mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Pen: no rubber band where a click adds no segment, and the label measures the band
A click on the last anchor retracts its handle, an Alt click over the path goes to the Anchor Point tool, and a join with an end that lies on the last anchor merges the two ends. None of these adds a segment, so no rubber band shows there. The measurement label measures to the anchor the band ends on instead of the snapped pointer. A press and a hover no longer search for an end at the snapped point when snapping left the pointer where it was.
This commit is contained in:
@@ -47,6 +47,25 @@ fn band(s: &mut Session, v: ViewInfo, h: Point) -> CubicBez {
|
||||
}
|
||||
}
|
||||
|
||||
/// How many rubber bands show with the pointer hovering at `h` with Alt held as `alt` says.
|
||||
fn bands(s: &mut Session, v: ViewInfo, h: Point, alt: bool) -> usize {
|
||||
let mut ev = PointerEvent::new(PointerKind::Move, h.x, h.y);
|
||||
ev.mods.alt = alt;
|
||||
s.pointer(&ev, v).unwrap();
|
||||
s.overlays(v).into_iter().filter(|o| matches!(o, Overlay::Path { .. })).count()
|
||||
}
|
||||
|
||||
/// The measurement label shown with the pointer hovering at `h`.
|
||||
fn label(s: &mut Session, v: ViewInfo, h: Point) -> String {
|
||||
at(s, v, PointerKind::Move, h);
|
||||
let labels: Vec<String> =
|
||||
s.overlays(v).into_iter().filter_map(|o| if let Overlay::Measure { text, .. } = o { Some(text) } else { None }).collect();
|
||||
match labels.as_slice() {
|
||||
[text] => text.clone(),
|
||||
other => panic!("one measurement label, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn paths(s: &Session) -> Vec<PathData> {
|
||||
let mut v = vec![];
|
||||
s.doc().unwrap().doc.walk(|n| {
|
||||
@@ -134,3 +153,112 @@ fn rubber_band_picks_the_end_at_the_pointer_before_grid_snapping() {
|
||||
assert_eq!(path.subpaths[0].anchors.len(), 3);
|
||||
assert_eq!(path.subpaths[0].segment(0), preview);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rubber_band_joins_an_end_the_grid_snaps_to_from_a_corner() {
|
||||
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 on the grid and the pointer is out of its pick tolerance: only the snapped point
|
||||
// joins. The path ends on a corner, so only the end's own handle makes the segment a curve.
|
||||
let end = Point::new(100.0, 300.0);
|
||||
smooth_ended_path(&mut s, end, Point::new(130.0, 250.0), Point::new(13.0, 300.0));
|
||||
s.select_tool("pen", v).unwrap();
|
||||
click(&mut s, v, Point::new(300.0, 500.0));
|
||||
let pointer = Point::new(88.0, 300.0);
|
||||
let preview = band(&mut s, v, pointer);
|
||||
assert_eq!((preview.p1, preview.p2, preview.p3), (Point::new(300.0, 500.0), Point::new(130.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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measurement_label_measures_the_segment_a_click_adds() {
|
||||
let mut s = session();
|
||||
let mut prefs = s.prefs.clone();
|
||||
prefs.gridline_every = 100.0;
|
||||
prefs.grid_subdivisions = 1;
|
||||
s.apply_prefs(prefs);
|
||||
let grid = ViewInfo { snap_to_grid: true, ..view() };
|
||||
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", grid).unwrap();
|
||||
smooth(&mut s, grid, Point::new(300.0, 500.0), Point::new(350.0, 450.0));
|
||||
// The pointer snaps to (100, 300), and the click joins at the end: the label measures to the end.
|
||||
let joining = label(&mut s, grid, Point::new(64.0, 303.0));
|
||||
assert_eq!(joining, label(&mut s, view(), end));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_rubber_band_over_the_last_anchor() {
|
||||
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));
|
||||
smooth(&mut s, v, Point::new(300.0, 300.0), Point::new(350.0, 340.0));
|
||||
// A click there retracts the last anchor's outgoing handle and adds no segment.
|
||||
let h = Point::new(303.0, 300.0);
|
||||
assert_eq!(bands(&mut s, v, h, false), 0);
|
||||
click(&mut s, v, h);
|
||||
let [path] = paths(&s).try_into().unwrap();
|
||||
assert_eq!(path.subpaths[0].anchors.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_rubber_band_where_alt_converts_an_anchor() {
|
||||
let (mut s, v) = (session(), view());
|
||||
// Another path, in front, starts on an inner anchor of the path being drawn.
|
||||
let shared = Point::new(300.0, 100.0);
|
||||
let drawn = s
|
||||
.execute(
|
||||
"path.create",
|
||||
&json!({"anchors": [{"x": 100.0, "y": 100.0, "out": [150.0, 60.0]}, {"x": shared.x, "y": shared.y}, {"x": 300.0, "y": 300.0, "in": [250.0, 260.0], "out": [350.0, 340.0]}]}),
|
||||
)
|
||||
.unwrap()["id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
smooth_ended_path(&mut s, shared, Point::new(330.0, 50.0), Point::new(400.0, 50.0));
|
||||
s.execute("select.set", &json!({"ids": [drawn]})).unwrap();
|
||||
s.select_tool("pen", v).unwrap();
|
||||
// A click on the last anchor goes on drawing the path.
|
||||
click(&mut s, v, Point::new(300.0, 300.0));
|
||||
let paths_before = paths(&s).len();
|
||||
let h = Point::new(301.0, 100.0);
|
||||
assert_eq!(band(&mut s, v, h).p3, shared);
|
||||
// With Alt the click hands the anchor under the pointer to the Anchor Point tool and joins nothing.
|
||||
assert_eq!(bands(&mut s, v, h, true), 0);
|
||||
let mut down = PointerEvent::new(PointerKind::Down, h.x, h.y);
|
||||
down.mods.alt = true;
|
||||
s.pointer(&down, v).unwrap();
|
||||
let mut up = PointerEvent::new(PointerKind::Up, h.x, h.y);
|
||||
up.mods.alt = true;
|
||||
s.pointer(&up, v).unwrap();
|
||||
assert_eq!(paths(&s).len(), paths_before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_rubber_band_where_a_join_merges_the_ends() {
|
||||
let (mut s, v) = (session(), view());
|
||||
// Another path's end lies less than the join's merge distance from where this path ends.
|
||||
let drawn = s
|
||||
.execute("path.create", &json!({"anchors": [{"x": 100.0, "y": 100.0, "out": [150.0, 60.0]}, {"x": 300.0, "y": 300.0, "in": [250.0, 260.0], "out": [350.0, 340.0]}]}))
|
||||
.unwrap()["id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
smooth_ended_path(&mut s, Point::new(300.000_000_9, 300.0), Point::new(330.0, 250.0), Point::new(400.0, 300.0));
|
||||
s.execute("select.set", &json!({"ids": [drawn]})).unwrap();
|
||||
s.select_tool("pen", v).unwrap();
|
||||
// A click on the last anchor goes on drawing the path.
|
||||
click(&mut s, v, Point::new(300.0, 300.0));
|
||||
// Just out of the pick tolerance of the last anchor and in it of the other end: the click
|
||||
// merges the two ends into one anchor and adds no segment.
|
||||
let h = Point::new(305.000_000_5, 300.0);
|
||||
assert_eq!(bands(&mut s, v, h, false), 0);
|
||||
click(&mut s, v, h);
|
||||
let [path] = paths(&s).try_into().unwrap();
|
||||
assert_eq!(path.subpaths[0].anchors.len(), 3);
|
||||
}
|
||||
|
||||
+37
-15
@@ -53,6 +53,8 @@ pub struct PenTool {
|
||||
hover: Option<Point>,
|
||||
/// The pointer at the last hover before snapping: a press picks anchors there first.
|
||||
hover_raw: Option<Point>,
|
||||
/// The modifiers at the last hover: with Alt a press may go to the Anchor Point tool.
|
||||
hover_mods: Mods,
|
||||
/// 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
|
||||
@@ -300,6 +302,7 @@ impl Tool for PenTool {
|
||||
PointerKind::Move => {
|
||||
self.hover = Some(p);
|
||||
self.hover_raw = Some(ev.pos);
|
||||
self.hover_mods = ev.mods;
|
||||
vec![]
|
||||
}
|
||||
PointerKind::Down => {
|
||||
@@ -340,7 +343,9 @@ impl Tool for PenTool {
|
||||
}
|
||||
}
|
||||
// An end of another open path: the two become one, and the path is finished.
|
||||
if let Some((other, other_si, at_first)) = raw_other.or_else(|| open_end_at(cx, p, tol, Some((id, si)))) {
|
||||
if let Some((other, other_si, at_first)) =
|
||||
raw_other.or_else(|| if p != ev.pos { open_end_at(cx, p, tol, Some((id, si))) } else { None })
|
||||
{
|
||||
self.stop();
|
||||
let end = if at_first { "first" } else { "last" };
|
||||
let mut params = json!({"ids": [id.0, other.0], "ends": ["last", end]});
|
||||
@@ -527,11 +532,14 @@ impl Tool for PenTool {
|
||||
&& self.handle.is_none()
|
||||
&& 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 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);
|
||||
// The segment a click at `h` adds, if any: it leaves the last anchor along its outgoing
|
||||
// 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 segment =
|
||||
if cx.pen_rubber_band || cx.measurement_labels { click_end(cx, (id, si, first, last), raw, h, self.hover_mods) } else { None };
|
||||
if cx.pen_rubber_band
|
||||
&& let Some((end, end_h)) = segment
|
||||
{
|
||||
let mut bp = BezPath::new();
|
||||
bp.move_to(last);
|
||||
if out.distance(last) > 1e-9 || end_h.distance(end) > 1e-9 {
|
||||
@@ -541,8 +549,11 @@ impl Tool for PenTool {
|
||||
}
|
||||
o.push(Overlay::Path { path: bp, color: cx.doc.layer_color(id), width: 1.0, dashed: false });
|
||||
}
|
||||
if cx.measurement_labels && last.distance(h) > 1e-9 {
|
||||
o.push(Overlay::Measure { p: h + Vec2::new(cx.tol(12.0), cx.tol(12.0)), text: cx.segment_measure_label(last, h) });
|
||||
if cx.measurement_labels
|
||||
&& let Some((end, _)) = segment
|
||||
&& last.distance(end) > 1e-9
|
||||
{
|
||||
o.push(Overlay::Measure { p: h + Vec2::new(cx.tol(12.0), cx.tol(12.0)), text: cx.segment_measure_label(last, end) });
|
||||
}
|
||||
}
|
||||
o.extend_from_slice(self.snap.guides());
|
||||
@@ -652,27 +663,38 @@ impl PenTool {
|
||||
/// 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) {
|
||||
/// None where the click adds no segment: on the last anchor (its outgoing handle retracts), with
|
||||
/// Alt over the path (the Anchor Point tool takes the press, [`alt_converts`]), and on an end of
|
||||
/// another path that lies on the last anchor (`path.join` merges the two ends into one anchor).
|
||||
fn click_end(cx: &ToolContext, (id, si, first, last): (NodeId, usize, Point, Point), raw: Point, h: Point, m: Mods) -> Option<(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),
|
||||
Some(Endpoint::First) => return Some((first, first)),
|
||||
Some(Endpoint::Last) => return None,
|
||||
None => {}
|
||||
}
|
||||
if alt_converts(cx, raw, m) {
|
||||
return 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),
|
||||
Some(Endpoint::First) => return Some((first, first)),
|
||||
Some(Endpoint::Last) => return None,
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
let joined = raw_other.or_else(|| open_end_at(cx, h, tol, Some((id, si)))).and_then(|(other, osi, at_first)| {
|
||||
let other = raw_other.or_else(|| if h != raw { open_end_at(cx, h, tol, Some((id, si))) } else { None });
|
||||
let joined = other.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))
|
||||
match joined {
|
||||
Some((end, _)) if end.distance(last) < 1e-6 => None,
|
||||
Some(joined) => Some(joined),
|
||||
None => Some((h, h)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The first anchor of subpath `si` of path `id`.
|
||||
|
||||
Reference in New Issue
Block a user