1use std::f32::consts::TAU;
2use std::time::Duration;
3
4#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Spring {
10 pub response: Duration,
12 pub damping_ratio: f32,
14}
15
16impl Spring {
17 pub const fn new(response: Duration, damping_ratio: f32) -> Self {
19 Self { response, damping_ratio }
20 }
21
22 pub const fn smooth() -> Self {
24 Self::new(Duration::from_millis(450), 1.0)
25 }
26
27 pub const fn snappy() -> Self {
29 Self::new(Duration::from_millis(300), 0.86)
30 }
31
32 pub const fn bouncy() -> Self {
34 Self::new(Duration::from_millis(500), 0.6)
35 }
36
37 pub(crate) fn omega(&self) -> f32 {
39 TAU / seconds(self.response).max(1e-4)
40 }
41}
42
43impl Default for Spring {
44 fn default() -> Self {
45 Self::new(Duration::from_millis(500), 0.85)
46 }
47}
48
49#[derive(Clone, Copy, Debug, PartialEq)]
51#[non_exhaustive]
52pub enum Easing {
53 Linear,
55 CubicBezier(f32, f32, f32, f32),
57}
58
59impl Easing {
60 pub const EASE: Self = Self::CubicBezier(0.25, 0.1, 0.25, 1.0);
62 pub const EASE_IN: Self = Self::CubicBezier(0.42, 0.0, 1.0, 1.0);
64 pub const EASE_OUT: Self = Self::CubicBezier(0.0, 0.0, 0.58, 1.0);
66 pub const EASE_IN_OUT: Self = Self::CubicBezier(0.42, 0.0, 0.58, 1.0);
68
69 pub fn apply(&self, x: f32) -> f32 {
71 match *self {
72 Self::Linear => x.clamp(0.0, 1.0),
73 Self::CubicBezier(x1, y1, x2, y2) => cubic_bezier(x1, y1, x2, y2, x),
74 }
75 }
76
77 pub fn slope(&self, x: f32) -> f32 {
80 match *self {
81 Self::Linear => 1.0,
82 Self::CubicBezier(x1, y1, x2, y2) => cubic_bezier_slope(x1, y1, x2, y2, x.clamp(0.0, 1.0)),
83 }
84 }
85}
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub enum Repeat {
90 Once,
92 Forever,
94 Alternate,
96}
97
98#[derive(Clone, Copy, Debug, PartialEq)]
100pub struct Tween {
101 pub duration: Duration,
103 pub easing: Easing,
105 pub repeat: Repeat,
107}
108
109impl Tween {
110 pub const fn new(duration: Duration) -> Self {
112 Self { duration, easing: Easing::EASE, repeat: Repeat::Once }
113 }
114
115 pub const fn forever(mut self) -> Self {
117 if matches!(self.repeat, Repeat::Once) {
118 self.repeat = Repeat::Forever;
119 }
120 self
121 }
122
123 pub const fn alternate(mut self, alternate: bool) -> Self {
125 self.repeat = match (alternate, self.repeat) {
126 (true, _) => Repeat::Alternate,
127 (false, Repeat::Alternate) => Repeat::Forever,
128 (false, repeat) => repeat,
129 };
130 self
131 }
132
133 pub const fn easing(mut self, easing: Easing) -> Self {
135 self.easing = easing;
136 self
137 }
138
139 pub const fn linear(self) -> Self {
141 self.easing(Easing::Linear)
142 }
143
144 pub const fn ease_in(self) -> Self {
146 self.easing(Easing::EASE_IN)
147 }
148
149 pub const fn ease_out(self) -> Self {
151 self.easing(Easing::EASE_OUT)
152 }
153
154 pub const fn ease_in_out(self) -> Self {
156 self.easing(Easing::EASE_IN_OUT)
157 }
158}
159
160pub(crate) fn seconds(duration: Duration) -> f32 {
162 duration.as_secs_f64() as f32
163}
164
165#[derive(Clone, Copy, Debug, PartialEq)]
167#[non_exhaustive]
168pub enum Curve {
169 Tween(Tween),
171 Spring(Spring),
173}
174
175impl From<Tween> for Curve {
176 fn from(tween: Tween) -> Self {
177 Self::Tween(tween)
178 }
179}
180
181impl From<Spring> for Curve {
182 fn from(spring: Spring) -> Self {
183 Self::Spring(spring)
184 }
185}
186
187fn coefficients(p1: f32, p2: f32) -> (f32, f32, f32) {
189 let c = 3.0 * p1;
190 let b = 3.0 * (p2 - p1) - c;
191 (1.0 - c - b, b, c)
192}
193
194fn solve(x1: f32, x2: f32, x: f32) -> f32 {
196 let (ax, bx, cx) = coefficients(x1, x2);
197 let sample_x = |t: f32| ((ax * t + bx) * t + cx) * t;
198 let slope_x = |t: f32| (3.0 * ax * t + 2.0 * bx) * t + cx;
199
200 let mut t = x;
201 for _ in 0..8 {
202 let error = sample_x(t) - x;
203 if error.abs() < 1e-6 {
204 return t;
205 }
206 let slope = slope_x(t);
207 if slope.abs() < 1e-6 {
208 break;
209 }
210 t -= error / slope;
211 }
212
213 let (mut lo, mut hi) = (0.0, 1.0);
214 t = x;
215 for _ in 0..32 {
216 let value = sample_x(t);
217 if (value - x).abs() < 1e-6 {
218 break;
219 }
220 if value < x {
221 lo = t;
222 } else {
223 hi = t;
224 }
225 t = (lo + hi) * 0.5;
226 }
227 t
228}
229
230fn cubic_bezier(x1: f32, y1: f32, x2: f32, y2: f32, x: f32) -> f32 {
232 if x <= 0.0 {
233 return 0.0;
234 }
235 if x >= 1.0 {
236 return 1.0;
237 }
238 let t = solve(x1, x2, x);
239 let (ay, by, cy) = coefficients(y1, y2);
240 ((ay * t + by) * t + cy) * t
241}
242
243fn cubic_bezier_slope(x1: f32, y1: f32, x2: f32, y2: f32, x: f32) -> f32 {
245 let t = if x <= 0.0 {
246 0.0
247 } else if x >= 1.0 {
248 1.0
249 } else {
250 solve(x1, x2, x)
251 };
252 let (ax, bx, cx) = coefficients(x1, x2);
253 let (ay, by, cy) = coefficients(y1, y2);
254 let dx = (3.0 * ax * t + 2.0 * bx) * t + cx;
255 let dy = (3.0 * ay * t + 2.0 * by) * t + cy;
256 if dx.abs() > 1e-4 {
257 return dy / dx;
258 }
259 const H: f32 = 1e-3;
260 let (a, b) = ((x - H).max(0.0), (x + H).min(1.0));
261 (cubic_bezier(x1, y1, x2, y2, b) - cubic_bezier(x1, y1, x2, y2, a)) / (b - a)
262}