use super::{segment_tick, wide, wide_ticks};
use crate::curves::{AutoTangent, CurveError, WideKey, between_keys, split_at, tangents_of};
use alpha2_model::{CubicBezier, Interpolation, TimeKeyframe, TimeMap};
use alpha2_time::MediaTime;
impl WideKey for TimeKeyframe {
fn time(&self) -> MediaTime {
self.time
}
fn value(&self) -> f64 {
wide_ticks(self.source_time)
}
fn interpolation(&self) -> &Interpolation {
&self.interpolation
}
fn set_interpolation(&mut self, interpolation: Interpolation) {
self.interpolation = interpolation;
}
#[expect(
clippy::cast_possible_truncation,
reason = "a float to integer cast saturates"
)]
fn keyed(time: MediaTime, value: f64, interpolation: Interpolation) -> Self {
Self {
time,
source_time: MediaTime(value as i64),
interpolation,
}
}
fn stored(value: f64) -> f64 {
libm::floor(value)
}
fn shown(left: &Self, right: &Self, tick: i64) -> f64 {
if tick >= right.time.ticks() {
return right.value();
}
let elapsed = (i128::from(tick) - i128::from(left.time.ticks())).max(0);
segment_tick(left, right, elapsed).map_or_else(|| left.value(), wide)
}
fn keeps_timing(curve: CubicBezier, start: f64, end: f64) -> bool {
curve
.turns()
.into_iter()
.flatten()
.all(|turn| turn <= start || turn >= end)
}
}
pub fn insert_time_key(curve: &mut TimeMap, time: MediaTime) -> Result<(), CurveError> {
let TimeMap::Curve { keyframes, .. } = curve else {
return Err(CurveError::NotKeyed);
};
if keyframes
.binary_search_by_key(&time, |key| key.time)
.is_ok()
{
return Ok(());
}
let (index, _) = between_keys(keyframes.iter().map(|key| key.time), time)?;
split_at(keyframes, index, time)
}
pub fn time_auto_tangents(curve: &mut TimeMap, mode: AutoTangent, keys: Option<&[MediaTime]>) {
if let TimeMap::Curve { keyframes, .. } = curve {
tangents_of(keyframes, mode, keys);
}
}
#[cfg(test)]
#[path = "edits_tests.rs"]
mod tests;