From 1687d6f4994487d763dca59bddb3f40fda45e90e Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Tue, 24 Nov 2026 12:06:39 +0100 Subject: [PATCH] feat(ui): Ctrl+T transforms only the selected anchors (#13209) With Direct Selection holding anchors of a work or saved path, Ctrl+T / Edit > Free Transform now frames those anchors and transforms only them: the box opens over the bounds they span, so the options bar reads the selection's own width and height, and the commit names the anchors so the transform moves them and leaves the rest of the path where it is. Measured case: the anchors (300,300) and (100,500) of the closed square in Path 1, W 50 % linked and OK, gives (250,350) and (150,450); the two anchors that are not selected and the other subpath stay put, in one "Transform Points" step. The outline drawn through the live box follows the same rule: the named knots, each with its own handles, go through the box's map and every other point of the path keeps its place, so what is previewed is what commits. The decision sits where the box picks up the path. Only the measured configuration takes the point transform: a work or saved path, every selected anchor in one subpath and no segment selected. A shape layer's path or a vector mask, anchors across subpaths, a segment on its own and no selection keep the whole-path transform and the whole-path box, as before. --- crates/ui-egui/src/tools/direct_select.rs | 3 + .../free_transform_points_tests.rs | 297 ++++++++++++++++++ crates/ui-egui/src/tools/transform_tool.rs | 4 + .../ui-egui/src/tools/transform_tool/begin.rs | 12 +- .../ui-egui/src/tools/transform_tool/draw.rs | 26 ++ crates/ui-egui/src/tools/vector_ui.rs | 4 +- .../ui-egui/src/tools/vector_ui/path_ops.rs | 72 ++++- 7 files changed, 413 insertions(+), 5 deletions(-) create mode 100644 crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs diff --git a/crates/ui-egui/src/tools/direct_select.rs b/crates/ui-egui/src/tools/direct_select.rs index 6b824a43f9..8d174af78f 100644 --- a/crates/ui-egui/src/tools/direct_select.rs +++ b/crates/ui-egui/src/tools/direct_select.rs @@ -523,6 +523,9 @@ mod options_bar_tests; #[cfg(test)] mod convert_point_delete_key_tests; +#[cfg(test)] +mod free_transform_points_tests; + #[cfg(test)] mod freeform_command_tests { use super::*; 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 new file mode 100644 index 0000000000..3b7e673821 --- /dev/null +++ b/crates/ui-egui/src/tools/direct_select/free_transform_points_tests.rs @@ -0,0 +1,297 @@ +//! #13209: ⌘T / Edit ▸ Free Transform with anchors selected in Direct Selection frames the box over +//! those anchors and transforms only them (Free Transform Points). Measured on the epic's document: +//! the anchors (300,300) and (100,500) of the closed square selected, the box spans 100,300 to +//! 300,500; W 50 % (linked) and Enter moves them to (250,350) and (150,450) about the centre of the +//! selection, the two unselected anchors of the square stay, and History holds one step "Transform +//! Points". With nothing selected, and in the neighbouring cases the ticket leaves unmeasured, the +//! whole path transforms as it does today. + +use craft::behavior::geometry::quad::BoxState; +use craft::behavior::kurbo::Point; +use egui::Modifiers; +use photocraft_doc::vector::Path; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing; +use crate::state::Tool; + +/// The epic's document: 800 x 600 on white, a layer "Red", and saved path Path 1 holding the closed +/// corner square (100,300) (300,300) (300,500) (100,500) and the open line (400,300)-(700,300), +/// targeted in the Paths panel, Direct Selection on. +fn saved_path_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("layer.new.layer", json!({"name": "Red"})).unwrap(); + let path = json!({"subpaths": [ + {"closed": true, "knots": [[100, 300], [300, 300], [300, 500], [100, 500]]}, + {"closed": false, "knots": [[400, 300], [700, 300]]}, + ]}); + app.run("path.set", json!({"name": "Path 1", "path": path})).unwrap(); + app.ui.selected_path = Some("Path 1".into()); + app.ui.tool = Tool::DirectSelection; + app +} + +/// A square work path whose knot 1 at (120,40) is smooth, with handles at (100,40) and (140,40). +fn work_path_app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::DirectSelection; + let path = json!({"subpaths": [{"closed": true, "knots": [ + [40, 40], + {"anchor": [120, 40], "in": [100, 40], "out": [140, 40], "smooth": true}, + [120, 120], + [40, 120], + ]}]}); + app.run("path.set", json!({"name": "work", "path": path})).unwrap(); + app +} + +fn click(app: &mut PhotocraftApp, at: [f64; 2], mods: Modifiers) { + testing::click(app, at, mods); +} + +/// The measured two-anchor selection: (300,300) and (100,500) of the square. +fn select_two_corners(app: &mut PhotocraftApp) { + click(app, [300.0, 300.0], Modifiers::NONE); + click(app, [100.0, 500.0], Modifiers::SHIFT); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1], [0, 3]], "both corners are selected"); +} + +/// ⌘T / Edit ▸ Free Transform. +fn open_box(app: &mut PhotocraftApp) { + let ctx = app.ctx().clone(); + crate::menus::invoke(app, &ctx, "edit.freeTransform", json!({})).expect("the path is transformable"); + assert!(app.ui.transform.is_some(), "the box opened"); +} + +/// The box's source frame. +fn box_rect(app: &PhotocraftApp) -> [f64; 4] { + app.ui.transform.as_ref().expect("a Free Transform is open").rect_array() +} + +/// What the box will send to `path.transform`. +fn box_params(app: &PhotocraftApp) -> serde_json::Value { + app.ui.transform.as_ref().expect("a Free Transform is open").path.clone().unwrap_or(serde_json::Value::Null) +} + +/// Puts the box on `quad`, as the transform controls do. +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)); +} + +/// OK: commit the open box. +fn commit_box(app: &mut PhotocraftApp) { + crate::transform_tool::commit(app); + assert!(app.ui.transform.is_none(), "the box closed: {}", app.ui.status); +} + +fn path1(app: &PhotocraftApp) -> Path { + app.session.active().unwrap().doc.paths.iter().find(|p| p.name == "Path 1").map(|p| p.path.clone()).expect("Path 1 is there") +} + +fn work_path(app: &PhotocraftApp) -> Path { + app.session.active().unwrap().doc.work_path.clone().expect("the work path is there") +} + +fn anchors(path: &Path, sub: usize) -> Vec<[f64; 2]> { + path.subpaths[sub].knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect() +} + +fn knot(path: &Path, sub: usize, knot: usize) -> (photocraft_geom::Point, photocraft_geom::Point, photocraft_geom::Point) { + let k = &path.subpaths[sub].knots[knot]; + (k.anchor, k.in_ctrl, k.out_ctrl) +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn last_step(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().history.undo_label().map(str::to_string) +} + +/// The measured row: the box frames the two selected anchors, not all of Path 1. +#[test] +fn the_box_frames_the_selected_anchors() { + let mut app = saved_path_app(); + select_two_corners(&mut app); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 300.0, 500.0], "the box spans 100,300 to 300,500"); + assert_eq!(box_params(&app), json!({"name": "Path 1", "anchors": [[0, 1], [0, 3]]}), "the box names the selected anchors"); +} + +/// The measured row: W 50 % (linked) and Enter moves the selected anchors about the centre of the +/// selection; the unselected anchors of the square and the other subpath stay where they are. +#[test] +fn a_half_wide_box_moves_only_the_selected_anchors() { + let mut app = saved_path_app(); + select_two_corners(&mut app); + open_box(&mut app); + // Half the width and half the height about the centre (200,400) of the selected anchors. + set_box(&mut app, [[150.0, 350.0], [250.0, 350.0], [250.0, 450.0], [150.0, 450.0]]); + commit_box(&mut app); + let p = path1(&app); + assert_eq!(anchors(&p, 0), vec![[100.0, 300.0], [250.0, 350.0], [300.0, 500.0], [150.0, 450.0]], "only the selected corners moved"); + assert_eq!(anchors(&p, 1), vec![[400.0, 300.0], [700.0, 300.0]], "the other subpath is untouched"); +} + +/// The measured row: one History step, named "Transform Points". +#[test] +fn the_point_transform_is_one_history_step_named_transform_points() { + let mut app = saved_path_app(); + let before = steps(&app); + select_two_corners(&mut app); + assert_eq!(steps(&app), before, "selecting anchors changes nothing"); + open_box(&mut app); + set_box(&mut app, [[150.0, 350.0], [250.0, 350.0], [250.0, 450.0], [150.0, 450.0]]); + commit_box(&mut app); + assert_eq!((steps(&app), last_step(&app).as_deref()), (before + 1, Some("Transform Points")), "{}", app.ui.status); + assert!(app.session.undo(), "the step undoes"); + assert_eq!(anchors(&path1(&app), 0), vec![[100.0, 300.0], [300.0, 300.0], [300.0, 500.0], [100.0, 500.0]], "Undo restores the square"); +} + +/// The Done-when row "with their own handles": a selected smooth anchor's direction handles follow +/// the box; the anchors and knots outside the selection do not move. +#[test] +fn a_selected_anchor_moves_its_own_handles() { + let mut app = work_path_app(); + click(&mut app, [120.0, 40.0], Modifiers::NONE); + click(&mut app, [120.0, 120.0], Modifiers::SHIFT); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1], [0, 2]]); + open_box(&mut app); + // The box covers the two anchors and the selected knot's handles: 100,40 to 140,120. + assert_eq!(box_rect(&app), [100.0, 40.0, 140.0, 120.0]); + // Twice as wide: the anchors sit on the box's centre line, so only the handles move in x. + set_box(&mut app, [[80.0, 40.0], [160.0, 40.0], [160.0, 120.0], [80.0, 120.0]]); + commit_box(&mut app); + let p = work_path(&app); + let (anchor, handle_in, handle_out) = knot(&p, 0, 1); + assert_eq!([anchor.x, anchor.y], [120.0, 40.0]); + assert_eq!([handle_in.x, handle_in.y], [80.0, 40.0], "the in handle followed the box"); + assert_eq!([handle_out.x, handle_out.y], [160.0, 40.0], "the out handle followed the box"); + 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)]); +} + +/// Neighbour: with nothing selected, ⌘T keeps today's whole-path transform and today's step name. +#[test] +fn nothing_selected_transforms_the_whole_path() { + let mut app = saved_path_app(); + let before = steps(&app); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "the box frames all of Path 1"); + 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!((steps(&app), last_step(&app).as_deref()), (before + 1, Some("Transform Path"))); + let p = path1(&app); + assert_eq!(anchors(&p, 0), vec![[250.0, 300.0], [350.0, 300.0], [350.0, 500.0], [250.0, 500.0]], "the whole square moved"); + assert_eq!(anchors(&p, 1), vec![[400.0, 300.0], [550.0, 300.0]], "and the line with it"); +} + +/// Neighbour: Path Selection is untouched by the point decision, even with a path targeted. +#[test] +fn path_selection_transforms_the_whole_path() { + let mut app = saved_path_app(); + app.ui.tool = Tool::PathSelection; + let before = steps(&app); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0]); + assert_eq!(box_params(&app), json!({"name": "Path 1"})); + 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!((steps(&app), last_step(&app).as_deref()), (before + 1, 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]]); +} + +/// Neighbour the ticket leaves unmeasured: anchors on several subpaths keep today's whole-path +/// transform. +#[test] +fn anchors_on_two_subpaths_keep_the_whole_path() { + let mut app = saved_path_app(); + click(&mut app, [300.0, 300.0], Modifiers::NONE); + click(&mut app, [400.0, 300.0], Modifiers::SHIFT); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1], [1, 0]], "one anchor in each subpath"); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0], "the box still 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]]); +} + +/// Neighbour the ticket leaves unmeasured: a segment selected on its own keeps today's whole-path +/// transform. +#[test] +fn a_segment_selection_keeps_the_whole_path() { + let mut app = saved_path_app(); + click(&mut app, [200.0, 300.0], Modifiers::NONE); + assert!(app.ui.direct_selection.anchors.is_empty(), "a segment click selects no anchor"); + assert_eq!(app.ui.direct_selection.segment, Some([0, 0]), "the segment between knots 0 and 1"); + open_box(&mut app); + assert_eq!(box_rect(&app), [100.0, 300.0, 700.0, 500.0]); + assert_eq!(box_params(&app), json!({"name": "Path 1"})); + 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!(anchors(&path1(&app), 0), vec![[250.0, 300.0], [350.0, 300.0], [350.0, 500.0], [250.0, 500.0]]); +} + +/// Neighbour: the box keeps the anchor selection while it is open and leaves it, as a path transform +/// does today, when it commits. Same for the whole-path transform of the unmeasured multi-subpath +/// selection, so the point decision changes nothing there. +#[test] +fn the_transform_leaves_the_anchor_selection_as_a_path_transform_does() { + let mut app = saved_path_app(); + select_two_corners(&mut app); + open_box(&mut app); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 1], [0, 3]], "the selection stands while the box is open"); + set_box(&mut app, [[150.0, 350.0], [250.0, 350.0], [250.0, 450.0], [150.0, 450.0]]); + commit_box(&mut app); + assert_eq!(app.ui.direct_selection.anchors, Vec::<[usize; 2]>::new(), "the box ends the selection, as a path transform does"); + + let mut app = saved_path_app(); + click(&mut app, [300.0, 300.0], Modifiers::NONE); + click(&mut app, [400.0, 300.0], Modifiers::SHIFT); + open_box(&mut app); + 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!(app.ui.direct_selection.anchors, Vec::<[usize; 2]>::new(), "and the same with the whole-path transform"); +} + +/// The command on its own: `path.transform` with `anchors` pivots numeric scaling on the centre of +/// the anchors it names, which is how the measured box move comes out. +#[test] +fn the_command_transforms_named_anchors_about_their_own_centre() { + let mut app = saved_path_app(); + app.run("path.transform", json!({"name": "Path 1", "anchors": [[0, 1], [0, 3]], "scaleX": 0.5, "scaleY": 0.5})).expect("the anchors scale"); + let p = path1(&app); + assert_eq!(anchors(&p, 0), vec![[100.0, 300.0], [250.0, 350.0], [300.0, 500.0], [150.0, 450.0]]); + assert_eq!(anchors(&p, 1), vec![[400.0, 300.0], [700.0, 300.0]], "the unlisted subpath is untouched"); + assert_eq!(last_step(&app).as_deref(), Some("Transform Points")); +} + +/// Hostile params: an anchor list the path does not have is an error, never a whole-path transform +/// and never a panic. +#[test] +fn anchors_off_the_path_are_an_error() { + let mut app = saved_path_app(); + let (before, n) = (path1(&app), steps(&app)); + for bad in [ + json!({"name": "Path 1", "anchors": [[9, 0]], "matrix": [1, 0, 0, 1, 5, 0]}), + json!({"name": "Path 1", "anchors": [[0, 9]], "matrix": [1, 0, 0, 1, 5, 0]}), + json!({"name": "Path 1", "anchors": [], "matrix": [1, 0, 0, 1, 5, 0]}), + json!({"name": "Path 1", "anchors": [[0]], "matrix": [1, 0, 0, 1, 5, 0]}), + json!({"name": "Path 1", "anchors": "0,1", "matrix": [1, 0, 0, 1, 5, 0]}), + ] { + assert!(app.run("path.transform", bad.clone()).is_err(), "{bad} is refused"); + } + assert_eq!(path1(&app), before, "nothing moved"); + assert_eq!(steps(&app), n, "and no step was added"); +} diff --git a/crates/ui-egui/src/tools/transform_tool.rs b/crates/ui-egui/src/tools/transform_tool.rs index d881c0b6b7..2c35f042f7 100644 --- a/crates/ui-egui/src/tools/transform_tool.rs +++ b/crates/ui-egui/src/tools/transform_tool.rs @@ -127,6 +127,9 @@ pub struct OpenPreview { tool: Tool, /// Free Transform Path: the path as it was, drawn through the box instead of pixels. path: Option, + /// Free Transform Points (#13209): the anchors of that path the box moves, `[subpath, knot]`. + /// Set, the outline follows the box only there and the rest of the path stays put. + path_points: Option>, /// A layer's session keeps `doc` and `opacity` up to date with edits made while the box is /// open ([`follow_edits`]); `None` for a lone mask or channel and Transform Selection. follow: Option, @@ -157,6 +160,7 @@ impl OpenPreview { split: None, tool, path: None, + path_points: None, follow: None, committing: None, content_aware: None, diff --git a/crates/ui-egui/src/tools/transform_tool/begin.rs b/crates/ui-egui/src/tools/transform_tool/begin.rs index c90ea547e7..9815f82576 100644 --- a/crates/ui-egui/src/tools/transform_tool/begin.rs +++ b/crates/ui-egui/src/tools/transform_tool/begin.rs @@ -584,8 +584,12 @@ fn begin_lone( /// Edit › Free Transform Path: the box frames the path's anchors and handles and draws the /// path through it; OK commits one `path.transform` step. +/// +/// Free Transform Points (#13209): the params can name anchors, in which case the box frames those +/// anchors and OK transforms only them. fn begin_path(app: &mut PhotocraftApp, params: serde_json::Value, path: photocraft_doc::vector::Path) -> Result<(), String> { - let (x0, y0, x1, y1) = path.control_bounds().ok_or("the path has no points to transform")?; + let (x0, y0, x1, y1) = + crate::vector_ui::free_transform_point_bounds(¶ms, &path).or_else(|| path.control_bounds()).ok_or("the path has no points to transform")?; let st = app.session.active().ok_or("no document")?; let doc = st.doc.clone(); let layer = st.active_layer.or_else(|| doc.layers.first().map(|l| l.id)).ok_or("no layer")?; @@ -598,7 +602,11 @@ fn begin_path(app: &mut PhotocraftApp, params: serde_json::Value, path: photocra format!("transform-{session}"), PreviewPixels::new(egui::ColorImage::new([1, 1], vec![Color32::TRANSPARENT]), [1.0, 1.0]), ); - app.transform_preview = Some(OpenPreview { path: Some(path), ..OpenPreview::new(session, doc, textures, app.ui.tool) }); + app.transform_preview = Some(OpenPreview { + path: Some(path), + path_points: crate::vector_ui::free_transform_point_anchors(¶ms), + ..OpenPreview::new(session, doc, textures, app.ui.tool) + }); app.ui.transform = Some(FreeTransform { session: open_session(app, rect, BoxAllow::default(), true), id: session, diff --git a/crates/ui-egui/src/tools/transform_tool/draw.rs b/crates/ui-egui/src/tools/transform_tool/draw.rs index dc7d45275b..72c4749703 100644 --- a/crates/ui-egui/src/tools/transform_tool/draw.rs +++ b/crates/ui-egui/src/tools/transform_tool/draw.rs @@ -313,10 +313,36 @@ fn path_outline(t: &FreeTransform, pv: &OpenPreview) -> Option { (Box::new(move |p| h.apply_in_front(p, rect)), false) } }; + // Free Transform Points (#13209): the box carries only the anchors it was opened on, so those + // points go through its map here and the outline of the whole path is drawn where it then stands. + let (source, map, bends) = match pv.path_points.as_deref() { + Some(anchors) => (points_through_box(source, anchors, &*map), Box::new(stays_where_it_is) as Box Option>, false), + None => (source, map, bends), + }; let subpaths = source.subpaths.iter().flat_map(|sub| mapped_runs(&flatten(sub, bends), sub.closed, &*map)).collect(); Some(EditPath::new(source.key, subpaths).colored(source.color)) } +/// The map of a point transform's outline: the points it leaves alone keep their place. +fn stays_where_it_is(p: Point) -> Option { + Some(p) +} + +/// Free Transform Points (#13209): `source` with the knots `anchors` names put where the box puts +/// them — each named knot's anchor and its own handles follow the map, every other point of the path +/// stays exactly where it is. A point the map sends behind a Distort's horizon keeps its place. +fn points_through_box(mut source: EditPath, anchors: &[[usize; 2]], map: &dyn Fn(Point) -> Option) -> EditPath { + for &[sub, knot] in anchors { + let Some(node) = source.subpaths.get_mut(sub).and_then(|sub| sub.knots.get_mut(knot)) else { continue }; + for p in [&mut node.anchor, &mut node.in_ctrl, &mut node.out_ctrl] { + if let Some(q) = map(*p) { + *p = q; + } + } + } + source +} + /// `sub`'s outline as points: each segment's start, and for a curved segment (or any segment when /// `bends`, since the map then bends a straight one) points along it. An open subpath ends with /// its last anchor. diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index 815fdf6802..79288cfd6b 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -38,8 +38,8 @@ pub use path_ops::{ active_path_name, make_shape, path_selection_finish, path_to_selection, pick_shape_at, selected_document_path, takes_path_mask, with_active_path, }; pub(crate) use path_ops::{ - active_shape_path, active_type_path, delete_key_edits_path, deselect_loaded_path, edit_path, free_transform_path, saved_path_components_selected, - targeted_vector_mask, work_path_shown, + active_shape_path, active_type_path, delete_key_edits_path, deselect_loaded_path, edit_path, free_transform_path, free_transform_point_anchors, + free_transform_point_bounds, saved_path_components_selected, targeted_vector_mask, work_path_shown, }; pub use paths_panel::{PathEntry, PathRow, path_rows, paths_panel}; #[cfg(test)] 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 12e2f56da3..d583bff620 100644 --- a/crates/ui-egui/src/tools/vector_ui/path_ops.rs +++ b/crates/ui-egui/src/tools/vector_ui/path_ops.rs @@ -324,13 +324,83 @@ pub(crate) fn edit_path(path: &Path, key: u64) -> EditPath { /// Edit › Free Transform Path (⌘T with Path Selection or Direct Selection): the path the /// box transforms (the one Path Selection edits) and `path.transform`'s params that name it. +/// +/// 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 +/// 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) { return None; } let (target, path) = target_path(app)?; path.control_bounds()?; - Some((target.params(app), path)) + let mut params = target.params(app); + if let Some(anchors) = free_transform_anchor_selection(app, target, &path) { + params.as_object_mut()?.insert("anchors".into(), json!(anchors)); + } + Some((params, path)) +} + +/// The anchors Free Transform Points acts on: the tool's own anchor selection of the path the box +/// transforms, in the configuration the reference application was measured in. `None` keeps the +/// whole-path transform. Every index addresses a knot of `path` and the selection is still the one +/// this document and revision made, so a selection left over from elsewhere cannot name a point that +/// is not there. +fn free_transform_anchor_selection(app: &PhotocraftApp, target: PathRef, path: &Path) -> Option> { + if app.ui.tool != Tool::DirectSelection || !matches!(target, PathRef::Work | PathRef::Saved(_)) { + return None; + } + let ds = &app.ui.direct_selection; + if ds.editing.is_some() || ds.segment.is_some() || ds.target != Some(target) || !crate::direct_select::still_valid(app, ds) { + return None; + } + let anchors = &ds.anchors; + let [sub, _] = *anchors.first()?; + let mut out = Vec::with_capacity(anchors.len()); + for a in anchors { + let [s, k] = *a; + // Anchors on several subpaths were not measured: that selection keeps the whole path. + if s != sub { + return None; + } + path.subpaths.get(s)?.knots.get(k)?; + out.push([s, k]); + } + Some(out) +} + +/// The anchors Free Transform Points params name (`[subpath, knot]`, as `path.moveAnchors` names +/// them); `None` when the params transform the whole path (#13209). +pub(crate) fn free_transform_point_anchors(params: &Value) -> Option> { + let list = params.get("anchors")?.as_array()?; + let mut out = Vec::with_capacity(list.len()); + for a in list { + let pair = a.as_array().filter(|pair| pair.len() == 2)?; + let (Some(s), Some(k)) = (pair[0].as_u64(), pair[1].as_u64()) else { return None }; + let (Ok(s), Ok(k)) = (usize::try_from(s), usize::try_from(k)) else { return None }; + out.push([s, k]); + } + (!out.is_empty()).then_some(out) +} + +/// The frame the Free Transform box opens over: the bounds of the anchors and their own handles the +/// params name (Free Transform Points, #13209), so the options bar reads the selection's X, Y, W and +/// H. `None` when the params name no anchors: the whole path's control bounds then frame the box. +pub(crate) fn free_transform_point_bounds(params: &Value, path: &Path) -> Option<(f64, f64, f64, f64)> { + let mut bounds: Option<(f64, f64, f64, f64)> = None; + for [s, k] in free_transform_point_anchors(params)? { + let node = path.subpaths.get(s)?.knots.get(k)?; + for p in [node.anchor, node.in_ctrl, node.out_ctrl] { + bounds = Some(match bounds { + 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 } fn point_in_subpath(sp: &photocraft_doc::vector::Subpath, pt: [f64; 2]) -> bool {