Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(brush): preserve pen pressure when joining Shift-click strokes (#2827) (#2890)

Nigel Ihza Firdausy03d45a80eee0
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 709e14c29..a1fcda0ad 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -4271,16 +4271,17 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
}
crate::paint_mouse::sync_tool_brush(app);
let erase = tool == Tool::Eraser || std::mem::take(&mut app.secondary_erase);
- // ⇧-click after a stroke: a straight line from where it ended (stroke_constraint.rs).
+ // ⇧-click connects to the last endpoint, including its pen pressure (#2827), so
+ // the renderer interpolates size and opacity across the segment.
let active = app.session.active().map(|st| st.doc.id);
let from = app.last_stroke_end.filter(|(doc, _)| mods.shift && crate::stroke_constraint::connects(tool) && Some(*doc) == active).map(|(_, p)| p);
let mut points = vec![[x, y, pressure as f64]];
if let Some(p) = from {
- points.insert(0, [p[0], p[1], pressure as f64]);
+ points.insert(0, p);
}
// ⌥ flips Dodge/Burn and Blur/Sharpen for this stroke.
let tool = crate::retouch_ui::alt_flipped(tool, mods.alt);
- app.drag = Some(Drag::new(tool, from.unwrap_or([x, y]), points, mods, erase));
+ app.drag = Some(Drag::new(tool, from.map_or([x, y], |p| [p[0], p[1]]), points, mods, erase));
app.trail = None;
app.stylus.begin_stroke();
if from.is_some() {
@@ -4567,7 +4568,7 @@ pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) {
if crate::stroke_constraint::connects(d.tool)
&& let Some(st) = app.session.active()
{
- app.last_stroke_end = Some((st.doc.id, [end[0], end[1]]));
+ app.last_stroke_end = Some((st.doc.id, end));
}
// A live Clone Stamp preview ends here; the commit below replaces it.
if live_retouch_command(d.tool).is_some() || crate::retouch_ui::dab_params(app, d.tool).is_some() {
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 0fe775243..8a7a49759 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -446,8 +446,8 @@ pub struct PhotocraftApp {
/// A live painting stroke started on the press (`canvas_view`): the drag egui recognises later,
/// or the click, continues or ends it rather than starting another.
press_stroke: bool,
- /// End of the last painting stroke: ⇧-click draws a straight line from it (#178).
- last_stroke_end: Option<(DocId, [f64; 2])>,
+ /// Position and pen pressure at the last stroke's endpoint: ⇧-click interpolates from them (#178, #2827).
+ last_stroke_end: Option<(DocId, [f64; 3])>,
/// Control+Alt-drag brush resize in progress (`brush_resize`, #231).
pub(crate) brush_resize: Option<brush_resize::Resize>,
/// A ⌘⌥⌃-click layer pick is in progress; its drag and release are swallowed (`quick_pick`).
diff --git a/crates/ui-egui/src/stroke_constraint.rs b/crates/ui-egui/src/stroke_constraint.rs
index d16db5d0d..a7cd51cb9 100644
--- a/crates/ui-egui/src/stroke_constraint.rs
+++ b/crates/ui-egui/src/stroke_constraint.rs
@@ -5,6 +5,7 @@
//! direction locks once the pointer has moved a few screen points.
//! - ⇧-click after a stroke paints a straight line from the end of the previous stroke to the
//! click. The line is part of the new stroke, so it is one `paint.stroke` and one undo step.
+//! The endpoint remembers pen pressure so size and opacity interpolate between the strokes.
//! - The Gradient tool's ⇧ snaps the gradient angle to 45° increments.
//!
//! Points go through the normal drag/`LiveStroke`/commit path, so the live preview, pen dynamics
@@ -89,7 +90,7 @@ pub fn line_preview_start(app: &crate::PhotocraftApp, tool: Tool, shift: bool) -
return None;
}
let active = app.session.active()?.doc.id;
- app.last_stroke_end.filter(|(doc, _)| *doc == active).map(|(_, p)| p)
+ app.last_stroke_end.filter(|(doc, _)| *doc == active).map(|(_, p)| [p[0], p[1]])
}
/// Photoshop's rubber band: while ⇧ is held after a stroke, a thin line from where that stroke
@@ -241,6 +242,39 @@ mod tests {
assert!(alpha(&app, 20, 100) > 0.9 && alpha(&app, 40, 80) > 0.9 && alpha(&app, 90, 30) > 0.9);
}
+ #[test]
+ fn shift_click_interpolates_from_previous_strokes_endpoint_pressure() {
+ let mut app = app();
+ app.ui.tool = Tool::Brush;
+ app.session.tools.brush.pressure_size = true;
+ app.session.tools.brush.pressure_opacity = true;
+ let none = egui::Modifiers::NONE;
+ tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 0.2 }, none);
+ tool_event(&mut app, ToolEvent::Move { x: 20.0, y: 20.0, pressure: 0.35 }, none);
+ tool_event(&mut app, ToolEvent::Up { x: 20.0, y: 20.0 }, none);
+ let before = app.session.documents()[0].history.past_len();
+
+ let shift = egui::Modifiers::SHIFT;
+ tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 100.0, pressure: 0.85 }, shift);
+ tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 100.0 }, shift);
+ let (_, stroke) = app.session.journal.iter().rev().find(|(id, _)| id == "paint.stroke").expect("connecting stroke committed");
+ let points = stroke["points"].as_array().expect("stroke points array");
+ assert_eq!(points.len(), 2, "one connecting segment, one stroke");
+ assert_eq!(last_stroke_points(&app), vec![[20.0, 20.0], [100.0, 100.0]]);
+ let pressure = |i: usize| points[i][2].as_f64().expect("pen pressure in stroke point");
+ assert!((pressure(0) - 0.35).abs() < 1e-6, "line starts at previous pen pressure, not the new click's");
+ assert!((pressure(1) - 0.85).abs() < 1e-6, "line ends at the new click's pen pressure");
+ assert_eq!(app.session.documents()[0].history.past_len(), before + 1, "connecting line is one undo step");
+
+ // Another Shift-click uses the just-completed endpoint, not the first stroke's.
+ tool_event(&mut app, ToolEvent::Down { x: 80.0, y: 30.0, pressure: 0.55 }, shift);
+ tool_event(&mut app, ToolEvent::Up { x: 80.0, y: 30.0 }, shift);
+ let (_, next) = app.session.journal.iter().rev().find(|(id, _)| id == "paint.stroke").expect("next connecting stroke");
+ let next = next["points"].as_array().expect("next stroke points");
+ assert!((next[0][2].as_f64().unwrap() - 0.85).abs() < 1e-6);
+ assert!((next[1][2].as_f64().unwrap() - 0.55).abs() < 1e-6);
+ }
+
#[test]
fn shift_click_without_a_previous_stroke_is_a_dab() {
let mut app = app();