#[cfg(feature = "nostd")]
use alloc::string::String;
#[cfg(not(feature = "nostd"))]
use std::string::String;
use super::timing::{AnimationInterpolation, AnimationTiming};
use super::value::{AnimatedResult, AnimatedValue};
#[derive(Debug, Clone)]
pub struct AnimationTrack {
pub property: String,
pub timing: AnimationTiming,
pub value: AnimatedValue,
pub interpolation: AnimationInterpolation,
}
impl AnimationTrack {
pub fn new(
property: String,
timing: AnimationTiming,
value: AnimatedValue,
interpolation: AnimationInterpolation,
) -> Self {
Self {
property,
timing,
value,
interpolation,
}
}
pub fn evaluate(&self, time_cs: u32) -> AnimatedResult {
let progress = self.timing.progress(time_cs);
let interpolated_progress = self.apply_interpolation(progress);
self.value.interpolate(interpolated_progress)
}
fn apply_interpolation(&self, progress: f32) -> f32 {
match self.interpolation {
AnimationInterpolation::Linear => progress,
AnimationInterpolation::Smooth => smooth_step(progress),
AnimationInterpolation::Smoother => smoother_step(progress),
}
}
}
fn smooth_step(t: f32) -> f32 {
t * t * (3.0 - 2.0 * t)
}
fn smoother_step(t: f32) -> f32 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}