FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Centre Transform's auto points in the clip's picture on apply and reset (#2588)
MarekSpodBiedryc7667aaa60fd
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rsindex 940c00af5..5a8ee62a4 100644--- a/crates/engine/src/clip_ops_tests.rs+++ b/crates/engine/src/clip_ops_tests.rs@@ -1082,3 +1082,30 @@ fn toggle_animation_creates_a_parameter_missing_from_an_older_instance() {let rot = it.effect("motion").unwrap().param("rotation").unwrap();assert!(rot.is_animated(), "the stopwatch is on");}++/// Transform's Anchor Point and Position are centred in the clip's own picture (on apply and on reset), so Scale grows it+/// from its centre (they used to be centred in the sequence frame, off to the side of a smaller picture).+#[test]+fn applied_transform_centres_its_points_in_the_clip_picture() {+ let mut s = demo();+ let r = s.execute("file.newColorMatte", json!({"width": 960, "height": 1080, "color": "#ff0000"})).unwrap();+ let matte = r["item"].as_u64().unwrap();+ let r = s.execute("timeline.place", json!({"item": matte, "track": "V3", "seconds": 0.0})).unwrap();+ let c = r["clips"][0].as_u64().unwrap();+ s.execute("effects.apply", json!({"clips": [c], "effect": "transform"})).unwrap();+ let centre = filmcraft_geom::Vec2::new(480.0, 540.0);+ let point = |s: &Session, k: &str| clip(s, c).effect("transform").unwrap().param(k).unwrap().value.as_vec2().unwrap();+ assert_eq!(point(&s, "anchor"), centre);+ assert_eq!(point(&s, "position"), centre);+ // Reset Effect and Reset Parameter centre them the same way+ for k in ["anchor", "position"] {+ s.execute("effects.setParam", json!({"clip": c, "effect": "transform", "param": k, "value": [10.0, 20.0]})).unwrap();+ }+ let idx = clip(&s, c).effects.iter().position(|e| e.effect == "transform").unwrap();+ s.execute("effects.reset", json!({"clip": c, "index": idx})).unwrap();+ assert_eq!(point(&s, "anchor"), centre);+ assert_eq!(point(&s, "position"), centre);+ s.execute("effects.setParam", json!({"clip": c, "effect": "transform", "param": "anchor", "value": [10.0, 20.0]})).unwrap();+ s.execute("effects.resetParam", json!({"clip": c, "effect": "transform", "param": "anchor"})).unwrap();+ assert_eq!(point(&s, "anchor"), centre);+}diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 162fb18ca..a037b4733 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -2955,6 +2955,12 @@ fn build() -> Vec<CommandSpec> {}let (fw, fh) = s.active_sequence().map(|q| (q.settings.width, q.settings.height)).unwrap_or((1920, 1080));let label = format!("Apply {}", def.name);+ // standard effects run on the clip's source picture, so "auto" points (the centre) are+ // measured in it, not in the sequence frame+ let source_sizes: std::collections::BTreeMap<ClipId, (u32, u32)> = s+ .active_sequence()+ .map(|q| clips.iter().filter_map(|c| q.find_item(*c).and_then(|(_, i)| s.project.source_size(i.item)).map(|sz| (*c, sz))).collect())+ .unwrap_or_default();s.edit_sequence(&label, |q, _, _| {let mut n = 0;for t in q.all_tracks_mut() {@@ -2963,7 +2969,8 @@ fn build() -> Vec<CommandSpec> {}for i in t.items.iter_mut().filter(|i| clips.contains(&i.id)) {let mut inst = def.instance();- resolve_auto_points(&mut inst, (fw, fh), (fw, fh));+ let size = source_sizes.get(&i.id).copied().unwrap_or((fw, fh));+ resolve_auto_points(&mut inst, size, size);// standard effects go before the intrinsic ones (render order)let pos = i.effects.iter().position(|e| e.def().is_some_and(|d| d.intrinsic)).unwrap_or(i.effects.len());let pos = if is_audio { i.effects.len() } else { pos };@@ -3150,12 +3157,13 @@ fn build() -> Vec<CommandSpec> {let c = clip_p(p, "clip").ok_or_else(|| bad("effects.reset", "need `clip`"))?;let idx = u64_p(p, "index").unwrap_or(0) as usize;let frame = s.active_sequence().map(|q| (q.settings.width, q.settings.height)).unwrap_or((1920, 1080));+ let source = s.active_sequence().and_then(|q| q.find_item(c)).and_then(|(_, i)| s.project.source_size(i.item)).unwrap_or(frame);let ph = s.playhead();s.edit_sequence("Reset Effect", |q, _, _| {let (_, it) = q.find_item_mut(c).ok_or(filmcraft_edit::EditError::NoItem(c))?;let mt = it.source_time_at(ph.clamp(it.start, (it.end() - Tick(1)).max(it.start)));if let Some(e) = it.effects.get_mut(idx) {- reset_params(e, None, frame, mt);+ reset_params(e, None, frame, source, mt);}Ok(())})?;@@ -3166,6 +3174,7 @@ fn build() -> Vec<CommandSpec> {let pid = str_p(p, "param").ok_or_else(|| bad("effects.resetParam", "need `param`"))?.to_string();let eff = p.get("effect").cloned().unwrap_or(json!("motion"));let frame = s.active_sequence().map(|q| (q.settings.width, q.settings.height)).unwrap_or((1920, 1080));+ let source = s.active_sequence().and_then(|q| q.find_item(c)).and_then(|(_, i)| s.project.source_size(i.item)).unwrap_or(frame);let ph = s.playhead();s.edit_sequence("Reset Parameter", |q, _, _| {let (_, it) = q.find_item_mut(c).ok_or(filmcraft_edit::EditError::NoItem(c))?;@@ -3176,7 +3185,7 @@ fn build() -> Vec<CommandSpec> {_ => None,}.ok_or_else(|| bad("effects.resetParam", "no such effect on clip"))?;- if !reset_params(e, Some(&pid), frame, mt) {+ if !reset_params(e, Some(&pid), frame, source, mt) {return Err(bad("effects.resetParam", format!("no param `{pid}`")));}Ok(())@@ -3455,11 +3464,12 @@ fn build() -> Vec<CommandSpec> {/// Put `e`'s parameters (all of them, or only `only`) back to their defaults, as Premiere's reset/// buttons do: a parameter that is not animated gets its default as its static value, and an/// animated one keeps its keyframes and gets the default as a keyframe at media time `mt`.-/// Masks are left alone. Returns whether any parameter was reset.-fn reset_params(e: &mut filmcraft_project::EffectInstance, only: Option<&str>, frame: (u32, u32), mt: Tick) -> bool {+/// Masks are left alone. Returns whether any parameter was reset. "Auto" points are centred as+/// when the effect was added: a standard effect's in the clip's `source` picture it runs on.+fn reset_params(e: &mut filmcraft_project::EffectInstance, only: Option<&str>, frame: (u32, u32), source: (u32, u32), mt: Tick) -> bool {let Some(d) = e.def() else { return false };let mut fresh = d.instance();- resolve_auto_points(&mut fresh, frame, frame);+ resolve_auto_points(&mut fresh, if d.intrinsic { frame } else { source }, source);let mut any = false;for (id, prm) in fresh.params {if only.is_some_and(|o| o != id) {