ass_renderer/animation/
track.rs1#[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#[derive(Debug, Clone)]
13pub struct AnimationTrack {
14 pub property: String,
16 pub timing: AnimationTiming,
18 pub value: AnimatedValue,
20 pub interpolation: AnimationInterpolation,
22}
23
24impl AnimationTrack {
25 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 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 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
57fn smooth_step(t: f32) -> f32 {
59 t * t * (3.0 - 2.0 * t)
60}
61
62fn smoother_step(t: f32) -> f32 {
64 t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
65}