freya_animation/
easing.rs

1use std::{
2    fmt,
3    time::Duration,
4};
5
6use easer::functions::*;
7
8use crate::hook::Ease;
9
10pub fn apply_value(
11    origin: f32,
12    destination: f32,
13    index: u128,
14    time: Duration,
15    ease: Ease,
16    function: Function,
17) -> f32 {
18    let (t, b, c, d) = (
19        index as f32,
20        origin,
21        destination - origin,
22        time.as_millis() as f32,
23    );
24
25    // We can skip the easing if we have reached the last index
26    if t >= d {
27        return destination;
28    }
29
30    match function {
31        Function::Back => match ease {
32            Ease::In => Back::ease_in(t, b, c, d),
33            Ease::InOut => Back::ease_in_out(t, b, c, d),
34            Ease::Out => Back::ease_out(t, b, c, d),
35        },
36        Function::Bounce => match ease {
37            Ease::In => Bounce::ease_in(t, b, c, d),
38            Ease::InOut => Bounce::ease_in_out(t, b, c, d),
39            Ease::Out => Bounce::ease_out(t, b, c, d),
40        },
41        Function::Circ => match ease {
42            Ease::In => Circ::ease_in(t, b, c, d),
43            Ease::InOut => Circ::ease_in_out(t, b, c, d),
44            Ease::Out => Circ::ease_out(t, b, c, d),
45        },
46        Function::Cubic => match ease {
47            Ease::In => Cubic::ease_in(t, b, c, d),
48            Ease::InOut => Cubic::ease_in_out(t, b, c, d),
49            Ease::Out => Cubic::ease_out(t, b, c, d),
50        },
51        Function::Elastic => match ease {
52            Ease::In => Elastic::ease_in(t, b, c, d),
53            Ease::InOut => Elastic::ease_in_out(t, b, c, d),
54            Ease::Out => Elastic::ease_out(t, b, c, d),
55        },
56        Function::Expo => match ease {
57            Ease::In => Expo::ease_in(t, b, c, d),
58            Ease::InOut => Expo::ease_in_out(t, b, c, d),
59            Ease::Out => Expo::ease_out(t, b, c, d),
60        },
61        Function::Linear => match ease {
62            Ease::In => Linear::ease_in(t, b, c, d),
63            Ease::InOut => Linear::ease_in_out(t, b, c, d),
64            Ease::Out => Linear::ease_out(t, b, c, d),
65        },
66        Function::Quad => match ease {
67            Ease::In => Quad::ease_in(t, b, c, d),
68            Ease::InOut => Quad::ease_in_out(t, b, c, d),
69            Ease::Out => Quad::ease_out(t, b, c, d),
70        },
71        Function::Quart => match ease {
72            Ease::In => Quart::ease_in(t, b, c, d),
73            Ease::InOut => Quart::ease_in_out(t, b, c, d),
74            Ease::Out => Quart::ease_out(t, b, c, d),
75        },
76        Function::Sine => match ease {
77            Ease::In => Sine::ease_in(t, b, c, d),
78            Ease::InOut => Sine::ease_in_out(t, b, c, d),
79            Ease::Out => Sine::ease_out(t, b, c, d),
80        },
81    }
82}
83
84#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
85pub enum Function {
86    Back,
87    Bounce,
88    Circ,
89    Cubic,
90    Elastic,
91    Expo,
92    #[default]
93    Linear,
94    Quad,
95    Quart,
96    Sine,
97}
98
99impl fmt::Display for Function {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        write!(f, "{self:?}")
102    }
103}