mirror of
https://github.com/storytold/AniCraft.git
synced 2026-12-01 02:24:23 +01:00
fix(stage): review fixes for Alt-drag curve handles
- raw.convert_points returns the ids of the elements that exist after the merge, and reports merged: false when the merge declined. - set_smooth_handles makes the closing edge after the anchor explicit, not the first one in the path, so an anchor after an earlier Close keeps its command index. - Once a handle pull has started, moving back within the drag threshold still updates the handle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d362bcf7f9
commit
0f4a171d7f
6 files changed
+138
-15
No files matched your search
@@ -745,23 +745,33 @@ fn merge_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// Merges the raw shapes `ids` again with the raw shapes they touch, keeping their stacking (an
|
||||
/// edit changed them in place). Selected shapes' pieces stay selected.
|
||||
pub(crate) fn remerge_in_place(s: &mut Session, ids: &[ElementId]) -> Result<()> {
|
||||
remerge_in_place_ids(s, ids).map(|_| ())
|
||||
}
|
||||
|
||||
/// [`remerge_in_place`], returning the replacement element ids from `ids` when a merge happened
|
||||
/// (`None` when nothing merged or the merge declined, so `ids` stayed as edited).
|
||||
pub(crate) fn remerge_in_place_ids(s: &mut Session, ids: &[ElementId]) -> Result<Option<Vec<ElementId>>> {
|
||||
if ids.is_empty() {
|
||||
return Ok(());
|
||||
return Ok(None);
|
||||
}
|
||||
let kf = s.editable_keyframe()?;
|
||||
let existing = kf.elements.clone();
|
||||
let area = existing.iter().filter(|e| ids.contains(&e.id)).filter_map(|e| mergeable(e).map(|sh| reach(sh, &e.transform))).fold(Rect::EMPTY, |r, b| r.union(&b));
|
||||
if area.is_empty() {
|
||||
return Ok(());
|
||||
return Ok(None);
|
||||
}
|
||||
let touching: Vec<usize> = existing.iter().enumerate().filter(|(_, e)| mergeable(e).is_some_and(|sh| !reach(sh, &e.transform).intersect(&area).is_empty())).map(|(i, _)| i).collect();
|
||||
let here = s.layer;
|
||||
let parts: Vec<Part> = touching.iter().filter_map(|&i| existing.get(i)).map(|e| Part::Shape(e.as_ref(), s.selection.contains(here, e.id))).collect();
|
||||
// Too much to merge at once: the shapes stay as edited.
|
||||
let Some((out, picked)) = merge::remerge(s, &parts) else { return Ok(()) };
|
||||
let Some(rm) = merge::remerge_with(s, &parts, None) else { return Ok(None) };
|
||||
let gone: Vec<ElementId> = touching.iter().filter_map(|&i| existing.get(i)).map(|e| e.id).collect();
|
||||
merge::replace(s, &touching, out, false)?;
|
||||
let mut mapped: Vec<ElementId> = rm.from.iter().filter_map(|&(piece, part)| gone.get(part).filter(|id| ids.contains(id)).map(|_| piece)).collect();
|
||||
if mapped.is_empty() {
|
||||
mapped = rm.out.iter().map(|e| e.id).collect();
|
||||
}
|
||||
merge::replace(s, &touching, rm.out, false)?;
|
||||
s.selection.retain(|l, id| l != here || !gone.contains(&id));
|
||||
s.selection.extend_on(here, picked);
|
||||
Ok(())
|
||||
s.selection.extend_on(here, rm.picked);
|
||||
Ok(Some(mapped))
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
//! copy alike: an added point is added to every shape with that edge; a deleted one is joined
|
||||
//! across in every shape that has it, so the drawing stays connected.
|
||||
|
||||
use crate::cmds_raw::{activate_layer_at, check_extent, follow_edge, positions_param, remerge_in_place, snap_drawn};
|
||||
use crate::cmds_raw::{activate_layer_at, check_extent, follow_edge, positions_param, remerge_in_place, remerge_in_place_ids, snap_drawn};
|
||||
use crate::merge::{mergeable, reach};
|
||||
use crate::params::*;
|
||||
use crate::{CommandSpec, EngineError, Result, Session};
|
||||
@@ -43,7 +43,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Convert Anchor Points",
|
||||
"",
|
||||
None,
|
||||
"{points: [[x, y], ..], smooth: bool, handle?: [dx, dy], merge?: bool} makes the raw anchors at those positions smooth (their handles in one line; with handle, mirrored at anchor ± (dx, dy)) or corners (handles drawn in), in every shape that has them, and merges again (unless merge is false)",
|
||||
"{points: [[x, y], ..], smooth: bool, handle?: [dx, dy], merge?: bool} makes the raw anchors at those positions smooth (their handles in one line; with handle, mirrored at anchor ± (dx, dy)) or corners (handles drawn in), in every shape that has them, and merges again (unless merge is false); returns {converted, changed, ids (the resulting element ids after any merge), merged}",
|
||||
true,
|
||||
convert_points,
|
||||
),
|
||||
@@ -251,10 +251,15 @@ fn convert_points(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if merge_after {
|
||||
remerge_in_place(s, &changed)?;
|
||||
}
|
||||
Ok(json!({"converted": converted, "changed": changed.len(), "ids": changed.iter().map(|e| e.0).collect::<Vec<_>>(), "merged": merge_after}))
|
||||
let (ids, merged) = if merge_after {
|
||||
match remerge_in_place_ids(s, &changed)? {
|
||||
Some(mapped) => (mapped, true),
|
||||
None => (changed, false),
|
||||
}
|
||||
} else {
|
||||
(changed, false)
|
||||
};
|
||||
Ok(json!({"converted": converted, "changed": ids.len(), "ids": ids.iter().map(|e| e.0).collect::<Vec<_>>(), "merged": merged}))
|
||||
}
|
||||
|
||||
// ----- snapping while drawing --------------------------------------------------------------------
|
||||
|
||||
@@ -264,6 +264,32 @@ fn convert_points_with_handle_curves_shared_raw_edges_and_undoes() {
|
||||
assert!(hs[0].at.distance(end + Vec2::new(20.0, 30.0)) < 0.3, "{hs:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_points_returns_existing_ids_after_remerge() {
|
||||
let mut s = raw_session();
|
||||
run(&mut s, "draw.rect", json!({"x": 0, "y": 0, "w": 100, "h": 80, "fill": "#ff0000", "stroke": "#000000"}));
|
||||
let corner = Vec2::new(100.0, 0.0);
|
||||
// Default merge: true replaces touched shapes; returned ids must name the resulting elements.
|
||||
let v = run(&mut s, "raw.convert_points", json!({"points": [[corner.x, corner.y]], "smooth": true, "handle": [25, 15]}));
|
||||
assert_eq!(v["merged"], json!(true), "{v}");
|
||||
let ids: Vec<u64> = v["ids"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect();
|
||||
assert!(!ids.is_empty(), "post-merge ids should be non-empty: {v}");
|
||||
assert_eq!(v["changed"], json!(ids.len()), "{v}");
|
||||
let live: Vec<u64> = s.visible_elements().iter().map(|e| e.id.0).collect();
|
||||
assert!(ids.iter().all(|id| live.contains(id)), "returned ids {ids:?} must exist in document {live:?}");
|
||||
// Follow-up headless queries using those ids find the curved handles and select the shapes.
|
||||
let pts = run(&mut s, "raw.points", json!({"ids": ids}));
|
||||
assert!(!pts["handles"].as_array().is_none_or(Vec::is_empty), "raw.points on returned ids must see handles: {pts}");
|
||||
run(&mut s, "element.select", json!({"ids": ids}));
|
||||
assert_eq!(s.selection.len(), ids.len(), "element.select accepts every returned id");
|
||||
// With merge: false, edited shapes are kept in place and returned ids also exist.
|
||||
let v2 = run(&mut s, "raw.convert_points", json!({"points": [[corner.x, corner.y]], "smooth": true, "handle": [10, 5], "merge": false}));
|
||||
assert_eq!(v2["merged"], json!(false), "{v2}");
|
||||
let ids2: Vec<u64> = v2["ids"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect();
|
||||
let live2: Vec<u64> = s.visible_elements().iter().map(|e| e.id.0).collect();
|
||||
assert!(!ids2.is_empty() && ids2.iter().all(|id| live2.contains(id)), "merge:false ids {ids2:?} must exist in {live2:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drawing_snaps_to_anchors_then_lines_while_snap_to_objects_is_on() {
|
||||
let mut s = raw_session();
|
||||
|
||||
+21
-1
@@ -306,7 +306,7 @@ impl Path {
|
||||
fn close_subpath_explicitly(&mut self, cmd: usize) {
|
||||
let Some(m) = self.subpath_start(cmd) else { return };
|
||||
let Some(PathCmd::MoveTo(start)) = self.cmds.get(m).copied() else { return };
|
||||
let Some(end) = (m + 1..self.cmds.len()).find(|&j| !self.is_segment(j)) else { return };
|
||||
let Some(end) = (cmd + 1..self.cmds.len()).find(|&j| !self.is_segment(j)) else { return };
|
||||
if !matches!(self.cmds.get(end), Some(PathCmd::Close)) {
|
||||
return;
|
||||
}
|
||||
@@ -752,6 +752,26 @@ mod tests {
|
||||
assert_eq!(p.point(1, C2), Some(a));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_smooth_handles_on_segment_after_close_does_not_shift_command() {
|
||||
// Segment after an earlier Close: closing the earlier subpath would shift cmd 3 to 4.
|
||||
let mut p = Path::new();
|
||||
p.move_to(Vec2::new(0.0, 0.0)).line_to(Vec2::new(100.0, 0.0)).close().line_to(Vec2::new(100.0, 100.0));
|
||||
let h = Vec2::new(15.0, 5.0);
|
||||
assert!(p.set_smooth_handles(3, h));
|
||||
assert_eq!(p.point(3, ANCHOR), Some(Vec2::new(100.0, 100.0)), "cmd 3 must not shift");
|
||||
assert_eq!(p.segment_start(3), Some(Vec2::new(0.0, 0.0)));
|
||||
assert_eq!(p.point(3, C2), Some(Vec2::new(100.0, 100.0) - h));
|
||||
// When followed by its own Close, the closing edge after cmd is made explicit without shifting cmd.
|
||||
let mut q = Path::new();
|
||||
q.move_to(Vec2::new(0.0, 0.0)).line_to(Vec2::new(100.0, 0.0)).close().line_to(Vec2::new(100.0, 100.0)).close();
|
||||
assert!(q.set_smooth_handles(3, h));
|
||||
let a = Vec2::new(100.0, 100.0);
|
||||
assert_eq!(q.point(3, ANCHOR), Some(a));
|
||||
assert_eq!(q.point(3, C2), Some(a - h));
|
||||
assert_eq!(q.point(4, C1), Some(a + h));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nearest_segment_finds_the_curve() {
|
||||
let p = cubic_path();
|
||||
|
||||
@@ -640,7 +640,7 @@ These commands have no menu item: the panels, the stage and the timeline use the
|
||||
- **Delete Anchor Points** `raw.delete_points` · changes the document
|
||||
Parameters: `{points: [[x, y], ..]} deletes the raw anchors at those positions (as raw.points lists them): every shape that has one is joined across it, so the drawing stays connected; a free end shortens its line; a shape left with nothing is removed; a point where three or more edges meet is kept`
|
||||
- **Convert Anchor Points** `raw.convert_points` · changes the document
|
||||
Parameters: `{points: [[x, y], ..], smooth: bool, handle?: [dx, dy], merge?: bool} makes the raw anchors at those positions smooth (their handles in one line; with handle, mirrored at anchor ± (dx, dy)) or corners (handles drawn in), in every shape that has them, and merges again (unless merge is false)`
|
||||
Parameters: `{points: [[x, y], ..], smooth: bool, handle?: [dx, dy], merge?: bool} makes the raw anchors at those positions smooth (their handles in one line; with handle, mirrored at anchor ± (dx, dy)) or corners (handles drawn in), in every shape that has them, and merges again (unless merge is false); returns {converted, changed, ids (the resulting element ids after any merge), merged}`
|
||||
- **Snap Point** `draw.snap`
|
||||
Parameters: ``{x, y, snap, moving?: bool} where a point drawn at (x, y) snaps within `snap` stage units, by the snap engine's priority (ADR 0076): an anchor (Snap to Objects), a guide (Snap to Guides), a line or fill edge, the grid (Snap to Grid), a whole pixel (Snap to Pixels); `moving`: the selection is what moves (a Free Transform handle), so it isn't a target; returns {x, y, snapped: {x, y, to: point|guide|line|grid|pixel} | null}``
|
||||
- **Replace** `find.replace` · changes the document
|
||||
|
||||
+63
-1
@@ -667,7 +667,13 @@ pub fn subselect_drag_start(app: &mut App, press: Pos2, shift: bool, alt: bool,
|
||||
/// Updates an anchor's curve handles while Alt-dragging from it with Subselection. Returns the
|
||||
/// raw element ids changed (for merging on release), if any.
|
||||
pub fn pull_handle(app: &mut App, target: PullTarget, anchor: Vec2, moved: &mut bool, cur: Vec2, shift: bool, xf: Xf) -> Option<serde_json::Value> {
|
||||
let h = drag_handle(anchor, cur, xf.zoom, shift)?;
|
||||
let h = if *moved {
|
||||
let q = if shift { constrain_45(anchor, cur) } else { cur };
|
||||
let d = q - anchor;
|
||||
(d.is_finite() && d != Vec2::ZERO).then_some(d)?
|
||||
} else {
|
||||
drag_handle(anchor, cur, xf.zoom, shift)?
|
||||
};
|
||||
*moved = true;
|
||||
match target {
|
||||
PullTarget::Object { id, cmd } => {
|
||||
@@ -1786,6 +1792,62 @@ mod tests {
|
||||
obj_app.run("edit.undo", json!({}));
|
||||
assert_eq!(path_of(&obj_app), before, "one undo restores the drawing object");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pull_handle_updates_below_drag_threshold_after_pull_starts() {
|
||||
// Drawing object: sub-threshold press stays unchanged, then once pulled past DRAG_MIN,
|
||||
// moving back within DRAG_MIN still updates the handle instead of keeping the old one.
|
||||
let ctx = egui::Context::default();
|
||||
let session = anicraft_engine::Session::new(anicraft_engine::doc::Document::default());
|
||||
let mut obj_app = App::with_context(&ctx, session, Some(std::path::PathBuf::from("unused.png")));
|
||||
obj_app.tool = Tool::Subselect;
|
||||
obj_app.view.zoom = 1.0;
|
||||
obj_app.run("draw.rect", json!({"x": 0, "y": 0, "w": 100, "h": 80, "object": true}));
|
||||
let id = obj_app.s.selection.ids().next().unwrap();
|
||||
let path_of = |a: &App| -> Path {
|
||||
let el = a.s.visible_elements().into_iter().find(|e| e.id == id).unwrap();
|
||||
let ElementKind::Shape(sh) = &el.kind else { panic!() };
|
||||
sh.path.transformed(&el.transform)
|
||||
};
|
||||
let before = path_of(&obj_app);
|
||||
let Drag::PullHandle { target, anchor, mut moved } = subselect_drag_start(&mut obj_app, pos2(100.0, 0.0), false, true, XF) else {
|
||||
panic!("expected PullHandle on object anchor");
|
||||
};
|
||||
// Initial sub-threshold movement leaves geometry unchanged.
|
||||
pull_handle(&mut obj_app, target, anchor, &mut moved, anchor + Vec2::new(1.0, 0.0), false, XF);
|
||||
assert!(!moved);
|
||||
assert_eq!(path_of(&obj_app), before);
|
||||
// Pull past threshold creates a long handle.
|
||||
pull_handle(&mut obj_app, target, anchor, &mut moved, anchor + Vec2::new(30.0, 20.0), false, XF);
|
||||
assert!(moved);
|
||||
let long = path_of(&obj_app).point(2, C1).unwrap().distance(anchor);
|
||||
assert!(long > 20.0, "expected long handle, got {long}");
|
||||
// Moving back to a nonzero sub-threshold offset updates the handle instead of leaving it long.
|
||||
pull_handle(&mut obj_app, target, anchor, &mut moved, anchor + Vec2::new(1.0, 0.0), false, XF);
|
||||
obj_app.s.end_gesture();
|
||||
let short = path_of(&obj_app).point(2, C1).unwrap().distance(anchor);
|
||||
assert!(short < DRAG_MIN && short > 0.5, "handle should shrink below threshold: {short} (was {long})");
|
||||
|
||||
// Same for a raw rectangle: moving back within DRAG_MIN updates raw handles too.
|
||||
let (mut raw_app, _) = app_and_ctx();
|
||||
subselect_click(&mut raw_app, pos2(50.0, 0.0), false, XF);
|
||||
let Drag::PullHandle { target, anchor, mut moved } = subselect_drag_start(&mut raw_app, pos2(100.0, 0.0), false, true, XF) else {
|
||||
panic!("expected PullHandle on raw anchor");
|
||||
};
|
||||
assert!(pull_handle(&mut raw_app, target, anchor, &mut moved, anchor + Vec2::new(1.0, 0.0), false, XF).is_none());
|
||||
assert!(!moved);
|
||||
assert!(pull_handle(&mut raw_app, target, anchor, &mut moved, anchor + Vec2::new(30.0, 20.0), false, XF).is_some());
|
||||
assert!(moved);
|
||||
let ids = pull_handle(&mut raw_app, target, anchor, &mut moved, anchor + Vec2::new(1.0, 0.0), false, XF);
|
||||
assert!(ids.is_some(), "established raw pull must still update below DRAG_MIN");
|
||||
if let Some(ids) = ids {
|
||||
raw_app.run("raw.merge", json!({"ids": ids}));
|
||||
}
|
||||
raw_app.s.end_gesture();
|
||||
let hs: Vec<_> = raw_shown(&raw_app).handles.into_iter().filter(|h| h.anchor.distance(anchor) < 0.3).collect();
|
||||
assert_eq!(hs.len(), 2, "{hs:?}");
|
||||
assert!(hs.iter().all(|h| h.at.distance(anchor) < DRAG_MIN && h.at.distance(anchor) > 0.5), "raw handles should reflect sub-threshold update: {hs:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user