diff --git a/crates/ui-egui/src/artboard_tool.rs b/crates/ui-egui/src/artboard_tool.rs index 6b996c3f14a..0b12c361274 100644 --- a/crates/ui-egui/src/artboard_tool.rs +++ b/crates/ui-egui/src/artboard_tool.rs @@ -238,20 +238,17 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> true } -/// The resize pointer for handle (`hx`, `hy`), or none away from the selected board's handles. -pub fn cursor(app: &PhotocraftApp, p: [f64; 2]) -> Option { +/// The resize pointer for handle (`hx`, `hy`) as the view shows it, or none away from the +/// selected board's handles. +pub fn cursor(app: &PhotocraftApp, xf: &ViewXform, p: [f64; 2]) -> Option { let (hx, hy) = match app.ui.artboard_tool.drag { Some(ArtboardDrag::Resize { hx, hy, .. }) => (hx, hy), Some(_) => return None, None if plus_at(app, p).is_some() => return Some(CursorIcon::PointingHand), None => handle_at(app, p).map(|h| (h.2, h.3))?, }; - Some(match (hx, hy) { - (_, 0) => CursorIcon::ResizeHorizontal, - (0, _) => CursorIcon::ResizeVertical, - _ if hx == hy => CursorIcon::ResizeNwSe, - _ => CursorIcon::ResizeNeSw, - }) + // (hx, hy) is the handle's outward direction: -1, 0 or 1 on each axis. + Some(crate::tool_cursor::resize([f64::from(hx), f64::from(hy)], xf)) } /// The selected board's handles, its "+" buttons, and the board a gesture in progress would leave. @@ -388,6 +385,24 @@ mod tests { assert_eq!(boards(&app).len(), 1); } + /// The handles' resize arrows turn with a rotated view: on a view turned 90°, the right edge's + /// handle resizes up and down on screen. + #[test] + fn resize_cursors_turn_with_the_view() { + use CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + let mut app = app(); + drag(&mut app, [20.0, 30.0], [120.0, 110.0]); + // The right edge, the top edge, the top-left and the top-right corner. + let handles = [[120.0, 70.0], [70.0, 30.0], [20.0, 30.0], [120.0, 30.0]]; + for (rotation, want) in [(0.0, [H, V, NwSe, NeSw]), (90.0, [V, H, NeSw, NwSe])] { + let view_index = app.session.active_index().unwrap(); + app.ui.views[view_index].rotation = rotation; + let xf = ViewXform::active(&app).unwrap(); + let got = handles.map(|p| cursor(&app, &xf, p)); + assert_eq!(got, want.map(Some), "view turned {rotation}°"); + } + } + /// #3033: a click on the selected board's right "+" adds a board of its size a gap to the right; /// ⇧ on a corner keeps the board's proportions. #[test] diff --git a/crates/ui-egui/src/cam_transform.rs b/crates/ui-egui/src/cam_transform.rs index a2afa8d8120..995645940e1 100644 --- a/crates/ui-egui/src/cam_transform.rs +++ b/crates/ui-egui/src/cam_transform.rs @@ -212,11 +212,11 @@ pub(crate) fn cursor(app: &PhotocraftApp, ctx: &egui::Context, xf: &ViewXform, p Some(match h { Hit::Corner(i) => { let (a, b) = (q.get(i)?, q.get((i + 2) % 4)?); - crate::type_transform::resize_cursor([a[0] - b[0], a[1] - b[1]], xf.flip) + crate::tool_cursor::resize([a[0] - b[0], a[1] - b[1]], xf) } Hit::Edge(i) => { let (a, b) = (mid(i)?, mid(i + 2)?); - crate::type_transform::resize_cursor([a[0] - b[0], a[1] - b[1]], xf.flip) + crate::tool_cursor::resize([a[0] - b[0], a[1] - b[1]], xf) } Hit::Outside => { crate::type_transform::rotation_cursor(ctx, p); diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 47c0ce4edbb..8f336010649 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -3715,9 +3715,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: command_guide_at(app, tool, d, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers))) }); if let Some((vertical, _)) = guide_hover { - ui.ctx().set_cursor_icon(if vertical { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::ResizeVertical }); + ui.ctx().set_cursor_icon(crate::rulers::guide_cursor(&xf, vertical)); } else if let Some(p) = response.hover_pos() - && let Some(c) = crate::transform_tool::cursor(app, xf.to_doc(p), ui.input(|i| i.modifiers.alt || i.modifiers.command)) + && let Some(c) = crate::transform_tool::cursor(app, &xf, xf.to_doc(p), ui.input(|i| i.modifiers.alt || i.modifiers.command)) { ui.ctx().set_cursor_icon(c); // CursorIcon::None hides the pointer; the curved arrow has to be drawn or the @@ -3745,7 +3745,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: crate::type_transform::cursor(app, &ctx, &xf, p, mods) }) { ui.ctx().set_cursor_icon(c); - } else if let Some(c) = response.hover_pos().filter(|_| tool == Tool::Artboard).and_then(|p| crate::artboard_tool::cursor(app, xf.to_doc(p))) { + } else if let Some(c) = response.hover_pos().filter(|_| tool == Tool::Artboard).and_then(|p| crate::artboard_tool::cursor(app, &xf, xf.to_doc(p))) { ui.ctx().set_cursor_icon(c); } else if let Some((p, c)) = response.hover_pos().filter(|_| tool == Tool::Crop).and_then(|p| Some((p, crate::crop_ui::cursor(app, xf.to_doc(p))?))) { ui.ctx().set_cursor_icon(c); diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs index 54ec3795d2a..9ce397008bc 100644 --- a/crates/ui-egui/src/crop_ui.rs +++ b/crates/ui-egui/src/crop_ui.rs @@ -881,8 +881,9 @@ pub fn cursor(app: &PhotocraftApp, p: [f64; 2]) -> Option { None => hit_turned(app.ui.crop_rect?, deg, p, tolerance(app)), }; Some(match h { - // The arrow as the handle faces on screen: the frame's turn plus the view's. - Hit::Handle(hx, hy) => resize_icon(hx, hy, deg + f64::from(crate::crop_mode::view_rotation(app))), + // The arrow as the handle faces on screen: the frame's turn plus the view's, mirrored when + // the view is flipped. + Hit::Handle(hx, hy) => resize_icon(hx, hy, deg + f64::from(crate::crop_mode::view_rotation(app)), app.ui.view.flip_horizontal), // Inside the untouched default frame a drag draws a new one (`pointer`), so nothing moves (#4461). Hit::Inside if app.crop.default_frame => CursorIcon::Crosshair, Hit::Inside => CursorIcon::Move, @@ -947,9 +948,12 @@ pub fn draw_turn_cursor(ctx: &egui::Context, p: egui::Pos2, toward: egui::Vec2) } /// The resize arrow for handle (`hx`, `hy`) of a frame turned `deg` degrees: the handle's outward -/// direction, turned with the frame, to the nearest of the four arrows. -fn resize_icon(hx: i8, hy: i8, deg: f64) -> CursorIcon { +/// direction, turned with the frame and mirrored by a flipped view (`flip`), to the nearest of the +/// four arrows. +fn resize_icon(hx: i8, hy: i8, deg: f64, flip: bool) -> CursorIcon { let d = turn([f64::from(hx), f64::from(hy)], [0.0, 0.0], deg); + // View › Flip Horizontal mirrors the screen after the turn. + let d = if flip { [-d[0], d[1]] } else { d }; let a = d[1].atan2(d[0]).rem_euclid(std::f64::consts::PI); match ((a / std::f64::consts::FRAC_PI_4).round() as i64).rem_euclid(4) { 0 => CursorIcon::ResizeHorizontal, @@ -1203,10 +1207,10 @@ mod tests { let b = turned_bounds(r, 90.0); assert!((b[0] - 50.0).abs() < 1e-9 && (b[3] - 80.0).abs() < 1e-9, "{b:?}"); // Handle arrows turn with the frame. - assert_eq!(resize_icon(1, 0, 0.0), CursorIcon::ResizeHorizontal); - assert_eq!(resize_icon(1, 0, 90.0), CursorIcon::ResizeVertical); - assert_eq!(resize_icon(1, 0, 45.0), CursorIcon::ResizeNwSe); - assert_eq!(resize_icon(-1, -1, 90.0), CursorIcon::ResizeNeSw); + assert_eq!(resize_icon(1, 0, 0.0, false), CursorIcon::ResizeHorizontal); + assert_eq!(resize_icon(1, 0, 90.0, false), CursorIcon::ResizeVertical); + assert_eq!(resize_icon(1, 0, 45.0, false), CursorIcon::ResizeNwSe); + assert_eq!(resize_icon(-1, -1, 90.0, false), CursorIcon::ResizeNeSw); assert_eq!(normalized_angle(540.0), 180.0); assert_eq!(normalized_angle(-190.0), 170.0); assert_eq!(normalized_angle(f64::NAN), 0.0); @@ -1786,6 +1790,12 @@ mod tests { assert_eq!(cursor(&app, [60.0, 60.0]), Some(CursorIcon::ResizeVertical)); assert_eq!(cursor(&app, [60.0, 40.0]), Some(CursorIcon::Move)); assert_eq!(cursor(&app, [150.0, 40.0]), Some(CursorIcon::None), "outside: the drawn turn cursor"); + // View › Flip Horizontal shows the top-left corner at the top right: its arrow mirrors too. + app.ui.view.flip_horizontal = true; + assert_eq!(cursor(&app, [20.0, 20.0]), Some(CursorIcon::ResizeNeSw), "flipped view"); + assert_eq!(cursor(&app, [100.0, 20.0]), Some(CursorIcon::ResizeNwSe), "flipped view"); + assert_eq!(cursor(&app, [60.0, 60.0]), Some(CursorIcon::ResizeVertical), "flipped view"); + app.ui.view.flip_horizontal = false; app.ui.tool = Tool::Brush; assert_eq!(cursor(&app, [60.0, 40.0]), None); } diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs index ff5f106bde0..bb1b5b9d54a 100644 --- a/crates/ui-egui/src/rulers.rs +++ b/crates/ui-egui/src/rulers.rs @@ -159,6 +159,12 @@ fn guide_segment(xf: &ViewXform, clip: Rect, vertical: bool, pos: f64) -> [Pos2; if vertical { [xf.to_screen(pos, lo), xf.to_screen(pos, hi)] } else { [xf.to_screen(lo, pos), xf.to_screen(hi, pos)] } } +/// The cursor over a guide: the arrow across it as the view shows it. A vertical guide moves left +/// and right in the document; a rotated view turns that, as it turns the guide (#3277). +pub fn guide_cursor(xf: &ViewXform, vertical: bool) -> egui::CursorIcon { + crate::tool_cursor::resize(if vertical { [1.0, 0.0] } else { [0.0, 1.0] }, xf) +} + pub fn draw_guides(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, doc: &Document) { let clip = painter.clip_rect(); let drag = app.guide_drag; @@ -890,6 +896,20 @@ mod tests { } } + /// The cursor over a guide turns with the view as the guide does: on a view turned 90°, a + /// vertical guide shows as a horizontal line and moves up and down. + #[test] + fn guide_cursors_turn_with_a_rotated_view() { + use egui::CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let level = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip: false, rotation: 0.0, aspect: 1.0 }; + // View rotation, flip → the cursor over a vertical guide, over a horizontal guide. + for (rotation, flip, want) in [(0.0, false, [H, V]), (90.0, false, [V, H]), (45.0, false, [NwSe, NeSw]), (0.0, true, [H, V])] { + let xf = ViewXform { rotation, flip, ..level }; + assert_eq!([guide_cursor(&xf, true), guide_cursor(&xf, false)], want, "view turned {rotation}°, flip {flip}"); + } + } + #[test] fn ruler_range_is_normalised_when_flipped() { // #1814: with View > Flip Horizontal, `to_doc` negates x, so the raw diff --git a/crates/ui-egui/src/tool_cursor.rs b/crates/ui-egui/src/tool_cursor.rs index 6539ddb8607..32f6aed82e6 100644 --- a/crates/ui-egui/src/tool_cursor.rs +++ b/crates/ui-egui/src/tool_cursor.rs @@ -9,6 +9,8 @@ use std::sync::Arc; use egui::{CursorIcon, CustomCursorImage, Painter, Pos2, Stroke, Vec2, vec2}; +use crate::canvas::ViewXform; + mod hand; mod zoom; @@ -104,6 +106,21 @@ pub(crate) fn native_scale(ctx: &egui::Context) -> f32 { if cfg!(target_os = "macos") { ctx.zoom_factor() } else { ctx.pixels_per_point() } } +/// The resize arrow for a handle that resizes along document direction `d`, as the view shows it +/// (its rotation, flip and pixel aspect): the nearest of the four arrows. Free Transform (handles +/// and Warp splits), the type box, Content-Aware Move's box, the Artboard tool and guides pick +/// their arrows here. +pub(crate) fn resize(d: [f64; 2], xf: &ViewXform) -> CursorIcon { + let s = xf.map_vec(vec2(d[0] as f32, d[1] as f32)); + let angle = f64::from(s.y).atan2(f64::from(s.x)).rem_euclid(std::f64::consts::PI); + match (angle / std::f64::consts::FRAC_PI_4).round() as u8 % 4 { + 0 => CursorIcon::ResizeHorizontal, + 1 => CursorIcon::ResizeNwSe, + 2 => CursorIcon::ResizeVertical, + _ => CursorIcon::ResizeNeSw, + } +} + /// The Hand tool's pointer: an open hand, a fist while it drags (`closed`). winit maps `Grab` and /// `Grabbing` to the four-arrow move cursor on Windows, so there it is a bitmap; macOS and the /// Linux themes already draw hands, and the OS moves those without waiting for a frame. diff --git a/crates/ui-egui/src/tool_cursor/tests.rs b/crates/ui-egui/src/tool_cursor/tests.rs index 8a240fa70c9..409d528b95c 100644 --- a/crates/ui-egui/src/tool_cursor/tests.rs +++ b/crates/ui-egui/src/tool_cursor/tests.rs @@ -458,3 +458,26 @@ fn circle_bitmaps_evaluate_only_the_ring_and_match_every_pixel() { } } } + +/// The arrow is the handle's direction as the view shows it: turned with a rotated view and +/// mirrored with a flipped one. +#[test] +fn resize_follows_the_view() { + use CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + let view = |rotation: f32, flip: bool| ViewXform { + rect: egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)), + zoom: 1.0, + center: [0.0, 0.0], + flip, + rotation, + aspect: 1.0, + }; + // Right, down-right, down and down-left in the document. + let directions = [[1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [-1.0, 1.0]]; + for (rotation, flip, want) in + [(0.0, false, [H, NwSe, V, NeSw]), (90.0, false, [V, NeSw, H, NwSe]), (45.0, false, [NwSe, V, NeSw, H]), (0.0, true, [H, NeSw, V, NwSe])] + { + let got = directions.map(|d| resize(d, &view(rotation, flip))); + assert_eq!(got, want, "view turned {rotation}°, flip {flip}"); + } +} diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rs index d4f58ef3a85..483043edd00 100644 --- a/crates/ui-egui/src/transform_tool.rs +++ b/crates/ui-egui/src/transform_tool.rs @@ -1806,9 +1806,8 @@ fn surface_at(mesh: &BezierMesh, p: [f64; 2]) -> Option<(f64, f64)> { } /// Ctrl-click (or Option-click) picks the split: crosswise in the open, and the perpendicular -/// split when the pointer is close to an existing grid line. -fn quick_split_kind(mesh: &BezierMesh, bounds: [f64; 4], p: [f64; 2]) -> SplitTool { - let (s, t) = mesh.param_at(p); +/// split when the pointer's grid position `(s, t)` is close to an existing grid line. +fn quick_split_kind(mesh: &BezierMesh, bounds: [f64; 4], (s, t): (f64, f64)) -> SplitTool { let w = (bounds[2] - bounds[0]).abs().max(1.0); let h = (bounds[3] - bounds[1]).abs().max(1.0); let su = (8.0 / w).clamp(0.02, 0.12); @@ -1824,12 +1823,18 @@ fn quick_split_kind(mesh: &BezierMesh, bounds: [f64; 4], p: [f64; 2]) -> SplitTo } } -fn split_cursor(tool: SplitTool) -> CursorIcon { - match tool { - SplitTool::Vertical => CursorIcon::ResizeHorizontal, - SplitTool::Horizontal => CursorIcon::ResizeVertical, - SplitTool::Cross => CursorIcon::Crosshair, - } +/// The cursor for a split armed at grid position `(s, t)` of warp grid `m`: the arrow across the +/// split line as it looks on screen. A vertical split line runs along the grid's columns and is +/// placed by moving across them, in the grid's own direction there, so the arrow turns with a +/// turned or bent grid and with the view. +fn split_cursor(m: &BezierMesh, tool: SplitTool, (s, t): (f64, f64), xf: &ViewXform) -> CursorIcon { + let e = 1e-3; + let (a, b) = match tool { + SplitTool::Vertical => (m.eval((s - e).max(0.0), t), m.eval((s + e).min(1.0), t)), + SplitTool::Horizontal => (m.eval(s, (t - e).max(0.0)), m.eval(s, (t + e).min(1.0))), + SplitTool::Cross => return CursorIcon::Crosshair, + }; + crate::tool_cursor::resize([b[0] - a[0], b[1] - a[1]], xf) } /// Armed split tool, or Option held: the guide tracks the pointer and the split lands on release. @@ -1862,7 +1867,10 @@ fn split_gesture(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) let kind = if let Some(tool) = armed { Some(tool) } else if quick { - app.ui.transform.as_ref().and_then(|t| t.warp.as_ref()).map(|w| quick_split_kind(&w.to_mesh(1, 1), w.bounds, [x, y])) + app.ui.transform.as_ref().and_then(|t| t.warp.as_ref()).map(|w| { + let m = w.to_mesh(1, 1); + quick_split_kind(&m, w.bounds, m.param_at([x, y])) + }) } else { None }; @@ -2001,20 +2009,22 @@ fn rotate_dir(t: &TransformSession, pv: Option<&TransformPreview>, p: [f64; 2], (l.is_finite() && l > 1e-9).then(|| [s[0] / l, s[1] / l]) } -/// Cursor for hovering a document point while transforming. `quick_split` is Ctrl or Option, -/// which arms a quick split while a warp is active. -pub fn cursor(app: &PhotocraftApp, p: [f64; 2], quick_split: bool) -> Option { +/// Cursor for hovering a document point while transforming. `xf` is the canvas's view; +/// `quick_split` is Ctrl or Option, which arms a quick split while a warp is active. +pub fn cursor(app: &PhotocraftApp, xf: &ViewXform, p: [f64; 2], quick_split: bool) -> Option { let t = app.ui.transform.as_ref()?; let tol = handle_tolerance(app); if let Some(w) = &t.warp { if warps_type(app) { return Some(CursorIcon::Default); } - if let Some(tool) = app.transform_preview.as_ref().and_then(|pv| pv.split_tool) { - return Some(split_cursor(tool)); - } - if quick_split { - return Some(split_cursor(quick_split_kind(&w.to_mesh(1, 1), w.bounds, p))); + let armed = app.transform_preview.as_ref().and_then(|pv| pv.split_tool); + if armed.is_some() || quick_split { + // One search for the grid position under the pointer, for both the kind and the arrow. + let m = w.to_mesh(1, 1); + let at = m.param_at(p); + let tool = armed.unwrap_or_else(|| quick_split_kind(&m, w.bounds, at)); + return Some(split_cursor(&m, tool, at, xf)); } if w.style != WarpStyle::Custom || warp_hit(w, p, tol).is_some() { return Some(CursorIcon::Crosshair); @@ -2029,11 +2039,17 @@ pub fn cursor(app: &PhotocraftApp, p: [f64; 2], quick_split: bool) -> Option CursorIcon::ResizeNwSe, - Hit::Corner(_) => CursorIcon::ResizeNeSw, - Hit::Edge(0 | 2) => CursorIcon::ResizeVertical, - Hit::Edge(_) => CursorIcon::ResizeHorizontal, + Hit::Corner(i) => resize(corner(i + 2), corner(i)), + Hit::Edge(i) => resize(edge_mid(i + 2), edge_mid(i)), Hit::Pivot => CursorIcon::Crosshair, Hit::Inside => CursorIcon::Move, Hit::Outside => CursorIcon::Alias, @@ -2116,7 +2132,12 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform let at = pv.split_pointer.or(over_canvas); let alt = painter.ctx().input(|i| i.modifiers.alt || i.modifiers.command); let guide = at.filter(|_| !warps_type(app)).and_then(|p| { - let tool = pv.split_tool.or_else(|| (alt || pv.split_quick).then(|| quick_split_kind(&w.to_mesh(1, 1), w.bounds, p))); + let tool = pv.split_tool.or_else(|| { + (alt || pv.split_quick).then(|| { + let m = w.to_mesh(1, 1); + quick_split_kind(&m, w.bounds, m.param_at(p)) + }) + }); tool.map(|tool| (tool, p)) }); draw_warp_overlay(painter, xf, w, pv, guide); @@ -4289,15 +4310,69 @@ mod tests { let tol = handle_tolerance(&app); let h = std::f64::consts::FRAC_1_SQRT_2; let (tl, br) = ([32.0 - tol * 1.5, 32.0 - tol * 1.5], [160.0 + tol * 1.5, 160.0 + tol * 1.5]); - assert_eq!(cursor(&app, tl, false), Some(CursorIcon::None), "the arrow is drawn instead"); + let xf = ViewXform::active(&app).unwrap(); + assert_eq!(cursor(&app, &xf, tl, false), Some(CursorIcon::None), "the arrow is drawn instead"); assert!(near2(rotate_cursor_dir(&app, tl), [h, h]), "{:?}", rotate_cursor_dir(&app, tl)); assert!(near2(rotate_cursor_dir(&app, br), [-h, -h]), "bends round the bottom-right corner"); - assert_eq!(cursor(&app, [60.0, 60.0], false), Some(CursorIcon::Move)); + assert_eq!(cursor(&app, &xf, [60.0, 60.0], false), Some(CursorIcon::Move)); assert_eq!(rotate_cursor_dir(&app, [60.0, 60.0]), None, "inside moves"); assert_eq!(rotate_cursor_dir(&app, [32.0, 32.0]), None, "the corner handle scales"); assert_eq!(rotate_cursor_dir(&app, [f64::NAN, 32.0]), None); } + /// The resize arrows follow the box's turn and the view's rotation and flip: each handle + /// shows the arrow it resizes along on screen, not the one it had on the upright box. + #[test] + fn resize_cursors_turn_with_the_box_and_the_view() { + use CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + // Box turned (degrees), view rotation, flip → corner 0, corner 1, edge 0, edge 1 (corner + // and edge i + 2 sit opposite, on the same axis). + let cases = [ + (0.0, 0.0, false, [NwSe, NeSw, V, H]), + (90.0, 0.0, false, [NeSw, NwSe, H, V]), + (45.0, 0.0, false, [V, H, NeSw, NwSe]), + (0.0, 0.0, true, [NeSw, NwSe, V, H]), + (0.0, 90.0, false, [NeSw, NwSe, H, V]), + ]; + for (deg, view, flip, want) in cases { + let mut app = app_with_square(256, photocraft_geom::Rect::new(64, 96, 192, 160)); + crate::menus::invoke(&mut app, &egui::Context::default(), "edit.freeTransform", json!({})).unwrap(); + let view_index = app.session.active_index().unwrap(); + app.ui.views[view_index].rotation = view; + app.ui.view.flip_horizontal = flip; + let t = app.ui.transform.as_mut().unwrap(); + let (s, c) = f64::to_radians(deg).sin_cos(); + let o = [128.0, 128.0]; + t.quad = t.quad.map(|[x, y]| [o[0] + (x - o[0]) * c - (y - o[1]) * s, o[1] + (x - o[0]) * s + (y - o[1]) * c]); + let t = app.ui.transform.clone().unwrap(); + let xf = ViewXform::active(&app).unwrap(); + let mid = |i: usize| [(t.quad[i][0] + t.quad[(i + 1) % 4][0]) / 2.0, (t.quad[i][1] + t.quad[(i + 1) % 4][1]) / 2.0]; + let got: Vec<_> = (0..4).map(|i| cursor(&app, &xf, t.quad[i], false)).chain((0..4).map(|i| cursor(&app, &xf, mid(i), false))).collect(); + let want = [want[0], want[1], want[0], want[1], want[2], want[3], want[2], want[3]].map(Some); + assert_eq!(got, want, "box {deg}°, view {view}°, flip {flip}"); + } + } + + /// A warp split's arrow crosses the split line as it looks on screen: it turns with a turned + /// grid (Warp entered on a rotated box) and with a rotated view. + #[test] + fn warp_split_cursors_turn_with_the_grid_and_the_view() { + use CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + let rect = [0.0, 0.0, 200.0, 100.0]; + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let level = ViewXform { rect: screen, zoom: 1.0, center: [100.0, 50.0], flip: false, rotation: 0.0, aspect: 1.0 }; + // Grid turned (degrees, about its centre), view rotation → Split Vertically, Split Horizontally. + for (grid, view, want) in [(0.0, 0.0, [H, V]), (0.0, 90.0, [V, H]), (90.0, 0.0, [V, H]), (45.0, 0.0, [NwSe, NeSw])] { + let (s, c) = f64::to_radians(grid).sin_cos(); + let m = BezierMesh::identity(rect, 1, 1).map_points(|[x, y]| [100.0 + (x - 100.0) * c - (y - 50.0) * s, 50.0 + (x - 100.0) * s + (y - 50.0) * c]); + let xf = ViewXform { rotation: view, ..level }; + let got = [SplitTool::Vertical, SplitTool::Horizontal].map(|tool| split_cursor(&m, tool, m.param_at([100.0, 50.0]), &xf)); + assert_eq!(got, want, "grid turned {grid}°, view {view}°"); + } + let m = BezierMesh::identity(rect, 1, 1); + assert_eq!(split_cursor(&m, SplitTool::Cross, (0.5, 0.5), &level), CursorIcon::Crosshair); + } + /// #1767: the readout beside the pointer during a drag (Photoshop's transformation values): /// the angle while rotating, W/H while scaling, ΔX/ΔY while moving; nothing otherwise. #[test] diff --git a/crates/ui-egui/src/type_transform.rs b/crates/ui-egui/src/type_transform.rs index 2a68366d6ef..b724cfe3211 100644 --- a/crates/ui-egui/src/type_transform.rs +++ b/crates/ui-egui/src/type_transform.rs @@ -349,16 +349,6 @@ pub(crate) fn draw_box(painter: &egui::Painter, xf: &ViewXform, t: &TransformSes } } -pub(crate) fn resize_cursor(d: [f64; 2], flip: bool) -> CursorIcon { - let angle = d[1].atan2(if flip { -d[0] } else { d[0] }).rem_euclid(std::f64::consts::PI); - match (angle / std::f64::consts::FRAC_PI_4).round() as u8 % 4 { - 0 => CursorIcon::ResizeHorizontal, - 1 => CursorIcon::ResizeNwSe, - 2 => CursorIcon::ResizeVertical, - _ => CursorIcon::ResizeNeSw, - } -} - pub(crate) fn rotation_cursor(ctx: &egui::Context, p: Pos2) { let tokens = crate::theme::Tokens::get(ctx); let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::new("type-rotate-cursor"))); @@ -399,7 +389,7 @@ pub(crate) fn cursor(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXfor let b = if handle.is_multiple_of(2) { t.quad[3] } else { t.quad[1] }; [b[0] - t.quad[0][0], b[1] - t.quad[0][1]] }; - return Some(resize_cursor(direction, xf.flip)); + return Some(crate::tool_cursor::resize(direction, xf)); } let Some(t) = frame(app) else { return Some(CursorIcon::Default) }; let h = app.ui.type_transform.as_ref().map(|t| t.gesture.hit).or_else(|| hit(app, &t, xf.to_doc(p))); @@ -407,7 +397,7 @@ pub(crate) fn cursor(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXfor Some(Hit::Corner(i)) => { let q = *t.quad.get(i)?; let opposite = *t.quad.get((i + 2) % 4)?; - resize_cursor([q[0] - opposite[0], q[1] - opposite[1]], xf.flip) + crate::tool_cursor::resize([q[0] - opposite[0], q[1] - opposite[1]], xf) } Some(Hit::Edge(_)) => { // Original arrowhead indicator, above the canvas, without a proprietary cursor asset.