diff --git a/crates/tools/src/draw2/gesture.rs b/crates/tools/src/draw2/gesture.rs index e7858660f1..5e8b4a9344 100644 --- a/crates/tools/src/draw2/gesture.rs +++ b/crates/tools/src/draw2/gesture.rs @@ -192,6 +192,8 @@ pub struct GestureTool { bearing: Option, /// `bearing` as it was at each of `poses`, for the outline of the area the Eraser's drag covers. bearings: Vec>, + /// The first of `bearings` that the pen gave: the release gives it to the points before it. + first_bearing: Option, /// The length of the stroke, in points. length: f64, /// The outline of the area a Blob Brush or Eraser drag covers, as far as it is built @@ -874,6 +876,7 @@ impl GestureTool { light: false, bearing: None, bearings: vec![], + first_bearing: None, length: 0.0, area: Cell::new(None), area_pending: Cell::new(false), @@ -942,6 +945,7 @@ impl GestureTool { self.bearing = pose.and_then(super::pen_bearing).or(self.bearing); self.poses.push(pose); self.bearings.push(self.bearing); + self.first_bearing = self.first_bearing.or(self.bearing); } } @@ -966,6 +970,11 @@ impl GestureTool { if let Some(b) = self.bearings.last_mut() { *b = self.bearing; } + // The bearings carry forward, so the last sample gives the first bearing only when the one + // before it has none. + if self.bearings.len().checked_sub(2).and_then(|i| self.bearings.get(i)).copied().flatten().is_none() { + self.first_bearing = self.bearing; + } } /// Would a press with `m` held draw a straight Pencil line? Shift or Alt held, unless Alt lends @@ -1054,6 +1063,7 @@ impl GestureTool { self.light = false; self.bearing = None; self.bearings.clear(); + self.first_bearing = None; self.length = 0.0; self.area.set(None); self.area_pending.set(false); @@ -1149,13 +1159,16 @@ impl GestureTool { /// The tip at point `i` of the stroke, as `path.eraseRegion` reads the pen there: the pressure /// once the pen has pressed less than fully, the tilt and rotation of the point, and the - /// bearing of the last point up to it that the pen leaned 1° or more at ([`Self::tip_for`]). - /// What the release does not read ([`Self::reads_pressures`], [`Self::reads_poses`]) gives - /// the values themselves. + /// bearing of the last point up to it that the pen leaned 1° or more at, or before the first + /// such point the bearing of that first one ([`Self::tip_for`]). What the release does not + /// read ([`Self::reads_pressures`], [`Self::reads_poses`]) gives the values themselves. fn tip_of_point(&self, i: usize) -> [f64; 3] { let pressure = if self.reads_pressures() { self.pressures.get(i).map_or(0.5, |p| f64::from(*p)) } else { 0.5 }; - let (pose, bearing) = - if self.reads_poses() { (self.poses.get(i).copied().flatten(), self.bearings.get(i).copied().flatten()) } else { (None, None) }; + let (pose, bearing) = if self.reads_poses() { + (self.poses.get(i).copied().flatten(), self.bearings.get(i).copied().flatten().or(self.first_bearing)) + } else { + (None, None) + }; self.tip_for(pressure, pose, bearing) } @@ -1722,6 +1735,7 @@ impl Tool for GestureTool { varies: shaped || size_varies, shaped, pressed: self.reads_pressures() && self.tip_modes().contains(&Variation::Pressure), + first_bearing: self.first_bearing.filter(|_| self.reads_poses() && self.tip_modes().contains(&Variation::Bearing)), modes: self.tip_modes(), variations: [self.angle_variation, self.roundness_variation, self.size_variation], }; diff --git a/crates/tools/src/draw2/gesture/area.rs b/crates/tools/src/draw2/gesture/area.rs index 5a2319f571..3b92c0291f 100644 --- a/crates/tools/src/draw2/gesture/area.rs +++ b/crates/tools/src/draw2/gesture/area.rs @@ -79,6 +79,9 @@ pub(super) struct AreaKey { /// a point pressed fully takes the tip of full pressure instead of that of a stroke without /// pressure. pub pressed: bool, + /// The bearing the tips take at the points before the pen first leaned, when a tip follows the + /// bearing: the first lean gives it to the points already outlined. + pub first_bearing: Option, /// The modes and Variations of the angle, roundness and size that the tips at the points follow. pub modes: [Variation; 3], pub variations: [f64; 3], diff --git a/crates/tools/src/draw2/gesture/tests.rs b/crates/tools/src/draw2/gesture/tests.rs index 554442bfad..a9cb4947e1 100644 --- a/crates/tools/src/draw2/gesture/tests.rs +++ b/crates/tools/src/draw2/gesture/tests.rs @@ -3104,6 +3104,69 @@ fn the_eraser_drag_area_matches_the_release_when_pressure_or_a_mode_starts_durin press(&mut t, upright, 0.5, PointerKind::Up, 150.0); } +/// The release gives the points of an Eraser drag before the pen first leans the bearing of that +/// first lean, so the outline of a drag that starts upright and then leans is the outline of the +/// same drag leaning that way from the press, the steps already outlined included. +#[test] +fn the_eraser_drag_area_gives_the_first_lean_to_the_points_before_it() { + let (d, _) = doc_with_rect(); + let s = Selection::default(); + let p = paint(); + let cx = ToolContext { precise_cursors: true, ..cx(&d, &s, &p) }; + let bounds = |t: &GestureTool| { + drain_area(t, &cx); + let o = t.overlays(&cx); + o.iter() + .filter_map(|o| match o { + Overlay::Path { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(path)), + Overlay::SharedPath { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(&**path)), + _ => None, + }) + .reduce(|a, b| a.union(b)) + .unwrap_or_else(|| panic!("{o:?}")) + }; + // A tip 20 by 5 pt lying level, its angle on Bearing by up to 90° and its Roundness on + // Rotation by up to 50%, so that each step has its own tip from the press on. The barrel stays + // turned 30° all along. `in_place` is a tilt given last, by a drag within a pixel of the last + // point. + let drag = |poses: [[f32; 2]; 5], in_place: Option<[f32; 2]>| { + let mut t = GestureTool::new("eraser"); + for (k, v) in [ + ("size", json!(20)), + ("roundness", json!(25)), + ("angleMode", json!("bearing")), + ("angleVariation", json!(90)), + ("roundnessMode", json!("rotation")), + ("roundnessVariation", json!(50)), + ] { + t.set_option(k, &v); + } + let mut shown = vec![]; + for (i, (tilt, x)) in poses.into_iter().zip([50.0, 100.0, 150.0, 200.0, 250.0]).enumerate() { + let kind = if i == 0 { PointerKind::Down } else { PointerKind::Drag }; + let pen = Some(PenPose { tilt: Some(tilt), rotation: Some(30.0) }); + t.pointer(&cx, &PointerEvent { pen, pressure: 1.0, ..PointerEvent::new(kind, x, 150.0) }); + if i > 0 { + shown.push(bounds(&t)); + } + } + if let Some(tilt) = in_place { + let pen = Some(PenPose { tilt: Some(tilt), rotation: Some(30.0) }); + t.pointer(&cx, &PointerEvent { pen, pressure: 1.0, ..PointerEvent::new(PointerKind::Drag, 250.2, 150.0) }); + shown.push(bounds(&t)); + } + shown.last().copied().unwrap_or_else(|| panic!("no outline")) + }; + let same = |b: (vectorcraft_geom::Rect, vectorcraft_geom::Rect)| { + assert!([b.0.x0 - b.1.x0, b.0.y0 - b.1.y0, b.0.x1 - b.1.x1, b.0.y1 - b.1.y1].iter().all(|e| e.abs() < 0.01), "{b:?}"); + }; + let leaning = drag([[0.0, 45.0]; 5], None); + // Upright for four points, outlined as they come, then leaning toward the bottom of the page. + same((drag([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 45.0]], None), leaning)); + // Upright for all five, then leaning that way in place at the last. + same((drag([[0.0, 0.0]; 5], Some([0.0, 45.0])), drag([[0.0, 45.0]; 5], Some([0.0, 45.0])))); +} + /// The outline of a long Eraser drag whose tip turns and changes shape with the pen is built a /// bounded amount per frame, and once finished it shows nothing inside the area: no point of it /// lies well inside the area of any step, each step with the tip halfway between those at its