M1: Shift with the Pencil draws a straight line at 45° steps (#2447)

Shift did nothing with the Pencil: the stroke stayed freehand, while the
Line tool and the Pen keep theirs to 45° steps. pencil_add, shared by the
drag and the release, now keeps one straight segment from where the
stroke started to the pointer at the nearest 45° step while Shift is
held (the Marker and Highlighter too); a Shift-click without a move still
draws nothing.
This commit is contained in:
bflatastic
2026-11-19 06:30:36 +07:00
parent 41be247d9f
commit fbeea11c4c
6 changed files with 86 additions and 12 deletions
+21
View File
@@ -544,6 +544,27 @@ fn at_45_degrees(last: Point, p: Point) -> Point {
last + Vec2::from_angle(ang) * v.hypot()
}
/// Add where the pointer is to a Pencil (Marker, Highlighter) stroke: a sample once it has moved
/// half a screen pixel, or with Shift one straight segment from where the stroke started, at the
/// nearest 45° step, as the Line tool and the Pen keep theirs (#2447).
fn pencil_add(points: &mut Vec<Point>, pressure: &mut Vec<Option<f32>>, ev: &PointerEvent) {
let min = 0.5 / ev.zoom.max(1e-6);
if ev.shift
&& let (Some(&start), Some(&first)) = (points.first(), pressure.first())
{
let end = at_45_degrees(start, ev.p());
points.truncate(1);
pressure.truncate(1);
if (end - start).hypot() > min {
points.push(end);
pressure.push(ev.pressure.or(first));
}
} else if points.last().is_none_or(|l| (*l - ev.p()).hypot() > min) {
points.push(ev.p());
pressure.push(ev.pressure);
}
}
/// The handles a pen path shows while it is drawn ([`Overlay::pen_handles`]): the last anchor's in
/// and out handles and the one before's out handle, those that aren't zero.
fn pen_handles(points: &[(Point, Vec2, Vec2)]) -> Vec<[f64; 4]> {
+3 -6
View File
@@ -4,8 +4,8 @@ use super::handles::{
};
use super::{
Affine, ArcHandle, ArcHint, Cursor, Document, Gesture, Guide, Handle, ImageScaleMode, LayoutMode, NodeId, NodeKind, PaintKind, Point,
PointerEvent, Rect, ResizeStart, Result, ScaleHint, Session, Sizing, Targets, TextAutoResize, ToolId, Vec2, move_world, roots, select_target,
textedit, within_slop,
PointerEvent, Rect, ResizeStart, Result, ScaleHint, Session, Sizing, Targets, TextAutoResize, ToolId, Vec2, move_world, pencil_add, roots,
select_target, textedit, within_slop,
};
impl Session {
@@ -444,10 +444,7 @@ impl Session {
}
Gesture::Pencil { points, pressure } => {
let (mut pts, mut pressure) = (points.clone(), pressure.clone());
if pts.last().is_none_or(|l| (*l - ev.p()).hypot() > 0.5 / ev.zoom.max(1e-6)) {
pts.push(ev.p());
pressure.push(ev.pressure);
}
pencil_add(&mut pts, &mut pressure, ev);
self.tool.overlay.pen = pts.iter().map(|p| [p.x, p.y]).collect();
self.tool.gesture = Gesture::Pencil { points: pts, pressure };
if let Ok(ds) = self.ds_mut() {
+3 -5
View File
@@ -1,7 +1,8 @@
use super::handles::SNAP_PX;
use super::pen::take_enclosed;
use super::{
Affine, Cursor, Gesture, Node, NodeId, NodeKind, Point, PointerEvent, Rect, Result, Session, StrokeCap, Targets, ToolId, Vec2, frame_at, textedit,
Affine, Cursor, Gesture, Node, NodeId, NodeKind, Point, PointerEvent, Rect, Result, Session, StrokeCap, Targets, ToolId, Vec2, frame_at,
pencil_add, textedit,
};
impl Session {
@@ -240,10 +241,7 @@ impl Session {
self.tool.overlay.pen.clear();
self.tool.overlay.pen_color = None;
// The stroke ends where the button came up, even without a move to get there.
if points.last().is_none_or(|l| (*l - ev.p()).hypot() > 0.5 / ev.zoom.max(1e-6)) {
points.push(ev.p());
pressure.push(ev.pressure);
}
pencil_add(&mut points, &mut pressure, ev);
self.pencil_finish(&points, &pressure)?;
}
Gesture::Connect { from, start } => self.connect_up(ev, from, start)?,
+2
View File
@@ -497,6 +497,8 @@ mod pen_undo_tests;
#[cfg(test)]
mod pen_vector_edit_tests;
#[cfg(test)]
mod pencil_shift_tests;
#[cfg(test)]
mod penpot_export_tests;
#[cfg(test)]
mod pixel_preview_tests;
+55
View File
@@ -0,0 +1,55 @@
//! Shift with the Pencil draws one straight segment from where the stroke started, at the nearest
//! 45° step, as the Line tool and the Pen keep theirs (#2447).
use serde_json::json;
use crate::Session;
use crate::tools::{PointerEvent, PointerKind};
use figcraft_doc::NodeKind;
fn ev(kind: PointerKind, x: f64, y: f64, shift: bool) -> PointerEvent {
PointerEvent { shift, ..PointerEvent::at(kind, x, y, 1.0) }
}
/// Draws a wobbly stroke from (0, 0) to `end`, Shift held throughout or not, and returns the
/// stroke's size.
fn stroke(shift: bool, end: (f64, f64)) -> (f64, f64) {
let mut s = Session::new();
s.execute("tool.set", &json!({"tool": "pencil"})).unwrap();
s.pointer(&ev(PointerKind::Down, 0.0, 0.0, shift)).unwrap();
for i in 1..=20 {
let t = f64::from(i) / 20.0;
s.pointer(&ev(PointerKind::Move, end.0 * t, end.1 * t + (t * 9.0).sin() * 6.0, shift)).unwrap();
}
if shift {
assert_eq!(s.tool.overlay.pen.len(), 2, "the preview is one segment: {:?}", s.tool.overlay.pen);
}
s.pointer(&ev(PointerKind::Up, end.0, end.1, shift)).unwrap();
let doc = s.doc().unwrap();
let n = doc.nodes().find(|n| matches!(n.kind, NodeKind::Vector { .. })).expect("a pencil stroke");
let b = doc.world_bounds(n.id).unwrap();
(b.width(), b.height())
}
#[test]
fn shift_keeps_a_pencil_stroke_straight_at_45_degree_steps() {
// Nearly level: level, as long as the pointer is far from the start.
let (w, h) = stroke(true, (100.0, 12.0));
assert!((w - 100.0_f64.hypot(12.0)).abs() < 0.5 && h < 0.5, "{w} x {h}");
// Nearly diagonal: at 45°.
let (w, h) = stroke(true, (80.0, 70.0));
assert!((w - h).abs() < 0.5 && (w.hypot(h) - 80.0_f64.hypot(70.0)).abs() < 0.5, "{w} x {h}");
// Without Shift the stroke follows the pointer.
let (_, h) = stroke(false, (100.0, 12.0));
assert!(h > 12.0, "{h}");
}
#[test]
fn a_shift_click_with_the_pencil_draws_nothing() {
let mut s = Session::new();
s.execute("tool.set", &json!({"tool": "pencil"})).unwrap();
let before = s.doc().unwrap().nodes().count();
s.pointer(&ev(PointerKind::Down, 10.0, 10.0, true)).unwrap();
s.pointer(&ev(PointerKind::Up, 10.0, 10.0, true)).unwrap();
assert_eq!(s.doc().unwrap().nodes().count(), before);
}
+2 -1
View File
File diff suppressed because one or more lines are too long