diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 83fe6f0716..2acba730f4 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -69,7 +69,7 @@ use display::texture_key; pub(crate) use display::{cache_key, canvas_display, display_doc, display_image}; use display::{checker, hdr_preview, sync_display_lut}; pub use drag::{Drag, fixed_size_px, marquee_corners, marquee_px, marquee_readout, readout_in, size_readout, square_px}; -pub(crate) use drag::{angle_readout, draw_readout, draw_value_readout}; +pub(crate) use drag::{angle_readout, doc_box_corners, draw_readout, draw_value_readout}; #[cfg(test)] pub(crate) use drag::{marquee_drag_ants, marquee_preview_px}; #[cfg(test)] diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index aae9dbdb86..53eb81c592 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -294,6 +294,15 @@ pub(crate) fn marquee_drag_ants(d: &Drag, xf: &ViewXform, marquee: Option<[f64; marquee_ants(xf, d.tool, a, b) } +/// The corners on screen of the document-axis box dragged from `a` to `b`, clockwise from its +/// top-left: a turned parallelogram in a rotated view, as the box the release creates (the Type +/// tools' text box, the Move tool's layer marquee). +pub(crate) fn doc_box_corners(xf: &ViewXform, a: [f64; 2], b: [f64; 2]) -> [Pos2; 4] { + let (x0, x1) = (a[0].min(b[0]) as f32, a[0].max(b[0]) as f32); + let (y0, y1) = (a[1].min(b[1]) as f32, a[1].max(b[1]) as f32); + [xf.to_screen(x0, y0), xf.to_screen(x1, y0), xf.to_screen(x1, y1), xf.to_screen(x0, y1)] +} + /// The size readout shown beside the cursor while dragging a marquee: the width and height values. pub fn marquee_readout(r: [f64; 4]) -> [String; 2] { [format!("{} px", r[2] - r[0]), format!("{} px", r[3] - r[1])] diff --git a/crates/ui-egui/src/canvas/paint.rs b/crates/ui-egui/src/canvas/paint.rs index e65cad697c..0f3a92edef 100644 --- a/crates/ui-egui/src/canvas/paint.rs +++ b/crates/ui-egui/src/canvas/paint.rs @@ -1,6 +1,7 @@ use super::display::{canvas_display, checker, display_doc, display_image}; use super::drag::{ - angle_readout, draw_marquee_readout, draw_value_readout, marquee_ants, marquee_drag_ants, marquee_preview_px, single_line_drag_px, size_readout, + angle_readout, doc_box_corners, draw_marquee_readout, draw_value_readout, marquee_ants, marquee_drag_ants, marquee_preview_px, single_line_drag_px, + size_readout, }; use super::gesture::constrained_polygon_point; use super::strokes::{EngineStroke, SPOT_HEALING_TRAIL, draw_smoothing_string, strokes_live}; @@ -615,8 +616,8 @@ pub(super) fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter painter.circle_filled(b, 3.0, Color32::WHITE); } Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => { - let r = Rect::from_two_pos(xf.to_screen(d.start[0] as f32, d.start[1] as f32), xf.to_screen(last[0] as f32, last[1] as f32)); - let pts = [r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom(), r.left_top()]; + let [p0, p1, p2, p3] = doc_box_corners(xf, d.start, last); + let pts = [p0, p1, p2, p3, p0]; painter.add(egui::Shape::line(pts.to_vec(), Stroke::new(1.0, Color32::WHITE))); painter.add(egui::Shape::dashed_line(&pts, Stroke::new(1.0, Color32::BLACK), 3.0, 3.0)); } diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rs index 87ff48ddb4..6cf286b52b 100644 --- a/crates/ui-egui/src/marquee_tests.rs +++ b/crates/ui-egui/src/marquee_tests.rs @@ -454,6 +454,115 @@ fn the_live_outline_matches_the_committed_selection_in_a_rotated_view() { } } +/// The closed outlines the last frame drew as a solid white line under black dashes: the Type +/// tools' drag box (four corners, the first repeated). +fn white_box_outlines(h: &Harness<'static, PhotocraftApp>) -> Vec> { + h.output() + .shapes + .iter() + .filter_map(|c| match &c.shape { + egui::Shape::Path(p) + if matches!(p.stroke.color, egui::epaint::ColorMode::Solid(col) if col == egui::Color32::WHITE) + && p.points.len() == 5 + && p.points.first() == p.points.last() => + { + Some(p.points.clone()) + } + _ => None, + }) + .collect() +} + +/// The slanted edges the last frame drew as marching ants (#7530): each is a white base segment +/// under black dashes (`ants::shapes`); upright edges go into a pixel mesh instead. The Move tool's +/// layer marquee in a rotated view. +fn white_ant_edges(h: &Harness<'static, PhotocraftApp>) -> Vec<[Pos2; 2]> { + h.output() + .shapes + .iter() + .filter_map(|c| match &c.shape { + egui::Shape::LineSegment { points, stroke } if stroke.color == egui::Color32::WHITE => Some(*points), + _ => None, + }) + .collect() +} + +/// Every drawn corner of `outline` maps back, through the view, to the document corner of `want` +/// ([x0, y0, x1, y1]) with the same index, clockwise from the top-left. +fn assert_doc_box(xf: &ViewXform, outline: &[Pos2], want: [f64; 4], what: &str) { + let corners = [[want[0], want[1]], [want[2], want[1]], [want[2], want[3]], [want[0], want[3]]]; + for (i, (p, w)) in outline.iter().zip(corners).enumerate() { + let [x, y] = xf.to_doc(*p); + assert!( + (x - w[0]).abs() <= 1.0 && (y - w[1]).abs() <= 1.0, + "{what}: corner {i} drawn at {p:?} is document ({x:.1}, {y:.1}), the box's corner is {w:?}" + ); + } +} + +/// #7063: in a rotated view the Type tools' drag box previews the document-axis text box the +/// release creates. An upright screen box through two of its corners is another shape (at 45° a +/// dragged square previews as a sliver). +#[test] +fn the_type_drag_box_matches_the_committed_text_box_in_a_rotated_view() { + for rotation in [30.0, 45.0] { + let mut h = harness(Tool::Type); + h.state_mut().ui.extras.snap = false; + h.state_mut().ui.views[0].rotation = rotation; + h.run_steps(2); + press_at(&mut h, 50.0, 50.0, Modifiers::NONE); + move_to(&mut h, 150.0, 90.0); + let xf = view_xform(&h); + let drawn = white_box_outlines(&h); + assert_eq!(drawn.len(), 1, "{rotation}°: one text box is previewed: {drawn:?}"); + release_at(&mut h, 150.0, 90.0, Modifiers::NONE); + // The text box the release created, in document pixels. + let committed = { + let st = h.state().session.active().unwrap(); + let layer = st.doc.layer(st.active_layer.unwrap()).unwrap(); + let photocraft_doc::LayerContent::Text(t) = &layer.content else { panic!("{rotation}°: a type layer") }; + let photocraft_doc::text::TextShape::Box { x, y, width, height } = t.shape else { panic!("{rotation}°: paragraph text") }; + let (tx, ty) = (t.transform.m[4], t.transform.m[5]); + [tx + f64::from(x), ty + f64::from(y), tx + f64::from(x + width), ty + f64::from(y + height)] + }; + assert!((committed[0] - 50.0).abs() <= 1.0 && (committed[3] - 90.0).abs() <= 1.0, "{rotation}°: the drag made {committed:?}"); + assert_doc_box(&xf, &drawn[0], committed, &format!("{rotation}°: the Type preview")); + } +} + +/// #7063: in a rotated view the Move tool's layer marquee (#2844) previews the document-axis box +/// `layer.selectInRect` gets on the release, not an upright screen box through two of its corners. +#[test] +fn the_move_layer_marquee_follows_the_document_axes_in_a_rotated_view() { + for rotation in [30.0, 45.0] { + let mut h = harness(Tool::Move); + let app = h.state_mut(); + app.ui.extras.snap = false; + app.ui.tool_options.move_auto_select = true; + app.ui.views[0].rotation = rotation; + h.run_steps(2); + // On the locked Background, so the drag draws the marquee (#4058). + press_at(&mut h, 50.0, 50.0, Modifiers::NONE); + move_to(&mut h, 150.0, 90.0); + assert!(h.state().layer_box.is_some(), "{rotation}°: the drag draws the layer marquee"); + let xf = view_xform(&h); + // Four slanted ant edges, each from one corner of the document box to the next. An upright + // screen box would have none (its edges go into the ants' pixel mesh). + let edges = white_ant_edges(&h); + assert_eq!(edges.len(), 4, "{rotation}°: the marquee's four edges are drawn slanted: {edges:?}"); + let ends: Vec = edges.iter().map(|e| e[0]).collect(); + assert_doc_box(&xf, &ends, [50.0, 50.0, 150.0, 90.0], &format!("{rotation}°: the layer marquee (edge starts)")); + let ends: Vec = edges.iter().map(|e| e[1]).collect(); + assert_doc_box( + &xf, + &ends[3..].iter().chain(&ends[..3]).copied().collect::>(), + [50.0, 50.0, 150.0, 90.0], + &format!("{rotation}°: the layer marquee (edge ends)"), + ); + release_at(&mut h, 150.0, 90.0, Modifiers::NONE); + } +} + /// 800 %, snapping off, the view centred on (30, 30): the pointer lands between pixel edges. fn zoomed(tool: Tool) -> Harness<'static, PhotocraftApp> { let mut h = harness(tool); diff --git a/crates/ui-egui/src/tools/move_box.rs b/crates/ui-egui/src/tools/move_box.rs index 05f12222b4..20edea5850 100644 --- a/crates/ui-egui/src/tools/move_box.rs +++ b/crates/ui-egui/src/tools/move_box.rs @@ -82,8 +82,7 @@ pub(crate) fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { /// The marquee as marching ants. pub(crate) fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) { let Some(b) = app.layer_box else { return }; - let r = egui::Rect::from_two_pos(xf.to_screen(b.start[0] as f32, b.start[1] as f32), xf.to_screen(b.end[0] as f32, b.end[1] as f32)); - crate::tool_feedback::draw_ants(painter, &[r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()], true); + crate::tool_feedback::draw_ants(painter, &crate::canvas::doc_box_corners(xf, b.start, b.end), true); } #[cfg(test)]