Selecting a property group also selects its layer, and edit.clear only looked for selected keys, mask vertices, puppet pins and effects before deleting the selected layers, so Delete with a text animator, mask or shape group selected deleted the whole layer. edit.clear now removes the selected removable groups (text animators, masks, shape items, trackers) of the active comp in one undo step before falling back to layers. It shares the removal with prop.removeGroup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
6.0 KiB
Rust
129 lines
6.0 KiB
Rust
//! Generic edits of property-tree groups by uid: rename, enable, remove, duplicate and reorder an
|
|
//! effect, mask, shape item or text animator (what scripting's `PropertyGroup.name`, `enabled`,
|
|
//! `remove()`, `duplicate()` and `moveTo()` do, and what agents need without a UI selection).
|
|
|
|
use effectcraft_project::{GroupKind, ItemId, LayerId, Node, PropGroup, Uid};
|
|
use serde_json::{Value, json};
|
|
|
|
use super::{CommandSpec, b_p, bad, has_layers, layer_mut, layer_p, str_p};
|
|
use crate::{EngineError, Result, Session, cmd};
|
|
|
|
/// Groups that can be renamed, removed, duplicated and reordered: instances in indexed lists.
|
|
fn is_instance(g: &PropGroup) -> bool {
|
|
matches!(g.kind, GroupKind::Indexed | GroupKind::Effect { .. } | GroupKind::Mask { .. } | GroupKind::Tracker { .. })
|
|
}
|
|
|
|
/// `{layer, prop: uid}` → (comp, layer, group uid), checking that the group is an instance.
|
|
fn group_ref(s: &Session, p: &Value, cmd: &str, need_instance: bool) -> Result<(ItemId, LayerId, Uid)> {
|
|
let (cid, lid) = layer_p(s, p, cmd)?;
|
|
let uid = p.get("prop").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "missing `prop` (group uid)"))?;
|
|
let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?;
|
|
let g = l.props.find_group(uid).ok_or_else(|| bad(cmd, format!("no property group @{uid}")))?;
|
|
if need_instance && !is_instance(g) {
|
|
return Err(bad(cmd, format!("`{}` is a fixed group (only effects, masks, shape items and animators)", g.name)));
|
|
}
|
|
Ok((cid, lid, uid))
|
|
}
|
|
|
|
fn rename(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let c = "prop.renameGroup";
|
|
let (cid, lid, uid) = group_ref(s, p, c, true)?;
|
|
let name = str_p(p, "name").ok_or_else(|| bad(c, "missing `name`"))?.to_string();
|
|
if name.trim().is_empty() {
|
|
return Err(bad(c, "the name can't be empty"));
|
|
}
|
|
s.edit("Rename", None, |proj, _| {
|
|
let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
g.name = name.clone();
|
|
Ok(json!(name))
|
|
})
|
|
}
|
|
|
|
fn set_enabled(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let c = "prop.setGroupEnabled";
|
|
let (cid, lid, uid) = group_ref(s, p, c, true)?;
|
|
let v = b_p(p, "value");
|
|
s.edit("Toggle", None, |proj, _| {
|
|
let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
g.enabled = v.unwrap_or(!g.enabled);
|
|
Ok(json!(g.enabled))
|
|
})
|
|
}
|
|
|
|
fn remove(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let (cid, lid, uid) = group_ref(s, p, "prop.removeGroup", true)?;
|
|
remove_groups(s, cid, &[(lid, uid)])?;
|
|
Ok(Value::Null)
|
|
}
|
|
|
|
/// Remove groups of comp `cid` in one undo step.
|
|
fn remove_groups(s: &mut Session, cid: ItemId, groups: &[(LayerId, Uid)]) -> Result<()> {
|
|
s.edit("Delete", None, |proj, st| {
|
|
for (lid, uid) in groups {
|
|
// A group inside one removed before it went with it.
|
|
if let Some(parent) = layer_mut(proj, cid, *lid)?.props.parent_of_mut(*uid) {
|
|
parent.children.retain(|n| n.uid() != *uid);
|
|
}
|
|
}
|
|
st.selected_props.retain(|(_, u)| !groups.iter().any(|(_, g)| g == u));
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
/// Edit ▸ Clear with masks, shape items, text animators or trackers selected in the active comp:
|
|
/// removes those groups (not their layers) in one undo step. None when none are selected.
|
|
pub(crate) fn remove_selected(s: &mut Session) -> Result<Option<Value>> {
|
|
let (Some(cid), Some(c)) = (s.active_comp_id(), s.active_comp()) else { return Ok(None) };
|
|
let groups: Vec<(LayerId, Uid)> =
|
|
s.state.selected_props.iter().filter(|(lid, uid)| c.layer(*lid).and_then(|l| l.props.find_group(*uid)).is_some_and(is_instance)).copied().collect();
|
|
if groups.is_empty() {
|
|
return Ok(None);
|
|
}
|
|
remove_groups(s, cid, &groups)?;
|
|
Ok(Some(json!(groups.len())))
|
|
}
|
|
|
|
fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let c = "prop.duplicateGroup";
|
|
let (cid, lid, uid) = group_ref(s, p, c, true)?;
|
|
s.edit("Duplicate", None, |proj, _| {
|
|
let mut next = proj.next_id;
|
|
let l = layer_mut(proj, cid, lid)?;
|
|
let parent = l.props.parent_of_mut(uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
let i = parent.children.iter().position(|n| n.uid() == uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
let Node::Group(mut g) = parent.children[i].clone() else { return Err(bad(c, "not a group")) };
|
|
g.reassign_uids(&mut next);
|
|
let new = g.uid;
|
|
parent.children.insert(i + 1, Node::Group(g));
|
|
proj.next_id = next + 1;
|
|
Ok(json!({"prop": new}))
|
|
})
|
|
}
|
|
|
|
fn move_to(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let c = "prop.moveGroup";
|
|
let (cid, lid, uid) = group_ref(s, p, c, true)?;
|
|
let to = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad(c, "missing `index` (1-based)"))? as usize;
|
|
s.edit("Reorder", None, |proj, _| {
|
|
let l = layer_mut(proj, cid, lid)?;
|
|
let parent = l.props.parent_of_mut(uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
let i = parent.children.iter().position(|n| n.uid() == uid).ok_or_else(|| bad(c, "group vanished"))?;
|
|
if to < 1 || to > parent.children.len() {
|
|
return Err(bad(c, format!("index {to} is out of range 1..{}", parent.children.len())));
|
|
}
|
|
let n = parent.children.remove(i);
|
|
parent.children.insert(to - 1, n);
|
|
Ok(json!(to))
|
|
})
|
|
}
|
|
|
|
pub fn specs() -> Vec<CommandSpec> {
|
|
vec![
|
|
cmd!("prop.renameGroup", "Rename Property Group", [], None, "{layer?, prop: uid, name}", has_layers, rename),
|
|
cmd!("prop.setGroupEnabled", "Enable Property Group", [], None, "{layer?, prop: uid, value?}", has_layers, set_enabled),
|
|
cmd!("prop.removeGroup", "Delete Property Group", [], None, "{layer?, prop: uid}", has_layers, remove),
|
|
cmd!("prop.duplicateGroup", "Duplicate Property Group", [], None, "{layer?, prop: uid} → {prop: new uid}", has_layers, duplicate),
|
|
cmd!("prop.moveGroup", "Reorder Property Group", [], None, "{layer?, prop: uid, index (1-based among its siblings)}", has_layers, move_to),
|
|
]
|
|
}
|