Skip to content

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

Brush: draw the smoothing string from the brush to the pointer while drawing (#3479) (#3526)

Valentin Klein80d646fc1768
diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs
index c7c93ed93..2167cb1a0 100644
--- a/crates/engine/src/brush_cmds.rs
+++ b/crates/engine/src/brush_cmds.rs
@@ -536,6 +536,12 @@ impl LiveStroke {
self.mirrors.iter().zip(&self.mirror_distinct).filter(|(_, distinct)| **distinct).fold(bounds, |b, (r, _)| b.union(&r.bounds()))
}
+ /// `(brush, pointer)` document positions while smoothing holds the brush back from the
+ /// pointer: the canvas draws a line between them.
+ pub fn smoothing_string(&self) -> Option<([f64; 2], [f64; 2])> {
+ self.renderer.smoothing_string()
+ }
+
/// Does the stroke change with time while the pointer is held still (airbrush Build-up, or
/// smoothing still catching up)? See `photocraft_paint::dynamics::DabGenerator::wants_time`.
pub fn wants_time(&self) -> bool {
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index 829d2360f..61772e4ea 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -656,6 +656,25 @@ fn smoothing_modes() {
assert!(out.last().unwrap().x < 70.0, "{}", out.last().unwrap().x);
}
+#[test]
+fn smoothing_string_runs_from_the_lagging_brush_to_the_pointer() {
+ // #3479: the canvas draws the line between the brush and the pointer.
+ let cfg = Smoothing { amount: 0.8, ..Default::default() };
+ let mut s = Smoother::new(&cfg, 1.0);
+ let mut out = Vec::new();
+ assert_eq!(s.string(), None, "no stroke yet");
+ s.push(StrokePoint::new(0.0, 0.0, 1.0), &mut out);
+ assert_eq!(s.string(), None, "the first point is the brush");
+ s.push(StrokePoint::new(100.0, 0.0, 1.0), &mut out);
+ let (brush, pointer) = s.string().unwrap();
+ assert!(brush[0] > 0.0 && brush[0] < 100.0, "the brush lags: {brush:?}");
+ assert_eq!(pointer, [100.0, 0.0]);
+ let mut off = Smoother::new(&Smoothing { amount: 0.0, ..cfg }, 1.0);
+ off.push(StrokePoint::new(0.0, 0.0, 1.0), &mut out);
+ off.push(StrokePoint::new(100.0, 0.0, 1.0), &mut out);
+ assert_eq!(off.string(), None, "smoothing off: no string");
+}
+
#[test]
fn smoothing_pulls_by_time_not_by_how_often_the_device_reports() {
// The pointer jumps 100 px and is held there for 32 ms, reported at 1000 Hz or about 60 Hz.
diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs
index 5e8b22591..6fd444026 100644
--- a/crates/paint/src/dynamics.rs
+++ b/crates/paint/src/dynamics.rs
@@ -128,6 +128,14 @@ impl Smoother {
}
}
+ /// The string between the brush and the pointer: `(brush, pointer)` document positions while
+ /// smoothing holds the brush back from the last input point, `None` when they coincide (or
+ /// smoothing is off). The canvas draws it, as Photoshop does for Pulled String Mode.
+ pub fn string(&self) -> Option<([f64; 2], [f64; 2])> {
+ let (pos, last) = (self.pos?, self.last_input?);
+ (self.active() && (pos.x != last.x || pos.y != last.y)).then_some(([pos.x, pos.y], [last.x, last.y]))
+ }
+
/// End of stroke: with Catch-Up On Stroke End the brush finishes at the last pointer position.
pub fn finish(&mut self, out: &mut Vec<StrokePoint>) {
if !self.active() || !self.cfg.catch_up_on_end {
@@ -668,6 +676,11 @@ impl DabGenerator {
self.walker.interval.is_some() || self.smoother.lagging()
}
+ /// See [`Smoother::string`].
+ pub fn smoothing_string(&self) -> Option<([f64; 2], [f64; 2])> {
+ self.smoother.string()
+ }
+
pub fn new(brush: &BrushSettings, zoom: f32) -> Self {
let brush = brush.bounded_for_render();
let interval = brush.build_up.then(|| 1000.0 / f64::from(brush.build_up_rate.clamp(0.1, 1000.0)));
diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs
index 50ec0a18c..aa6bd0b96 100644
--- a/crates/paint/src/render.rs
+++ b/crates/paint/src/render.rs
@@ -649,6 +649,12 @@ impl StrokeRenderer {
self.generator.wants_time()
}
+ /// The string between the brush and the pointer while smoothing lags (see
+ /// [`Smoother::string`](crate::dynamics::Smoother::string)).
+ pub fn smoothing_string(&self) -> Option<([f64; 2], [f64; 2])> {
+ self.generator.smoothing_string()
+ }
+
/// Feed input points (any chunking gives the same result).
pub fn push(&mut self, pts: &[StrokePoint]) {
self.generator.push(pts, &mut self.dab_buf, &mut self.dual_buf);
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index b3e6c864e..5b68f6579 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -4103,6 +4103,19 @@ fn draw_transform_controls(app: &mut PhotocraftApp, painter: &egui::Painter, xf:
}
}
+/// The line from the brush to the pointer while stroke smoothing lags (Pulled String Mode and
+/// Stroke Catch-up, #3479): a dark underlay and a light line stay visible on any pixels, and a dot
+/// marks the brush end.
+fn draw_smoothing_string(painter: &egui::Painter, xf: &ViewXform, (brush, pointer): ([f64; 2], [f64; 2])) {
+ let (a, b) = (xf.to_screen(brush[0] as f32, brush[1] as f32), xf.to_screen(pointer[0] as f32, pointer[1] as f32));
+ if a.distance(b) < 2.0 {
+ return;
+ }
+ painter.line_segment([a, b], Stroke::new(3.0, Color32::from_black_alpha(110)));
+ painter.line_segment([a, b], Stroke::new(1.0, Color32::WHITE));
+ painter.circle_filled(a, 2.5, Color32::WHITE);
+}
+
fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
draw_tool_state(app, painter, xf, painter.ctx().input(|i| i.pointer.hover_pos()));
let Some(d) = &app.drag else {
@@ -4121,7 +4134,14 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
match d.tool {
// The canvas shows the live stroke itself (`LiveStroke`); a stroke that couldn't start one
// (Sample All Layers, say) keeps the trail.
- t if strokes_live(t) && app.live_stroke.is_some() => {}
+ t if strokes_live(t) && app.live_stroke.is_some() => {
+ // Smoothing holds the brush back from the pointer: draw the string between them.
+ if let Some(EngineStroke::Brush(l)) = app.live_stroke.as_ref().map(|l| &l.stroke)
+ && let Some(s) = l.smoothing_string()
+ {
+ draw_smoothing_string(painter, xf, s);
+ }
+ }
t if t.is_brushlike() || t == Tool::QuickSelection => {
// Retouching strokes preview as a translucent trail of the brush footprint: a mask,
// not a brush-wide egui polyline (which zoomed in tessellates into wedges, #189).
@@ -6486,20 +6506,44 @@ mod tests {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 120, "height": 60})).unwrap();
app.run("tools.setBrush", json!({"brush": {"size": 30, "hardness": 0.0}})).unwrap();
+ // A smoothed stroke also draws its string line (see
+ // `smoothed_brush_drag_draws_the_string_from_brush_to_pointer`): that is no stand-in shape.
+ let overlay = |app: &mut PhotocraftApp, ctx: &egui::Context| {
+ drag_preview_shapes(app, ctx, 12.0).iter().filter(|s| !matches!(s.shape, egui::Shape::LineSegment { .. } | egui::Shape::Circle(_))).count()
+ };
for tool in [Tool::Brush, Tool::Eraser] {
app.ui.tool = tool;
- let idle = drag_preview_shapes(&mut app, &ctx, 12.0).len();
+ let idle = overlay(&mut app, &ctx);
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE);
for i in 1..40 {
let t = f64::from(i);
tool_event(&mut app, ToolEvent::Move { x: 20.0 + t, y: 30.0 + (t / 3.0).sin() * 4.0, pressure: 1.0 }, egui::Modifiers::NONE);
}
assert!(app.live_stroke.is_some(), "{tool:?}: the canvas shows the live stroke");
- assert_eq!(drag_preview_shapes(&mut app, &ctx, 12.0).len(), idle, "{tool:?}: no overlay while dragging");
+ assert_eq!(overlay(&mut app, &ctx), idle, "{tool:?}: no overlay while dragging");
tool_event(&mut app, ToolEvent::Up { x: 59.0, y: 30.0 }, egui::Modifiers::NONE);
}
}
+ #[test]
+ fn smoothed_brush_drag_draws_the_string_from_brush_to_pointer() {
+ // #3479: with smoothing the brush lags behind the pointer; a line joins them while drawing.
+ let ctx = egui::Context::default();
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.run("file.new", json!({"width": 120, "height": 60})).unwrap();
+ app.ui.tool = Tool::Brush;
+ let lines = |app: &mut PhotocraftApp| drag_preview_shapes(app, &ctx, 4.0).iter().filter(|s| matches!(s.shape, egui::Shape::LineSegment { .. })).count();
+ for (amount, string) in [(0.0, false), (0.8, true)] {
+ app.run("tools.setBrush", json!({"brush": {"size": 10, "smoothing": {"amount": amount}}})).unwrap();
+ tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ for x in [30.0, 60.0, 90.0] {
+ tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ }
+ assert_eq!(lines(&mut app) > 0, string, "smoothing {amount}");
+ tool_event(&mut app, ToolEvent::Up { x: 90.0, y: 30.0 }, egui::Modifiers::NONE);
+ }
+ }
+
#[test]
fn retouch_drags_show_the_footprint_without_wedges() {
// The retouching tools' trail was the same brush-wide polyline (#189): now a mask of the