Skip to main content

ass_renderer/animation/
track.rs

1//! Per-property animation tracks and their interpolation helpers
2
3#[cfg(feature = "nostd")]
4use alloc::string::String;
5#[cfg(not(feature = "nostd"))]
6use std::string::String;
7
8use super::timing::{AnimationInterpolation, AnimationTiming};
9use super::value::{AnimatedResult, AnimatedValue};
10
11/// Animation track for a single property
12#[derive(Debug, Clone)]
13pub struct AnimationTrack {
14    /// Property name being animated
15    pub property: String,
16    /// Animation timing
17    pub timing: AnimationTiming,
18    /// Animated value
19    pub value: AnimatedValue,
20    /// Interpolation type
21    pub interpolation: AnimationInterpolation,
22}
23
24impl AnimationTrack {
25    /// Create a new animation track
26    pub fn new(
27        property: String,
28        timing: AnimationTiming,
29        value: AnimatedValue,
30        interpolation: AnimationInterpolation,
31    ) -> Self {
32        Self {
33            property,
34            timing,
35            value,
36            interpolation,
37        }
38    }
39
40    /// Evaluate animation at given time
41    pub fn evaluate(&self, time_cs: u32) -> AnimatedResult {
42        let progress = self.timing.progress(time_cs);
43        let interpolated_progress = self.apply_interpolation(progress);
44        self.value.interpolate(interpolated_progress)
45    }
46
47    /// Apply interpolation function to progress
48    fn apply_interpolation(&self, progress: f32) -> f32 {
49        match self.interpolation {
50            AnimationInterpolation::Linear => progress,
51            AnimationInterpolation::Smooth => smooth_step(progress),
52            AnimationInterpolation::Smoother => smoother_step(progress),
53        }
54    }
55}
56
57/// Smooth step interpolation (ease-in-out)
58fn smooth_step(t: f32) -> f32 {
59    t * t * (3.0 - 2.0 * t)
60}
61
62/// Smoother step interpolation (smoother ease-in-out)
63fn smoother_step(t: f32) -> f32 {
64    t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
65}