diff --git a/crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs b/crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs index 3b7e673821..f917d91ee5 100644 --- a/crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs +++ b/crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs @@ -7,7 +7,7 @@ //! whole path transforms as it does today. use craft::behavior::geometry::quad::BoxState; -use craft::behavior::kurbo::Point; +use craft::behavior::kurbo::{Point, Vec2}; use egui::Modifiers; use photocraft_doc::vector::Path; use serde_json::json; @@ -50,6 +50,16 @@ fn work_path_app() -> Box { app } +/// A live rectangle shape layer "Rectangle 1" at 550,50 to 750,200, Direct Selection on. +fn shape_app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); + app.sync_views(); + app.run("shape.create", json!({"kind": "rect", "rect": [550, 50, 200, 150]})).unwrap(); + app.ui.tool = Tool::DirectSelection; + app +} + fn click(app: &mut PhotocraftApp, at: [f64; 2], mods: Modifiers) { testing::click(app, at, mods); } @@ -83,6 +93,11 @@ fn set_box(app: &mut PhotocraftApp, quad: [[f64; 2]; 4]) { app.ui.transform.as_mut().unwrap().edit_box(|b| *b = BoxState::new(quad.map(|[x, y]| Point::new(x, y)), b.pivot)); } +/// Moves the box and its reference point, as a drag inside the box does. +fn move_box(app: &mut PhotocraftApp, dx: f64, dy: f64) { + app.ui.transform.as_mut().unwrap().edit_box(|b| *b = b.moved(Vec2::new(dx, dy))); +} + /// OK: commit the open box. fn commit_box(app: &mut PhotocraftApp) { crate::transform_tool::commit(app); @@ -176,6 +191,13 @@ fn a_selected_anchor_moves_its_own_handles() { assert_eq!(anchors(&p, 0), vec![[40.0, 40.0], [120.0, 40.0], [120.0, 120.0], [40.0, 120.0]], "the unselected anchors stayed"); let (corner, corner_in, corner_out) = knot(&p, 0, 0); assert_eq!([(corner.x, corner.y), (corner_in.x, corner_in.y), (corner_out.x, corner_out.y)], [(40.0, 40.0), (40.0, 40.0), (40.0, 40.0)]); + assert!(app.session.undo(), "one Undo takes the point transform back"); + let (anchor, handle_in, handle_out) = knot(&work_path(&app), 0, 1); + assert_eq!( + [(anchor.x, anchor.y), (handle_in.x, handle_in.y), (handle_out.x, handle_out.y)], + [(120.0, 40.0), (100.0, 40.0), (140.0, 40.0)], + "Undo restores the anchor and both handles" + ); } /// Neighbour: with nothing selected, ⌘T keeps today's whole-path transform and today's step name. @@ -295,3 +317,98 @@ fn anchors_off_the_path_are_an_error() { assert_eq!(path1(&app), before, "nothing moved"); assert_eq!(steps(&app), n, "and no step was added"); } + +/// The box needs an area to work in: one straight anchor spans none, so that selection keeps today's +/// whole-path transform. The reference application was not measured with a frame-less selection. +#[test] +fn one_anchor_selected_keeps_the_whole_path() { + let mut app = saved_path_app(); + click(&mut app, [300.0, 300.0], Modifiers::NONE); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1]], "one straight anchor is selected"); + let before = steps(&app); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "the box frames the whole path"); + assert_eq!(box_params(&app), json!({"name": "Path 1"}), "no anchors are named"); + // Drag the box 20 right and 20 down, OK: every anchor of Path 1 follows. + move_box(&mut app, 20.0, 20.0); + commit_box(&mut app); + assert_eq!((steps(&app), last_step(&app).as_deref()), (before + 1, Some("Transform Path"))); + assert_eq!(anchors(&path1(&app), 0), vec![[120.0, 320.0], [320.0, 320.0], [320.0, 520.0], [120.0, 520.0]]); + assert_eq!(anchors(&path1(&app), 1), vec![[420.0, 320.0], [720.0, 320.0]], "the line moved with it"); +} + +/// Two anchors on one line span an area of nothing either: the same whole-path transform. +#[test] +fn collinear_anchors_keep_the_whole_path() { + let mut app = saved_path_app(); + click(&mut app, [100.0, 300.0], Modifiers::NONE); + click(&mut app, [300.0, 300.0], Modifiers::SHIFT); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 0], [0, 1]], "both top corners"); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "the box frames the whole path"); + assert_eq!(box_params(&app), json!({"name": "Path 1"}), "no anchors are named"); + set_box(&mut app, [[250.0, 300.0], [550.0, 300.0], [550.0, 500.0], [250.0, 500.0]]); + commit_box(&mut app); + assert_eq!(last_step(&app).as_deref(), Some("Transform Path")); + assert_eq!(anchors(&path1(&app), 0), vec![[250.0, 300.0], [350.0, 300.0], [350.0, 500.0], [250.0, 500.0]]); + assert_eq!(anchors(&path1(&app), 1), vec![[400.0, 300.0], [550.0, 300.0]]); +} + +/// Path Selection's component pick fills the same anchor list; the tool is checked first, so Ctrl+T +/// keeps the whole-path transform. +#[test] +fn a_path_selection_component_pick_keeps_the_whole_path() { + let mut app = saved_path_app(); + app.ui.tool = Tool::PathSelection; + click(&mut app, [200.0, 400.0], Modifiers::NONE); + assert!(!app.ui.direct_selection.anchors.is_empty(), "the component pick writes the anchors it covers"); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "the box frames the whole path"); + assert_eq!(box_params(&app), json!({"name": "Path 1"}), "no anchors are named"); + move_box(&mut app, 20.0, 20.0); + commit_box(&mut app); + assert_eq!(last_step(&app).as_deref(), Some("Transform Path")); + assert_eq!(anchors(&path1(&app), 0), vec![[120.0, 320.0], [320.0, 320.0], [320.0, 520.0], [120.0, 520.0]]); + assert_eq!(anchors(&path1(&app), 1), vec![[420.0, 320.0], [720.0, 320.0]]); +} + +/// #13284 makes Path Selection with Ctrl held act as Direct Selection, and the anchor it presses ends +/// up in that same list. The free-transform entry reads the tool in hand, so this keeps today's +/// whole-path transform; asked on the issue whether the held tool should decide instead. +#[test] +fn path_selection_with_ctrl_held_keeps_the_whole_path() { + let mut app = saved_path_app(); + app.ui.tool = Tool::PathSelection; + click(&mut app, [300.0, 300.0], Modifiers::COMMAND); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1]], "the Ctrl press selected the anchor"); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "today: the box frames the whole path"); + assert_eq!(box_params(&app), json!({"name": "Path 1"}), "today: no anchors are named"); +} + +/// Neighbour the ticket leaves unmeasured: anchors selected on a shape layer's path keep today's +/// whole-path box, params and step name. +#[test] +fn anchors_on_a_shape_layer_keep_the_whole_path() { + let mut app = shape_app(); + let layer = app.session.active().unwrap().active_layer.unwrap(); + click(&mut app, [750.0, 50.0], Modifiers::NONE); + assert_eq!(app.ui.direct_selection.target, Some(crate::direct_select::PathRef::Layer(layer.0)), "the shape path is targeted"); + let selected = app.ui.direct_selection.anchors.clone(); + assert_eq!(selected.len(), 1, "the corner anchor is selected"); + let before = steps(&app); + open_box(&mut app); + assert_eq!(box_rect(&app), [550.0, 50.0, 750.0, 200.0], "the box frames the whole shape path"); + assert_eq!(box_params(&app), json!({"name": "layer", "layer": layer.0}), "no anchors are named"); + move_box(&mut app, 20.0, 20.0); + commit_box(&mut app); + assert_eq!((steps(&app), last_step(&app).as_deref()), (before + 1, Some("Transform Path")), "{}", app.ui.status); + let shape = app.session.active().unwrap().doc.layer(layer).and_then(|l| match &l.content { + photocraft_doc::LayerContent::Shape(sh) => Some(sh.path.clone()), + _ => None, + }); + let p = shape.expect("the layer is still a shape"); + let mut moved: Vec<[f64; 2]> = p.subpaths[0].knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect(); + moved.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap().then(a[1].partial_cmp(&b[1]).unwrap())); + assert_eq!(moved, vec![[570.0, 70.0], [570.0, 220.0], [770.0, 70.0], [770.0, 220.0]], "the whole rect moved"); +} diff --git a/crates/ui-egui/src/tools/vector_ui/path_ops.rs b/crates/ui-egui/src/tools/vector_ui/path_ops.rs index d583bff620..ea000ff53a 100644 --- a/crates/ui-egui/src/tools/vector_ui/path_ops.rs +++ b/crates/ui-egui/src/tools/vector_ui/path_ops.rs @@ -328,8 +328,9 @@ pub(crate) fn edit_path(path: &Path, key: u64) -> EditPath { /// Free Transform Points (#13209): when Direct Selection has anchors selected on that same path, the /// params name those anchors as well, so the box transforms only them, and /// [`free_transform_point_bounds`] is the frame they span. The reference application was measured on -/// a work or saved path with every selected anchor in one subpath and no segment selected; a shape -/// layer's path, a vector mask, anchors across subpaths, a segment on its own and no selection keep +/// a work or saved path with every selected anchor in one subpath, no segment selected and a frame +/// that has an area; a shape layer's path, a vector mask, anchors across subpaths, a segment on its +/// own, a selection spanning no area (one straight anchor, anchors on one line) and no selection keep /// today's whole-path transform. pub(crate) fn free_transform_path(app: &PhotocraftApp) -> Option<(Value, Path)> { if !matches!(app.ui.tool, Tool::PathSelection | Tool::DirectSelection) { @@ -369,7 +370,10 @@ fn free_transform_anchor_selection(app: &PhotocraftApp, target: PathRef, path: & path.subpaths.get(s)?.knots.get(k)?; out.push([s, k]); } - Some(out) + // A box needs an area to work in: one straight anchor, or anchors on one line, span none, and the + // session then has no map to move them by. That was not measured, so it keeps the whole path. + let (x0, y0, x1, y1) = point_frame(path, &out)?; + (x1 > x0 && y1 > y0).then_some(out) } /// The anchors Free Transform Points params name (`[subpath, knot]`, as `path.moveAnchors` names @@ -390,17 +394,23 @@ pub(crate) fn free_transform_point_anchors(params: &Value) -> Option Option<(f64, f64, f64, f64)> { - let mut bounds: Option<(f64, f64, f64, f64)> = None; - for [s, k] in free_transform_point_anchors(params)? { + point_frame(path, &free_transform_point_anchors(params)?) +} + +/// The bounds the knots `anchors` span: their anchors and their own handles, the same measure +/// [`Path::control_bounds`] is for the whole path. `None` when an index is not a knot of `path`. +fn point_frame(path: &Path, anchors: &[[usize; 2]]) -> Option<(f64, f64, f64, f64)> { + let mut frame: Option<(f64, f64, f64, f64)> = None; + for &[s, k] in anchors { let node = path.subpaths.get(s)?.knots.get(k)?; for p in [node.anchor, node.in_ctrl, node.out_ctrl] { - bounds = Some(match bounds { + frame = Some(match frame { Some((x0, y0, x1, y1)) => (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y)), None => (p.x, p.y, p.x, p.y), }); } } - bounds + frame } fn point_in_subpath(sp: &photocraft_doc::vector::Subpath, pt: [f64; 2]) -> bool {