diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 077ed64a9b..7d0ba04a4e 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1297,6 +1297,16 @@ fn handle_canvas_tool_clicks( app.defer_live_stroke = false; crate::magnetic_lasso_ui::flush(app); } + // Perspective Crop: between corner clicks the preview quad follows the pointer (#11804). + if tool == Tool::PerspectiveCrop + && !app.pcrop.clicks.is_empty() + && !(buttons.started || buttons.dragged || buttons.stopped) + && response.hovered() + && let Some(p) = ui.input(|i| i.pointer.latest_pos()).filter(|p| rect.contains(*p)) + { + let d = xf.to_doc(p); + tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: 1.0 }, mods); + } if buttons.clicked && let Some(p) = response.interact_pointer_pos() { diff --git a/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs new file mode 100644 index 0000000000..aef086e406 --- /dev/null +++ b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs @@ -0,0 +1,190 @@ +//! #11804: while corners are clicked, the quad follows the pointer with its grid. After two clicks +//! the third and fourth corner are the pointer (a triangle), after three the fourth is. + +use super::*; + +/// A 200 × 150 document with the Perspective Crop tool. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 150})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::PerspectiveCrop; + app +} + +fn click(app: &mut PhotocraftApp, p: [f64; 2]) { + assert!(pointer(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, egui::Modifiers::NONE)); + assert!(pointer(app, ToolEvent::Up { x: p[0], y: p[1] }, egui::Modifiers::NONE)); +} + +/// The pointer moved with the button up. +fn hover(app: &mut PhotocraftApp, p: [f64; 2]) { + assert!(pointer(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 }, egui::Modifiers::NONE)); +} + +const A: [f64; 2] = [30.0, 20.0]; +const B: [f64; 2] = [170.0, 25.0]; +const C: [f64; 2] = [160.0, 130.0]; +const D: [f64; 2] = [40.0, 120.0]; + +#[test] +fn after_two_clicks_the_pointer_is_the_third_and_fourth_corner() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [120.0, 100.0]); + assert_eq!(click_preview(&app), Some([A, B, [120.0, 100.0], [120.0, 100.0]])); + // Every frame's move updates it. + hover(&mut app, [90.0, 140.0]); + assert_eq!(click_preview(&app), Some([A, B, [90.0, 140.0], [90.0, 140.0]])); +} + +#[test] +fn after_three_clicks_the_pointer_is_the_fourth_corner() { + let mut app = app(); + for p in [A, B, C] { + click(&mut app, p); + } + hover(&mut app, [45.0, 110.0]); + assert_eq!(click_preview(&app), Some([A, B, C, [45.0, 110.0]])); + hover(&mut app, D); + assert_eq!(click_preview(&app), Some([A, B, C, D])); +} + +#[test] +fn with_one_click_or_none_nothing_follows_the_pointer() { + let mut app = app(); + hover(&mut app, [100.0, 70.0]); + assert_eq!(click_preview(&app), None); + assert_eq!(app.pcrop.hover, None, "no clicks: the pointer isn't kept"); + click(&mut app, A); + hover(&mut app, [100.0, 70.0]); + assert_eq!(click_preview(&app), None); +} + +#[test] +fn esc_and_the_fourth_click_work_as_before() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [100.0, 70.0]); + cancel(&mut app); + assert!(app.pcrop.clicks.is_empty()); + assert_eq!(app.pcrop.hover, None); + assert_eq!(click_preview(&app), None); + // The fourth click makes the same ordered quad as without the preview, clicked in any order. + for p in [C, A, D] { + click(&mut app, p); + hover(&mut app, [100.0, 70.0]); + } + click(&mut app, B); + assert_eq!(app.ui.perspective_crop, Some(ordered([C, A, D, B]))); + assert_eq!(app.ui.perspective_crop, Some([A, B, C, D])); + assert_eq!(click_preview(&app), None); +} + +#[test] +fn a_non_finite_pointer_or_a_document_switch_leaves_no_preview() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [100.0, 70.0]); + hover(&mut app, [f64::NAN, 70.0]); + assert_eq!(click_preview(&app), None, "a non-finite pointer"); + hover(&mut app, [100.0, f64::INFINITY]); + assert_eq!(click_preview(&app), None); + hover(&mut app, [100.0, 70.0]); + assert!(click_preview(&app).is_some()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.sync_views(); + assert_eq!(click_preview(&app), None, "another document: the clicks and preview are dropped"); + assert!(app.pcrop.clicks.is_empty()); +} + +#[test] +fn the_two_click_grid_meets_at_the_pointer() { + let h = [120.0, 100.0]; + let lines = grid_lines([A, B, h, h], 4); + assert_eq!(lines.len(), 6); + let meets = lines.iter().filter(|[_, b]| (b[0] - h[0]).hypot(b[1] - h[1]) < 1e-9).count(); + assert_eq!(meets, 3, "one family of lines runs from side A–B to the pointer"); + // With three clicks and the pointer making a convex quad, the finished quad's perspective grid. + let q = [A, B, C, D]; + let hom = Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], q).unwrap(); + let (x, y) = hom.apply(0.5, 0.0); + assert!(grid_lines(q, 2).iter().any(|[a, _]| (a[0] - x).abs() < 1e-9 && (a[1] - y).abs() < 1e-9)); +} + +/// Shapes `draw_overlay` paints for the app as it is. +fn painted(app: &PhotocraftApp) -> usize { + let ctx = egui::Context::default(); + let rect = egui::Rect::from_min_size(Pos2::ZERO, egui::vec2(200.0, 150.0)); + let xf = ViewXform { rect, zoom: 1.0, center: [100.0, 75.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let mut o = ctx.run_ui(Default::default(), |ui| draw_overlay(app, ui.painter(), &xf)); + o.textures_delta.clear(); + o.shapes.len() +} + +#[test] +fn the_preview_is_drawn_with_its_grid() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + let without = painted(&app); + hover(&mut app, [120.0, 100.0]); + app.ui.tool_options.perspective_crop_grid = true; + let with_grid = painted(&app); + app.ui.tool_options.perspective_crop_grid = false; + let without_grid = painted(&app); + assert_eq!(without_grid, without, "with Show Grid off the outline replaces the line between the clicks"); + assert!(with_grid > without_grid, "Show Grid adds the grid: {with_grid} vs {without_grid}"); +} + +/// Real egui input: the pointer moving over the canvas with the button up reaches the tool, so +/// the preview follows it. +#[test] +fn hover_over_the_canvas_moves_the_preview() { + use egui::{Event, Modifiers, PointerButton}; + let mut a = app(); + a.ui.extras.snap = false; + a.ui.extras.rulers = false; + a.ui.views = vec![crate::state::View { + zoom: 2.0, + center: [100.0, 75.0], + fit_pending: false, + fill_pending: false, + doc_size: [200, 150], + rotation: 0.0, + zoom_animating: false, + zoom_anchor: None, + display_camera: None, + zoom_transition: None, + }]; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(600.0, 400.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let v = app.ui.views[0].clone(); + crate::canvas::canvas_view(app, ui, 0, ui.max_rect(), v, true); + }, + a, + ); + h.run_steps(2); + let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 400.0)); + let xf = ViewXform { rect, zoom: 2.0, center: [100.0, 75.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + for p in [[60.0, 40.0], [140.0, 40.0]] { + let at = xf.to_screen(p[0], p[1]); + h.event(Event::PointerMoved(at)); + h.step(); + h.event(button(at, true)); + h.step(); + h.event(button(at, false)); + h.step(); + } + assert_eq!(h.state().pcrop.clicks.len(), 2, "{:?}", h.state().pcrop.clicks); + let to = xf.to_screen(110.0, 100.0); + h.event(Event::PointerMoved(to)); + h.step(); + let q = click_preview(h.state()).expect("a preview after two clicks and a move"); + assert!((q[2][0] - 110.0).abs() < 0.5 && (q[2][1] - 100.0).abs() < 0.5, "{q:?}"); + assert_eq!(q[2], q[3]); +} diff --git a/crates/ui-egui/src/tools/perspective_crop_ui.rs b/crates/ui-egui/src/tools/perspective_crop_ui.rs index 1b1a4b3a51..06296d7a61 100644 --- a/crates/ui-egui/src/tools/perspective_crop_ui.rs +++ b/crates/ui-egui/src/tools/perspective_crop_ui.rs @@ -41,6 +41,8 @@ pub struct PerspectiveCrop { pub drag: Option, /// Corners clicked so far, before the fourth makes the quad. pub clicks: Vec<[f64; 2]>, + /// The pointer between clicks (button up), the preview quad's free corner (#11804). + pub hover: Option<[f64; 2]>, /// The document the pending quad belongs to. doc: Option, } @@ -76,6 +78,7 @@ pub fn cancel(app: &mut PhotocraftApp) { app.ui.perspective_crop = None; app.pcrop.drag = None; app.pcrop.clicks.clear(); + app.pcrop.hover = None; } /// Another document became active: its pending quad is dropped (also from `sync_views`, so it is @@ -141,6 +144,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } | ToolEvent::Up { x, y } => [x, y], }; if app.session.active().is_none() || !p[0].is_finite() || !p[1].is_finite() { + app.pcrop.hover = None; return true; } cancel_stale(app); @@ -161,7 +165,11 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> None => Some(Drag::New { start: p, cur: p, square: mods.shift, centred: mods.alt }), }; } - ToolEvent::Move { .. } => drag_to(app, p, mods), + ToolEvent::Move { .. } => { + // Between corner clicks the preview's free corner follows the pointer (#11804). + app.pcrop.hover = (app.pcrop.drag.is_none() && !app.pcrop.clicks.is_empty()).then_some(p); + drag_to(app, p, mods); + } ToolEvent::Up { .. } => { drag_to(app, p, mods); if let Some(Drag::New { start, cur, square, centred }) = app.pcrop.drag { @@ -282,6 +290,39 @@ fn grid_steps(side: f32) -> usize { ((side / 48.0).round() as usize).clamp(2, 12) } +/// The quad that follows the pointer while corners are clicked (#11804): after two clicks the two +/// points and the pointer as the third and fourth corner (a triangle), after three the three points +/// and the pointer as the fourth. `None` with fewer clicks or no pointer position. +pub(crate) fn click_preview(app: &PhotocraftApp) -> Option<[[f64; 2]; 4]> { + let h = app.pcrop.hover.filter(|h| h.iter().all(|v| v.is_finite()))?; + match app.pcrop.clicks[..] { + [a, b] => Some([a, b, h, h]), + [a, b, c] => Some([a, b, c, h]), + _ => None, + } +} + +/// The grid lines of the quad `q` (document px, `n` steps): its perspective grid, or for a quad +/// with two corners on one point (the two-click preview) the lines between matching points of +/// opposite sides, which meet at that point. +fn grid_lines(q: [[f64; 2]; 4], n: usize) -> Vec<[[f64; 2]; 2]> { + let lerp = |a: [f64; 2], b: [f64; 2], t: f64| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; + let homography = if convex_quad(&q) { Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], q) } else { None }; + let at = |u: f64, v: f64| match &homography { + Some(h) => { + let (x, y) = h.apply(u, v); + [x, y] + } + None => lerp(lerp(q[0], q[1], u), lerp(q[3], q[2], u), v), + }; + (1..n) + .flat_map(|k| { + let t = k as f64 / n as f64; + [[at(t, 0.0), at(t, 1.0)], [at(0.0, t), at(1.0, t)]] + }) + .collect() +} + /// The shield over the image outside a pending quad (#11972): grey 89 at 18 % opacity, which shows /// red 255, 0, 0 as 225, 16, 16, as measured in the reference application. pub(crate) const SHIELD: Color32 = Color32::from_rgba_premultiplied(16, 16, 16, 46); @@ -376,12 +417,29 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform } } let clicks: Vec = app.pcrop.clicks.iter().map(|c| scr(*c)).collect(); - if clicks.len() > 1 { + if let Some(q) = click_preview(app) { + // The quad to the pointer with its grid, in the finished quad's style (#11804). A third + // click that makes it cross turns it red as the finished quad (not measured); the + // two-click triangle is drawn as a quad. + let ok = clicks.len() == 2 || convex_quad(&q); + let color = if ok { accent } else { Color32::from_rgb(0xe5, 0x3e, 0x3e) }; + let pts: Vec = q.iter().map(|c| scr(*c)).collect(); + if ok && app.ui.tool_options.perspective_crop_grid { + let side = pts.iter().zip(pts.iter().skip(1)).map(|(a, b)| a.distance(*b)).fold(0.0, f32::max); + let thin = Stroke::new(0.75, Color32::from_rgba_unmultiplied(255, 255, 255, 150)); + for [a, b] in grid_lines(q, grid_steps(side)) { + painter.line_segment([scr(a), scr(b)], thin); + } + } + painter.add(egui::Shape::closed_line(pts, Stroke::new(1.5, color))); + } else if clicks.len() > 1 { painter.add(egui::Shape::line(clicks.clone(), Stroke::new(1.5, accent))); } + // The clicked corners: small square handles, as the reference application marks them. for c in clicks { - painter.circle_filled(c, 4.0, Color32::WHITE); - painter.circle_stroke(c, 4.0, Stroke::new(1.25, accent)); + let r = egui::Rect::from_center_size(c, egui::vec2(8.0, 8.0)); + painter.rect_filled(r, 0.0, Color32::WHITE); + painter.rect_stroke(r, 0.0, Stroke::new(1.25, accent), egui::StrokeKind::Inside); } } @@ -418,6 +476,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { }); } +#[cfg(test)] +#[path = "perspective_crop_click_preview_tests.rs"] +mod perspective_crop_click_preview_tests; + #[cfg(test)] #[path = "perspective_crop_bar_buttons_tests.rs"] mod perspective_crop_bar_buttons_tests;