M13.14: puppet pin editing — click / Shift-click selects pins (puppet.selectPins), a drag from empty canvas or Alt-drag marquee-selects them, Select All with a pin selected takes every pin of that kind, Delete removes the selected pins instead of their layer; Advanced and Bend pins rotate by their ring and scale by its square (Shift: 15° / 5 % steps, one undo step per drag); ⌘/Ctrl-dragging one of several selected pins records them all; pins in their kind's colour, larger under the pointer; pushpin icons for the five pin tools

This commit is contained in:
bflatastic
2026-10-04 00:19:24 +07:00
parent 244cee4687
commit 6869cff51b
9 changed files with 562 additions and 64 deletions
+36 -5
View File
@@ -34,7 +34,12 @@ pub enum Icon {
Eraser,
RotoBrush,
RefineEdge,
/// Puppet Position Pin: a pushpin. The other pin tools add a badge at the lower right.
Puppet,
PuppetAdvanced,
PuppetBend,
PuppetStarch,
PuppetOverlap,
// switches & timeline
Eye,
Speaker,
@@ -163,6 +168,12 @@ impl Pen16<'_> {
.collect();
self.line(&pts);
}
/// An upright pushpin centred on `cx`: a cap, a body flaring to its base and a needle.
fn pushpin(&self, cx: f32) {
self.fill(&[(cx - 3.0, 1.5), (cx + 3.0, 1.5), (cx + 2.0, 3.0), (cx - 2.0, 3.0)]);
self.closed(&[(cx - 2.0, 3.0), (cx + 2.0, 3.0), (cx + 2.4, 7.0), (cx + 4.0, 8.8), (cx - 4.0, 8.8), (cx - 2.4, 7.0)]);
self.line(&[(cx, 8.8), (cx, 14.8)]);
}
fn ellipse(&self, cx: f32, cy: f32, rx: f32, ry: f32) {
let n = 32;
let pts: Vec<(f32, f32)> = (0..n)
@@ -294,11 +305,31 @@ pub fn paint(painter: &Painter, rect: Rect, icon: Icon, color: Color32) {
pen.line(&[(8.0, 14.0), (9.5, 12.5)]);
pen.line(&[(11.0, 14.0), (12.5, 12.5)]);
}
Puppet => {
pen.circle(8.0, 3.6, 1.6);
pen.line(&[(8.0, 5.2), (8.0, 10.0)]);
pen.line(&[(3.5, 7.0), (8.0, 6.5), (12.5, 7.0)]);
pen.line(&[(5.5, 14.0), (8.0, 10.0), (10.5, 14.0)]);
Puppet => pen.pushpin(8.0),
PuppetAdvanced => {
// Rotation ring with its square scale handle.
pen.pushpin(6.5);
pen.circle(12.0, 12.0, 2.4);
pen.rect_fill(13.6, 11.2, 15.2, 12.8);
}
PuppetBend => {
// A curved arrow.
pen.pushpin(6.5);
pen.arc(11.8, 13.2, 3.0, 200.0, 340.0);
pen.fill(&[(14.9, 11.0), (15.4, 13.8), (12.9, 12.6)]);
}
PuppetStarch => {
// Stiff bars.
pen.pushpin(6.5);
for y in [10.5, 12.75, 15.0] {
pen.line(&[(10.5, y), (15.0, y)]);
}
}
PuppetOverlap => {
// Two overlapping sheets.
pen.pushpin(6.5);
pen.rect(10.0, 9.8, 13.4, 13.2);
pen.rect_fill(11.8, 11.6, 15.2, 15.0);
}
Eye => {
pen.line(&[(1.5, 8.0), (4.0, 5.0), (8.0, 3.8), (12.0, 5.0), (14.5, 8.0), (12.0, 11.0), (8.0, 12.2), (4.0, 11.0), (1.5, 8.0)]);
+86 -15
View File
@@ -1,6 +1,7 @@
//! Puppet tools in the Composition viewer: the deformed mesh and pins of the selected layers'
//! Puppet effect, click to add a pin (`puppet.addPin`), drag a pin to move it
//! (`puppet.movePin`, merged into one undo step per drag).
//! Puppet effect, click to add a pin (`puppet.addPin`), click / Shift-click a pin to select it
//! (`puppet.selectPins`), drag a pin to move it (`puppet.movePin`), and drag an Advanced or Bend
//! pin's ring to rotate it or its square to scale it (`puppet.setPin`); each drag is one undo step.
use std::sync::Arc;
@@ -17,6 +18,9 @@ use crate::EffectcraftApp;
/// Deformed meshes of a layer's Puppet effect: (mesh uid, mesh, deformed vertices, pins).
pub type Overlay = Arc<Vec<(u64, Arc<Mesh>, Vec<[f64; 2]>, Vec<Pin>)>>;
/// Rotation ring radius (screen points) of Advanced and Bend pins.
pub const RING: f32 = 9.0;
/// A pin drawn this frame.
#[derive(Clone, Copy, Debug)]
pub struct PinHit {
@@ -24,6 +28,36 @@ pub struct PinHit {
pub pin: u64,
pub kind: PinKind,
pub pos: Pos2,
/// Rotation (°) and scale (1 = 100 %) now, for Advanced and Bend pins.
pub rotation: f64,
pub scale: f64,
/// The scale handle on the ring (Advanced and Bend pins).
pub handle: Option<Pos2>,
}
/// The part of a pin under the pointer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PinPart {
/// The pin itself (drag moves Position, Advanced, Starch and Overlap pins).
Center,
/// The rotation ring of an Advanced or Bend pin.
Ring,
/// The scale handle on that ring.
Scale,
}
/// The pin part under `p`, topmost pin first.
pub fn hit(hits: &[PinHit], p: Pos2) -> Option<(PinHit, PinPart)> {
hits.iter().rev().find_map(|h| {
let d = h.pos.distance(p);
match h.handle {
Some(s) if s.distance(p) < 5.0 => Some((*h, PinPart::Scale)),
Some(_) if d < 6.0 => Some((*h, PinPart::Center)),
Some(_) if (d - RING).abs() < 4.0 => Some((*h, PinPart::Ring)),
None if d < 8.0 => Some((*h, PinPart::Center)),
_ => None,
}
})
}
#[derive(Clone)]
@@ -65,8 +99,31 @@ pub fn pin_position(mesh: &Mesh, def: &[[f64; 2]], p: &Pin) -> [f64; 2] {
}
}
/// Draw the mesh (when `show_mesh`) and pins of `layer`; returns pin hit targets.
pub fn draw(painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, layer: &Layer, ov: &Overlay, show_mesh: bool, selected: &[u64]) -> Vec<PinHit> {
/// Pin colours by kind: Position yellow, Starch red, Overlap blue, Advanced blue-green, Bend
/// orange-brown.
pub fn pin_color(kind: PinKind) -> Color32 {
match kind {
PinKind::Position => Color32::from_rgb(0xf0, 0xd0, 0x40),
PinKind::Starch => Color32::from_rgb(0xe0, 0x50, 0x50),
PinKind::Overlap => Color32::from_rgb(0x40, 0x90, 0xf0),
PinKind::Advanced => Color32::from_rgb(0x30, 0xc8, 0xb4),
PinKind::Bend => Color32::from_rgb(0xc8, 0x82, 0x3c),
}
}
/// Draw the mesh (when `show_mesh`) and pins of `layer` (the pin under `hover` is drawn larger);
/// returns pin hit targets.
#[allow(clippy::too_many_arguments)]
pub fn draw(
painter: &egui::Painter,
map: &ViewerMap,
ectx: &EvalCtx,
layer: &Layer,
ov: &Overlay,
show_mesh: bool,
selected: &[u64],
hover: Option<Pos2>,
) -> Vec<PinHit> {
let (m, _) = l2c(ectx, layer);
let scr = |q: [f64; 2]| {
let c = m.apply(gv2(q[0], q[1]));
@@ -84,25 +141,39 @@ pub fn draw(painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, layer: &La
}
}
for pn in pins {
let pos = scr(pin_position(mesh, def, pn));
let at = pin_position(mesh, def, pn);
let pos = scr(at);
let sel = selected.contains(&pn.uid);
let col = match pn.kind {
PinKind::Starch => Color32::from_rgb(0xe0, 0x50, 0x50),
PinKind::Overlap => Color32::from_rgb(0x40, 0x90, 0xf0),
PinKind::Bend => Color32::from_rgb(0xf0, 0x90, 0x30),
_ => Color32::from_rgb(0xf0, 0xd0, 0x40),
};
let col = pin_color(pn.kind);
if matches!(pn.kind, PinKind::Starch | PinKind::Overlap) {
// Extent ring (layer pixels → screen).
let r = (scr([pn.rest[0] + pn.extent, pn.rest[1]]) - scr(pn.rest)).length();
painter.circle_stroke(pos, r.max(2.0), Stroke::new(1.0, col.gamma_multiply(0.6)));
}
// Advanced and Bend pins: the rotation ring with a square scale handle at the pin's
// rotation (the layer's own orientation on screen).
let mut handle = None;
if matches!(pn.kind, PinKind::Bend | PinKind::Advanced) {
painter.circle_stroke(pos, 9.0, Stroke::new(1.5, col));
painter.circle_stroke(pos, RING, Stroke::new(1.5, col));
let (s, c) = pn.rotation.to_radians().sin_cos();
let dir = (scr([at[0] + c, at[1] + s]) - pos).normalized();
if dir.x.is_finite() && dir.y.is_finite() {
let hp = pos + dir * RING;
painter.rect_filled(egui::Rect::from_center_size(hp, egui::vec2(5.0, 5.0)), 0.0, col);
painter.rect_stroke(
egui::Rect::from_center_size(hp, egui::vec2(5.0, 5.0)),
0.0,
Stroke::new(1.0, Color32::BLACK),
egui::StrokeKind::Outside,
);
handle = Some(hp);
}
}
painter.circle_filled(pos, if sel { 5.5 } else { 4.5 }, if sel { col } else { col.gamma_multiply(0.85) });
painter.circle_stroke(pos, if sel { 5.5 } else { 4.5 }, Stroke::new(1.0, Color32::BLACK));
hits.push(PinHit { layer: layer.id, pin: pn.uid, kind: pn.kind, pos });
// Pins grow under the pointer.
let r = if sel { 5.5 } else { 4.5 } + if hover.is_some_and(|h| h.distance(pos) < 8.0) { 1.5 } else { 0.0 };
painter.circle_filled(pos, r, if sel { col } else { col.gamma_multiply(0.85) });
painter.circle_stroke(pos, r, Stroke::new(1.0, Color32::BLACK));
hits.push(PinHit { layer: layer.id, pin: pn.uid, kind: pn.kind, pos, rotation: pn.rotation, scale: pn.scale, handle });
}
}
hits
+112 -17
View File
@@ -4,6 +4,7 @@
//! 3D views (Active Camera / Front / … / Custom View 3), camera tools (orbit, pan, dolly) and
//! camera/light wireframes.
use effectcraft_engine::effects::puppet::PinKind;
use effectcraft_engine::geom::{Mat3, vec2 as gv2};
use effectcraft_engine::project::{Layer, LayerId};
use effectcraft_engine::render::EvalCtx;
@@ -121,12 +122,36 @@ enum Gesture {
pin: u64,
inv: Mat3,
},
/// Rotating an Advanced / Bend pin by its ring: the pin's screen centre, the pointer's last
/// angle (radians) and the rotation so far (°, accumulated so it can pass a full turn).
PuppetRotate {
layer: LayerId,
pin: u64,
center: Pos2,
last: f32,
rotation: f64,
},
/// Scaling an Advanced / Bend pin by its square: the pin's screen centre, the press distance
/// from it and the scale at the press (1 = 100 %).
PuppetScale {
layer: LayerId,
pin: u64,
center: Pos2,
start: f32,
scale: f64,
},
/// Puppet tools: a marquee selecting pins (dragged outside the layer's art, or Alt-dragged).
PinMarquee {
start: Pos2,
},
/// Puppet tool Record mode (⌘/Ctrl-drag a pin): the drag is sampled in real time (seconds
/// since it began, layer space) and becomes keyframes on release (`puppet.recordPin`); the
/// current time runs along while recording.
PuppetRecord {
layer: LayerId,
pin: u64,
/// Other selected pins of the layer, moving with the dragged one.
others: Vec<u64>,
inv: Mat3,
began: f64,
cti: Tick,
@@ -801,11 +826,17 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
if let Some(l) = comp.layer(*lid)
&& let Some(ov) = super::puppet_tool::overlay(app, &ctx, cid, l, time)
{
pin_hits.extend(super::puppet_tool::draw(&painter, &map, &ectx, l, &ov, app.session.state.puppet.show_mesh, &sel_props));
let hover = ui.input(|i| i.pointer.hover_pos());
pin_hits.extend(super::puppet_tool::draw(&painter, &map, &ectx, l, &ov, app.session.state.puppet.show_mesh, &sel_props, hover));
}
}
for h in &pin_hits {
app.auto.add(&format!("viewer.puppetPin.{}", h.pin), Rect::from_center_size(h.pos, vec2(10.0, 10.0)), "Puppet pin");
if let Some(hp) = h.handle {
let ring = h.pos + (h.pos - hp);
app.auto.add(&format!("viewer.puppetPin.{}.rotate", h.pin), Rect::from_center_size(ring, vec2(6.0, 6.0)), "Rotate puppet pin");
app.auto.add(&format!("viewer.puppetPin.{}.scale", h.pin), Rect::from_center_size(hp, vec2(6.0, 6.0)), "Scale puppet pin");
}
}
}
@@ -826,13 +857,13 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
Tool::Dolly => egui::CursorIcon::ResizeVertical,
t if t.is_shape() || t.is_pen() => egui::CursorIcon::Crosshair,
_ if app.ui.viewer.roi_draw => egui::CursorIcon::Crosshair,
t if t.puppet_kind().is_some() => {
if pin_hits.iter().any(|h| h.pos.distance(hp) < 8.0) {
egui::CursorIcon::Move
} else {
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,
Some((_, super::puppet_tool::PinPart::Scale)) => egui::CursorIcon::ResizeNwSe,
Some((h, _)) if h.kind == PinKind::Bend => egui::CursorIcon::PointingHand,
Some(_) => egui::CursorIcon::Move,
None => egui::CursorIcon::Crosshair,
},
_ => {
if super::tracker::hit_at(&track_hits, hp).is_some() || key_hits.iter().any(|h| h.pos.distance(hp) < 6.0) {
egui::CursorIcon::Move
@@ -944,13 +975,36 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
})
}),
t if t.is_shape() => Some(Gesture::Create { tool: t, start: cpt }),
t if t.puppet_kind().is_some() => pin_hits.iter().rev().find(|h| h.pos.distance(press) < 8.0).and_then(|h| {
t if t.puppet_kind().is_some() && super::puppet_tool::hit(&pin_hits, press).is_none() => {
// Off the pins: Alt or a press outside the art drags a marquee that selects pins.
(mods.alt || pick(app, &ectx, cpt, false).is_none()).then_some(Gesture::PinMarquee { start: press })
}
t if t.puppet_kind().is_some() => super::puppet_tool::hit(&pin_hits, press).and_then(|(h, part)| {
use super::puppet_tool::PinPart;
let angle = (press - h.pos).angle();
match part {
PinPart::Ring => return Some(Gesture::PuppetRotate { layer: h.layer, pin: h.pin, center: h.pos, last: angle, rotation: h.rotation }),
PinPart::Scale => {
let start = h.pos.distance(press).max(1.0);
return Some(Gesture::PuppetScale { layer: h.layer, pin: h.pin, center: h.pos, start, scale: h.scale });
}
// A Bend pin has no position: it turns and scales by its ring only.
PinPart::Center if h.kind == PinKind::Bend => return None,
PinPart::Center => {}
}
let l = comp.layer(h.layer)?;
let inv = l2c(&ectx, l).0.inverse()?;
if mods.command {
let lp = inv.apply(gv2(cpt[0], cpt[1]));
let began = ui.input(|i| i.time);
return Some(Gesture::PuppetRecord { layer: h.layer, pin: h.pin, inv, began, cti: time, samples: vec![(0.0, [lp.x, lp.y])] });
// Dragging one of several selected pins records them all.
let sel = &app.session.state.selected_props;
let others: Vec<u64> = if sel.contains(&(h.layer, h.pin)) {
pin_hits.iter().filter(|o| o.layer == h.layer && o.pin != h.pin && sel.contains(&(o.layer, o.pin))).map(|o| o.pin).collect()
} else {
vec![]
};
return Some(Gesture::PuppetRecord { layer: h.layer, pin: h.pin, others, inv, began, cti: time, samples: vec![(0.0, [lp.x, lp.y])] });
}
Some(Gesture::PuppetPin { layer: h.layer, pin: h.pin, inv })
}),
@@ -1188,7 +1242,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
}
let _ = app.session.execute("mask.setVertex", params);
}
Gesture::PuppetRecord { layer, pin, inv, began, cti, mut samples } => {
Gesture::PuppetRecord { layer, pin, others, inv, began, cti, mut samples } => {
let lp = inv.apply(gv2(cpt[0], cpt[1]));
let now = ui.input(|i| i.time) - began;
if samples.last().is_none_or(|s| now > s.0) {
@@ -1199,9 +1253,30 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
app.session.set_time(cti + Tick::from_seconds_f64(now / speed));
painter.circle_filled(pos, 6.0, Color32::from_rgb(0xe0, 0x30, 0x30));
painter.text(pos + vec2(10.0, -10.0), Align2::LEFT_BOTTOM, "Recording", Tokens::ui(11.0), Color32::from_rgb(0xff, 0x60, 0x60));
ui.data_mut(|dd| dd.insert_temp(gid, Gesture::PuppetRecord { layer, pin, inv, began, cti, samples }));
ui.data_mut(|dd| dd.insert_temp(gid, Gesture::PuppetRecord { layer, pin, others, inv, began, cti, samples }));
ui.ctx().request_repaint();
}
Gesture::PuppetRotate { layer, pin, center, last, rotation } => {
// Accumulate the turn since the last frame (wrapped to ±180°) so a drag can go
// past a full revolution; Shift snaps to 15°.
let a = (pos - center).angle();
let mut d = (a - last) as f64;
d = (d + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI;
let rotation = rotation + d.to_degrees();
let deg = if mods.shift { (rotation / 15.0).round() * 15.0 } else { rotation };
ui.data_mut(|dd| dd.insert_temp(gid, Gesture::PuppetRotate { layer, pin, center, last: a, rotation }));
if let Err(e) = app.session.execute("puppet.setPin", json!({"layer": layer.0, "pin": pin, "rotation": deg, "merge": merge})) {
app.ui.status = e.to_string();
}
}
Gesture::PuppetScale { layer, pin, center, start, scale } => {
// Shift snaps the scale to 5 % steps.
let pct = scale * (pos.distance(center) / start) as f64 * 100.0;
let pct = if mods.shift { (pct / 5.0).round() * 5.0 } else { pct }.max(1.0);
if let Err(e) = app.session.execute("puppet.setPin", json!({"layer": layer.0, "pin": pin, "scale": pct, "merge": merge})) {
app.ui.status = e.to_string();
}
}
Gesture::PuppetPin { layer, pin, inv } => {
let lp = inv.apply(gv2(cpt[0], cpt[1]));
if let Err(e) = app.session.execute("puppet.movePin", json!({"layer": layer.0, "pin": pin, "position": [lp.x, lp.y], "merge": merge})) {
@@ -1224,7 +1299,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
q["merge"] = json!(merge);
let _ = app.session.execute("track.setPoint", q);
}
Gesture::Marquee { start } => {
Gesture::Marquee { start } | Gesture::PinMarquee { start } => {
let r = Rect::from_two_pos(start, pos);
painter.rect_filled(r, 0.0, t.accent.gamma_multiply(0.12));
painter.rect_stroke(r, 0.0, Stroke::new(1.0, t.accent), StrokeKind::Middle);
@@ -1234,9 +1309,27 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
if resp.drag_stopped() {
if let (Some(g), Some(pos)) = (gesture, resp.interact_pointer_pos()) {
match g {
Gesture::PuppetRecord { layer, pin, cti, samples, .. } => {
Gesture::PinMarquee { start } => {
// Pins inside the box, per layer; Shift adds to the selection.
let r = Rect::from_two_pos(start, pos);
let mut first = !mods.shift;
let mut layers: Vec<LayerId> = pin_hits.iter().map(|h| h.layer).collect();
layers.dedup();
if first && pin_hits.iter().all(|h| !r.contains(h.pos)) {
let _ = app.session.execute("puppet.selectPins", json!({"pins": []}));
}
for l in layers {
let pins: Vec<u64> = pin_hits.iter().filter(|h| h.layer == l && r.contains(h.pos)).map(|h| h.pin).collect();
if !pins.is_empty() {
let _ = app.session.execute("puppet.selectPins", json!({"layer": l.0, "pins": pins, "add": !first}));
first = false;
}
}
}
Gesture::PuppetRecord { layer, pin, others, cti, samples, .. } => {
let pts: Vec<serde_json::Value> = samples.iter().map(|(t, p)| json!([t, p[0], p[1]])).collect();
if let Err(e) = app.session.execute("puppet.recordPin", json!({"layer": layer.0, "pin": pin, "samples": pts, "start": cti.seconds()})) {
let q = json!({"layer": layer.0, "pin": pin, "pins": others, "samples": pts, "start": cti.seconds()});
if let Err(e) = app.session.execute("puppet.recordPin", q) {
app.ui.status = e.to_string();
}
}
@@ -1333,8 +1426,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
ov::end_free_transform(&ctx);
}
t if t.puppet_kind().is_some() => {
if pin_hits.iter().all(|h| h.pos.distance(pos) >= 8.0)
&& let Some(l) = pick(app, &ectx, cpt, false).and_then(|l| comp.layer(l))
if let Some((h, _)) = super::puppet_tool::hit(&pin_hits, pos) {
// Click selects the pin; Shift-click adds or removes it.
let _ = app.session.execute("puppet.selectPins", json!({"layer": h.layer.0, "pins": [h.pin], "toggle": mods.shift}));
} else if let Some(l) = pick(app, &ectx, cpt, false).and_then(|l| comp.layer(l))
&& let Some(inv) = l2c(&ectx, l).0.inverse()
{
let lp = inv.apply(gv2(cpt[0], cpt[1]));
+5 -1
View File
@@ -173,7 +173,11 @@ impl Tool {
Tool::Eraser => Icon::Eraser,
Tool::RotoBrush => Icon::RotoBrush,
Tool::RefineEdge => Icon::RefineEdge,
Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Icon::Puppet,
Tool::Puppet => Icon::Puppet,
Tool::PuppetStarch => Icon::PuppetStarch,
Tool::PuppetBend => Icon::PuppetBend,
Tool::PuppetAdvanced => Icon::PuppetAdvanced,
Tool::PuppetOverlap => Icon::PuppetOverlap,
}
}
pub fn from_name(s: &str) -> Option<Tool> {