PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Rotate View: the image follows the pointer in a flipped view (#4226) (#4227)
David Mannf52549571ff0
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 911e428a6..af3a11f04 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2900,7 +2900,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}let rotate_trackpad = app.session.prefs().enhanced_controls.rotate_view_with_trackpad;if let Some(delta) = crate::rotate_view::trackpad_delta(&ctx, rotate_trackpad) {- let next = view.rotation + delta;+ let next = view.rotation + crate::rotate_view::screen_delta(delta, xf.flip);if next.is_finite() {view.rotation = crate::rotate_view::wrap_deg(next);}diff --git a/crates/ui-egui/src/rotate_view.rs b/crates/ui-egui/src/rotate_view.rsindex 0204ba5b3..01841b48f 100644--- a/crates/ui-egui/src/rotate_view.rs+++ b/crates/ui-egui/src/rotate_view.rs@@ -1,6 +1,7 @@//! Rotate View tool: turn the canvas camera around its centre without rewriting pixels.//!-//! Drag around the view centre (clockwise increases the angle). Shift snaps to 15°. Reset View,+//! Drag around the view centre: the image follows the pointer (clockwise increases the angle, or+//! decreases it in a flipped view). Shift snaps to 15°. Reset View,//! Escape, a double-click, or the on-canvas compass put the angle back to 0. History is not//! recorded. Trackpad rotate is gated by Preferences › Enhanced Controls › Rotate View with//! Trackpad.@@ -59,6 +60,14 @@ pub fn snap_deg(a: f32, step: f32) -> f32 {wrap_deg((a / step).round() * step)}+/// Turn a pointer or trackpad angle change (screen degrees, clockwise positive) into a change of+/// `View::rotation`. [`ViewXform::to_screen`] mirrors x after rotating, so in a flipped view a+/// larger angle turns the image the other way on screen; negating keeps it under the pointer.+pub fn screen_delta(delta: f32, flip: bool) -> f32 {+ let d = wrap_deg(delta);+ if flip { wrap_deg(-d) } else { d }+}+/// Pointer angle in degrees around `pivot` (Y-down, so clockwise is positive).pub fn pointer_deg(p: Pos2, pivot: Pos2) -> f32 {let d = p - pivot;@@ -180,7 +189,7 @@ pub fn drag(ctx: &egui::Context, view: &mut View, xf: &ViewXform, b: &Buttons, preturn b.started || b.dragged || b.stopped;};if let Some(p) = pointer.filter(|_| b.dragged || b.stopped) {- let delta = wrap_deg(pointer_deg(p, z.pivot) - z.start_pointer);+ let delta = screen_delta(pointer_deg(p, z.pivot) - z.start_pointer, xf.flip);let mut angle = z.start_rotation + delta;if !angle.is_finite() {angle = z.start_rotation;@@ -358,6 +367,49 @@ mod tests {assert!((s.x - c.x).abs() < 1e-3 && (s.y - (c.y + 1.0)).abs() < 1e-3, "flip then rotate {s:?}");}+ /// Screen angle, about the canvas centre, of the document point that was under `p0` at the press.+ fn followed_angle(h: &Harness<'static, PhotocraftApp>, p0: Pos2, start_rotation: f32) -> f32 {+ let st = h.state();+ let (rect, v, flip) = (st.last_canvas_rect, &st.ui.views[0], st.ui.view.flip_horizontal);+ let before = ViewXform { rect, zoom: 1.0, center: v.center, flip, rotation: start_rotation, aspect: 1.0 };+ let d = before.to_doc(p0);+ let after = ViewXform { rotation: v.rotation, ..before };+ let s = after.to_screen(d[0] as f32, d[1] as f32) - rect.center();+ wrap_deg(s.y.atan2(s.x).to_degrees() - pointer_deg(p0, rect.center()))+ }++ #[test]+ fn drag_follows_the_pointer_in_a_flipped_view() {+ for flip in [false, true] {+ let mut h = harness();+ h.state_mut().ui.view.flip_horizontal = flip;+ h.run_steps(2);+ let c = h.state().last_canvas_rect.center();+ let p0 = c + vec2(120.0, 0.0);+ h.event(Event::PointerMoved(p0));+ h.step();+ press(&mut h, p0, true, false);+ // 30° clockwise about the pivot: the image turns 30° clockwise on screen.+ let p1 = c + vec2(120.0 * 30f32.to_radians().cos(), 120.0 * 30f32.to_radians().sin());+ h.event(Event::PointerMoved(p1));+ h.step();+ let got = followed_angle(&h, p0, 0.0);+ assert!((got - 30.0).abs() < 0.75, "flip={flip}: the image turned {got}°, the pointer 30°");+ press(&mut h, p1, false, false);+ }+ }++ #[test]+ fn screen_delta_mirrors_only_in_a_flipped_view() {+ assert_eq!(screen_delta(12.5, false), 12.5);+ assert_eq!(screen_delta(12.5, true), -12.5);+ assert_eq!(screen_delta(-180.0, true), 180.0);+ assert_eq!(screen_delta(f32::NAN, true), 0.0);+ assert_eq!(screen_delta(f32::INFINITY, false), 0.0);+ // A flipped view still lands on the snapped angle with Shift.+ assert_eq!(snap_deg(0.0 + screen_delta(-40.0, true), SNAP_DEG), 45.0);+ }+fn app_docs(n: usize) -> PhotocraftApp {let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());for i in 0..n {