Merge pull request #1098 from vangel76/feat-gap21-percent-guides-effect-labels
# Conflicts: # crates/engine/src/tests_menu_cmds.rs # docs/effects-parity.md # docs/gaps.md # docs/ui-parity.md
This commit is contained in:
@@ -22,6 +22,7 @@ fn crop(s: &mut Session, cid: effectcraft_project::ItemId, r: [f64; 4], label: &
|
||||
let (w, h) = (effectcraft_project::limits::size_f64(r[2]), effectcraft_project::limits::size_f64(r[3]));
|
||||
s.edit(label, None, |proj, st| {
|
||||
let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?;
|
||||
let (old_w, old_h) = (c.width, c.height);
|
||||
c.width = w;
|
||||
c.height = h;
|
||||
for l in c.layers.iter_mut().filter(|l| l.parent.is_none()) {
|
||||
@@ -30,7 +31,8 @@ fn crop(s: &mut Session, cid: effectcraft_project::ItemId, r: [f64; 4], label: &
|
||||
}
|
||||
}
|
||||
for g in &mut c.guides {
|
||||
g.position -= if g.vertical { x } else { y };
|
||||
let px = g.px(old_w, old_h) - if g.vertical { x } else { y };
|
||||
g.set_px(px, w, h);
|
||||
}
|
||||
st.region_of_interest = None;
|
||||
Ok(())
|
||||
|
||||
@@ -227,6 +227,25 @@ fn toggle(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!(r))
|
||||
}
|
||||
|
||||
/// `effect.setLabel`: the effect's colour label (None clears it).
|
||||
fn set_label(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let c = "effect.setLabel";
|
||||
let (cid, lid, uid) = find_fx(s, p, c)?;
|
||||
let lab = match p.get("label") {
|
||||
Some(Value::Number(n)) => n.as_u64().and_then(|i| effectcraft_color::Label::ALL.get(usize::try_from(i).ok()?).copied()),
|
||||
Some(Value::String(name)) => s.prefs.label_from_name(name),
|
||||
_ => None,
|
||||
}
|
||||
.ok_or_else(|| bad(c, "missing or unknown `label` (name or index 0-16; 0 / None clears it)"))?;
|
||||
s.edit("Effect Label", None, |proj, _| {
|
||||
let l = layer_mut(proj, cid, lid)?;
|
||||
let g = l.props.find_group_mut(uid).ok_or_else(|| bad(c, "gone"))?;
|
||||
g.label = (lab != effectcraft_color::Label::None).then_some(lab);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
fn reorder(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (cid, lid, uid) = find_fx(s, p, "effect.reorder")?;
|
||||
let to = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad("effect.reorder", "missing `index` (1-based)"))? as usize;
|
||||
@@ -482,6 +501,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!("effect.removeAll", "Remove All", ["Effect"], Some("Cmd+Shift+E"), "{layers?}", has_layers, remove_all),
|
||||
cmd!("effect.remove", "Remove Effect", [], None, "{layer?, effect: index|uid|name}", has_layers, remove),
|
||||
cmd!("effect.toggle", "Toggle Effect", [], None, "{layer?, effect, value?}", has_layers, toggle),
|
||||
cmd!("effect.setLabel", "Effect Label", [], None, "{layer?, effect, label: name|index 0-16 (None clears)}", has_layers, set_label),
|
||||
cmd!("effect.reorder", "Reorder Effect", [], None, "{layer?, effect, index}", has_layers, reorder),
|
||||
cmd!("effect.duplicate", "Duplicate Effect", [], None, "{layer?, effect}", has_layers, duplicate),
|
||||
cmd!("effect.copy", "Copy Effects", [], None, "{layer?, effect? | effects?: [uid|name|index]} (default: the selected effects)", has_layers, copy),
|
||||
|
||||
@@ -20,15 +20,58 @@ fn add_guide(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
o => return Err(bad("view.addGuide", format!("orientation must be vertical or horizontal, not `{o}`"))),
|
||||
};
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?;
|
||||
let position = f_p(p, "position").unwrap_or(if vertical { comp.width as f64 / 2.0 } else { comp.height as f64 / 2.0 });
|
||||
let n = s.edit("Add Guide", None, |proj, _| {
|
||||
let percent = p.get("percent").and_then(Value::as_bool).unwrap_or(false);
|
||||
let default = if percent {
|
||||
50.0
|
||||
} else if vertical {
|
||||
comp.width as f64 / 2.0
|
||||
} else {
|
||||
comp.height as f64 / 2.0
|
||||
};
|
||||
let position = f_p(p, "position").filter(|v| v.is_finite()).unwrap_or(default);
|
||||
let n = s.edit(if percent { "Add Percentage Guide" } else { "Add Guide" }, None, |proj, _| {
|
||||
let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?;
|
||||
c.guides.push(Guide { vertical, position });
|
||||
c.guides.push(Guide { vertical, position, percent });
|
||||
Ok(c.guides.len())
|
||||
})?;
|
||||
Ok(json!({"guides": n}))
|
||||
}
|
||||
|
||||
/// View ▸ Convert Guides to Percentage / Pixels: every guide (or just `index`) changes unit
|
||||
/// without moving on the comp.
|
||||
fn convert_guides(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cid = comp_id(s, p)?;
|
||||
let percent = match str_p(p, "to").unwrap_or("percent") {
|
||||
"percent" | "percentage" => true,
|
||||
"pixels" | "px" => false,
|
||||
o => return Err(bad("view.convertGuides", format!("`to` must be percent or pixels, not `{o}`"))),
|
||||
};
|
||||
let only = p.get("index").and_then(Value::as_u64).map(|i| i as usize);
|
||||
let n = s.project.comp(cid).ok_or(EngineError::NoComp)?.guides.len();
|
||||
if only.is_some_and(|i| i >= n) {
|
||||
return Err(bad("view.convertGuides", format!("no guide {} (the comp has {n})", only.unwrap_or(0))));
|
||||
}
|
||||
s.edit(if percent { "Convert Guides to Percentage" } else { "Convert Guides to Pixels" }, None, |proj, _| {
|
||||
let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?;
|
||||
let (w, h) = (c.width, c.height);
|
||||
for (i, g) in c.guides.iter_mut().enumerate() {
|
||||
if only.is_none_or(|o| o == i) {
|
||||
g.set_percent(percent, w, h);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({"guides": n, "percent": percent}))
|
||||
}
|
||||
|
||||
fn add_percent_guide(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut q = p.clone();
|
||||
if let Some(o) = q.as_object_mut() {
|
||||
o.insert("percent".into(), json!(true));
|
||||
}
|
||||
add_guide(s, &q)
|
||||
}
|
||||
|
||||
fn clear_guides(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cid = comp_id(s, p)?;
|
||||
s.edit("Clear Guides", None, |proj, _| {
|
||||
@@ -128,7 +171,25 @@ macro_rules! res {
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!("view.addGuide", "Add Guide...", ["View"], None, "{orientation?: vertical|horizontal, position? (comp px)}", has_comp, add_guide),
|
||||
cmd!(
|
||||
"view.addGuide",
|
||||
"Add Guide...",
|
||||
["View"],
|
||||
None,
|
||||
"{orientation?: vertical|horizontal, position? (comp px, or percent with percent: true), percent?: bool}",
|
||||
has_comp,
|
||||
add_guide
|
||||
),
|
||||
cmd!(
|
||||
"view.addPercentGuide",
|
||||
"Add Percentage Guide...",
|
||||
["View"],
|
||||
None,
|
||||
"{orientation?: vertical|horizontal, position? (percent of the comp)}",
|
||||
has_comp,
|
||||
add_percent_guide
|
||||
),
|
||||
cmd!("view.convertGuides", "Convert Guides to Percentage", ["View"], None, "{comp?, to?: percent|pixels, index?}", has_guides, convert_guides),
|
||||
cmd!("view.clearGuides", "Clear Guides", ["View"], None, "{comp?}", has_guides, clear_guides),
|
||||
cmd!("view.importGuides", "Import Guides...", ["View"], None, "{path}", has_comp, import_guides),
|
||||
cmd!("view.exportGuides", "Export Guides...", ["View"], None, "{path}", has_guides, export_guides),
|
||||
|
||||
@@ -210,8 +210,12 @@ fn guide_index(s: &Session, p: &Value, cmd: &str) -> Result<(effectcraft_project
|
||||
fn move_guide(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (cid, i) = guide_index(s, p, "view.moveGuide")?;
|
||||
let pos = f_p(p, "position").ok_or_else(|| bad("view.moveGuide", "missing `position` (comp px)"))?;
|
||||
let pos = if pos.is_finite() { pos } else { return Err(bad("view.moveGuide", "`position` must be a finite number")) };
|
||||
s.edit("Move Guide", merge_p(p), |proj, _| {
|
||||
proj.comp_mut(cid).ok_or(EngineError::NoComp)?.guides[i].position = pos;
|
||||
let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?;
|
||||
let (w, h) = (c.width, c.height);
|
||||
let g = c.guides.get_mut(i).ok_or(EngineError::NoComp)?;
|
||||
g.set_px(pos, w, h);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!(pos))
|
||||
|
||||
@@ -1117,6 +1117,9 @@ View
|
||||
Snap to Guides | view.snapToGuides
|
||||
Lock Guides | view.lockGuides
|
||||
Add Guide... | view.addGuide
|
||||
Add Percentage Guide... | view.addPercentGuide
|
||||
Convert Guides to Percentage | view.convertGuides {"to":"percent"}
|
||||
Convert Guides to Pixels | view.convertGuides {"to":"pixels"}
|
||||
Clear Guides | view.clearGuides
|
||||
Import Guides... | view.importGuides
|
||||
Export Guides... | view.exportGuides
|
||||
|
||||
@@ -369,3 +369,35 @@ fn ocio_effect_warns_about_unsupported_config_transforms() {
|
||||
set(&mut s, "configFile", json!("/no/such/config.ocio"));
|
||||
assert!(warning(&mut s).as_str().is_some_and(|w| w.starts_with("Can't read the OCIO config")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effect_colour_labels_set_clear_undo_and_round_trip() {
|
||||
let (mut s, a, _) = comp_with_two_solids();
|
||||
let blur = apply(&mut s, a, "Gaussian Blur");
|
||||
let label = |s: &Session| layer(s, a).effects().unwrap().groups().next().unwrap().label;
|
||||
assert_eq!(label(&s), None);
|
||||
s.execute("effect.setLabel", json!({"layer": a, "effect": blur, "label": "Blue"})).unwrap();
|
||||
assert_eq!(label(&s), Some(effectcraft_color::Label::Blue));
|
||||
// Duplicates keep the label.
|
||||
s.execute("effect.duplicate", json!({"layer": a, "effect": blur})).unwrap();
|
||||
assert_eq!(layer(&s, a).effects().unwrap().groups().filter(|g| g.label.is_some()).count(), 2);
|
||||
s.undo();
|
||||
// It saves with the project and survives a reload.
|
||||
let json = s.project.to_file_json().unwrap();
|
||||
let back = effectcraft_project::Project::from_json(&json).unwrap();
|
||||
let l = back.comp(s.active_comp_id().unwrap()).unwrap().layer(LayerId(a)).unwrap().clone();
|
||||
assert_eq!(l.effects().unwrap().groups().next().unwrap().label, Some(effectcraft_color::Label::Blue));
|
||||
// None (or index 0) clears it; the change undoes.
|
||||
s.execute("effect.setLabel", json!({"layer": a, "effect": blur, "label": 0})).unwrap();
|
||||
assert_eq!(label(&s), None);
|
||||
s.undo();
|
||||
assert_eq!(label(&s), Some(effectcraft_color::Label::Blue));
|
||||
assert!(s.execute("effect.setLabel", json!({"layer": a, "effect": blur, "label": "Chartreuse"})).is_err());
|
||||
assert!(s.execute("effect.setLabel", json!({"layer": a, "effect": blur, "label": 99})).is_err());
|
||||
// A project saved before labels existed has none.
|
||||
let old = json.replace("\"label\":\"Blue\"", "\"x\":0").replace("\"label\": \"Blue\"", "\"x\": 0");
|
||||
assert_ne!(old, json, "the replace removed the effect's label");
|
||||
let old = effectcraft_project::Project::from_json(&old).unwrap();
|
||||
let l = old.comp(s.active_comp_id().unwrap()).unwrap().layer(LayerId(a)).unwrap().clone();
|
||||
assert_eq!(l.effects().unwrap().groups().next().unwrap().label, None);
|
||||
}
|
||||
|
||||
@@ -1555,3 +1555,53 @@ fn interpret_footage_loop_extends_video_layers() {
|
||||
assert_eq!(out(&s, before), 6.0, "a layer that ran to the clip's end still does");
|
||||
assert_eq!(s.project.item(effectcraft_project::ItemId(p)).unwrap().duration(), Some(Tick::from_seconds_f64(6.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn percentage_guides_follow_the_comp_size() {
|
||||
let mut s = comp();
|
||||
s.execute("view.addGuide", json!({"orientation": "vertical", "position": 25, "percent": true})).unwrap();
|
||||
s.execute("view.addPercentGuide", json!({"orientation": "horizontal"})).unwrap();
|
||||
s.execute("view.addGuide", json!({"orientation": "vertical", "position": 100})).unwrap();
|
||||
let c = s.active_comp().unwrap().clone();
|
||||
assert!(c.guides[0].percent && c.guides[1].percent && !c.guides[2].percent);
|
||||
assert_eq!((c.guides[0].px(c.width, c.height), c.guides[1].px(c.width, c.height)), (160.0, 180.0));
|
||||
// A comp resize moves percentage guides with it and leaves pixel guides alone.
|
||||
s.execute("comp.settings", json!({"width": 1280, "height": 720})).unwrap();
|
||||
let c = s.active_comp().unwrap().clone();
|
||||
assert_eq!((c.guides[0].px(c.width, c.height), c.guides[1].px(c.width, c.height), c.guides[2].px(c.width, c.height)), (320.0, 360.0, 100.0));
|
||||
// Dragging keeps the unit: the position arrives in comp pixels.
|
||||
s.execute("view.moveGuide", json!({"index": 0, "position": 640})).unwrap();
|
||||
assert_eq!(s.active_comp().unwrap().guides[0].position, 50.0);
|
||||
// Snapping sees the pixel position.
|
||||
let c = s.active_comp().unwrap().clone();
|
||||
assert!(c.guides.iter().any(|g| g.percent && g.vertical && g.px(c.width, c.height) == 640.0));
|
||||
// Converting keeps the guide where it is, and undoes in one step.
|
||||
s.execute("view.convertGuides", json!({"to": "pixels"})).unwrap();
|
||||
let g = s.active_comp().unwrap().guides.clone();
|
||||
assert!(g.iter().all(|g| !g.percent));
|
||||
assert_eq!((g[0].position, g[1].position), (640.0, 360.0));
|
||||
s.execute("view.convertGuides", json!({"to": "percent", "index": 2})).unwrap();
|
||||
assert_eq!(s.active_comp().unwrap().guides[2].position, 100.0 / 1280.0 * 100.0);
|
||||
s.undo();
|
||||
assert!(!s.active_comp().unwrap().guides[2].percent);
|
||||
assert!(s.execute("view.convertGuides", json!({"to": "furlongs"})).is_err());
|
||||
assert!(s.execute("view.convertGuides", json!({"index": 9})).is_err());
|
||||
// A crop keeps percentage guides where they were on the image.
|
||||
s.execute("view.convertGuides", json!({"to": "percent"})).unwrap();
|
||||
s.execute("view.setRegionOfInterest", json!({"rect": [320, 0, 640, 720]})).unwrap();
|
||||
s.execute("comp.cropToRegionOfInterest", json!({})).unwrap();
|
||||
let c = s.active_comp().unwrap();
|
||||
assert!((c.guides[0].px(c.width, c.height) - 320.0).abs() < 1e-9, "640 - 320 crop offset");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_guides_load_as_pixel_guides_and_percent_round_trips() {
|
||||
let old: effectcraft_project::Guide = serde_json::from_str(r#"{"vertical":true,"position":12.5}"#).unwrap();
|
||||
assert!(!old.percent);
|
||||
assert!(!serde_json::to_string(&old).unwrap().contains("percent"), "pixel guides stay in the old shape");
|
||||
let g = effectcraft_project::Guide { vertical: false, position: 10.0, percent: true };
|
||||
let back: effectcraft_project::Guide = serde_json::from_str(&serde_json::to_string(&g).unwrap()).unwrap();
|
||||
assert_eq!(back, g);
|
||||
// A zero-sized comp never divides by zero.
|
||||
assert!(g.px(0, 0).is_finite());
|
||||
}
|
||||
|
||||
@@ -335,11 +335,8 @@ fn targets_in(ctx: &EvalCtx, exclude: &[LayerId], opts: SnapOptions) -> Vec<Snap
|
||||
}
|
||||
if opts.guides {
|
||||
for g in &comp.guides {
|
||||
out.push(if g.vertical {
|
||||
SnapTarget::vline(g.position, PRI_GUIDE, SnapSource::Guide)
|
||||
} else {
|
||||
SnapTarget::hline(g.position, PRI_GUIDE, SnapSource::Guide)
|
||||
});
|
||||
let pos = g.px(comp.width, comp.height);
|
||||
out.push(if g.vertical { SnapTarget::vline(pos, PRI_GUIDE, SnapSource::Guide) } else { SnapTarget::hline(pos, PRI_GUIDE, SnapSource::Guide) });
|
||||
}
|
||||
}
|
||||
if opts.grid && opts.grid_spacing >= 1.0 {
|
||||
|
||||
Reference in New Issue
Block a user