Skip to main content

alpha2_eval/time_curve/
edits.rs

1use 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
56/// Adds a keyframe at `time`, inside the keyed range, to a time curve. The segment it falls in
57/// is cut as [`crate::curves::insert_key`] cuts a number's, and the new keyframe holds the
58/// source tick the curve shows at `time`. Between the keyframes the curve then names source
59/// times within one tick of those it named, since a keyframe holds a whole tick; handles are
60/// `f32` offsets, whole ticks up to 2^24 of them. A `time` with a keyframe changes nothing.
61///
62/// A `cubic_bezier` segment that turns back inside a part gives that part as a `value_bezier`.
63///
64/// # Errors
65///
66/// [`CurveError::NotKeyed`] for a map that is not a curve, [`CurveError::OutsideKeys`] for a
67/// time before the first keyframe or after the last, whatever the curve does there, or
68/// [`CurveError::TooManyKeys`].
69pub 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
83/// Writes value-curve handles, in ticks of source time, at the chosen keyframes of a time
84/// curve (every keyframe when `keys` is `None`) from their neighbours, as
85/// [`crate::curves::auto_tangents`] does for a number. [`AutoTangent::Monotone`] keeps a run
86/// of rising keyframes rising and a falling run falling. Other time maps are left alone.
87pub 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;