diff --git a/crates/engine/src/cmds_camera.rs b/crates/engine/src/cmds_camera.rs index 579fe605..618c68b5 100644 --- a/crates/engine/src/cmds_camera.rs +++ b/crates/engine/src/cmds_camera.rs @@ -139,6 +139,9 @@ fn set(s: &mut Session, p: &Value) -> Result { return Err(EngineError::Param("param 'zoom_by' must be above 0".into())); } let stage = s.doc.stage; + // On a held frame (inside a keyframe's span, no tween) the edit lands on that keyframe, as + // any edit does: say so, so a caller keying the camera over time sees it was not keyed here. + let held = s.timeline().layer(id).and_then(|l| l.keyframe_at(frame)).filter(|k| k.start != frame && k.tween.is_none() && k.motion.is_none()).map(|k| (k.start, k.start + k.duration.max(1) - 1)); // A camera is created on a keyframe that has none (a blank camera keyframe is at rest). // (Its id is taken only when it is used, so a no-op leaves the document unchanged.) let fresh = ElementId(s.doc.next_id.max(1)); @@ -184,7 +187,22 @@ fn set(s: &mut Session, p: &Value) -> Result { if used { s.doc.alloc_id(); } - Ok(camera_json(&c, &stage)) + let mut out = camera_json(&c, &stage); + if let (Some((start, end)), Some(o)) = (held, out.as_object_mut()) { + o.insert("keyframe".into(), json!(start)); + o.insert( + "note".into(), + json!(format!( + "frame {} has no camera keyframe of its own: the keyframe on frame {} (frames {}–{}) was edited. To key the camera on frame {}, insert a keyframe there first or tween the camera layer", + frame + 1, + start + 1, + start + 1, + end + 1, + frame + 1 + )), + ); + } + Ok(out) } fn camera_json(c: &Camera, stage: &anicraft_doc::Stage) -> Value { diff --git a/crates/engine/src/tests_camera.rs b/crates/engine/src/tests_camera.rs index fad49877..5eebea0a 100644 --- a/crates/engine/src/tests_camera.rs +++ b/crates/engine/src/tests_camera.rs @@ -425,3 +425,21 @@ fn picking_through_the_camera_finds_what_the_movie_shows() { let d = v.inverse().unwrap().apply_vector(anicraft_geom::Vec2::new(10.0, 0.0)); assert!((d.x - 5.0).abs() < 1e-4, "{d:?}"); } + +#[test] +fn set_on_a_held_frame_reports_the_keyframe_it_edited() { + let mut s = stage_with(&[(95.0, 45.0, 10.0, 10.0)]); + run(&mut s, "camera.add", json!({})); + // Frame 10 is held by the camera's one keyframe (frames 1–20): the edit lands on that keyframe. + let v = run(&mut s, "camera.set", json!({"zoom": 200, "frame": 10})); + assert_eq!(v["keyframe"], 0, "{v}"); + assert!(v["note"].as_str().is_some_and(|n| n.contains("frame 11") && n.contains("frame 1 ")), "{v}"); + assert_eq!(s.doc.timeline(0).layers[0].keyframes.len(), 1, "no keyframe was added"); + let zoom = |s: &mut Session, f: u32| run(s, "camera.info", json!({"frame": f}))["zoom"].as_f64().unwrap(); + assert!((zoom(&mut s, 0) - zoom(&mut s, 19)).abs() < 1e-6, "the whole span changed together"); + // On a keyframe of its own there is nothing to report. + run(&mut s, "timeline.insert_keyframe", json!({"frame": 10})); + let v = run(&mut s, "camera.set", json!({"zoom": 300, "frame": 10})); + assert!(v.get("note").is_none() && v.get("keyframe").is_none(), "{v}"); + assert!(zoom(&mut s, 10) > zoom(&mut s, 0), "the new keyframe holds its own zoom"); +}