mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 14:17:14 +00:00
Eraser: the tip outline turns with a pen turned in place and passes 1296 pt
A drag or release within a pixel of the last stroke point now gives that point the pen's tilt and rotation, so the outline and the erased area follow a pen turned or tilted in place. A release without a pen reading keeps the last pose. The outline's size on Pressure, Tilt, Bearing or Rotation goes past 1296 pt, as path.eraseRegion sweeps it.
This commit is contained in:
@@ -941,6 +941,26 @@ impl GestureTool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Give the Eraser's last stroke sample the pen's tilt and rotation of the event `ev`, a drag
|
||||
/// within a pixel of that sample that adds none: a pen turned or tilted in place turns its tip
|
||||
/// there, in the outline and in what the release erases. The sample keeps its pressure, which
|
||||
/// the area outline of the drag was built with, and its pose when `ev` reads none.
|
||||
fn refresh_last_pose(&mut self, ev: &PointerEvent) {
|
||||
if self.id != "eraser" || self.poses.len() != self.points.len() || ev.pen.is_none() {
|
||||
return;
|
||||
}
|
||||
let pose = ev.pen.map(PenPose::sanitized);
|
||||
let Some(last) = self.poses.last_mut() else { return };
|
||||
let had_bearing = last.and_then(super::pen_bearing).is_some();
|
||||
*last = pose;
|
||||
// The bearing is that of the last sample the pen leaned at, as `push` keeps it.
|
||||
self.bearing = match pose.and_then(super::pen_bearing) {
|
||||
Some(b) => Some(b),
|
||||
None if had_bearing => self.poses.iter().rev().find_map(|q| q.and_then(super::pen_bearing)),
|
||||
None => self.bearing,
|
||||
};
|
||||
}
|
||||
|
||||
/// Would a press with `m` held draw a straight Pencil line? Shift or Alt held, unless Alt lends
|
||||
/// the Smooth tool: the rule `PointerKind::Down` sets `straight` by.
|
||||
fn straight_at_press(&self, m: Mods) -> bool {
|
||||
@@ -1133,7 +1153,8 @@ impl GestureTool {
|
||||
Variation::Rotation => controls[3],
|
||||
Variation::Fixed | Variation::Random => continue,
|
||||
};
|
||||
*v = Calligraphic::moved(i, *v, variations[i] * (2.0 * c - 1.0));
|
||||
// The size goes past a nib's 1296 pt, as `path.eraseRegion` sweeps it.
|
||||
*v = if i == 2 { super::pressure_size(*v, variations[i], c) } else { Calligraphic::moved(i, *v, variations[i] * (2.0 * c - 1.0)) };
|
||||
}
|
||||
let [angle, roundness, size] = out;
|
||||
[angle, roundness.clamp(1.0, 100.0), size.max(super::MIN_BRUSH_SIZE)]
|
||||
@@ -1461,6 +1482,9 @@ impl Tool for GestureTool {
|
||||
let release_end = kept.unwrap_or(end);
|
||||
self.near_start = self.release_closes_near_start(cx, release_end);
|
||||
self.connects = self.connects_at(cx, release_end).is_some();
|
||||
if self.points.last().is_some_and(|l| l.distance(p) < cx.tol(1.0)) {
|
||||
self.refresh_last_pose(ev);
|
||||
}
|
||||
return vec![];
|
||||
}
|
||||
self.push(p, ev);
|
||||
@@ -1514,13 +1538,17 @@ impl Tool for GestureTool {
|
||||
}
|
||||
} else if self.points.last().is_some_and(|l| l.distance(p) >= cx.tol(1.0)) {
|
||||
self.push(end, ev);
|
||||
} else if self.grid_snaps(cx)
|
||||
&& !self.smoothing
|
||||
&& self.points.len() > 1
|
||||
&& let Some(last) = self.points.last_mut()
|
||||
{
|
||||
// The last step was where the button went up: it moves where that snaps.
|
||||
*last = end;
|
||||
} else {
|
||||
// A release within a pixel of the last step gives it the pen's pose there.
|
||||
self.refresh_last_pose(ev);
|
||||
if self.grid_snaps(cx)
|
||||
&& !self.smoothing
|
||||
&& self.points.len() > 1
|
||||
&& let Some(last) = self.points.last_mut()
|
||||
{
|
||||
// The last step was where the button went up: it moves where that snaps.
|
||||
*last = end;
|
||||
}
|
||||
}
|
||||
let out = if cancel { vec![Action::Cancel] } else { self.finish(cx, ev.mods) };
|
||||
self.active = false;
|
||||
|
||||
@@ -2765,6 +2765,70 @@ fn the_eraser_tip_follows_its_variation_modes() {
|
||||
assert_eq!(b.options()["sizeMode"], json!("pressure"));
|
||||
}
|
||||
|
||||
/// The Eraser's tip outline follows a pen turned in place, which adds no stroke point, and the
|
||||
/// release erases with that reading; and its size passes 1296 pt as `path.eraseRegion` sweeps it.
|
||||
#[test]
|
||||
fn the_eraser_tip_turns_in_place_and_passes_a_nib_size() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let s = Selection::default();
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let tip = |t: &GestureTool| match &t.overlays(&cx)[..] {
|
||||
[.., Overlay::Path { path, color: [0, 0, 0], width: 1.0, .. }] => vectorcraft_geom::Shape::bounding_box(path),
|
||||
o => panic!("{o:?}"),
|
||||
};
|
||||
let near = |b: vectorcraft_geom::Rect, want: (f64, f64, f64, f64)| {
|
||||
assert!([b.x0 - want.0, b.y0 - want.1, b.x1 - want.2, b.y1 - want.3].iter().all(|e| e.abs() < 0.01), "{b:?} vs {want:?}");
|
||||
};
|
||||
let mut t = GestureTool::new("eraser");
|
||||
for (k, v) in [("size", json!(20)), ("roundness", json!(25)), ("angleMode", json!("rotation")), ("angleVariation", json!(90))] {
|
||||
t.set_option(k, &v);
|
||||
}
|
||||
let level = Some(PenPose { tilt: None, rotation: Some(0.0) });
|
||||
let turned = Some(PenPose { tilt: None, rotation: Some(180.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: level, pressure: 0.5, ..PointerEvent::new(PointerKind::Down, 100.0, 50.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: level, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
near(tip(&t), (190.0, 47.5, 210.0, 52.5));
|
||||
// The barrel turns half way round with the pointer still: the tip stands upright.
|
||||
t.pointer(&cx, &PointerEvent { pen: turned, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
near(tip(&t), (197.5, 40.0, 202.5, 60.0));
|
||||
let acts = t.pointer(&cx, &PointerEvent { pen: turned, pressure: 0.5, ..PointerEvent::new(PointerKind::Up, 200.0, 50.0) });
|
||||
let Some(Action::Exec(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(v["points"], json!([[100.0, 50.0, 0.5, null, null, 0.0], [200.0, 50.0, 0.5, null, null, 180.0]]));
|
||||
// A release at the last point erases with the pose the pen has as it lifts.
|
||||
t.pointer(&cx, &PointerEvent { pen: level, pressure: 0.5, ..PointerEvent::new(PointerKind::Down, 100.0, 50.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: level, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
let acts = t.pointer(&cx, &PointerEvent { pen: turned, pressure: 0.5, ..PointerEvent::new(PointerKind::Up, 200.0, 50.0) });
|
||||
let Some(Action::Exec(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(v["points"], json!([[100.0, 50.0, 0.5, null, null, 0.0], [200.0, 50.0, 0.5, null, null, 180.0]]));
|
||||
// A release there that reads no pose keeps the last one.
|
||||
t.pointer(&cx, &PointerEvent { pen: level, pressure: 0.5, ..PointerEvent::new(PointerKind::Down, 100.0, 50.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: turned, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
let acts = t.pointer(&cx, &PointerEvent { pressure: 0.5, ..PointerEvent::new(PointerKind::Up, 200.0, 50.0) });
|
||||
let Some(Action::Exec(_, v)) = acts.first() else { panic!("{acts:?}") };
|
||||
assert_eq!(v["points"], json!([[100.0, 50.0, 0.5, null, null, 0.0], [200.0, 50.0, 0.5, null, null, 180.0]]));
|
||||
// With Angle on Bearing, a pen that leaned only at the last point and stands up there again
|
||||
// leaves no bearing: the tip lies level, as set.
|
||||
t.set_option("angleMode", &json!("bearing"));
|
||||
let upright = Some(PenPose { tilt: Some([0.0, 0.0]), rotation: None });
|
||||
let leaning = Some(PenPose { tilt: Some([0.0, 45.0]), rotation: None });
|
||||
t.pointer(&cx, &PointerEvent { pen: upright, pressure: 0.5, ..PointerEvent::new(PointerKind::Down, 100.0, 50.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: leaning, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
assert!(tip(&t).height() > 6.0, "{:?}", tip(&t));
|
||||
t.pointer(&cx, &PointerEvent { pen: upright, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
near(tip(&t), (190.0, 47.5, 210.0, 52.5));
|
||||
t.pointer(&cx, &PointerEvent { pen: upright, pressure: 0.5, ..PointerEvent::new(PointerKind::Up, 200.0, 50.0) });
|
||||
// Size 1000 with a Variation of 1000 on Tilt, the pen upright: a tip 2000 pt across.
|
||||
let mut t = GestureTool::new("eraser");
|
||||
for (k, v) in [("size", json!(1000)), ("sizeMode", json!("tilt")), ("sizeVariation", json!(1000))] {
|
||||
t.set_option(k, &v);
|
||||
}
|
||||
let upright = Some(PenPose { tilt: Some([0.0, 0.0]), rotation: None });
|
||||
t.pointer(&cx, &PointerEvent { pen: upright, pressure: 0.5, ..PointerEvent::new(PointerKind::Down, 100.0, 50.0) });
|
||||
t.pointer(&cx, &PointerEvent { pen: upright, pressure: 0.5, ..PointerEvent::new(PointerKind::Drag, 200.0, 50.0) });
|
||||
near(tip(&t), (-800.0, -950.0, 1200.0, 1050.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smooth_needs_selection_and_brackets_resize() {
|
||||
let (d, _) = doc_with_rect();
|
||||
|
||||
Reference in New Issue
Block a user