1use std::fmt::Debug;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
21pub enum Curve {
22 #[default]
24 Linear,
25 EaseIn,
27 EaseOut,
29 EaseInOut,
31}
32
33impl Curve {
34 pub fn transform(&self, t: f32) -> f32 {
38 let t = t.clamp(0.0, 1.0);
39 match self {
40 Curve::Linear => t,
41 Curve::EaseIn => t * t,
42 Curve::EaseOut => 1.0 - (1.0 - t) * (1.0 - t),
43 Curve::EaseInOut => t * t * (3.0 - 2.0 * t),
44 }
45 }
46}
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum AnimationStatus {
51 Dismissed,
53 Forward,
55 Reverse,
57 Completed,
59}
60
61#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
63pub enum Repeat {
64 #[default]
66 None,
67 Loop,
69 PingPong,
71}
72
73#[derive(Clone, Copy, Debug, PartialEq)]
75pub enum TickOutcome {
76 Value(f32),
78 Finished,
80 Idle,
82}
83
84pub struct AnimationController {
106 duration_ms: u64,
107 curve: Curve,
108 repeat: Repeat,
109 status: AnimationStatus,
110 anchor_ms: u64,
112 anchor_progress: f32,
114 on_status_changed: Option<Box<dyn FnMut(AnimationStatus)>>,
116}
117
118impl std::fmt::Debug for AnimationController {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 f.debug_struct("AnimationController")
121 .field("duration_ms", &self.duration_ms)
122 .field("curve", &self.curve)
123 .field("repeat", &self.repeat)
124 .field("status", &self.status)
125 .field("anchor_ms", &self.anchor_ms)
126 .field("anchor_progress", &self.anchor_progress)
127 .field("has_callback", &self.on_status_changed.is_some())
128 .finish()
129 }
130}
131
132impl AnimationController {
133 pub fn new(duration_ms: u64) -> Self {
134 Self {
135 duration_ms: duration_ms.max(1),
136 curve: Curve::default(),
137 repeat: Repeat::default(),
138 status: AnimationStatus::Dismissed,
139 anchor_ms: 0,
140 anchor_progress: 0.0,
141 on_status_changed: None,
142 }
143 }
144
145 pub fn with_curve(mut self, curve: Curve) -> Self {
146 self.curve = curve;
147 self
148 }
149
150 pub fn with_repeat(mut self, repeat: Repeat) -> Self {
151 self.repeat = repeat;
152 self
153 }
154
155 pub fn with_on_status_changed(mut self, cb: impl FnMut(AnimationStatus) + 'static) -> Self {
156 self.on_status_changed = Some(Box::new(cb));
157 self
158 }
159
160 fn set_status(&mut self, new_status: AnimationStatus) {
162 if self.status != new_status {
163 self.status = new_status;
164 if let Some(cb) = self.on_status_changed.as_mut() {
165 cb(new_status);
166 }
167 }
168 }
169
170 pub fn status(&self) -> AnimationStatus {
171 self.status
172 }
173
174 pub fn is_animating(&self) -> bool {
176 matches!(self.status, AnimationStatus::Forward | AnimationStatus::Reverse)
177 }
178
179 pub fn curve(&self) -> Curve {
180 self.curve
181 }
182
183 pub fn repeat(&self) -> Repeat {
184 self.repeat
185 }
186
187 pub fn duration_ms(&self) -> u64 {
188 self.duration_ms
189 }
190
191 pub fn forward(&mut self, now_ms: u64) {
193 match self.status {
194 AnimationStatus::Reverse => self.reanchor(now_ms),
195 AnimationStatus::Dismissed | AnimationStatus::Completed => {
196 self.anchor_ms = now_ms;
197 self.anchor_progress = 0.0;
198 }
199 AnimationStatus::Forward => {}
200 }
201 self.set_status(AnimationStatus::Forward);
202 }
203
204 pub fn reverse(&mut self, now_ms: u64) {
206 match self.status {
207 AnimationStatus::Forward => self.reanchor(now_ms),
208 AnimationStatus::Completed => {
209 self.anchor_ms = now_ms;
210 self.anchor_progress = 1.0;
211 }
212 AnimationStatus::Dismissed | AnimationStatus::Reverse => {}
213 }
214 self.set_status(AnimationStatus::Reverse);
215 }
216
217 pub fn stop(&mut self) {
219 self.set_status(match self.status {
220 AnimationStatus::Completed => AnimationStatus::Completed,
221 _ => AnimationStatus::Dismissed,
222 });
223 }
224
225 fn reanchor(&mut self, now_ms: u64) {
227 let p = self.raw_progress(now_ms);
228 self.anchor_progress = if self.repeat == Repeat::None { p.clamp(0.0, 1.0) } else { p };
233 self.anchor_ms = now_ms;
234 }
235
236 pub fn value(&self, now_ms: u64) -> f32 {
238 if !self.is_animating() {
239 return match self.status {
240 AnimationStatus::Completed => 1.0,
241 _ => 0.0,
242 };
243 }
244 let linear = match self.repeat {
245 Repeat::None => self.linear_progress(now_ms),
246 Repeat::Loop => self.loop_phase(now_ms).rem_euclid(1.0),
247 Repeat::PingPong => {
248 let phase = self.loop_phase(now_ms).rem_euclid(2.0);
249 if phase <= 1.0 {
250 phase
251 } else {
252 2.0 - phase
253 }
254 }
255 };
256 self.curve.transform(linear)
257 }
258
259 pub fn tick(&mut self, now_ms: u64) -> TickOutcome {
261 if !self.is_animating() {
262 return TickOutcome::Idle;
263 }
264 let progress = self.raw_progress(now_ms);
265 match self.repeat {
266 Repeat::None => {
267 let done = if self.status == AnimationStatus::Forward { progress >= 1.0 } else { progress <= 0.0 };
268 if done {
269 let terminal = if self.status == AnimationStatus::Forward {
270 AnimationStatus::Completed
271 } else {
272 AnimationStatus::Dismissed
273 };
274 self.set_status(terminal);
275 TickOutcome::Finished
276 } else {
277 TickOutcome::Value(self.curve.transform(progress.clamp(0.0, 1.0)))
278 }
279 }
280 Repeat::Loop => {
281 let phase = self.loop_phase(now_ms).rem_euclid(1.0);
284 TickOutcome::Value(self.curve.transform(phase))
285 }
286 Repeat::PingPong => {
287 let phase = self.loop_phase(now_ms).rem_euclid(2.0);
288 let p = if phase <= 1.0 { phase } else { 2.0 - phase };
289 TickOutcome::Value(self.curve.transform(p.clamp(0.0, 1.0)))
290 }
291 }
292 }
293
294 fn loop_phase(&self, now_ms: u64) -> f32 {
296 let dt = now_ms.saturating_sub(self.anchor_ms) as f32 / self.duration_ms as f32;
297 match self.status {
298 AnimationStatus::Reverse => self.anchor_progress - dt,
299 _ => self.anchor_progress + dt,
300 }
301 }
302
303 fn raw_progress(&self, now_ms: u64) -> f32 {
305 let dt = now_ms.saturating_sub(self.anchor_ms) as f32 / self.duration_ms as f32;
306 match self.status {
307 AnimationStatus::Reverse => self.anchor_progress - dt,
308 AnimationStatus::Forward => self.anchor_progress + dt,
309 AnimationStatus::Dismissed => 0.0,
310 AnimationStatus::Completed => 1.0,
311 }
312 }
313
314 fn linear_progress(&self, now_ms: u64) -> f32 {
315 self.raw_progress(now_ms).clamp(0.0, 1.0)
316 }
317}
318
319pub fn tick_animation(controller: &mut AnimationController, now_ms: u64, mut on_value: impl FnMut(f32)) -> bool {
348 if let TickOutcome::Value(t) = controller.tick(now_ms) {
349 on_value(t);
350 }
351 controller.is_animating()
352}
353
354#[cfg(test)]
355mod tests {
356 use super::*;
357
358 #[test]
360 fn reverse_loop_wraps_negative_phase() {
361 let mut c = AnimationController::new(1000).with_repeat(Repeat::Loop);
362 c.forward(0);
363 c.tick(500);
364 c.reverse(500);
365 match c.tick(1200) {
367 TickOutcome::Value(v) => assert!((v - 0.8).abs() < 1e-4, "phase -0.2 应回绕到 0.8,实际 {v}"),
368 other => panic!("expected Value, got {other:?}"),
369 }
370 assert!((c.value(1200) - 0.8).abs() < 1e-4, "value() 也应回绕");
371 }
372
373 #[test]
374 fn ping_pong_reverse_wraps() {
375 let mut c = AnimationController::new(1000).with_repeat(Repeat::PingPong);
376 c.forward(0);
377 c.tick(500);
378 c.reverse(500);
379 match c.tick(1200) {
381 TickOutcome::Value(v) => assert!((v - 0.2).abs() < 1e-4, "expected 0.2, got {v}"),
382 other => panic!("expected Value, got {other:?}"),
383 }
384 }
385}