FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#284: reset buttons keep keyframes; a reset button on every property
vangel7610ffc016b897
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rsindex b6fcfe86d..8e90933ab 100644--- a/crates/engine/src/clip_ops_tests.rs+++ b/crates/engine/src/clip_ops_tests.rs@@ -923,3 +923,37 @@ fn linked_slip_keeps_picture_and_sound_aligned_at_unequal_media_limits() {s.execute("timeline.slip", json!({"clip": v, "deltaFrames": -10})).unwrap();assert_eq!((clip(&s, v).source_in, clip(&s, a).source_in), (Tick(fr.0 * 10), Tick(fr.0 * 10)));}++#[test]+fn reset_keeps_keyframes_and_resets_one_parameter() {+ // #284: Reset Effect used to replace the effect with a fresh one, deleting every keyframe+ let mut s = demo();+ let a = v1(&s)[0].clone();+ let sec = Tick(TICKS_PER_SECOND);+ let (t0, t1, mid) = (a.start, a.start + sec, a.start + Tick(TICKS_PER_SECOND / 2));+ for (t, v) in [(t0, 150.0), (t1, 50.0)] {+ s.execute("effects.setParam", json!({"clip": a.id.0, "effect": "motion", "param": "scale", "value": v, "keyframe": true, "time": t.0})).unwrap();+ }+ s.execute("effects.setParam", json!({"clip": a.id.0, "effect": "motion", "param": "rotation", "value": 30.0})).unwrap();+ let idx = clip(&s, a.id.0).effects.iter().position(|e| e.effect == "motion").unwrap();+ s.execute("playhead.set", json!({"time": mid.0})).unwrap();+ s.execute("effects.reset", json!({"clip": a.id.0, "index": idx})).unwrap();+ let it = clip(&s, a.id.0);+ let scale = it.effect("motion").unwrap().param("scale").unwrap();+ // (the playhead snaps to a frame)+ let mid_media = it.source_time_at(s.playhead());+ let kf: Vec<(Tick, f64)> = scale.keyframes.iter().map(|k| (k.time, k.value.as_f64().unwrap())).collect();+ assert_eq!(kf, vec![(it.source_time_at(t0), 150.0), (mid_media, 100.0), (it.source_time_at(t1), 50.0)], "keyframes kept, default at the playhead");+ assert_eq!(f64_param(&it, "motion", "rotation"), 0.0);+ // one parameter: only it goes back to its default+ s.execute("effects.setParam", json!({"clip": a.id.0, "effect": "motion", "param": "rotation", "value": 45.0})).unwrap();+ s.execute("effects.setParam", json!({"clip": a.id.0, "effect": "opacity", "param": "opacity", "value": 40.0})).unwrap();+ s.execute("effects.resetParam", json!({"clip": a.id.0, "effect": "motion", "param": "rotation"})).unwrap();+ let it = clip(&s, a.id.0);+ assert_eq!(f64_param(&it, "motion", "rotation"), 0.0);+ assert_eq!(f64_param(&it, "opacity", "opacity"), 40.0);+ assert_eq!(it.effect("motion").unwrap().param("scale").unwrap().keyframes.len(), 3);+ assert!(s.execute("effects.resetParam", json!({"clip": a.id.0, "effect": "motion", "param": "nope"})).is_err());+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(f64_param(&clip(&s, a.id.0), "motion", "rotation"), 45.0);+}diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 0b3a9f8a6..928c25d18 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -2619,15 +2619,35 @@ fn build() -> Vec<CommandSpec> {cmd!("effects.reset", "Reset Effect", [], None, r#"{"clip":id,"index":n}"#, has_seq, |s, p| {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 (fw, fh) = s.active_sequence().map(|q| (q.settings.width, q.settings.height)).unwrap_or((1920, 1080));+ let frame = s.active_sequence().map(|q| (q.settings.width, q.settings.height)).unwrap_or((1920, 1080));+ let ph = s.playhead();s.edit_sequence("Reset Effect", |q, _, _| {let (_, it) = q.find_item_mut(c).ok_or(filmcraft_edit::EditError::NoItem(c))?;- if let Some(e) = it.effects.get_mut(idx)- && let Some(d) = e.def()- {- let mut fresh = d.instance();- resolve_auto_points(&mut fresh, (fw, fh), (fw, fh));- *e = fresh;+ 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);+ }+ Ok(())+ })?;+ Ok(Value::Null)+ }),+ cmd!("effects.resetParam", "Reset Parameter", [], None, r#"{"clip":id,"effect":str|index,"param":str}"#, has_seq, |s, p| {+ let c = clip_p(p, "clip").ok_or_else(|| bad("effects.resetParam", "need `clip`"))?;+ 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 ph = s.playhead();+ s.edit_sequence("Reset Parameter", |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)));+ let e = match &eff {+ Value::Number(n) => it.effects.get_mut(n.as_u64().unwrap_or(0) as usize),+ Value::String(sid) => it.effects.iter_mut().find(|e| &e.effect == sid),+ _ => None,+ }+ .ok_or_else(|| bad("effects.resetParam", "no such effect on clip"))?;+ if !reset_params(e, Some(&pid), frame, mt) {+ return Err(bad("effects.resetParam", format!("no param `{pid}`")));}Ok(())})?;@@ -2843,6 +2863,30 @@ fn build() -> Vec<CommandSpec> {v}+/// 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 {+ let Some(d) = e.def() else { return false };+ let mut fresh = d.instance();+ resolve_auto_points(&mut fresh, frame, frame);+ let mut any = false;+ for (id, prm) in fresh.params {+ if only.is_some_and(|o| o != id) {+ continue;+ }+ any = true;+ match e.params.get_mut(&id) {+ Some(old) if old.is_animated() => old.set_at(mt, prm.value),+ _ => {+ e.params.insert(id, prm);+ }+ }+ }+ any+}+fn keyframe_op(s: &mut Session, p: &Value, op: &str) -> Result<Value> {let c = clip_p(p, "clip").ok_or_else(|| bad("keyframe", "need `clip`"))?;let pid = str_p(p, "param").ok_or_else(|| bad("keyframe", "need `param`"))?.to_string();diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex 08f899ecb..980ef5374 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -968,6 +968,7 @@Reset… Restablecer…Source · {name} Origen · {name}Reset Effect Restablecer efecto+ Reset Parameter Restablecer parámetroSave Preset… Guardar ajuste preestablecido…Custom Setup Configuración personalizadaEdit… Editar…diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex 8dbf44199..f9ec82d09 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -970,6 +970,7 @@Reset… リセット…Source · {name} ソース · {name}Reset Effect エフェクトをリセット+ Reset Parameter パラメーターをリセットSave Preset… プリセットを保存…Custom Setup カスタム設定Edit… 編集…diff --git a/crates/ui-egui/src/i18n/zh-cn.tsv b/crates/ui-egui/src/i18n/zh-cn.tsvindex 731067f98..9e46602c2 100644--- a/crates/ui-egui/src/i18n/zh-cn.tsv+++ b/crates/ui-egui/src/i18n/zh-cn.tsv@@ -963,6 +963,7 @@Reset… 重置…Source · {name} 源 · {name}Reset Effect 重置效果+ Reset Parameter 重置参数Save Preset… 保存预设…Custom Setup 自定义设置Edit… 编辑…diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rsindex 1eeb87b20..e1f3aebb8 100644--- a/crates/ui-egui/src/panels/effect_controls.rs+++ b/crates/ui-egui/src/panels/effect_controls.rs@@ -544,6 +544,17 @@ pub(crate) fn param_row(actions.push(("effects.addKeyframe".into(), with_mask(json!({"clip": clip.0, "effect": eff_json, "param": pd.id}))));}}+ // reset to the default, under the effect's own reset button (an animated parameter keeps its+ // keyframes and gets one at the playhead)+ if mask.is_none() {+ let rr = Rect::from_center_size(pos2(r.max.x - 14.0, r.center().y), vec2(12.0, 12.0));+ let rresp = ui.interact(rr, egui::Id::new(("param-reset", clip.0, idx, pkey)), Sense::click()).on_hover_text(tl!("Reset Parameter"));+ icons::paint(ui.painter(), rr, Icon::Reset, if rresp.hovered() { t.text } else { t.text_dim });+ app.auto.add(&format!("effectControls.{}.{}.reset", e.effect, pkey), rr, "Reset Parameter");+ if rresp.clicked() {+ actions.push(("effects.resetParam".into(), json!({"clip": clip.0, "effect": eff_json, "param": pd.id})));+ }+ }// keyframes in the lane: draggable diamonds; right-click for interpolationif param.is_animated() {let y = r.center().y;diff --git a/crates/ui-egui/tests/keyframes_ui.rs b/crates/ui-egui/tests/keyframes_ui.rsindex 630a8ceae..87986f547 100644--- a/crates/ui-egui/tests/keyframes_ui.rs+++ b/crates/ui-egui/tests/keyframes_ui.rs@@ -184,6 +184,22 @@ fn properties_diamond_adds_and_removes_the_keyframe_at_the_playhead() {assert!(d.find(&format!("{SCALE}.prevKeyframe")).is_none());}+/// #284: every Effect Controls property has a reset button; on an animated property it puts the+/// default in as a keyframe at the playhead and keeps the other keyframes.+#[test]+fn effect_controls_reset_button_keeps_keyframes() {+ let (mut d, clip) = Driver::demo();+ let t0 = d.seek(0.5);+ d.click(&format!("{SCALE}.addKeyframe"));+ d.exec("effects.setParam", json!({"clip": clip.id, "effect": "motion", "param": "scale", "value": 150.0, "time": t0}));+ d.seek(2.5);+ d.click("effectControls.motion.scale.reset");+ d.shot("effect-controls-reset");+ let (keys, value) = d.scale(&clip);+ assert_eq!(keys.len(), 2, "the keyframe stays and the default is keyed at the playhead: {keys:?}");+ assert_eq!(value, 100.0);+}+/// Crop is not on a clip until it is used: its diamond applies the effect, then adds the keyframe.#[test]fn properties_diamond_applies_crop_first() {