freya_hooks/use_animation/
anim_num.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::time::Duration;

use super::{
    apply_value,
    AnimDirection,
    AnimatedValue,
    Ease,
    Function,
};
use crate::ReadAnimatedValue;

/// Animate a numeric value.
#[derive(Clone, PartialEq)]
pub struct AnimNum {
    origin: f32,
    destination: f32,
    time: Duration,
    ease: Ease,
    function: Function,

    value: f32,
}

impl AnimNum {
    pub fn new(origin: f32, destination: f32) -> Self {
        Self {
            origin,
            destination,
            time: Duration::default(),
            ease: Ease::default(),
            function: Function::default(),

            value: origin,
        }
    }

    /// Reverses the `origin` and the `destination` of the [AnimNum].
    pub fn into_reversed(self) -> AnimNum {
        Self {
            origin: self.destination,
            destination: self.origin,
            ..self
        }
    }

    /// Set the animation duration using milliseconds. Use `Self::duration` if you want to specify the duration in another form.
    pub fn time(mut self, time: u64) -> Self {
        self.time = Duration::from_millis(time);
        self
    }

    /// Set the animation duration using milliseconds.
    pub fn duration(mut self, duration: Duration) -> Self {
        self.time = duration;
        self
    }

    /// Set the easing type. See `Ease` for all the types.
    pub fn ease(mut self, ease: Ease) -> Self {
        self.ease = ease;
        self
    }

    /// Set the easing function. See `Function` for all the types.
    pub fn function(mut self, function: Function) -> Self {
        self.function = function;
        self
    }

    /// Read the value of the [AnimNum] as a f32.
    pub fn value(&self) -> f32 {
        self.value
    }
}

impl ReadAnimatedValue for AnimNum {
    type Output = f32;

    fn value(&self) -> Self::Output {
        self.value
    }
}

impl From<&AnimNum> for f32 {
    fn from(value: &AnimNum) -> Self {
        value.value()
    }
}

impl From<AnimNum> for f32 {
    fn from(value: AnimNum) -> Self {
        value.value()
    }
}

impl AnimatedValue for AnimNum {
    fn prepare(&mut self, direction: AnimDirection) {
        match direction {
            AnimDirection::Forward => self.value = self.origin,
            AnimDirection::Reverse => {
                self.value = self.destination;
            }
        }
    }

    fn is_finished(&self, index: u128, direction: AnimDirection) -> bool {
        match direction {
            AnimDirection::Forward => {
                index >= self.time.as_millis() && self.value == self.destination
            }
            AnimDirection::Reverse => index >= self.time.as_millis() && self.value == self.origin,
        }
    }

    fn advance(&mut self, index: u128, direction: AnimDirection) {
        let (origin, destination) = match direction {
            AnimDirection::Forward => (self.origin, self.destination),
            AnimDirection::Reverse => (self.destination, self.origin),
        };
        self.value = apply_value(
            origin,
            destination,
            index.min(self.time.as_millis()),
            self.time,
            self.ease,
            self.function,
        );
    }

    fn finish(&mut self, direction: AnimDirection) {
        self.advance(self.time.as_millis(), direction);
    }
}