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
//! Collection of predefined lenses for common Bevy components and assets.
//!
//! # Predefined lenses
//!
//! This module contains predefined lenses for common use cases. Those lenses
//! are entirely optional. They can be used if they fit your use case, to save
//! some time, but are not treated any differently from a custom user-provided
//! lens.
//!
//! # Rotations
//!
//! Several rotation lenses are provided, with different properties.
//!
//! ## Shortest-path rotation
//!
//! The [`TransformRotationLens`] animates the [`rotation`] field of a
//! [`Transform`] component using [`Quat::slerp()`]. It inherits the properties
//! of that method, and in particular the fact it always finds the "shortest
//! path" from start to end. This is well suited for animating a rotation
//! between two given directions, but will provide unexpected results if you try
//! to make an entity rotate around a given axis for more than half a turn, as
//! [`Quat::slerp()`] will then try to move "the other way around".
//!
//! ## Angle-focused rotations
//!
//! Conversely, for cases where the rotation direction is important, like when
//! trying to do a full 360-degree turn, a series of angle-based interpolation
//! lenses is provided:
//! - [`TransformRotateXLens`]
//! - [`TransformRotateYLens`]
//! - [`TransformRotateZLens`]
//! - [`TransformRotateAxisLens`]
//!
//! [`rotation`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html#structfield.rotation
//! [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
//! [`Quat::slerp()`]: https://docs.rs/bevy/0.10.0/bevy/math/struct.Quat.html#method.slerp

use bevy::prelude::*;

/// A lens over a subset of a component.
///
/// The lens takes a `target` component or asset from a query, as a mutable
/// reference, and animates (tweens) a subset of the fields of the
/// component/asset based on the linear ratio `ratio` in \[0:1\], already
/// sampled from the easing curve.
///
/// # Example
///
/// Implement `Lens` for a custom type:
///
/// ```rust
/// # use bevy::prelude::*;
/// # use bevy_tweening::*;
/// struct MyLens {
///   start: f32,
///   end: f32,
/// }
///
/// #[derive(Component)]
/// struct MyStruct(f32);
///
/// impl Lens<MyStruct> for MyLens {
///   fn lerp(&mut self, target: &mut MyStruct, ratio: f32) {
///     target.0 = self.start + (self.end - self.start) * ratio;
///   }
/// }
/// ```
pub trait Lens<T> {
    /// Perform a linear interpolation (lerp) over the subset of fields of a
    /// component or asset the lens focuses on, based on the linear ratio
    /// `ratio`. The `target` component or asset is mutated in place. The
    /// implementation decides which fields are interpolated, and performs
    /// the animation in-place, overwriting the target.
    fn lerp(&mut self, target: &mut T, ratio: f32);
}

/// A lens to manipulate the [`color`] field of a section of a [`Text`]
/// component.
///
/// [`color`]: https://docs.rs/bevy/0.10.0/bevy/text/struct.TextStyle.html#structfield.color
/// [`Text`]: https://docs.rs/bevy/0.10.0/bevy/text/struct.Text.html
#[cfg(feature = "bevy_text")]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TextColorLens {
    /// Start color.
    pub start: Color,
    /// End color.
    pub end: Color,
    /// Index of the text section in the [`Text`] component.
    pub section: usize,
}

#[cfg(feature = "bevy_text")]
impl Lens<Text> for TextColorLens {
    fn lerp(&mut self, target: &mut Text, ratio: f32) {
        // Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
        // consistency.
        let start: Vec4 = self.start.into();
        let end: Vec4 = self.end.into();
        let value = start.lerp(end, ratio);
        target.sections[self.section].style.color = value.into();
    }
}

/// A lens to manipulate the [`translation`] field of a [`Transform`] component.
///
/// [`translation`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html#structfield.translation
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformPositionLens {
    /// Start value of the translation.
    pub start: Vec3,
    /// End value of the translation.
    pub end: Vec3,
}

impl Lens<Transform> for TransformPositionLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let value = self.start + (self.end - self.start) * ratio;
        target.translation = value;
    }
}

/// A lens to manipulate the [`rotation`] field of a [`Transform`] component.
///
/// This lens interpolates the [`rotation`] field of a [`Transform`] component
/// from a `start` value to an `end` value using the spherical linear
/// interpolation provided by [`Quat::slerp()`]. This means the rotation always
/// uses the shortest path from `start` to `end`. In particular, this means it
/// cannot make entities do a full 360 degrees turn. Instead use
/// [`TransformRotateXLens`] and similar to interpolate the rotation angle
/// around a given axis.
///
/// See the [top-level `lens` module documentation] for a comparison of rotation
/// lenses.
///
/// [`rotation`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html#structfield.rotation
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
/// [`Quat::slerp()`]: https://docs.rs/bevy/0.10.0/bevy/math/struct.Quat.html#method.slerp
/// [top-level `lens` module documentation]: crate::lens
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformRotationLens {
    /// Start value of the rotation.
    pub start: Quat,
    /// End value of the rotation.
    pub end: Quat,
}

impl Lens<Transform> for TransformRotationLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        target.rotation = self.start.slerp(self.end, ratio);
    }
}

/// A lens to rotate a [`Transform`] component around its local X axis.
///
/// This lens interpolates the rotation angle of a [`Transform`] component from
/// a `start` value to an `end` value, for a rotation around the X axis. Unlike
/// [`TransformRotationLens`], it can produce an animation that rotates the
/// entity any number of turns around its local X axis.
///
/// See the [top-level `lens` module documentation] for a comparison of rotation
/// lenses.
///
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
/// [top-level `lens` module documentation]: crate::lens
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformRotateXLens {
    /// Start value of the rotation angle, in radians.
    pub start: f32,
    /// End value of the rotation angle, in radians.
    pub end: f32,
}

impl Lens<Transform> for TransformRotateXLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let angle = (self.end - self.start).mul_add(ratio, self.start);
        target.rotation = Quat::from_rotation_x(angle);
    }
}

/// A lens to rotate a [`Transform`] component around its local Y axis.
///
/// This lens interpolates the rotation angle of a [`Transform`] component from
/// a `start` value to an `end` value, for a rotation around the Y axis. Unlike
/// [`TransformRotationLens`], it can produce an animation that rotates the
/// entity any number of turns around its local Y axis.
///
/// See the [top-level `lens` module documentation] for a comparison of rotation
/// lenses.
///
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
/// [top-level `lens` module documentation]: crate::lens
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformRotateYLens {
    /// Start value of the rotation angle, in radians.
    pub start: f32,
    /// End value of the rotation angle, in radians.
    pub end: f32,
}

impl Lens<Transform> for TransformRotateYLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let angle = (self.end - self.start).mul_add(ratio, self.start);
        target.rotation = Quat::from_rotation_y(angle);
    }
}

/// A lens to rotate a [`Transform`] component around its local Z axis.
///
/// This lens interpolates the rotation angle of a [`Transform`] component from
/// a `start` value to an `end` value, for a rotation around the Z axis. Unlike
/// [`TransformRotationLens`], it can produce an animation that rotates the
/// entity any number of turns around its local Z axis.
///
/// See the [top-level `lens` module documentation] for a comparison of rotation
/// lenses.
///
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
/// [top-level `lens` module documentation]: crate::lens
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformRotateZLens {
    /// Start value of the rotation angle, in radians.
    pub start: f32,
    /// End value of the rotation angle, in radians.
    pub end: f32,
}

impl Lens<Transform> for TransformRotateZLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let angle = (self.end - self.start).mul_add(ratio, self.start);
        target.rotation = Quat::from_rotation_z(angle);
    }
}

/// A lens to rotate a [`Transform`] component around a given fixed axis.
///
/// This lens interpolates the rotation angle of a [`Transform`] component from
/// a `start` value to an `end` value, for a rotation around a given axis.
/// Unlike [`TransformRotationLens`], it can produce an animation that rotates
/// the entity any number of turns around that axis.
///
/// See the [top-level `lens` module documentation] for a comparison of rotation
/// lenses.
///
/// # Panics
///
/// This method panics if the `axis` vector is not normalized.
///
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
/// [top-level `lens` module documentation]: crate::lens
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformRotateAxisLens {
    /// The normalized rotation axis.
    pub axis: Vec3,
    /// Start value of the rotation angle, in radians.
    pub start: f32,
    /// End value of the rotation angle, in radians.
    pub end: f32,
}

impl Lens<Transform> for TransformRotateAxisLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let angle = (self.end - self.start).mul_add(ratio, self.start);
        target.rotation = Quat::from_axis_angle(self.axis, angle);
    }
}

/// A lens to manipulate the [`scale`] field of a [`Transform`] component.
///
/// [`scale`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html#structfield.scale
/// [`Transform`]: https://docs.rs/bevy/0.10.0/bevy/transform/components/struct.Transform.html
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TransformScaleLens {
    /// Start value of the scale.
    pub start: Vec3,
    /// End value of the scale.
    pub end: Vec3,
}

impl Lens<Transform> for TransformScaleLens {
    fn lerp(&mut self, target: &mut Transform, ratio: f32) {
        let value = self.start + (self.end - self.start) * ratio;
        target.scale = value;
    }
}

/// A lens to manipulate the [`position`] field of a UI [`Style`] component.
///
/// [`position`]: https://docs.rs/bevy/0.10.0/bevy/ui/struct.Style.html#structfield.position
/// [`Style`]: https://docs.rs/bevy/0.10.0/bevy/ui/struct.Style.html
#[cfg(feature = "bevy_ui")]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct UiPositionLens {
    /// Start position.
    pub start: UiRect,
    /// End position.
    pub end: UiRect,
}

#[cfg(feature = "bevy_ui")]
fn lerp_val(start: &Val, end: &Val, ratio: f32) -> Val {
    match (start, end) {
        (Val::Percent(start), Val::Percent(end)) => {
            Val::Percent((end - start).mul_add(ratio, *start))
        }
        (Val::Px(start), Val::Px(end)) => Val::Px((end - start).mul_add(ratio, *start)),
        _ => *start,
    }
}

#[cfg(feature = "bevy_ui")]
impl Lens<Style> for UiPositionLens {
    fn lerp(&mut self, target: &mut Style, ratio: f32) {
        target.position = UiRect {
            left: lerp_val(&self.start.left, &self.end.left, ratio),
            right: lerp_val(&self.start.right, &self.end.right, ratio),
            top: lerp_val(&self.start.top, &self.end.top, ratio),
            bottom: lerp_val(&self.start.bottom, &self.end.bottom, ratio),
        };
    }
}

/// A lens to manipulate the [`color`] field of a [`ColorMaterial`] asset.
///
/// [`color`]: https://docs.rs/bevy/0.10.0/bevy/sprite/struct.ColorMaterial.html#structfield.color
/// [`ColorMaterial`]: https://docs.rs/bevy/0.10.0/bevy/sprite/struct.ColorMaterial.html
#[cfg(feature = "bevy_sprite")]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ColorMaterialColorLens {
    /// Start color.
    pub start: Color,
    /// End color.
    pub end: Color,
}

#[cfg(feature = "bevy_sprite")]
impl Lens<ColorMaterial> for ColorMaterialColorLens {
    fn lerp(&mut self, target: &mut ColorMaterial, ratio: f32) {
        // Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
        // consistency.
        let start: Vec4 = self.start.into();
        let end: Vec4 = self.end.into();
        let value = start.lerp(end, ratio);
        target.color = value.into();
    }
}

/// A lens to manipulate the [`color`] field of a [`Sprite`] asset.
///
/// [`color`]: https://docs.rs/bevy/0.10.0/bevy/sprite/struct.Sprite.html#structfield.color
/// [`Sprite`]: https://docs.rs/bevy/0.10.0/bevy/sprite/struct.Sprite.html
#[cfg(feature = "bevy_sprite")]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct SpriteColorLens {
    /// Start color.
    pub start: Color,
    /// End color.
    pub end: Color,
}

#[cfg(feature = "bevy_sprite")]
impl Lens<Sprite> for SpriteColorLens {
    fn lerp(&mut self, target: &mut Sprite, ratio: f32) {
        // Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
        // consistency.
        let start: Vec4 = self.start.into();
        let end: Vec4 = self.end.into();
        let value = start.lerp(end, ratio);
        target.color = value.into();
    }
}

#[cfg(test)]
mod tests {
    use std::f32::consts::TAU;

    use super::*;

    #[cfg(feature = "bevy_text")]
    #[test]
    fn text_color() {
        let mut lens = TextColorLens {
            start: Color::RED,
            end: Color::BLUE,
            section: 0,
        };
        let mut text = Text::from_section("", default());

        lens.lerp(&mut text, 0.);
        assert_eq!(text.sections[0].style.color, Color::RED);

        lens.lerp(&mut text, 1.);
        assert_eq!(text.sections[0].style.color, Color::BLUE);

        lens.lerp(&mut text, 0.3);
        assert_eq!(text.sections[0].style.color, Color::rgba(0.7, 0., 0.3, 1.0));
    }

    #[test]
    fn transform_position() {
        let mut lens = TransformPositionLens {
            start: Vec3::ZERO,
            end: Vec3::new(1., 2., -4.),
        };
        let mut transform = Transform::default();

        lens.lerp(&mut transform, 0.);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));

        lens.lerp(&mut transform, 1.);
        assert!(transform
            .translation
            .abs_diff_eq(Vec3::new(1., 2., -4.), 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));

        lens.lerp(&mut transform, 0.3);
        assert!(transform
            .translation
            .abs_diff_eq(Vec3::new(0.3, 0.6, -1.2), 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_rotation() {
        let mut lens = TransformRotationLens {
            start: Quat::IDENTITY,
            end: Quat::from_rotation_z(100_f32.to_radians()),
        };
        let mut transform = Transform::default();

        lens.lerp(&mut transform, 0.);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));

        lens.lerp(&mut transform, 1.);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_rotation_z(100_f32.to_radians()), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));

        lens.lerp(&mut transform, 0.3);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_rotation_z(30_f32.to_radians()), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_rotate_x() {
        let mut lens = TransformRotateXLens {
            start: 0.,
            end: 1440_f32.to_radians(), // 4 turns
        };
        let mut transform = Transform::default();

        for (index, ratio) in [0., 0.25, 0.5, 0.75, 1.].iter().enumerate() {
            lens.lerp(&mut transform, *ratio);
            assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
            if index == 1 || index == 3 {
                // For odd-numbered turns, the opposite Quat is produced. This is equivalent in
                // terms of rotation to the IDENTITY one, but numerically the w component is not
                // the same so would fail an equality test.
                assert!(transform
                    .rotation
                    .abs_diff_eq(Quat::from_xyzw(0., 0., 0., -1.), 1e-5));
            } else {
                assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
            }
            assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
        }

        lens.lerp(&mut transform, 0.1);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_rotation_x(0.1 * (4. * TAU)), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_rotate_y() {
        let mut lens = TransformRotateYLens {
            start: 0.,
            end: 1440_f32.to_radians(), // 4 turns
        };
        let mut transform = Transform::default();

        for (index, ratio) in [0., 0.25, 0.5, 0.75, 1.].iter().enumerate() {
            lens.lerp(&mut transform, *ratio);
            assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
            if index == 1 || index == 3 {
                // For odd-numbered turns, the opposite Quat is produced. This is equivalent in
                // terms of rotation to the IDENTITY one, but numerically the w component is not
                // the same so would fail an equality test.
                assert!(transform
                    .rotation
                    .abs_diff_eq(Quat::from_xyzw(0., 0., 0., -1.), 1e-5));
            } else {
                assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
            }
            assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
        }

        lens.lerp(&mut transform, 0.1);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_rotation_y(0.1 * (4. * TAU)), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_rotate_z() {
        let mut lens = TransformRotateZLens {
            start: 0.,
            end: 1440_f32.to_radians(), // 4 turns
        };
        let mut transform = Transform::default();

        for (index, ratio) in [0., 0.25, 0.5, 0.75, 1.].iter().enumerate() {
            lens.lerp(&mut transform, *ratio);
            assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
            if index == 1 || index == 3 {
                // For odd-numbered turns, the opposite Quat is produced. This is equivalent in
                // terms of rotation to the IDENTITY one, but numerically the w component is not
                // the same so would fail an equality test.
                assert!(transform
                    .rotation
                    .abs_diff_eq(Quat::from_xyzw(0., 0., 0., -1.), 1e-5));
            } else {
                assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
            }
            assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
        }

        lens.lerp(&mut transform, 0.1);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_rotation_z(0.1 * (4. * TAU)), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_rotate_axis() {
        let axis = Vec3::ONE.normalize();
        let mut lens = TransformRotateAxisLens {
            axis,
            start: 0.,
            end: 1440_f32.to_radians(), // 4 turns
        };
        let mut transform = Transform::default();

        for (index, ratio) in [0., 0.25, 0.5, 0.75, 1.].iter().enumerate() {
            lens.lerp(&mut transform, *ratio);
            assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
            if index == 1 || index == 3 {
                // For odd-numbered turns, the opposite Quat is produced. This is equivalent in
                // terms of rotation to the IDENTITY one, but numerically the w component is not
                // the same so would fail an equality test.
                assert!(transform
                    .rotation
                    .abs_diff_eq(Quat::from_xyzw(0., 0., 0., -1.), 1e-5));
            } else {
                assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
            }
            assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
        }

        lens.lerp(&mut transform, 0.1);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform
            .rotation
            .abs_diff_eq(Quat::from_axis_angle(axis, 0.1 * (4. * TAU)), 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ONE, 1e-5));
    }

    #[test]
    fn transform_scale() {
        let mut lens = TransformScaleLens {
            start: Vec3::ZERO,
            end: Vec3::new(1., 2., -4.),
        };
        let mut transform = Transform::default();

        lens.lerp(&mut transform, 0.);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::ZERO, 1e-5));

        lens.lerp(&mut transform, 1.);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::new(1., 2., -4.), 1e-5));

        lens.lerp(&mut transform, 0.3);
        assert!(transform.translation.abs_diff_eq(Vec3::ZERO, 1e-5));
        assert!(transform.rotation.abs_diff_eq(Quat::IDENTITY, 1e-5));
        assert!(transform.scale.abs_diff_eq(Vec3::new(0.3, 0.6, -1.2), 1e-5));
    }

    #[cfg(feature = "bevy_ui")]
    #[test]
    fn ui_position() {
        let mut lens = UiPositionLens {
            start: UiRect {
                left: Val::Px(0.),
                top: Val::Px(0.),
                right: Val::Auto,
                bottom: Val::Percent(25.),
            },
            end: UiRect {
                left: Val::Px(1.),
                top: Val::Px(5.),
                right: Val::Auto,
                bottom: Val::Percent(45.),
            },
        };
        let mut style = Style::default();

        lens.lerp(&mut style, 0.);
        assert_eq!(style.position.left, Val::Px(0.));
        assert_eq!(style.position.top, Val::Px(0.));
        assert_eq!(style.position.right, Val::Auto);
        assert_eq!(style.position.bottom, Val::Percent(25.));

        lens.lerp(&mut style, 1.);
        assert_eq!(style.position.left, Val::Px(1.));
        assert_eq!(style.position.top, Val::Px(5.));
        assert_eq!(style.position.right, Val::Auto);
        assert_eq!(style.position.bottom, Val::Percent(45.));

        lens.lerp(&mut style, 0.3);
        assert_eq!(style.position.left, Val::Px(0.3));
        assert_eq!(style.position.top, Val::Px(1.5));
        assert_eq!(style.position.right, Val::Auto);
        assert_eq!(style.position.bottom, Val::Percent(31.));
    }

    #[cfg(feature = "bevy_sprite")]
    #[test]
    fn colormaterial_color() {
        let mut lens = ColorMaterialColorLens {
            start: Color::RED,
            end: Color::BLUE,
        };
        let mut mat = ColorMaterial {
            color: Color::WHITE,
            texture: None,
        };

        lens.lerp(&mut mat, 0.);
        assert_eq!(mat.color, Color::RED);

        lens.lerp(&mut mat, 1.);
        assert_eq!(mat.color, Color::BLUE);

        lens.lerp(&mut mat, 0.3);
        assert_eq!(mat.color, Color::rgba(0.7, 0., 0.3, 1.0));
    }

    #[cfg(feature = "bevy_sprite")]
    #[test]
    fn sprite_color() {
        let mut lens = SpriteColorLens {
            start: Color::RED,
            end: Color::BLUE,
        };
        let mut sprite = Sprite {
            color: Color::WHITE,
            ..default()
        };

        lens.lerp(&mut sprite, 0.);
        assert_eq!(sprite.color, Color::RED);

        lens.lerp(&mut sprite, 1.);
        assert_eq!(sprite.color, Color::BLUE);

        lens.lerp(&mut sprite, 0.3);
        assert_eq!(sprite.color, Color::rgba(0.7, 0., 0.3, 1.0));
    }
}