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