Skip to content

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

Remove tool: every queued stroke is tinted in full (#6555) (#6642)

David Mann0296c8e0b5c2
diff --git a/crates/ui-egui/src/retouch_ui.rs b/crates/ui-egui/src/retouch_ui.rs
index adf3c8cf6..417df92b6 100644
--- a/crates/ui-egui/src/retouch_ui.rs
+++ b/crates/ui-egui/src/retouch_ui.rs
@@ -177,7 +177,7 @@ pub fn remove_pending_add(app: &mut PhotocraftApp, points: &[[f64; 3]]) {
app.remove_pending_doc = Some(doc);
}
let diameter = app.session.tools.brush.size;
- app.remove_trail.get_or_insert_with(|| crate::stroke_trail::Trail::new(size)).feed(points, diameter);
+ app.remove_trail.get_or_insert_with(|| crate::stroke_trail::Trail::new(size)).feed_stroke(points, diameter);
app.remove_pending.push(points.to_vec());
app.ui.status = crate::i18n::fmt(tl!("Strokes to remove: {n} (Enter applies, Esc cancels)"), &[("n", &app.remove_pending.len().to_string())]);
app.ui.status_error = false;
@@ -1141,6 +1141,46 @@ mod tests {
assert!(!remove_pending_commit(&mut app), "nothing left to apply");
}
+ #[test]
+ fn every_waiting_remove_stroke_is_tinted_in_full() {
+ // #6555: each stroke used to be stamped only past the previous stroke's point count.
+ let mut app = app();
+ app.ui.tool = Tool::Remove;
+ app.ui.tool_options.remove_after_each_stroke = false;
+ let strokes: [&[[f64; 3]]; 3] = [
+ &[[10.0, 10.0, 1.0], [40.0, 10.0, 1.0], [70.0, 10.0, 1.0]],
+ &[[10.0, 30.0, 1.0], [40.0, 30.0, 1.0]],
+ &[[10.0, 50.0, 1.0], [20.0, 50.0, 1.0], [30.0, 50.0, 1.0], [50.0, 50.0, 1.0], [90.0, 50.0, 1.0]],
+ ];
+ for s in strokes {
+ remove_pending_add(&mut app, s);
+ }
+ assert_eq!(app.remove_pending.len(), 3);
+ let t = app.remove_trail.as_ref().expect("the waiting strokes are tinted");
+ let k = f64::from(t.scale());
+ for s in strokes {
+ for w in s.windows(2) {
+ for i in 0..=8 {
+ let f = f64::from(i) / 8.0;
+ let (x, y) = (w[0][0] + (w[1][0] - w[0][0]) * f, w[0][1] + (w[1][1] - w[0][1]) * f);
+ assert_eq!(t.coverage((x / k) as usize, (y / k) as usize), 255, "({x}, {y}) on a waiting stroke is tinted");
+ }
+ }
+ }
+ // Enter, Esc, another tool and another document all drop the tint with the strokes.
+ assert!(remove_pending_commit(&mut app));
+ assert!(app.remove_trail.is_none());
+ remove_pending_add(&mut app, strokes[0]);
+ assert!(remove_pending_cancel(&mut app) && app.remove_trail.is_none());
+ remove_pending_add(&mut app, strokes[1]);
+ app.ui.tool = Tool::SpotHealing;
+ assert!(!remove_pending_live(&mut app) && app.remove_trail.is_none());
+ app.ui.tool = Tool::Remove;
+ remove_pending_add(&mut app, strokes[2]);
+ app.run("file.new", json!({"width": 100, "height": 60})).unwrap();
+ assert!(!remove_pending_live(&mut app) && app.remove_trail.is_none());
+ }
+
#[test]
fn remove_strokes_waiting_are_dropped_when_the_tool_changes() {
let mut app = app();
diff --git a/crates/ui-egui/src/stroke_trail.rs b/crates/ui-egui/src/stroke_trail.rs
index 6a5465ca7..af09fca1c 100644
--- a/crates/ui-egui/src/stroke_trail.rs
+++ b/crates/ui-egui/src/stroke_trail.rs
@@ -74,6 +74,13 @@ impl Trail {
self.fed = points.len();
}
+ /// Stamp a whole new drag into the same mask (the Remove tool's waiting strokes, #6555): its
+ /// points are counted from its own start, not from where the previous drag ended.
+ pub fn feed_stroke(&mut self, points: &[[f64; 3]], diameter: f32) {
+ self.fed = 0;
+ self.feed(points, diameter);
+ }
+
/// Show the coverage the brush's own dabs leave along the drag, as the stroke commit renders
/// it (`photocraft_paint::retouch::stroke_coverage`): soft edges for a soft brush, the size and
/// shape each dab takes from pen pressure and dynamics. `brush` is asked for once, on the first
@@ -224,6 +231,24 @@ mod tests {
}
}
+ #[test]
+ fn each_stroke_fed_whole_is_stamped_in_full() {
+ let mut t = Trail::new([300, 300]);
+ let strokes: [&[[f64; 3]]; 3] = [
+ &[[50.0, 50.0, 1.0], [120.0, 50.0, 1.0], [200.0, 50.0, 1.0]],
+ &[[50.0, 120.0, 1.0], [120.0, 120.0, 1.0]],
+ &[[50.0, 200.0, 1.0], [80.0, 200.0, 1.0], [120.0, 200.0, 1.0], [160.0, 200.0, 1.0], [200.0, 200.0, 1.0], [260.0, 200.0, 1.0]],
+ ];
+ for s in strokes {
+ t.feed_stroke(s, 10.0);
+ }
+ for (x, y) in [(50, 50), (85, 50), (199, 50), (50, 120), (85, 120), (119, 120), (50, 200), (65, 200), (100, 200), (259, 200)] {
+ assert_eq!(t.coverage(x, y), 255, "({x}, {y}) lies on a stroke");
+ }
+ assert_eq!(t.coverage(50, 85), 0, "no capsule joins one stroke's end to the next one's start");
+ assert_eq!(t.coverage(150, 160), 0);
+ }
+
#[test]
fn hostile_points_and_huge_documents_are_safe() {
let mut t = Trail::new([300_000, 2]);