alpha2_eval/time_curve/
edits.rs1use super::{segment_tick, wide, wide_ticks};
2use crate::curves::{AutoTangent, CurveError, WideKey, between_keys, split_at, tangents_of};
3use alpha2_model::{CubicBezier, Interpolation, TimeKeyframe, TimeMap};
4use alpha2_time::MediaTime;
5
6impl WideKey for TimeKeyframe {
7 fn time(&self) -> MediaTime {
8 self.time
9 }
10
11 fn value(&self) -> f64 {
12 wide_ticks(self.source_time)
13 }
14
15 fn interpolation(&self) -> &Interpolation {
16 &self.interpolation
17 }
18
19 fn set_interpolation(&mut self, interpolation: Interpolation) {
20 self.interpolation = interpolation;
21 }
22
23 #[expect(
24 clippy::cast_possible_truncation,
25 reason = "a float to integer cast saturates"
26 )]
27 fn keyed(time: MediaTime, value: f64, interpolation: Interpolation) -> Self {
28 Self {
29 time,
30 source_time: MediaTime(value as i64),
31 interpolation,
32 }
33 }
34
35 fn stored(value: f64) -> f64 {
36 libm::floor(value)
37 }
38
39 fn shown(left: &Self, right: &Self, tick: i64) -> f64 {
40 if tick >= right.time.ticks() {
41 return right.value();
42 }
43 let elapsed = (i128::from(tick) - i128::from(left.time.ticks())).max(0);
44 segment_tick(left, right, elapsed).map_or_else(|| left.value(), wide)
45 }
46
47 fn keeps_timing(curve: CubicBezier, start: f64, end: f64) -> bool {
48 curve
49 .turns()
50 .into_iter()
51 .flatten()
52 .all(|turn| turn <= start || turn >= end)
53 }
54}
55
56pub fn insert_time_key(curve: &mut TimeMap, time: MediaTime) -> Result<(), CurveError> {
70 let TimeMap::Curve { keyframes, .. } = curve else {
71 return Err(CurveError::NotKeyed);
72 };
73 if keyframes
74 .binary_search_by_key(&time, |key| key.time)
75 .is_ok()
76 {
77 return Ok(());
78 }
79 let (index, _) = between_keys(keyframes.iter().map(|key| key.time), time)?;
80 split_at(keyframes, index, time)
81}
82
83pub fn time_auto_tangents(curve: &mut TimeMap, mode: AutoTangent, keys: Option<&[MediaTime]>) {
88 if let TimeMap::Curve { keyframes, .. } = curve {
89 tangents_of(keyframes, mode, keys);
90 }
91}
92
93#[cfg(test)]
94#[path = "edits_tests.rs"]
95mod tests;