waft-animation 0.18.2

Animation controllers and curves for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! Flutter counterpart: `animation/tween.dart`.

use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::rc::Rc;

use waft_embedder::{Color, Offset, Rect, Size};
use waft_foundation::{App, Handle, Listener, Ref};

use crate::animation::{
    Animation, AnimationRef, AnimationStatus, AnimationStatusListener, AnyAnimation,
};
use crate::curves::Curve;

/// An object that can produce a value of type `T` given an `Animation<double>`
/// as input.
///
/// Typically, the values of the input animation are nominally in the range 0.0
/// to 1.0. In principle, however, any value could be provided.
///
/// The main implementor of [`Animatable`] is [`Tween`].
pub trait Animatable<T> {
    /// Returns the value of the object at point `t`.
    ///
    /// The value of `t` is nominally a fraction in the range 0.0 to 1.0,
    /// though in practice it may extend outside this range.
    fn transform(&self, app: &App, t: f64) -> T;

    /// The current value of this object for the given animation.
    ///
    /// This function is implemented by deferring to [`transform`]. Implementors
    /// that want to provide custom behavior should override [`transform`], not
    /// [`evaluate`].
    ///
    /// [`transform`]: Animatable::transform
    /// [`evaluate`]: Animatable::evaluate
    fn evaluate(&self, app: &App, animation: AnyAnimation<f64>) -> T
    where
        Self: Sized,
    {
        self.transform(app, animation.value(app))
    }

    /// Returns a new animation that is driven by the given animation but
    /// that takes on values determined by this object. The new animation keeps
    /// `parent`; whoever keeps the result keeps both.
    #[track_caller]
    fn animate(self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<T>
    where
        Self: Sized + Clone + 'static,
        T: 'static,
    {
        app.create(AnimatedEvaluation {
            parent,
            evaluatable: self,
            _value: PhantomData,
        })
        .into_erased()
    }

    /// Returns a new [`Animatable`] whose value is determined by first
    /// evaluating the given parent and then evaluating this object at the
    /// result.
    fn chain<P>(self, parent: P) -> ChainedEvaluation<P, Self>
    where
        Self: Sized + Clone,
        P: Animatable<f64> + Clone,
    {
        ChainedEvaluation {
            parent,
            evaluatable: self,
        }
    }
}

/// Dart's `_CallbackAnimatable<T>`.
#[derive(Clone)]
pub struct CallbackAnimatable<T> {
    callback: Rc<dyn Fn(f64) -> T>,
}

impl<T> CallbackAnimatable<T> {
    /// Dart's `Animatable.fromCallback`.
    pub fn from_callback(callback: impl Fn(f64) -> T + 'static) -> CallbackAnimatable<T> {
        CallbackAnimatable {
            callback: Rc::new(callback),
        }
    }
}

impl<T> Animatable<T> for CallbackAnimatable<T> {
    fn transform(&self, _app: &App, t: f64) -> T {
        (self.callback)(t)
    }
}

impl<T> Debug for CallbackAnimatable<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("CallbackAnimatable")
    }
}

/// Dart's `_AnimatedEvaluation<T>` — the animation returned by
/// [`Animatable::animate`].
struct AnimatedEvaluation<T, A> {
    parent: AnimationRef<f64>,
    evaluatable: A,
    _value: PhantomData<fn() -> T>,
}

impl<T: 'static, A: Animatable<T> + Clone + 'static> Animation<T> for AnimatedEvaluation<T, A> {
    fn add_listener(self: Handle<Self>, app: &mut App, listener: Listener) {
        app.get(self).parent.handle().add_listener(app, listener);
    }

    fn remove_listener(self: Handle<Self>, app: &mut App, listener: &Listener) {
        app.get(self).parent.handle().remove_listener(app, listener);
    }

    fn add_status_listener(self: Handle<Self>, app: &mut App, listener: AnimationStatusListener) {
        app.get(self)
            .parent
            .handle()
            .add_status_listener(app, listener);
    }

    fn remove_status_listener(
        self: Handle<Self>,
        app: &mut App,
        listener: &AnimationStatusListener,
    ) {
        app.get(self)
            .parent
            .handle()
            .remove_status_listener(app, listener);
    }

    fn status(self: Handle<Self>, app: &App) -> AnimationStatus {
        app.get(self).parent.status(app)
    }

    fn value(self: Handle<Self>, app: &App) -> T {
        let parent = app.get(self).parent.handle();
        let evaluatable = app.get(self).evaluatable.clone();
        evaluatable.evaluate(app, parent)
    }
}

/// Dart's `_ChainedEvaluation<T>`.
#[derive(Clone)]
pub struct ChainedEvaluation<P, C> {
    parent: P,
    evaluatable: C,
}

impl<P: Animatable<f64>, C: Animatable<T>, T> Animatable<T> for ChainedEvaluation<P, C> {
    fn transform(&self, app: &App, t: f64) -> T {
        self.evaluatable
            .transform(app, self.parent.transform(app, t))
    }
}

impl<P: Debug, C: Debug> Debug for ChainedEvaluation<P, C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}\u{27A9}{:?}", self.parent, self.evaluatable)
    }
}

/// Types that [`Tween<T>`] can interpolate with `+`, `-`, and `*`.
///
/// Dart's `Tween.lerp` is `begin + (end - begin) * t` on `dynamic`, and its assert rejects a
/// type that does not carry the three operators (`Rect` is the type it names: use
/// [`RectTween`]). This trait is the set of types that do.
pub trait TweenLerp: Clone {
    /// Dart's `Tween.lerp`.
    fn lerp(begin: &Self, end: &Self, t: f64) -> Self;
}

impl TweenLerp for f64 {
    fn lerp(begin: &f64, end: &f64, t: f64) -> f64 {
        begin + (end - begin) * t
    }
}

impl TweenLerp for Offset {
    fn lerp(begin: &Offset, end: &Offset, t: f64) -> Offset {
        *begin + (*end - *begin) * t
    }
}

impl TweenLerp for Size {
    fn lerp(begin: &Size, end: &Size, t: f64) -> Size {
        // Dart: `Size - Size` is an `Offset`, and `Size + Offset` is a `Size`.
        *begin + (*end - *begin) * t
    }
}

/// A linear interpolation between a beginning and ending value.
///
/// Tweens are mutable: [`set_begin`] / [`set_end`] after [`Animatable::animate`]
/// is visible to the driven animation, as in Dart.
///
/// [`set_begin`]: Tween::set_begin
/// [`set_end`]: Tween::set_end
#[derive(Debug)]
pub struct Tween<T> {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<T>,

    /// The value this variable has at the end of the animation.
    pub end: Option<T>,
}

impl<T: 'static> Tween<T> {
    /// Creates a tween.
    ///
    /// The [`begin`] and [`end`] properties must be non-null before the tween is
    /// first used, but the arguments can be null if the values are going to be
    /// filled in later.
    ///
    /// [`begin`]: Tween::begin
    /// [`end`]: Tween::end
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<T>, end: Option<T>) -> Ref<Tween<T>> {
        app.create(Tween { begin, end })
    }

    /// The value this variable has at the beginning of the animation.
    pub fn begin(self: Handle<Self>, app: &App) -> Option<&T> {
        app.get(self).begin.as_ref()
    }

    /// Dart's `begin` setter.
    pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<T>) {
        app.get_mut(self).begin = begin;
    }

    /// The value this variable has at the end of the animation.
    pub fn end(self: Handle<Self>, app: &App) -> Option<&T> {
        app.get(self).end.as_ref()
    }

    /// Dart's `end` setter.
    pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<T>) {
        app.get_mut(self).end = end;
    }
}

fn tween_transform<T: Clone>(
    begin: &Option<T>,
    end: &Option<T>,
    t: f64,
    lerp: impl FnOnce(f64) -> T,
) -> T {
    if t == 0.0 {
        return begin.clone().expect("Tween.begin must be set before use");
    }
    if t == 1.0 {
        return end.clone().expect("Tween.end must be set before use");
    }
    lerp(t)
}

impl<T: TweenLerp + 'static> Tween<T> {
    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> T {
        let tween = app.get(self);
        let begin = tween
            .begin
            .as_ref()
            .expect("Tween.begin must be set before use");
        let end = tween
            .end
            .as_ref()
            .expect("Tween.end must be set before use");
        T::lerp(begin, end, t)
    }
}

impl<T: TweenLerp + 'static> Animatable<T> for Ref<Tween<T>> {
    fn transform(&self, app: &App, t: f64) -> T {
        let this = self.handle();
        let tween = app.get(this);
        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
    }
}

/// A [`Tween`] that evaluates its [`parent`] in reverse.
///
/// [`parent`]: ReverseTween::parent
pub struct ReverseTween<T: 'static> {
    parent: Ref<Tween<T>>,
}

impl<T: TweenLerp + 'static> ReverseTween<T> {
    /// Construct a [`Tween`] that evaluates its parent in reverse.
    #[track_caller]
    pub fn new(app: &mut App, parent: Ref<Tween<T>>) -> Ref<ReverseTween<T>> {
        app.create(ReverseTween { parent })
    }

    /// This tween's value is the same as the parent's value evaluated in reverse.
    pub fn parent(self: Handle<Self>, app: &App) -> Handle<Tween<T>> {
        app.get(self).parent.handle()
    }
}

impl<T: TweenLerp + 'static> Animatable<T> for Ref<ReverseTween<T>> {
    fn transform(&self, app: &App, t: f64) -> T {
        let this = self.handle();
        this.parent(app).lerp(app, 1.0 - t)
    }
}

/// An interpolation between two colors.
pub struct ColorTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<Color>,

    /// The value this variable has at the end of the animation.
    pub end: Option<Color>,
}

impl ColorTween {
    /// Creates a [`Color`] tween.
    ///
    /// The begin and end properties may be null; the null value is treated as
    /// transparent.
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<Color>, end: Option<Color>) -> Ref<ColorTween> {
        app.create(ColorTween { begin, end })
    }

    /// The value this variable has at the beginning of the animation.
    pub fn begin(self: Handle<Self>, app: &App) -> Option<Color> {
        app.get(self).begin
    }

    /// Dart's `begin` setter.
    pub fn set_begin(self: Handle<Self>, app: &mut App, begin: Option<Color>) {
        app.get_mut(self).begin = begin;
    }

    /// The value this variable has at the end of the animation.
    pub fn end(self: Handle<Self>, app: &App) -> Option<Color> {
        app.get(self).end
    }

    /// Dart's `end` setter.
    pub fn set_end(self: Handle<Self>, app: &mut App, end: Option<Color>) {
        app.get_mut(self).end = end;
    }

    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Color> {
        let tween = app.get(self);
        Color::lerp(tween.begin, tween.end, t)
    }
}

impl Animatable<Option<Color>> for Ref<ColorTween> {
    fn transform(&self, app: &App, t: f64) -> Option<Color> {
        let this = self.handle();
        if t == 0.0 {
            return this.begin(app);
        }
        if t == 1.0 {
            return this.end(app);
        }
        this.lerp(app, t)
    }
}

/// An interpolation between two sizes.
pub struct SizeTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<Size>,

    /// The value this variable has at the end of the animation.
    pub end: Option<Size>,
}

impl SizeTween {
    /// Creates a [`Size`] tween.
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<Size>, end: Option<Size>) -> Ref<SizeTween> {
        app.create(SizeTween { begin, end })
    }

    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Size> {
        let tween = app.get(self);
        Size::lerp(tween.begin, tween.end, t)
    }
}

impl Animatable<Option<Size>> for Ref<SizeTween> {
    fn transform(&self, app: &App, t: f64) -> Option<Size> {
        let this = self.handle();
        let tween = app.get(this);
        if t == 0.0 {
            return tween.begin;
        }
        if t == 1.0 {
            return tween.end;
        }
        this.lerp(app, t)
    }
}

/// An interpolation between two rectangles.
pub struct RectTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<Rect>,

    /// The value this variable has at the end of the animation.
    pub end: Option<Rect>,
}

impl RectTween {
    /// Creates a [`Rect`] tween.
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<Rect>, end: Option<Rect>) -> Ref<RectTween> {
        app.create(RectTween { begin, end })
    }

    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> Option<Rect> {
        let tween = app.get(self);
        Rect::lerp(tween.begin, tween.end, t)
    }
}

impl Animatable<Option<Rect>> for Ref<RectTween> {
    fn transform(&self, app: &App, t: f64) -> Option<Rect> {
        let this = self.handle();
        let tween = app.get(this);
        if t == 0.0 {
            return tween.begin;
        }
        if t == 1.0 {
            return tween.end;
        }
        this.lerp(app, t)
    }
}

/// An interpolation between two integers that rounds.
pub struct IntTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<i64>,

    /// The value this variable has at the end of the animation.
    pub end: Option<i64>,
}

impl IntTween {
    /// Creates an int tween.
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<IntTween> {
        app.create(IntTween { begin, end })
    }

    /// Returns the interpolated integer, rounded.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
        let tween = app.get(self);
        let begin = tween.begin.expect("IntTween.begin must be set before use");
        let end = tween.end.expect("IntTween.end must be set before use");
        (begin as f64 + (end - begin) as f64 * t).round() as i64
    }
}

impl Animatable<i64> for Ref<IntTween> {
    fn transform(&self, app: &App, t: f64) -> i64 {
        let this = self.handle();
        let tween = app.get(this);
        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
    }
}

/// An interpolation between two integers that floors.
pub struct StepTween {
    /// The value this variable has at the beginning of the animation.
    pub begin: Option<i64>,

    /// The value this variable has at the end of the animation.
    pub end: Option<i64>,
}

impl StepTween {
    /// Creates an [`i64`] tween that floors.
    #[track_caller]
    pub fn new(app: &mut App, begin: Option<i64>, end: Option<i64>) -> Ref<StepTween> {
        app.create(StepTween { begin, end })
    }

    /// Returns the interpolated integer, floored.
    pub fn lerp(self: Handle<Self>, app: &App, t: f64) -> i64 {
        let tween = app.get(self);
        let begin = tween.begin.expect("StepTween.begin must be set before use");
        let end = tween.end.expect("StepTween.end must be set before use");
        (begin as f64 + (end - begin) as f64 * t).floor() as i64
    }
}

impl Animatable<i64> for Ref<StepTween> {
    fn transform(&self, app: &App, t: f64) -> i64 {
        let this = self.handle();
        let tween = app.get(this);
        tween_transform(&tween.begin, &tween.end, t, |t| this.lerp(app, t))
    }
}

/// A tween with a constant value.
pub struct ConstantTween<T> {
    // Dart: `class ConstantTween<T> extends Tween<T>` — the superclass's
    // fields, with `begin` and `end` both set to the value.
    tween: Tween<T>,
}

impl<T: Clone + 'static> ConstantTween<T> {
    /// Create a tween whose begin and end values equal `value`.
    #[track_caller]
    pub fn new(app: &mut App, value: T) -> Ref<ConstantTween<T>> {
        app.create(ConstantTween {
            tween: Tween {
                begin: Some(value.clone()),
                end: Some(value),
            },
        })
    }
}

impl<T: Clone + 'static> Animatable<T> for Ref<ConstantTween<T>> {
    fn transform(&self, app: &App, _t: f64) -> T {
        app.get(self)
            .tween
            .begin
            .clone()
            .expect("ConstantTween.begin must be set")
    }
}

/// Transforms the value of the given animation by the given curve.
#[derive(Debug)]
pub struct CurveTween {
    /// The curve to use when transforming the value of the animation.
    pub curve: Rc<dyn Curve>,
}

impl CurveTween {
    /// Creates a curve tween.
    #[track_caller]
    pub fn new(app: &mut App, curve: Rc<dyn Curve>) -> Ref<CurveTween> {
        app.create(CurveTween { curve })
    }

    /// The curve to use when transforming the value of the animation.
    pub fn curve(self: Handle<Self>, app: &App) -> Rc<dyn Curve> {
        Rc::clone(&app.get(self).curve)
    }

    /// Dart's `curve` setter.
    pub fn set_curve(self: Handle<Self>, app: &mut App, curve: Rc<dyn Curve>) {
        app.get_mut(self).curve = curve;
    }
}

impl Animatable<f64> for Ref<CurveTween> {
    fn transform(&self, app: &App, t: f64) -> f64 {
        let curve = &app.get(self).curve;
        if t == 0.0 || t == 1.0 {
            debug_assert_eq!(curve.transform(t).round(), t);
            return t;
        }
        curve.transform(t)
    }
}

/// Dart's `Tween<T>` held as a value rather than an arena object: for a tween whose ends
/// are fixed once it is made, as a Dart `static final` or `const` tween's are, or one a build
/// makes and drops.
///
/// A driven animation holds a copy, so nothing frees it and nothing can change its ends; a
/// tween whose `begin` or `end` is written after it is driven is a [`Tween`]
/// (`patterns/value-tweens.md` in the porting skill).
#[derive(Clone, Copy, Debug)]
pub struct ValueTween<T> {
    /// The value this variable has at the beginning of the animation.
    pub begin: T,

    /// The value this variable has at the end of the animation.
    pub end: T,
}

impl<T> ValueTween<T> {
    /// Creates a tween from `begin` to `end`.
    pub const fn new(begin: T, end: T) -> ValueTween<T> {
        ValueTween { begin, end }
    }
}

impl<T: TweenLerp> ValueTween<T> {
    /// Returns the value this variable has at the given animation clock value.
    pub fn lerp(&self, t: f64) -> T {
        T::lerp(&self.begin, &self.end, t)
    }
}

impl<T: TweenLerp> Animatable<T> for ValueTween<T> {
    fn transform(&self, _app: &App, t: f64) -> T {
        if t == 0.0 {
            return self.begin.clone();
        }
        if t == 1.0 {
            return self.end.clone();
        }
        self.lerp(t)
    }
}

/// Dart's `ConstantTween<T>` held as a value, for the tweens [`ValueTween`] is for.
#[derive(Clone, Copy, Debug)]
pub struct ValueConstantTween<T> {
    /// The value at every point of the animation: Dart's `begin` and `end`.
    pub value: T,
}

impl<T> ValueConstantTween<T> {
    /// Create a tween whose begin and end values equal `value`.
    pub const fn new(value: T) -> ValueConstantTween<T> {
        ValueConstantTween { value }
    }
}

impl<T: Clone> ValueConstantTween<T> {
    /// Dart's `ConstantTween.lerp`: the value, whatever `t` is.
    pub fn lerp(&self, _t: f64) -> T {
        self.value.clone()
    }
}

impl<T: Clone> Animatable<T> for ValueConstantTween<T> {
    fn transform(&self, _app: &App, t: f64) -> T {
        self.lerp(t)
    }
}

/// Dart's `CurveTween` held as a value, for the tweens [`ValueTween`] is for.
#[derive(Clone, Debug)]
pub struct ValueCurveTween {
    /// The curve to use when transforming the value of the animation.
    pub curve: Rc<dyn Curve>,
}

impl ValueCurveTween {
    /// Creates a curve tween.
    pub fn new(curve: Rc<dyn Curve>) -> ValueCurveTween {
        ValueCurveTween { curve }
    }
}

impl Animatable<f64> for ValueCurveTween {
    fn transform(&self, _app: &App, t: f64) -> f64 {
        if t == 0.0 || t == 1.0 {
            debug_assert_eq!(self.curve.transform(t).round(), t);
            return t;
        }
        self.curve.transform(t)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::animations::AlwaysStoppedAnimation;
    use crate::curves::SawTooth;
    use waft_foundation::AppCell;

    #[test]
    fn a_driven_tween_sees_end_assigned_after_animate() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let tween = Tween::new(&mut app, Some(0.0), Some(1.0));
        let parent: AnimationRef<f64> = app.create(AlwaysStoppedAnimation::new(1.0)).into_erased();
        let driven = tween.clone().animate(&mut app, parent);

        assert_eq!(driven.value(&app), 1.0);
        tween.set_end(&mut app, Some(0.4));
        assert_eq!(driven.value(&app), 0.4);
    }

    #[test]
    fn lerp_walks_the_line() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let tween = Tween::new(&mut app, Some(0.0), Some(10.0));
        assert_eq!(tween.transform(&app, 0.0), 0.0);
        assert_eq!(tween.transform(&app, 0.25), 2.5);
        assert_eq!(tween.transform(&app, 1.0), 10.0);
    }

    #[test]
    fn an_offset_tween_walks_both_axes() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let tween = Tween::new(
            &mut app,
            Some(Offset::new(0.0, 10.0)),
            Some(Offset::new(4.0, -10.0)),
        );
        assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
        assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
        assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
    }

    #[test]
    fn a_size_tween_walks_both_dimensions() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let tween = Tween::new(
            &mut app,
            Some(Size::new(10.0, 20.0)),
            Some(Size::new(20.0, 0.0)),
        );
        assert_eq!(tween.transform(&app, 0.5), Size::new(15.0, 10.0));
    }

    #[test]
    fn a_value_tween_walks_the_line_without_an_arena_entry() {
        let cell = AppCell::new();
        let app = cell.borrow();
        let tween = ValueTween::new(Offset::new(0.0, 10.0), Offset::new(4.0, -10.0));
        assert_eq!(tween.transform(&app, 0.0), Offset::new(0.0, 10.0));
        assert_eq!(tween.transform(&app, 0.25), Offset::new(1.0, 5.0));
        assert_eq!(tween.transform(&app, 1.0), Offset::new(4.0, -10.0));
    }

    #[test]
    fn a_value_tween_chained_onto_a_value_curve_tween_reads_the_curved_input() {
        let cell = AppCell::new();
        let app = cell.borrow();
        let tween =
            ValueTween::new(0.0, 10.0).chain(ValueCurveTween::new(Rc::new(SawTooth::new(2))));
        assert_eq!(tween.transform(&app, 0.25), 5.0);
        assert_eq!(tween.transform(&app, 0.75), 5.0);
        assert_eq!(ValueConstantTween::new(3.0).transform(&app, 0.5), 3.0);
    }
}