1use std::fmt::{self, Debug};
4use std::marker::PhantomData;
5use std::rc::Rc;
6
7use waft_embedder::{Color, Offset, Rect, Size};
8use waft_foundation::{App, Handle, Listener, Ref};
9
10use crate::animation::{
11 Animation, AnimationRef, AnimationStatus, AnimationStatusListener, AnyAnimation,
12};
13use crate::curves::Curve;
14
15pub trait Animatable<T> {
23 fn transform(&self, app: &App, t: f64) -> T;
28
29 fn evaluate(&self, app: &App, animation: AnyAnimation<f64>) -> T
38 where
39 Self: Sized,
40 {
41 self.transform(app, animation.value(app))
42 }
43
44 #[track_caller]
48 fn animate(self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<T>
49 where
50 Self: Sized + Clone + 'static,
51 T: 'static,
52 {
53 app.create(AnimatedEvaluation {
54 parent,
55 evaluatable: self,
56 _value: PhantomData,
57 })
58 .into_erased()
59 }
60
61 fn chain<P>(self, parent: P) -> ChainedEvaluation<P, Self>
65 where
66 Self: Sized + Clone,
67 P: Animatable<f64> + Clone,
68 {
69 ChainedEvaluation {
70 parent,
71 evaluatable: self,
72 }
73 }
74}
75
76#[derive(Clone)]
78pub struct CallbackAnimatable<T> {
79 callback: Rc<dyn Fn(f64) -> T>,
80}
81
82impl<T> CallbackAnimatable<T> {
83 pub fn from_callback(callback: impl Fn(f64) -> T + 'static) -> CallbackAnimatable<T> {
85 CallbackAnimatable {
86 callback: Rc::new(callback),
87 }
88 }
89}
90
91impl<T> Animatable<T> for CallbackAnimatable<T> {
92 fn transform(&self, _app: &App, t: f64) -> T {
93 (self.callback)(t)
94 }
95}
96
97impl<T> Debug for CallbackAnimatable<T> {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 f.write_str("CallbackAnimatable")
100 }
101}
102
103struct AnimatedEvaluation<T, A> {
106 parent: AnimationRef<f64>,
107 evaluatable: A,
108 _value: PhantomData<fn() -> T>,
109}
110
111impl<T: 'static, A: Animatable<T> + Clone + 'static> Animation<T> for AnimatedEvaluation<T, A> {
112 fn add_listener(self: Handle<Self>, app: &mut App, listener: Listener) {
113 app.get(self).parent.handle().add_listener(app, listener);
114 }
115
116 fn remove_listener(self: Handle<Self>, app: &mut App, listener: &Listener) {
117 app.get(self).parent.handle().remove_listener(app, listener);
118 }
119
120 fn add_status_listener(self: Handle<Self>, app: &mut App, listener: AnimationStatusListener) {
121 app.get(self)
122 .parent
123 .handle()
124 .add_status_listener(app, listener);
125 }
126
127 fn remove_status_listener(
128 self: Handle<Self>,
129 app: &mut App,
130 listener: &AnimationStatusListener,
131 ) {
132 app.get(self)
133 .parent
134 .handle()
135 .remove_status_listener(app, listener);
136 }
137
138 fn status(self: Handle<Self>, app: &App) -> AnimationStatus {
139 app.get(self).parent.status(app)
140 }
141
142 fn value(self: Handle<Self>, app: &App) -> T {
143 let parent = app.get(self).parent.handle();
144 let evaluatable = app.get(self).evaluatable.clone();
145 evaluatable.evaluate(app, parent)
146 }
147}
148
149#[derive(Clone)]
151pub struct ChainedEvaluation<P, C> {
152 parent: P,
153 evaluatable: C,
154}
155
156impl<P: Animatable<f64>, C: Animatable<T>, T> Animatable<T> for ChainedEvaluation<P, C> {
157 fn transform(&self, app: &App, t: f64) -> T {
158 self.evaluatable
159 .transform(app, self.parent.transform(app, t))
160 }
161}
162
163impl<P: Debug, C: Debug> Debug for ChainedEvaluation<P, C> {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 write!(f, "{:?}\u{27A9}{:?}", self.parent, self.evaluatable)
166 }
167}
168
169pub trait TweenLerp: Clone {
175 fn lerp(begin: &Self, end: &Self, t: f64) -> Self;
177}
178
179impl TweenLerp for f64 {
180 fn lerp(begin: &f64, end: &f64, t: f64) -> f64 {
181 begin + (end - begin) * t
182 }
183}
184
185impl TweenLerp for Offset {
186 fn lerp(begin: &Offset, end: &Offset, t: f64) -> Offset {
187 *begin + (*end - *begin) * t
188 }
189}
190
191impl TweenLerp for Size {
192 fn lerp(begin: &Size, end: &Size, t: f64) -> Size {
193 *begin + (*end - *begin) * t
195 }
196}
197
198#[derive(Debug)]
206pub struct Tween<T> {
207 pub begin: Option<T>,
209
210 pub end: Option<T>,
212}
213
214impl<T: 'static> Tween<T> {
215 #[track_caller]
224 pub fn new(app: &mut App, begin: Option<T>, end: Option<T>) -> Ref<Tween<T>> {
225 app.create(Tween { begin, end })
226 }
227
228 pub fn begin(self: Handle<Self>, app: &App) -> Option<&T> {
230 app.get(self).begin.as_ref()
231 }
232
233 pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<T>) {
235 app.get_mut(self).begin = begin;
236 }
237
238 pub fn end(self: Handle<Self>, app: &App) -> Option<&T> {
240 app.get(self).end.as_ref()
241 }
242
243 pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<T>) {
245 app.get_mut(self).end = end;
246 }
247}
248
249fn tween_transform<T: Clone>(
250 begin: &Option<T>,
251 end: &Option<T>,
252 t: f64,
253 lerp: impl FnOnce(f64) -> T,
254) -> T {
255 if t == 0.0 {
256 return begin.clone().expect("Tween.begin must be set before use");
257 }
258 if t == 1.0 {
259 return end.clone().expect("Tween.end must be set before use");
260 }
261 lerp(t)
262}
263
264impl<T: TweenLerp + 'static> Tween<T> {
265 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> T {
267 let tween = app.get(self);
268 let begin = tween
269 .begin
270 .as_ref()
271 .expect("Tween.begin must be set before use");
272 let end = tween
273 .end
274 .as_ref()
275 .expect("Tween.end must be set before use");
276 T::lerp(begin, end, t)
277 }
278}
279
280impl<T: TweenLerp + 'static> Animatable<T> for Ref<Tween<T>> {
281 fn transform(&self, app: &App, t: f64) -> T {
282 let this = self.handle();
283 let tween = app.get(this);
284 tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
285 }
286}
287
288pub struct ReverseTween<T: 'static> {
292 parent: Ref<Tween<T>>,
293}
294
295impl<T: TweenLerp + 'static> ReverseTween<T> {
296 #[track_caller]
298 pub fn new(app: &mut App, parent: Ref<Tween<T>>) -> Ref<ReverseTween<T>> {
299 app.create(ReverseTween { parent })
300 }
301
302 pub fn parent(self: Handle<Self>, app: &App) -> Handle<Tween<T>> {
304 app.get(self).parent.handle()
305 }
306}
307
308impl<T: TweenLerp + 'static> Animatable<T> for Ref<ReverseTween<T>> {
309 fn transform(&self, app: &App, t: f64) -> T {
310 let this = self.handle();
311 this.parent(app).lerp(app, 1.0 - t)
312 }
313}
314
315pub struct ColorTween {
317 pub begin: Option<Color>,
319
320 pub end: Option<Color>,
322}
323
324impl ColorTween {
325 #[track_caller]
330 pub fn new(app: &mut App, begin: Option<Color>, end: Option<Color>) -> Ref<ColorTween> {
331 app.create(ColorTween { begin, end })
332 }
333
334 pub fn begin(self: Handle<Self>, app: &App) -> Option<Color> {
336 app.get(self).begin
337 }
338
339 pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<Color>) {
341 app.get_mut(self).begin = begin;
342 }
343
344 pub fn end(self: Handle<Self>, app: &App) -> Option<Color> {
346 app.get(self).end
347 }
348
349 pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<Color>) {
351 app.get_mut(self).end = end;
352 }
353
354 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Color> {
356 let tween = app.get(self);
357 Color::lerp(tween.begin, tween.end, t)
358 }
359}
360
361impl Animatable<Option<Color>> for Ref<ColorTween> {
362 fn transform(&self, app: &App, t: f64) -> Option<Color> {
363 let this = self.handle();
364 if t == 0.0 {
365 return this.begin(app);
366 }
367 if t == 1.0 {
368 return this.end(app);
369 }
370 this.lerp(app, t)
371 }
372}
373
374pub struct SizeTween {
376 pub begin: Option<Size>,
378
379 pub end: Option<Size>,
381}
382
383impl SizeTween {
384 #[track_caller]
386 pub fn new(app: &mut App, begin: Option<Size>, end: Option<Size>) -> Ref<SizeTween> {
387 app.create(SizeTween { begin, end })
388 }
389
390 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Size> {
392 let tween = app.get(self);
393 Size::lerp(tween.begin, tween.end, t)
394 }
395}
396
397impl Animatable<Option<Size>> for Ref<SizeTween> {
398 fn transform(&self, app: &App, t: f64) -> Option<Size> {
399 let this = self.handle();
400 let tween = app.get(this);
401 if t == 0.0 {
402 return tween.begin;
403 }
404 if t == 1.0 {
405 return tween.end;
406 }
407 this.lerp(app, t)
408 }
409}
410
411pub struct RectTween {
413 pub begin: Option<Rect>,
415
416 pub end: Option<Rect>,
418}
419
420impl RectTween {
421 #[track_caller]
423 pub fn new(app: &mut App, begin: Option<Rect>, end: Option<Rect>) -> Ref<RectTween> {
424 app.create(RectTween { begin, end })
425 }
426
427 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Rect> {
429 let tween = app.get(self);
430 Rect::lerp(tween.begin, tween.end, t)
431 }
432}
433
434impl Animatable<Option<Rect>> for Ref<RectTween> {
435 fn transform(&self, app: &App, t: f64) -> Option<Rect> {
436 let this = self.handle();
437 let tween = app.get(this);
438 if t == 0.0 {
439 return tween.begin;
440 }
441 if t == 1.0 {
442 return tween.end;
443 }
444 this.lerp(app, t)
445 }
446}
447
448pub struct IntTween {
450 pub begin: Option<i64>,
452
453 pub end: Option<i64>,
455}
456
457impl IntTween {
458 #[track_caller]
460 pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<IntTween> {
461 app.create(IntTween { begin, end })
462 }
463
464 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
466 let tween = app.get(self);
467 let begin = tween.begin.expect("IntTween.begin must be set before use");
468 let end = tween.end.expect("IntTween.end must be set before use");
469 (begin as f64 + (end - begin) as f64 * t).round() as i64
470 }
471}
472
473impl Animatable<i64> for Ref<IntTween> {
474 fn transform(&self, app: &App, t: f64) -> i64 {
475 let this = self.handle();
476 let tween = app.get(this);
477 tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
478 }
479}
480
481pub struct StepTween {
483 pub begin: Option<i64>,
485
486 pub end: Option<i64>,
488}
489
490impl StepTween {
491 #[track_caller]
493 pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<StepTween> {
494 app.create(StepTween { begin, end })
495 }
496
497 pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
499 let tween = app.get(self);
500 let begin = tween.begin.expect("StepTween.begin must be set before use");
501 let end = tween.end.expect("StepTween.end must be set before use");
502 (begin as f64 + (end - begin) as f64 * t).floor() as i64
503 }
504}
505
506impl Animatable<i64> for Ref<StepTween> {
507 fn transform(&self, app: &App, t: f64) -> i64 {
508 let this = self.handle();
509 let tween = app.get(this);
510 tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
511 }
512}
513
514pub struct ConstantTween<T> {
516 tween: Tween<T>,
519}
520
521impl<T: Clone + 'static> ConstantTween<T> {
522 #[track_caller]
524 pub fn new(app: &mut App, value: T) -> Ref<ConstantTween<T>> {
525 app.create(ConstantTween {
526 tween: Tween {
527 begin: Some(value.clone()),
528 end: Some(value),
529 },
530 })
531 }
532}
533
534impl<T: Clone + 'static> Animatable<T> for Ref<ConstantTween<T>> {
535 fn transform(&self, app: &App, _t: f64) -> T {
536 app.get(self)
537 .tween
538 .begin
539 .clone()
540 .expect("ConstantTween.begin must be set")
541 }
542}
543
544#[derive(Debug)]
546pub struct CurveTween {
547 pub curve: Rc<dyn Curve>,
549}
550
551impl CurveTween {
552 #[track_caller]
554 pub fn new(app: &mut App, curve: Rc<dyn Curve>) -> Ref<CurveTween> {
555 app.create(CurveTween { curve })
556 }
557
558 pub fn curve(self: Handle<Self>, app: &App) -> Rc<dyn Curve> {
560 Rc::clone(&app.get(self).curve)
561 }
562
563 pub fn set_curve(self: Handle<Self>, app: &mut App, curve: Rc<dyn Curve>) {
565 app.get_mut(self).curve = curve;
566 }
567}
568
569impl Animatable<f64> for Ref<CurveTween> {
570 fn transform(&self, app: &App, t: f64) -> f64 {
571 let curve = &app.get(self).curve;
572 if t == 0.0 || t == 1.0 {
573 debug_assert_eq!(curve.transform(t).round(), t);
574 return t;
575 }
576 curve.transform(t)
577 }
578}
579
580#[derive(Clone, Copy, Debug)]
588pub struct ValueTween<T> {
589 pub begin: T,
591
592 pub end: T,
594}
595
596impl<T> ValueTween<T> {
597 pub const fn new(begin: T, end: T) -> ValueTween<T> {
599 ValueTween { begin, end }
600 }
601}
602
603impl<T: TweenLerp> ValueTween<T> {
604 pub fn lerp(&self, t: f64) -> T {
606 T::lerp(&self.begin, &self.end, t)
607 }
608}
609
610impl<T: TweenLerp> Animatable<T> for ValueTween<T> {
611 fn transform(&self, _app: &App, t: f64) -> T {
612 if t == 0.0 {
613 return self.begin.clone();
614 }
615 if t == 1.0 {
616 return self.end.clone();
617 }
618 self.lerp(t)
619 }
620}
621
622#[derive(Clone, Copy, Debug)]
624pub struct ValueConstantTween<T> {
625 pub value: T,
627}
628
629impl<T> ValueConstantTween<T> {
630 pub const fn new(value: T) -> ValueConstantTween<T> {
632 ValueConstantTween { value }
633 }
634}
635
636impl<T: Clone> ValueConstantTween<T> {
637 pub fn lerp(&self, _t: f64) -> T {
639 self.value.clone()
640 }
641}
642
643impl<T: Clone> Animatable<T> for ValueConstantTween<T> {
644 fn transform(&self, _app: &App, t: f64) -> T {
645 self.lerp(t)
646 }
647}
648
649#[derive(Clone, Debug)]
651pub struct ValueCurveTween {
652 pub curve: Rc<dyn Curve>,
654}
655
656impl ValueCurveTween {
657 pub fn new(curve: Rc<dyn Curve>) -> ValueCurveTween {
659 ValueCurveTween { curve }
660 }
661}
662
663impl Animatable<f64> for ValueCurveTween {
664 fn transform(&self, _app: &App, t: f64) -> f64 {
665 if t == 0.0 || t == 1.0 {
666 debug_assert_eq!(self.curve.transform(t).round(), t);
667 return t;
668 }
669 self.curve.transform(t)
670 }
671}
672
673#[cfg(test)]
674mod tests {
675 use super::*;
676 use crate::animations::AlwaysStoppedAnimation;
677 use crate::curves::SawTooth;
678 use waft_foundation::AppCell;
679
680 #[test]
681 fn a_driven_tween_sees_end_assigned_after_animate() {
682 let cell = AppCell::new();
683 let mut app = cell.borrow_mut();
684 let tween = Tween::new(&mut app, Some(0.0), Some(1.0));
685 let parent: AnimationRef<f64> = app.create(AlwaysStoppedAnimation::new(1.0)).into_erased();
686 let driven = tween.clone().animate(&mut app, parent);
687
688 assert_eq!(driven.value(&app), 1.0);
689 tween.set_end(&mut app, Some(0.4));
690 assert_eq!(driven.value(&app), 0.4);
691 }
692
693 #[test]
694 fn lerp_walks_the_line() {
695 let cell = AppCell::new();
696 let mut app = cell.borrow_mut();
697 let tween = Tween::new(&mut app, Some(0.0), Some(10.0));
698 assert_eq!(tween.transform(&app, 0.0), 0.0);
699 assert_eq!(tween.transform(&app, 0.25), 2.5);
700 assert_eq!(tween.transform(&app, 1.0), 10.0);
701 }
702
703 #[test]
704 fn an_offset_tween_walks_both_axes() {
705 let cell = AppCell::new();
706 let mut app = cell.borrow_mut();
707 let tween = Tween::new(
708 &mut app,
709 Some(Offset::new(0.0, 10.0)),
710 Some(Offset::new(4.0, -10.0)),
711 );
712 assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
713 assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
714 assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
715 }
716
717 #[test]
718 fn a_size_tween_walks_both_dimensions() {
719 let cell = AppCell::new();
720 let mut app = cell.borrow_mut();
721 let tween = Tween::new(
722 &mut app,
723 Some(Size::new(10.0, 20.0)),
724 Some(Size::new(20.0, 0.0)),
725 );
726 assert_eq!(tween.transform(&app, 0.5), Size::new(15.0, 10.0));
727 }
728
729 #[test]
730 fn a_value_tween_walks_the_line_without_an_arena_entry() {
731 let cell = AppCell::new();
732 let app = cell.borrow();
733 let tween = ValueTween::new(Offset::new(0.0, 10.0), Offset::new(4.0, -10.0));
734 assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
735 assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
736 assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
737 }
738
739 #[test]
740 fn a_value_tween_chained_onto_a_value_curve_tween_reads_the_curved_input() {
741 let cell = AppCell::new();
742 let app = cell.borrow();
743 let tween =
744 ValueTween::new(0.0, 10.0).chain(ValueCurveTween::new(Rc::new(SawTooth::new(2))));
745 assert_eq!(tween.transform(&app, 0.25), 5.0);
746 assert_eq!(tween.transform(&app, 0.75), 5.0);
747 assert_eq!(ValueConstantTween::new(3.0).transform(&app, 0.5), 3.0);
748 }
749}