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
+8
View File
@@ -56,6 +56,10 @@ fn select_all(s: &mut Session, _: &Value) -> Result<Value> {
if s.state.text_edit.is_some() {
return s.execute("text.setSelection", json!({"select": "all"}));
}
// A puppet pin selected: every pin of that kind.
if let Some(r) = super::puppet::select_all_of_kind(s) {
return Ok(r);
}
if let Some(c) = s.active_comp() {
s.state.selected_layers = c.layers.iter().map(|l| l.id).collect();
}
@@ -120,6 +124,10 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
if !s.state.selected_vertices.is_empty() && p.get("layers").is_none() {
return s.execute("mask.deleteVertices", json!({}));
}
// Puppet pins selected → delete the pins (not their layer).
if p.get("layers").is_none() && !super::puppet::selected_pins(s).is_empty() {
return s.execute("puppet.removePin", json!({}));
}
// Effects selected → remove them.
if p.get("layers").is_none() && !super::effect::selected_effects(s).is_empty() {
return s.execute("effect.remove", json!({}));
+152 -26
View File
@@ -1,6 +1,9 @@
//! Puppet tools: pins on the layer's Puppet effect (Position, Advanced, Bend, Starch and
//! Overlap pins), mesh options, and a query of the current mesh for overlays and agents.
//!
//! Pins are selected like properties (`puppet.selectPins`; Edit ▸ Clear / Delete removes the
//! selected pins, not their layer).
//!
//! Pin positions are layer space. A new pin is attached to the mesh under it: the click is
//! mapped back through the current deformation to the rest mesh (its hidden rest position), and
//! Position / Advanced pins get a Position keyframe at the current time, as in After Effects.
@@ -9,7 +12,7 @@ use effectcraft_effects::puppet::{self, MeshOpts, PinKind};
use effectcraft_effects::{Buf, Params};
use effectcraft_keyframe::{Keyframe, Value as KV};
use effectcraft_project::build::Ids;
use effectcraft_project::{ItemId, LayerId, Node, PropGroup, Uid};
use effectcraft_project::{ItemId, Layer, LayerId, Node, PropGroup, Uid};
use effectcraft_render::{EvalCtx, RenderOpts, Renderer};
use effectcraft_time::Tick;
use serde_json::{Value, json};
@@ -162,25 +165,102 @@ fn add_pin(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"effect": fx, "mesh": mesh, "pin": pin, "rest": [target.map(|x| x.1).unwrap_or(at)[0], target.map(|x| x.1).unwrap_or(at)[1]]}))
}
/// The pin groups of a layer's Puppet effects, in stack order.
pub(crate) fn layer_pins(l: &Layer) -> impl Iterator<Item = &PropGroup> {
l.effects().into_iter().flat_map(|fx| fx.groups().filter(|g| puppet::is_puppet(g))).flat_map(puppet::meshes).flat_map(puppet::pins)
}
/// Selected puppet pins of the active comp: (layer, pin uid).
pub(crate) fn selected_pins(s: &Session) -> Vec<(LayerId, Uid)> {
let Some(c) = s.active_comp() else { return vec![] };
s.state.selected_props.iter().filter(|(l, u)| c.layer(*l).is_some_and(|l| layer_pins(l).any(|g| g.uid == *u))).copied().collect()
}
/// Edit ▸ Select All with puppet pins selected: every pin of the selected pins' kinds on their
/// layers. `None` when no pin is selected (Select All then selects layers).
pub(crate) fn select_all_of_kind(s: &mut Session) -> Option<Value> {
let sel = selected_pins(s);
let comp = s.active_comp()?;
let kind = |g: &PropGroup| PinKind::from_index(g.get("kind").map(|k| k.value.as_enum()).unwrap_or(0));
let mut kinds = vec![];
for (l, u) in &sel {
if let Some(g) = comp.layer(*l).and_then(|l| l.props.find_group(*u)) {
kinds.push((*l, kind(g)));
}
}
if kinds.is_empty() {
return None;
}
let all: Vec<(LayerId, Uid)> =
comp.layers.iter().flat_map(|l| layer_pins(l).filter(|g| kinds.contains(&(l.id, kind(g)))).map(|g| (l.id, g.uid)).collect::<Vec<_>>()).collect();
s.state.selected_props = all.clone();
Some(json!({ "pins": all.iter().map(|(_, u)| *u).collect::<Vec<_>>() }))
}
/// Find a pin group (uid or name) in the layer's Puppet effects.
fn pin_uid(s: &Session, cid: ItemId, lid: LayerId, key: &Value, cmd: &str) -> Result<Uid> {
let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?;
let fx = l.effects().ok_or_else(|| bad(cmd, "no effects"))?;
for g in fx.groups().filter(|g| puppet::is_puppet(g)) {
for m in puppet::meshes(g) {
for pin in puppet::pins(m) {
let hit = match key {
Value::Number(n) => n.as_u64() == Some(pin.uid),
Value::String(name) => &pin.name == name,
_ => false,
};
if hit {
return Ok(pin.uid);
}
let hit = |pin: &&PropGroup| match key {
Value::Number(n) => n.as_u64() == Some(pin.uid),
Value::String(name) => &pin.name == name,
_ => false,
};
layer_pins(l).find(hit).map(|g| g.uid).ok_or_else(|| bad(cmd, format!("no puppet pin {key}")))
}
/// Pins named by `pin` or `pins` (uids or names) on `layer`, else the selected pins (of `layer`
/// when given): (comp, layer, pin uid).
fn pins_p(s: &Session, p: &Value, cmd: &str) -> Result<Vec<(ItemId, LayerId, Uid)>> {
let keys: Vec<Value> = match (p.get("pin"), p.get("pins")) {
(Some(k), _) => vec![k.clone()],
(None, Some(Value::Array(a))) => a.clone(),
(None, Some(_)) => return Err(bad(cmd, "`pins` must be an array of pin uids or names")),
(None, None) => vec![],
};
if !keys.is_empty() {
let (cid, lid) = layer_p(s, p, cmd)?;
return keys.iter().map(|k| pin_uid(s, cid, lid, k, cmd).map(|u| (cid, lid, u))).collect();
}
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;
let only = match p.get("layer") {
Some(_) => Some(layer_p(s, p, cmd)?.1),
None => None,
};
let sel: Vec<_> = selected_pins(s).into_iter().filter(|(l, _)| only.is_none_or(|o| o == *l)).map(|(l, u)| (cid, l, u)).collect();
if sel.is_empty() {
return Err(bad(cmd, "missing `pin` / `pins` (uids or names), and no puppet pins are selected"));
}
Ok(sel)
}
/// Select pins (the viewer's click / Shift-click on a pin). Plain: only these pins; `add`: add
/// them; `toggle`: flip each. An empty `pins` deselects every pin.
fn select_pins(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "puppet.selectPins";
let want = match p.get("pins") {
Some(Value::Array(a)) if a.is_empty() => vec![],
_ => pins_p(s, p, cmd)?,
};
let (add, toggle) = (super::b_p(p, "add").unwrap_or(false), super::b_p(p, "toggle").unwrap_or(false));
let st = &mut s.state;
if !add && !toggle {
st.selected_props.clear();
st.selected_keys.clear();
}
for (_, l, u) in want {
let at = st.selected_props.iter().position(|x| *x == (l, u));
match at {
Some(i) if toggle => {
st.selected_props.remove(i);
}
Some(_) => {}
None => st.selected_props.push((l, u)),
}
if !st.selected_layers.contains(&l) {
st.selected_layers.push(l);
}
}
Err(bad(cmd, format!("no puppet pin {key}")))
Ok(json!({ "pins": selected_pins(s).iter().map(|(_, u)| *u).collect::<Vec<_>>() }))
}
fn with_pin<T>(s: &mut Session, p: &Value, cmd: &str, label: &str, f: impl FnOnce(&mut PropGroup, Tick) -> Result<T>) -> Result<T> {
@@ -237,19 +317,19 @@ fn set_pin(s: &mut Session, p: &Value) -> Result<Value> {
}
fn remove_pin(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "puppet.removePin";
let (cid, lid) = layer_p(s, p, cmd)?;
let key = p.get("pin").cloned().ok_or_else(|| bad(cmd, "missing `pin` (uid or name)"))?;
let uid = pin_uid(s, cid, lid, &key, cmd)?;
s.edit("Delete Puppet Pin", None, |proj, st| {
let l = layer_mut(proj, cid, lid)?;
if let Some(parent) = l.props.parent_of_mut(uid) {
parent.children.retain(|c| c.uid() != uid);
let pins = pins_p(s, p, "puppet.removePin")?;
let label = if pins.len() == 1 { "Delete Puppet Pin" } else { "Delete Puppet Pins" };
s.edit(label, None, |proj, st| {
for (cid, lid, uid) in &pins {
let l = layer_mut(proj, *cid, *lid)?;
if let Some(parent) = l.props.parent_of_mut(*uid) {
parent.children.retain(|c| c.uid() != *uid);
}
st.selected_props.retain(|(_, u)| u != uid);
}
st.selected_props.retain(|(_, u)| *u != uid);
Ok(())
})?;
Ok(Value::Null)
Ok(json!({ "removed": pins.len() }))
}
fn mesh_opts(s: &mut Session, p: &Value) -> Result<Value> {
@@ -341,6 +421,43 @@ fn record_pin(s: &mut Session, p: &Value) -> Result<Value> {
let key = p.get("pin").cloned().ok_or_else(|| bad(cmd, "missing `pin` (uid or name)"))?;
let uid = pin_uid(s, cid, lid, &key, cmd)?;
let rec = samples(p, cmd)?;
// Several selected pins dragged together (`pins`): each follows the drag's displacement from
// where it is when recording begins; one undo step.
if let Some(Value::Array(others)) = p.get("pins")
&& !others.is_empty()
{
let start = match p.get("start").and_then(Value::as_f64) {
Some(t) => Tick::from_seconds_f64(t),
None => s.time_of(cid),
};
let lt = layer_time(s, cid, lid, start)?;
let mut pins = vec![(uid, rec[0].1)];
for k in others {
let u = pin_uid(s, cid, lid, k, cmd)?;
if pins.iter().any(|(x, _)| *x == u) {
continue;
}
let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?;
let at = l.props.find_group(u).and_then(|g| g.get("position")).map(|pr| pr.value_at(lt).as_vec2());
pins.push((u, at.ok_or_else(|| bad(cmd, "only Position and Advanced pins record motion"))?));
}
let o = rec[0].1;
return super::app_more::grouped(s, "Record Puppet Pins", |s| {
let mut last = Value::Null;
for (u, base) in &pins {
let moved: Vec<Value> = rec.iter().map(|(t, q)| json!([t, base[0] + q[0] - o[0], base[1] + q[1] - o[1]])).collect();
let mut one = p.clone();
if let Some(m) = one.as_object_mut() {
m.remove("pins");
m.insert("pin".into(), json!(u));
m.insert("samples".into(), Value::Array(moved));
}
last = s.execute(cmd, one)?;
}
last["pins"] = json!(pins.iter().map(|(u, _)| *u).collect::<Vec<_>>());
Ok(last)
});
}
let opts = &s.state.puppet;
let speed = f_p(p, "speed").unwrap_or(opts.record_speed).clamp(1.0, 10_000.0) / 100.0;
// Smoothing 10 (the default) keeps the path within 1 layer pixel of the drag.
@@ -469,7 +586,16 @@ pub fn specs() -> Vec<CommandSpec> {
has_comp,
set_pin
),
cmd!("puppet.removePin", "Delete Puppet Pin", [], None, "{layer?, pin: uid | name}", has_comp, remove_pin),
cmd!(
"puppet.removePin",
"Delete Puppet Pin",
[],
None,
"{layer?, pin?: uid | name, pins?: [uid | name…]} (default: the selected pins)",
has_comp,
remove_pin
),
cmd!("puppet.selectPins", "Select Puppet Pins", [], None, "{layer?, pins: [uid | name…] ([] deselects), add?, toggle?}", has_comp, select_pins),
cmd!(
"puppet.mesh",
"Puppet Mesh Options",
@@ -485,7 +611,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Record Puppet Pin",
[],
None,
"{layer?, pin: uid | name, samples: [[t, x, y]…] (t = seconds since the drag began, layer space), start? (s, default current time), speed? (%, Record Options), smoothing? (Record Options)}",
"{layer?, pin: uid | name, samples: [[t, x, y]…] (t = seconds since the drag began, layer space), pins?: [uid | name…] (more pins that move by the same displacement), start? (s, default current time), speed? (%, Record Options), smoothing? (Record Options)}",
has_comp,
record_pin
),
+53
View File
@@ -216,6 +216,59 @@ fn puppet_pin_kinds_and_mesh_options() {
assert_eq!(pin_count(&s, id), 4);
}
#[test]
fn puppet_pins_select_and_delete_without_touching_the_layer() {
let (mut s, id) = setup();
let a = s.execute("puppet.addPin", json!({"layer": id, "position": [10, 30]})).unwrap()["pin"].clone();
let b = s.execute("puppet.addPin", json!({"layer": id, "position": [90, 30]})).unwrap()["pin"].clone();
s.execute("puppet.addPin", json!({"layer": id, "position": [50, 30]})).unwrap();
// A new pin is selected: Delete removes it, and the layer stays.
assert_eq!(s.execute("edit.clear", json!({})).unwrap(), json!({"removed": 1}));
assert_eq!(pin_count(&s, id), 2);
assert!(s.active_comp().unwrap().layer(effectcraft_project::LayerId(id)).is_some());
// Click, Shift-click (toggle) and add by name.
let sel = |s: &mut Session, q: Value| s.execute("puppet.selectPins", q).unwrap()["pins"].clone();
assert_eq!(sel(&mut s, json!({"layer": id, "pins": [a, b]})), json!([a, b]));
assert_eq!(sel(&mut s, json!({"layer": id, "pins": [b], "toggle": true})), json!([a]));
assert_eq!(sel(&mut s, json!({"layer": id, "pins": ["Puppet Pin 2"], "add": true})), json!([a, b]));
assert!(s.execute("puppet.selectPins", json!({"layer": id, "pins": ["Puppet Pin 9"]})).is_err());
// Deleting both is one undo step.
assert_eq!(s.execute("edit.clear", json!({})).unwrap(), json!({"removed": 2}));
assert_eq!(pin_count(&s, id), 0);
s.undo();
assert_eq!(pin_count(&s, id), 2);
// Nothing selected: removing needs a pin.
assert_eq!(sel(&mut s, json!({"layer": id, "pins": []})), json!([]));
assert!(s.execute("puppet.removePin", json!({"layer": id})).is_err());
}
#[test]
fn puppet_select_all_takes_the_pin_kind_and_several_pins_record_together() {
let (mut s, id) = setup();
let a = s.execute("puppet.addPin", json!({"layer": id, "position": [10, 30]})).unwrap()["pin"].clone();
let b = s.execute("puppet.addPin", json!({"layer": id, "position": [90, 30]})).unwrap()["pin"].clone();
let st = s.execute("puppet.addPin", json!({"layer": id, "kind": "starch", "position": [50, 30]})).unwrap()["pin"].clone();
// Ctrl+A with a Position pin selected: every Position pin, not the Starch pin.
s.execute("puppet.selectPins", json!({"layer": id, "pins": [a]})).unwrap();
assert_eq!(s.execute("edit.selectAll", json!({})).unwrap()["pins"], json!([a, b]));
s.execute("puppet.selectPins", json!({"layer": id, "pins": [st]})).unwrap();
assert_eq!(s.execute("edit.selectAll", json!({})).unwrap()["pins"], json!([st]));
// Without pins selected, Select All selects layers as before.
s.execute("puppet.selectPins", json!({"layer": id, "pins": []})).unwrap();
assert_eq!(s.execute("edit.selectAll", json!({})).unwrap(), json!(1));
// Recording a drag of pin A with B selected too: B moves by the same displacement.
let steps = s.history.undo.len();
let samples: Vec<Value> = [0.0, 0.25, 0.5].iter().map(|t| json!([t, 10.0, 30.0 + 40.0 * t])).collect();
let r = s.execute("puppet.recordPin", json!({"layer": id, "pin": a, "pins": [b], "samples": samples, "smoothing": 0})).unwrap();
assert_eq!(r["pins"], json!([a, b]));
assert_eq!(s.history.undo.len(), steps + 1, "one undo step");
let last = |s: &Session, pin: &Value| pin_keys(s, id, pin.as_u64().unwrap()).last().unwrap().1;
assert_eq!(last(&s, &a), [10.0, 50.0]);
assert_eq!(last(&s, &b), [90.0, 50.0]);
// Starch pins don't record.
assert!(s.execute("puppet.recordPin", json!({"layer": id, "pin": a, "pins": [st], "samples": samples})).is_err());
}
#[test]
fn project_with_paint_and_puppet_round_trips() {
let (mut s, id) = setup();