use alpha2_model::{Animated, Extrapolation, Interpolation, Keyframe, StepPosition};
use alpha2_time::MediaTime;
use crate::SampleContext;
#[must_use]
pub fn next_knot(
animation: &Animated<f32>,
ticks: i64,
context: &SampleContext<'_>,
) -> Option<i64> {
match animation {
Animated::Data(binding) => {
crate::data::next_data_knot(binding, MediaTime(ticks), context).map(MediaTime::ticks)
}
Animated::Keyframes {
keyframes,
before,
after,
} => keyed_knot(keyframes, *before, *after, ticks),
Animated::Constant { .. } | Animated::Split { .. } => None,
}
}
fn keyed_knot(
keyframes: &[Keyframe<f32>],
before: Extrapolation,
after: Extrapolation,
ticks: i64,
) -> Option<i64> {
let [first, .., last] = keyframes else {
return None;
};
if first.time.ticks() <= ticks && ticks < last.time.ticks() {
return knot_after(keyframes, ticks);
}
let (start, end, at) = (
i128::from(first.time.ticks()),
i128::from(last.time.ticks()),
i128::from(ticks),
);
let period = end - start;
let within = |offset: i128| i64::try_from(start + offset).ok();
let forward = |offset: i128| {
let knot = knot_after(keyframes, within(offset)?)?;
Some(i128::from(knot) - start - offset)
};
let ahead = match if at < start { before } else { after } {
Extrapolation::Hold | Extrapolation::Linear => {
return (at < start).then_some(first.time.ticks());
}
Extrapolation::Repeat | Extrapolation::RepeatOffset => {
forward((at - start).rem_euclid(period))?
}
Extrapolation::PingPong => {
let offset = (at - start).rem_euclid(2 * period);
if offset < period {
forward(offset)?
} else {
let back = knot_at_or_before(keyframes, within(2 * period - offset)?)?;
let back = i128::from(back) - start;
2 * period - offset + if back == 0 { 0 } else { 1 - back }
}
}
};
let knot = i64::try_from(at + ahead).unwrap_or(i64::MAX);
(knot > ticks).then_some(knot)
}
fn jumps_at_start(position: StepPosition) -> bool {
matches!(position, StepPosition::Start | StepPosition::Both)
}
fn jump(from: i64, length: i128, count: i128, index: i128) -> Option<i64> {
i64::try_from(i128::from(from) + (index * length + count - 1).div_euclid(count)).ok()
}
fn knot_after(keyframes: &[Keyframe<f32>], ticks: i64) -> Option<i64> {
let next = keyframes.partition_point(|keyframe| keyframe.time.ticks() <= ticks);
let (left, right) = (keyframes.get(next.checked_sub(1)?)?, keyframes.get(next)?);
let (from, to) = (left.time.ticks(), right.time.ticks());
let Interpolation::Steps(steps) = &left.interpolation else {
return Some(to);
};
if ticks == from && jumps_at_start(steps.position) {
return Some(from.saturating_add(1).min(to));
}
let length = i128::from(to) - i128::from(from);
let count = i128::from(steps.count);
let next = steps.index(i128::from(ticks) - i128::from(from), length) + 1;
if next >= count {
return Some(to);
}
Some(jump(from, length, count, next)?.min(to))
}
fn knot_at_or_before(keyframes: &[Keyframe<f32>], ticks: i64) -> Option<i64> {
let next = keyframes.partition_point(|keyframe| keyframe.time.ticks() <= ticks);
let left = keyframes.get(next.checked_sub(1)?)?;
let from = left.time.ticks();
let (Some(right), Interpolation::Steps(steps)) = (keyframes.get(next), &left.interpolation)
else {
return Some(from);
};
let length = i128::from(right.time.ticks()) - i128::from(from);
let index = steps.index(i128::from(ticks) - i128::from(from), length);
if index > 0 {
jump(from, length, i128::from(steps.count), index)
} else if ticks > from && jumps_at_start(steps.position) {
Some(from.saturating_add(1))
} else {
Some(from)
}
}
#[cfg(test)]
#[path = "knots_tests.rs"]
mod tests;