Skip to main content

glory_render/
motion.rs

1use crate::Error;
2
3/// Finite host-owned graphic motion. There is no application frame task.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum GraphicAnimation {
6    Rotate {
7        period_millis: u32,
8    },
9    /// A linear opacity wave between one quarter and full opacity. Phase is
10    /// bounded within one period; Reduce presents the fully opaque shape.
11    Pulse {
12        period_millis: u32,
13        phase_millis: u32,
14    },
15}
16impl GraphicAnimation {
17    pub fn validate(self) -> Result<(), Error> {
18        match self {
19            Self::Rotate { period_millis } if (16..=60000).contains(&period_millis) => Ok(()),
20            Self::Pulse {
21                period_millis,
22                phase_millis,
23            } if (16..=60000).contains(&period_millis) && phase_millis < period_millis => Ok(()),
24            _ => Err(Error::InvalidProps(
25                "graphic motion period must be in 16..=60000 milliseconds and pulse phase within its period",
26            )),
27        }
28    }
29    /// The phase stays finite even for a large finite host timestamp.
30    pub fn radians(self, seconds: f64) -> Result<f64, Error> {
31        self.validate()?;
32        if !seconds.is_finite() || seconds < 0. {
33            return Err(Error::InvalidProps(
34                "graphic clock must be finite and nonnegative",
35            ));
36        }
37        let Self::Rotate { period_millis } = self else {
38            return Err(Error::InvalidProps("graphic radians requires rotation"));
39        };
40        let period = f64::from(period_millis) / 1000.;
41        Ok(seconds.rem_euclid(period) / period * std::f64::consts::TAU)
42    }
43    pub fn opacity(self, seconds: f64) -> Result<f64, Error> {
44        self.validate()?;
45        if !seconds.is_finite() || seconds < 0. {
46            return Err(Error::InvalidProps(
47                "graphic clock must be finite and nonnegative",
48            ));
49        }
50        let Self::Pulse {
51            period_millis,
52            phase_millis,
53        } = self
54        else {
55            return Ok(1.);
56        };
57        let period = f64::from(period_millis) / 1000.;
58        let phase = ((seconds.rem_euclid(period) + f64::from(phase_millis) / 1000.) / period)
59            .rem_euclid(1.);
60        let wave = if phase <= 0.5 {
61            phase * 2.
62        } else {
63            (1. - phase) * 2.
64        };
65        Ok(0.25 + 0.75 * wave)
66    }
67}
68
69/// Native hosts explicitly choose their preference. Unknown defaults to Reduce.
70#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
71pub enum MotionPreference {
72    #[default]
73    Reduce,
74    NoPreference,
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    #[test]
81    fn periods_and_clock_values_are_independently_bounded() {
82        for period_millis in [0, 1, 15, 60001, u32::MAX] {
83            assert!(
84                GraphicAnimation::Rotate { period_millis }
85                    .validate()
86                    .is_err()
87            );
88        }
89        for period_millis in [16, 1200, 60000] {
90            GraphicAnimation::Rotate { period_millis }
91                .validate()
92                .unwrap();
93        }
94        let animation = GraphicAnimation::Rotate {
95            period_millis: 1000,
96        };
97        for seconds in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1.] {
98            assert!(animation.radians(seconds).is_err());
99        }
100        assert_eq!(animation.radians(0.).unwrap(), 0.);
101        assert!((animation.radians(0.25).unwrap() - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
102        assert!(animation.radians(f64::MAX).unwrap().is_finite());
103    }
104    #[test]
105    fn pulses_have_bounded_phases_finite_clocks_and_a_linear_opacity_wave() {
106        let pulse = GraphicAnimation::Pulse {
107            period_millis: 1000,
108            phase_millis: 0,
109        };
110        for (seconds, expected) in [
111            (0., 0.25),
112            (0.25, 0.625),
113            (0.5, 1.),
114            (0.75, 0.625),
115            (1., 0.25),
116        ] {
117            assert!((pulse.opacity(seconds).unwrap() - expected).abs() < 1e-12);
118        }
119        assert_eq!(
120            GraphicAnimation::Pulse {
121                period_millis: 1000,
122                phase_millis: 500
123            }
124            .opacity(0.)
125            .unwrap(),
126            1.
127        );
128        for config in [
129            GraphicAnimation::Pulse {
130                period_millis: 15,
131                phase_millis: 0,
132            },
133            GraphicAnimation::Pulse {
134                period_millis: 60001,
135                phase_millis: 0,
136            },
137            GraphicAnimation::Pulse {
138                period_millis: 1000,
139                phase_millis: 1000,
140            },
141            GraphicAnimation::Pulse {
142                period_millis: 16,
143                phase_millis: u32::MAX,
144            },
145        ] {
146            assert!(config.validate().is_err());
147        }
148        for seconds in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1.] {
149            assert!(pulse.opacity(seconds).is_err());
150        }
151        assert!((0.25..=1.).contains(&pulse.opacity(f64::MAX).unwrap()));
152        assert!(pulse.radians(0.).is_err());
153    }
154}