From 6a3d5c670391e980540302cbf1f52f7b19134125 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 10 Oct 2026 22:26:43 +0000 Subject: [PATCH] Viewer: the Pen, Add, Delete and Convert Vertex tools edit motion paths, with pen pointers (#553) Motion-path hits were only handled for the Selection tool, so the Pen slot's tools did nothing on a layer's motion path, and every Pen slot tool showed the same crosshair. As in After Effects, on a motion path: - Add Vertex (and the Pen) add a Position keyframe at the clicked frame dot, with the path's value there (prop.addKey), so the path keeps its shape; - Delete Vertex removes the clicked keyframe (keys.select + keys.delete); - Convert Vertex switches it between Linear and Auto Bezier spatial interpolation (keys.interpolation {spatial}). motion_path now reports its frame dots for the hit test. Over the viewer the Pen slot tools draw their own pen glyph (Pen, Add, Delete, Convert, Mask Feather) at the pointer in place of the system cursor, and the Pen shows Add Vertex's pen over a mask, shape or motion path it would add a point to. Test: ui_viewer clicks a two-key motion path with each tool and checks the added key, its spatial interpolation both ways, its removal, and the pointer. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QiAJeFttZp83Fh7YuzAR5f --- crates/ui-egui/src/panels/viewer.rs | 92 ++++++++++++++++++-- crates/ui-egui/src/panels/viewer_overlays.rs | 42 ++++++++- crates/ui-egui/tests/ui_viewer.rs | 47 ++++++++++ 3 files changed, 174 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index a532cdeb..248f9bcb 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -872,6 +872,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let mut handle_hits: Vec<(LayerId, usize, Pos2, egui::CursorIcon)> = Vec::new(); let mut vertex_hits: Vec = Vec::new(); let mut key_hits: Vec = Vec::new(); + let mut motion_frames: Vec = Vec::new(); let mut paths: Vec = Vec::new(); let sel_vertices = app.session.state.selected_vertices.clone(); // Pen path in progress ends when the tool changes or its mask is gone; Esc / Enter also end @@ -891,7 +892,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { for l in comp.layers.iter().filter(|l| selected.contains(&l.id) && l.is_active_at(time)) { let col = if label_handles { t.label(l.label) } else { t.accent }; // Motion path of animated position (keyframes and spatial tangents are draggable). - ov::motion_path(app, &painter, &map, &ectx, l, col, &mut key_hits); + ov::motion_path(app, &painter, &map, &ectx, l, col, &mut key_hits, &mut motion_frames); // Masks and shape paths. if app.ui.viewer.show_masks { let pen = ui.data(|d| d.get_temp::<(LayerId, u64)>(pen_id())); @@ -1153,7 +1154,15 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { Tool::Orbit => egui::CursorIcon::Alias, Tool::PanCamera => egui::CursorIcon::Move, Tool::Dolly => egui::CursorIcon::ResizeVertical, - t if t.is_shape() || t.is_pen() => egui::CursorIcon::Crosshair, + t if t.is_pen() => { + pen_cursor( + &ctx, + hp, + pen_cursor_icon(t, ov::segment_at(&paths, &map, hp, 5.0).is_some() || ov::motion_frame_at(&motion_frames, &key_hits, hp).is_some()), + ); + egui::CursorIcon::None + } + t if t.is_shape() => egui::CursorIcon::Crosshair, _ if app.ui.viewer.roi_draw => egui::CursorIcon::Crosshair, t if t.puppet_kind().is_some() => match super::puppet_tool::hit(&pin_hits, hp) { Some((_, super::puppet_tool::PinPart::Ring)) => egui::CursorIcon::Alias, @@ -1201,7 +1210,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { && ui.input(|i| i.pointer.primary_pressed()) && let Some(pos) = ui.input(|i| i.pointer.press_origin()) { - pen_press(app, ui, &ectx, &map, &paths, pos, mods); + pen_press(app, ui, &ectx, &map, &paths, ov::motion_frame_at(&motion_frames, &key_hits, pos), pos, mods); } // A pen click (press + release without a drag) ends its tangent gesture on release. if ui.input(|i| i.pointer.primary_released()) @@ -1703,19 +1712,35 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } Tool::Type | Tool::TypeVertical => {} Tool::Pen | Tool::MaskFeather => {} + // On a motion path (After Effects): Add Vertex adds a keyframe where it is clicked, + // Delete Vertex removes the keyframe, Convert Vertex switches it between Linear and + // Auto Bezier spatial interpolation. Tool::PenAdd => { if let Some((l, m, seg, tt)) = ov::segment_at(&paths, &map, pos, 6.0) { let _ = app.session.execute("mask.insertVertex", json!({"layer": l.0, "mask": m, "segment": seg, "t": tt})); + } else if let Some(f) = ov::motion_frame_at(&motion_frames, &key_hits, pos) { + add_motion_key(app, f); } } Tool::PenDelete => { if let Some(h) = vertex_at(pos, false) { let _ = app.session.execute("mask.deleteVertices", json!({"vertices": [{"layer": h.layer.0, "mask": h.mask, "index": h.index}]})); + } else if let Some(k) = ov::motion_key_at(&key_hits, pos) + && select_motion_key(app, &k) + { + let _ = app.session.execute("keys.delete", json!({})); } } Tool::PenConvert => { if let Some(h) = vertex_at(pos, false) { let _ = app.session.execute("mask.convertVertex", json!({"layer": h.layer.0, "mask": h.mask, "index": h.index})); + } else if let Some(k) = ov::motion_key_at(&key_hits, pos) { + // A key showing direction handles becomes Linear; one without, Auto Bezier. + let curved = key_hits.iter().any(|h| h.tangent.is_some() && h.layer == k.layer && h.prop == k.prop && h.time == k.time); + if select_motion_key(app, &k) { + let spatial = if curved { "linear" } else { "autoBezier" }; + let _ = app.session.execute("keys.interpolation", json!({"spatial": spatial})); + } } } Tool::Selection if mods.alt && vertex_at(pos, false).is_some() => { @@ -1907,7 +1932,60 @@ fn draw_rigs(app: &mut EffectcraftApp, painter: &egui::Painter, map: &ViewerMap, /// footage/solid layer (or shape layer, with Tool Creates Mask), a shape path (Shape n group with /// the Tools bar's Fill and Stroke) on the selected shape layer, or a new shape layer when /// nothing is selected. The placed point snaps. -fn pen_press(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, map: &ViewerMap, paths: &[ov::PathInfo], pos: Pos2, mods: egui::Modifiers) { +/// Add a Position keyframe at a motion path's frame, keeping the path where it is. +fn add_motion_key(app: &mut EffectcraftApp, f: ov::PathFrame) { + if let Err(e) = app.session.execute("prop.addKey", json!({"layer": f.layer.0, "prop": f.prop, "time": f.time.seconds()})) { + app.ui.status = e.to_string(); + } +} + +/// Select only the motion path's keyframe `k` (what the vertex tools act on). +fn select_motion_key(app: &mut EffectcraftApp, k: &ov::KeyHit) -> bool { + app.session.execute("keys.select", json!({"keys": [{"layer": k.layer.0, "prop": k.prop, "time": k.time.seconds()}]})).is_ok() +} + +/// The pointer for a Pen slot tool: the tool's own pen, the Pen showing Add Vertex's over a path +/// it would add a point to (After Effects). +fn pen_cursor_icon(tool: Tool, over_path: bool) -> crate::icons::Icon { + use crate::icons::Icon; + match tool { + Tool::Pen if over_path => Icon::PenAdd, + Tool::PenAdd => Icon::PenAdd, + Tool::PenDelete => Icon::PenDelete, + Tool::PenConvert => Icon::PenConvert, + Tool::MaskFeather => Icon::MaskFeather, + _ => Icon::Pen, + } +} + +/// Paint pen pointer `icon` with its tip at `at`, outlined for any background. +fn pen_cursor(ctx: &egui::Context, at: Pos2, icon: crate::icons::Icon) { + use crate::icons::Icon; + // Where each glyph's tip sits in its 16 × 16 box. + let tip = match icon { + Icon::PenAdd | Icon::PenDelete | Icon::MaskFeather => vec2(6.0, 2.5), + Icon::PenConvert => vec2(8.0, 3.5), + _ => vec2(8.0, 1.8), + }; + let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::new("viewer-pen-cursor"))); + let rect = Rect::from_min_size(at - tip, vec2(16.0, 16.0)); + for d in [vec2(-1.0, 0.0), vec2(1.0, 0.0), vec2(0.0, -1.0), vec2(0.0, 1.0)] { + crate::icons::paint(&painter, rect.translate(d), icon, Color32::BLACK); + } + crate::icons::paint(&painter, rect, icon, Color32::WHITE); +} + +#[allow(clippy::too_many_arguments)] +fn pen_press( + app: &mut EffectcraftApp, + ui: &mut egui::Ui, + ectx: &EvalCtx, + map: &ViewerMap, + paths: &[ov::PathInfo], + motion_frame: Option, + pos: Pos2, + mods: egui::Modifiers, +) { use effectcraft_engine::project::LayerSource; let gid = egui::Id::new("viewer-gesture"); let current: Option<(LayerId, u64)> = ui.data(|d| d.get_temp(pen_id())); @@ -1939,7 +2017,11 @@ fn pen_press(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, map: & } return; } - // On a visible path's segment the Pen adds a vertex there. + // On a visible path's segment the Pen adds a vertex there; on a motion path, a keyframe. + if let Some(f) = motion_frame { + add_motion_key(app, f); + return; + } if let Some((l, m, seg, t)) = ov::segment_at(paths, map, pos, 5.0) { let _ = app.session.execute("mask.insertVertex", json!({"layer": l.0, "mask": m, "segment": seg, "t": t})); return; diff --git a/crates/ui-egui/src/panels/viewer_overlays.rs b/crates/ui-egui/src/panels/viewer_overlays.rs index 0038f192..54218e9f 100644 --- a/crates/ui-egui/src/panels/viewer_overlays.rs +++ b/crates/ui-egui/src/panels/viewer_overlays.rs @@ -248,6 +248,32 @@ pub(crate) struct KeyHit { pub p2c: Mat3, } +/// A frame on a motion path (one of its dots): where the Pen and Add Vertex tools add a +/// keyframe. +#[derive(Clone, Copy)] +pub(crate) struct PathFrame { + pub layer: LayerId, + pub prop: u64, + /// Layer time of the frame. + pub time: Tick, + pub pos: Pos2, +} + +/// The keyframe dot on a motion path under `pos`. +pub(crate) fn motion_key_at(hits: &[KeyHit], pos: Pos2) -> Option { + hits.iter().rev().find(|h| h.tangent.is_none() && h.pos.distance(pos) < 6.0).copied() +} + +/// The motion-path frame nearest `pos` (within a few points) where a keyframe can be added: not +/// on a keyframe dot already there. +pub(crate) fn motion_frame_at(frames: &[PathFrame], hits: &[KeyHit], pos: Pos2) -> Option { + if motion_key_at(hits, pos).is_some() { + return None; + } + let near = frames.iter().filter(|f| f.pos.distance(pos) < 5.0).min_by(|a, b| a.pos.distance(pos).total_cmp(&b.pos.distance(pos)))?; + (!hits.iter().any(|h| h.tangent.is_none() && h.layer == near.layer && h.prop == near.prop && h.time == near.time)).then_some(*near) +} + /// Parent space → comp matrix of a 2D layer at the context time. pub(crate) fn parent_to_comp(ectx: &EvalCtx, l: &Layer) -> Mat3 { match l.parent.and_then(|p| ectx.comp.layer(p)) { @@ -261,7 +287,17 @@ pub(crate) fn parent_to_comp(ectx: &EvalCtx, l: &Layer) -> Mat3 { /// Draw the position motion path of a selected 2D layer: frame dots along the path, keyframe /// squares (filled when selected) and spatial Bezier tangent handles. Works through parents. -pub(crate) fn motion_path(app: &mut EffectcraftApp, painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, l: &Layer, col: Color32, hits: &mut Vec) { +#[allow(clippy::too_many_arguments)] +pub(crate) fn motion_path( + app: &mut EffectcraftApp, + painter: &egui::Painter, + map: &ViewerMap, + ectx: &EvalCtx, + l: &Layer, + col: Color32, + hits: &mut Vec, + frames: &mut Vec, +) { let Some(pos) = l.transform().and_then(|tr| tr.get("position")) else { return }; if pos.keys.len() < 2 || l.is_3d() { return; @@ -278,7 +314,9 @@ pub(crate) fn motion_path(app: &mut EffectcraftApp, painter: &egui::Painter, map let mut pts = vec![]; let mut lt = lt0; while lt <= lt1 && pts.len() < 20_000 { - pts.push(to_scr(pos.value_at(lt).as_vec3())); + let q = to_scr(pos.value_at(lt).as_vec3()); + frames.push(PathFrame { layer: l.id, prop: pos.uid, time: lt, pos: q }); + pts.push(q); lt += fd; } pts.push(to_scr(pos.value_at(lt1).as_vec3())); diff --git a/crates/ui-egui/tests/ui_viewer.rs b/crates/ui-egui/tests/ui_viewer.rs index 120c4db0..a1ca76ab 100644 --- a/crates/ui-egui/tests/ui_viewer.rs +++ b/crates/ui-egui/tests/ui_viewer.rs @@ -1900,3 +1900,50 @@ fn roto_brush_ctrl_drag_resizes_the_brush() { assert_eq!(s.len(), 1); assert!((s[0].radius - dia / 2.0).abs() < 1e-6, "{} vs {dia}", s[0].radius); } + +/// #553: the Pen slot's tools work on a motion path as in After Effects: Add Vertex (and the +/// Pen) add a keyframe where the path is clicked, Convert Vertex switches a keyframe between +/// Linear and Auto Bezier, Delete Vertex removes it; the pointer is the tool's pen. +#[test] +fn vertex_tools_edit_motion_path_keyframes() { + let mut h = harness(); + let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id; + let s = &mut h.state_mut().session; + s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap(); + s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 0.0, "value": [100, 100, 0]})).unwrap(); + s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 2.0, "value": [500, 100, 0]})).unwrap(); + s.execute("view.snapping", json!({"value": false})).unwrap(); + s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(3.0)); + let keys = |h: &Harness<'_, EffectcraftApp>| { + h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/position").unwrap().keys.clone() + }; + h.state_mut().ui.tool = Tool::PenAdd; + h.run_steps(3); + let mid = screen(&h, [300.0, 100.0]); + h.input_mut().events.push(Event::PointerMoved(mid)); + h.step(); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::None, "the pen is painted in place of the system pointer"); + click(&mut h, mid); + let k = keys(&h); + assert_eq!(k.len(), 3, "Add Vertex adds a keyframe on the path"); + let v = k[1].value.as_vec3(); + assert!((v[0] - 300.0).abs() < 10.0 && (v[1] - 100.0).abs() < 1.0, "{v:?}"); + assert!((k[1].time.seconds() - 1.0).abs() < 0.1, "{:?}", k[1].time); + // Convert Vertex: the new key's handles go (Linear), then come back (Auto Bezier). + h.state_mut().ui.tool = Tool::PenConvert; + let at = screen(&h, [v[0] as f32, v[1] as f32]); + click(&mut h, at); + let k = keys(&h); + assert!(!k[1].spatial_auto && k[1].spatial_in == [0.0; 3] && k[1].spatial_out == [0.0; 3], "Linear"); + click(&mut h, at); + assert!(keys(&h)[1].spatial_auto, "Auto Bezier"); + // Delete Vertex removes it. + h.state_mut().ui.tool = Tool::PenDelete; + click(&mut h, at); + assert_eq!(keys(&h).len(), 2); + // The Pen adds one too. + h.state_mut().ui.tool = Tool::Pen; + let quarter = screen(&h, [200.0, 100.0]); + click(&mut h, quarter); + assert_eq!(keys(&h).len(), 3); +}