freya_hooks/use_animation/
anim_num.rs

1use std::time::Duration;
2
3use super::{
4    apply_value,
5    AnimDirection,
6    AnimatedValue,
7    Ease,
8    Function,
9};
10
11/// Animate a numeric value.
12#[derive(Clone, PartialEq)]
13pub struct AnimNum {
14    origin: f32,
15    destination: f32,
16    time: Duration,
17    ease: Ease,
18    function: Function,
19
20    value: f32,
21}
22
23impl AnimNum {
24    pub fn new(origin: f32, destination: f32) -> Self {
25        Self {
26            origin,
27            destination,
28            time: Duration::default(),
29            ease: Ease::default(),
30            function: Function::default(),
31
32            value: origin,
33        }
34    }
35
36    /// Set the animation duration using milliseconds. Use `Self::duration` if you want to specify the duration in another form.
37    pub fn time(mut self, time: u64) -> Self {
38        self.time = Duration::from_millis(time);
39        self
40    }
41
42    /// Set the animation duration using milliseconds.
43    pub fn duration(mut self, duration: Duration) -> Self {
44        self.time = duration;
45        self
46    }
47
48    /// Set the easing type. See `Ease` for all the types.
49    pub fn ease(mut self, ease: Ease) -> Self {
50        self.ease = ease;
51        self
52    }
53
54    /// Set the easing function. See `Function` for all the types.
55    pub fn function(mut self, function: Function) -> Self {
56        self.function = function;
57        self
58    }
59
60    /// Read the value of the [AnimNum] as a f32.
61    pub fn read(&self) -> f32 {
62        self.value
63    }
64}
65
66impl From<&AnimNum> for f32 {
67    fn from(value: &AnimNum) -> Self {
68        value.read()
69    }
70}
71
72impl AnimatedValue for AnimNum {
73    fn prepare(&mut self, direction: AnimDirection) {
74        match direction {
75            AnimDirection::Forward => self.value = self.origin,
76            AnimDirection::Reverse => {
77                self.value = self.destination;
78            }
79        }
80    }
81
82    fn is_finished(&self, index: u128, direction: AnimDirection) -> bool {
83        match direction {
84            AnimDirection::Forward => {
85                index >= self.time.as_millis() && self.value == self.destination
86            }
87            AnimDirection::Reverse => index >= self.time.as_millis() && self.value == self.origin,
88        }
89    }
90
91    fn advance(&mut self, index: u128, direction: AnimDirection) {
92        let (origin, destination) = match direction {
93            AnimDirection::Forward => (self.origin, self.destination),
94            AnimDirection::Reverse => (self.destination, self.origin),
95        };
96        self.value = apply_value(
97            origin,
98            destination,
99            index.min(self.time.as_millis()),
100            self.time,
101            self.ease,
102            self.function,
103        );
104    }
105
106    fn finish(&mut self, direction: AnimDirection) {
107        self.advance(self.time.as_millis(), direction);
108    }
109}