Skip to main content

ass_renderer/animation/
timing.rs

1//! Animation timing, interpolation kinds, and `\t` tag representation
2
3#[cfg(feature = "nostd")]
4use alloc::{string::String, vec::Vec};
5#[cfg(not(feature = "nostd"))]
6use std::{string::String, vec::Vec};
7
8/// Animation interpolation types
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum AnimationInterpolation {
11    /// Linear interpolation
12    Linear,
13    /// Smooth step (ease-in-out)
14    Smooth,
15    /// Smoother step (smoother ease-in-out)
16    Smoother,
17}
18
19/// Animation tag (\t) representation
20#[derive(Debug, Clone)]
21pub struct AnimationTag {
22    /// Start time offset in centiseconds
23    pub t1: Option<u32>,
24    /// End time offset in centiseconds
25    pub t2: Option<u32>,
26    /// Acceleration factor
27    pub accel: Option<f32>,
28    /// Style modifiers to animate
29    pub modifiers: Vec<String>,
30}
31
32/// Interpolation function type
33pub type InterpolationFn = fn(f32) -> f32;
34
35/// Animation timing parameters
36#[derive(Debug, Clone)]
37pub struct AnimationTiming {
38    /// Start time in centiseconds
39    pub start_cs: u32,
40    /// End time in centiseconds
41    pub end_cs: u32,
42    /// Acceleration factor (default 1.0)
43    pub accel: f32,
44}
45
46impl AnimationTiming {
47    /// Create new animation timing
48    pub fn new(start_cs: u32, end_cs: u32, accel: f32) -> Self {
49        Self {
50            start_cs,
51            end_cs,
52            accel,
53        }
54    }
55
56    /// Calculate progress at given time (0.0 to 1.0)
57    pub fn progress(&self, time_cs: u32) -> f32 {
58        if time_cs <= self.start_cs {
59            return 0.0;
60        }
61        if time_cs >= self.end_cs {
62            return 1.0;
63        }
64
65        let duration = (self.end_cs - self.start_cs) as f32;
66        let elapsed = (time_cs - self.start_cs) as f32;
67        let linear_progress = elapsed / duration;
68
69        // Apply acceleration
70        if (self.accel - 1.0).abs() < 0.001 {
71            linear_progress
72        } else {
73            linear_progress.powf(self.accel)
74        }
75    }
76}