PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix logic Show the clone crosshair only while Option is held (#301) (#301)
Sebastian Solano4b76aff35d30
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex fbb67e338..f277275a4 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -207,6 +207,19 @@ pub(crate) fn pencil_cursor_rect(xf: &ViewXform, doc: [f64; 2], size: f32, ppp:Rect::from_min_max(pos2(snap(r.min.x), snap(r.min.y)), pos2(snap(r.max.x), snap(r.max.y)))}+/// Whether a brush-tip circle draws its centre mark.+/// Clone Stamp and Healing Brush stay an empty circle until Option is held, the source-point+/// cursor. Other brushes follow the cursor preference, the large-tip mark, or the Background Eraser.+fn brush_tip_centre(tool: Tool, option: bool, show_crosshair: bool, radius: f32) -> bool {+ if tool == Tool::QuickSelection {+ return false;+ }+ if matches!(tool, Tool::CloneStamp | Tool::Healing) {+ return option || show_crosshair;+ }+ show_crosshair || radius > 6.0 || tool == Tool::BackgroundEraser+}+fn begin_live_stroke(app: &PhotocraftApp) -> Option<LiveStroke> {static STROKES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);let st = app.session.active()?;@@ -1626,9 +1639,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:_ => {painter.circle_stroke(p, r + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140)));painter.circle_stroke(p, r, Stroke::new(1.0, Color32::from_white_alpha(220)));- // The Background Eraser always shows its sampling hotspot (Photoshop).- // Quick Selection shows its +/− badge there instead.- if (cur.show_crosshair_in_brush_tip || r > 6.0 || tool == Tool::BackgroundEraser) && tool != Tool::QuickSelection {+ if brush_tip_centre(tool, alt || app.ui.shell.sticky_alt, cur.show_crosshair_in_brush_tip, r) {crosshair(3.0);}egui::CursorIcon::None@@ -2324,6 +2335,18 @@ mod tests {}}+ #[test]+ fn clone_stamp_centre_appears_only_with_option() {+ assert!(!brush_tip_centre(Tool::CloneStamp, false, false, 20.0));+ assert!(brush_tip_centre(Tool::CloneStamp, true, false, 20.0));+ assert!(brush_tip_centre(Tool::Healing, true, false, 20.0));+ assert!(!brush_tip_centre(Tool::Healing, false, false, 20.0));+ assert!(brush_tip_centre(Tool::CloneStamp, false, true, 20.0));+ assert!(brush_tip_centre(Tool::Brush, false, false, 20.0));+ assert!(!brush_tip_centre(Tool::QuickSelection, false, false, 20.0));+ assert!(brush_tip_centre(Tool::BackgroundEraser, false, false, 2.0));+ }+#[test]fn view_transform_roundtrip() {for flip in [false, true] {diff --git a/crates/ui-egui/src/preset_panels.rs b/crates/ui-egui/src/preset_panels.rsindex 54ffe80e1..7820639da 100644--- a/crates/ui-egui/src/preset_panels.rs+++ b/crates/ui-egui/src/preset_panels.rs@@ -1036,12 +1036,15 @@ pub fn clone_source_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}/// Where the active clone source samples for document point `at` (for the canvas marker).+/// The first stroke anchors at its press, so the marker follows before the stroke commits.pub fn clone_sample_point(app: &PhotocraftApp, at: Option<[f64; 2]>) -> Option<[f64; 2]> {use photocraft_engine::presets::clone_source::{Mapping, transform_matrix};let s = app.session.presets.clone.active();let src = s.source?;- match (s.anchor, at) {- (Some(a), Some(h)) if app.ui.tool_options.clone_aligned => {+ let stroking = app.drag.as_ref().filter(|d| matches!(d.tool, Tool::CloneStamp | Tool::Healing)).map(|d| d.start);+ let anchor = if app.ui.tool_options.clone_aligned { s.anchor.or(stroking) } else { stroking };+ match (anchor, at) {+ (Some(a), Some(h)) => {let m = Mapping { source: (src[0], src[1]), anchor: (a[0], a[1]), m: transform_matrix(s.scale, s.rotation, s.flip_h, s.flip_v) };let (x, y) = m.map(h[0], h[1]);Some([x, y])@@ -1119,6 +1122,9 @@ mod tests {assert_eq!(clone_sample_point(&app, Some([5.0, 5.0])), None);app.run("cloneSource.set", json!({"source": [10, 10]})).unwrap();assert_eq!(clone_sample_point(&app, Some([30.0, 30.0])), Some([10.0, 10.0]));+ app.drag = Some(crate::canvas::Drag::new(Tool::CloneStamp, [40.0, 40.0], vec![[50.0, 30.0, 1.0]], egui::Modifiers::NONE, false));+ assert_eq!(clone_sample_point(&app, Some([50.0, 30.0])), Some([20.0, 0.0]));+ app.drag = None;app.session.presets.clone.active_mut().anchor = Some([20.0, 20.0]);assert_eq!(clone_sample_point(&app, Some([30.0, 30.0])), Some([20.0, 20.0]));}diff --git a/crates/ui-egui/src/retouch_ui.rs b/crates/ui-egui/src/retouch_ui.rsindex c9ac8f7bd..8afb4bce3 100644--- a/crates/ui-egui/src/retouch_ui.rs+++ b/crates/ui-egui/src/retouch_ui.rs@@ -109,7 +109,9 @@ pub fn draw_source_marker(app: &PhotocraftApp, painter: &egui::Painter, xf: &Vieif !matches!(app.ui.tool, Tool::CloneStamp | Tool::Healing) {return;}- let src = match (crate::preset_panels::clone_sample_point(app, app.hover_doc), app.ui.clone_offset, app.hover_doc, app.ui.clone_source) {+ let at =+ app.drag.as_ref().filter(|d| matches!(d.tool, Tool::CloneStamp | Tool::Healing)).and_then(|d| d.points.last().map(|p| [p[0], p[1]])).or(app.hover_doc);+ let src = match (crate::preset_panels::clone_sample_point(app, at), app.ui.clone_offset, at, app.ui.clone_source) {(Some(p), ..) => Some(p),(None, Some(off), Some(h), _) => Some([h[0] + off[0], h[1] + off[1]]),(None, None, _, Some(s)) => Some(s),