Skip to main content

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::Fn(function) => origin + (destination - origin) * function(t / d, ease),
32        Function::Back => match ease {
33            Ease::In => Back::ease_in(t, b, c, d),
34            Ease::InOut => Back::ease_in_out(t, b, c, d),
35            Ease::Out => Back::ease_out(t, b, c, d),
36        },
37        Function::Bounce => match ease {
38            Ease::In => Bounce::ease_in(t, b, c, d),
39            Ease::InOut => Bounce::ease_in_out(t, b, c, d),
40            Ease::Out => Bounce::ease_out(t, b, c, d),
41        },
42        Function::Circ => match ease {
43            Ease::In => Circ::ease_in(t, b, c, d),
44            Ease::InOut => Circ::ease_in_out(t, b, c, d),
45            Ease::Out => Circ::ease_out(t, b, c, d),
46        },
47        Function::Cubic => match ease {
48            Ease::In => Cubic::ease_in(t, b, c, d),
49            Ease::InOut => Cubic::ease_in_out(t, b, c, d),
50            Ease::Out => Cubic::ease_out(t, b, c, d),
51        },
52        Function::Elastic => match ease {
53            Ease::In => Elastic::ease_in(t, b, c, d),
54            Ease::InOut => Elastic::ease_in_out(t, b, c, d),
55            Ease::Out => Elastic::ease_out(t, b, c, d),
56        },
57        Function::Expo => match ease {
58            Ease::In => Expo::ease_in(t, b, c, d),
59            Ease::InOut => Expo::ease_in_out(t, b, c, d),
60            Ease::Out => Expo::ease_out(t, b, c, d),
61        },
62        Function::Linear => match ease {
63            Ease::In => Linear::ease_in(t, b, c, d),
64            Ease::InOut => Linear::ease_in_out(t, b, c, d),
65            Ease::Out => Linear::ease_out(t, b, c, d),
66        },
67        Function::Quad => match ease {
68            Ease::In => Quad::ease_in(t, b, c, d),
69            Ease::InOut => Quad::ease_in_out(t, b, c, d),
70            Ease::Out => Quad::ease_out(t, b, c, d),
71        },
72        Function::Quart => match ease {
73            Ease::In => Quart::ease_in(t, b, c, d),
74            Ease::InOut => Quart::ease_in_out(t, b, c, d),
75            Ease::Out => Quart::ease_out(t, b, c, d),
76        },
77        Function::Sine => match ease {
78            Ease::In => Sine::ease_in(t, b, c, d),
79            Ease::InOut => Sine::ease_in_out(t, b, c, d),
80            Ease::Out => Sine::ease_out(t, b, c, d),
81        },
82    }
83}
84
85/// Built-in easing functions or a custom normalized easing function.
86///
87/// ```
88/// # use freya::prelude::*;
89/// # use freya::animation::*;
90///
91/// let animation = AnimNum::new(0., 100.).time(300).function(Function::Cubic);
92/// ```
93#[derive(Default, Clone, Copy, Debug, Eq)]
94pub enum Function {
95    Back,
96    Bounce,
97    Circ,
98    Cubic,
99    Elastic,
100    Expo,
101    #[default]
102    Linear,
103    Quad,
104    Quart,
105    Sine,
106    Fn(fn(f32, Ease) -> f32),
107}
108
109impl From<fn(f32, Ease) -> f32> for Function {
110    fn from(function: fn(f32, Ease) -> f32) -> Self {
111        Self::Fn(function)
112    }
113}
114
115impl PartialEq for Function {
116    fn eq(&self, other: &Self) -> bool {
117        match (self, other) {
118            (Self::Fn(function), Self::Fn(other)) => std::ptr::fn_addr_eq(*function, *other),
119            _ => std::mem::discriminant(self) == std::mem::discriminant(other),
120        }
121    }
122}
123
124impl fmt::Display for Function {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        write!(f, "{self:?}")
127    }
128}