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
use crate::{Tween, TweenValue};

/// A [Looper] is a wrapper around a [Tween], which makes it so that
/// every time the tweener *would* fuse (end), it loops from the start.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[repr(transparent)]
pub struct Looper<T: ?Sized>(pub T);

impl<T> Looper<T> {
    /// Creates a new Looper around a [Tween].
    pub fn new(tween: T) -> Self {
        Self(tween)
    }
}

impl<Value, T> Tween<Value> for Looper<T>
where
    Value: TweenValue,
    T: Tween<Value>,
{
    #[inline(always)]
    fn tween(&mut self, value_delta: Value, mut percent: f32) -> Value {
        if percent == 0.0 {
            return self.0.tween(value_delta, percent);
        }

        percent %= 1.0;
        if percent == 0.0 {
            percent = 1.0
        }

        // pass through to the underlying tween
        self.0.tween(value_delta, percent)
    }

    fn is_finite(&self) -> bool {
        false
    }
}

#[cfg(test)]
mod tests {
    use crate::{FixedTweener, Linear, Tweener};

    use super::*;

    #[test]
    fn tweener_loop() {
        let mut looper = Tweener::new(0, 2, 2, Looper::new(Linear));

        assert_eq!(looper.move_to(0), 0);
        assert_eq!(looper.move_to(1), 1);
        assert_eq!(looper.move_to(2), 2);
        assert_eq!(looper.move_to(3), 1);
        assert_eq!(looper.move_to(4), 2);
        assert_eq!(looper.move_to(5), 1);
        assert_eq!(looper.move_to(6), 2);
    }

    #[test]
    fn delta_tweener_loop() {
        let mut looper = Tweener::new(0, 2, 2, Looper::new(Linear));

        assert_eq!(looper.move_by(1), 1);
        assert_eq!(looper.move_by(1), 2);
        assert_eq!(looper.move_by(1), 1);
        assert_eq!(looper.move_by(1), 2);
        assert_eq!(looper.move_by(1), 1);
        assert_eq!(looper.move_by(1), 2);
    }

    #[test]
    fn fixed_delta_tweener_loop() {
        let mut looper = FixedTweener::new(0, 2, 2, Looper::new(Linear), 1);

        assert_eq!(looper.next().unwrap(), 1);
        assert_eq!(looper.next().unwrap(), 2);
        assert_eq!(looper.next().unwrap(), 1);
        assert_eq!(looper.next().unwrap(), 2);
        assert_eq!(looper.next().unwrap(), 1);
        assert_eq!(looper.next().unwrap(), 2);
    }

    #[test]
    fn tweener_loop_frac() {
        let mut looper = Tweener::new(0, 2, 0.5, Looper::new(Linear));

        assert_eq!(looper.move_by(0.25), 1);
        assert_eq!(looper.move_by(0.25), 2);
        assert_eq!(looper.move_by(0.25), 1);
        assert_eq!(looper.move_by(0.25), 2);
        assert_eq!(looper.move_by(0.25), 1);
    }

    #[test]
    fn tweener_big_loop() {
        let mut looper = Tweener::new(0, 2, 2, Looper::new(Linear));

        assert_eq!(looper.move_by(3), 1);
        assert_eq!(looper.move_by(4), 1);
        assert_eq!(looper.move_by(4), 1);
        assert_eq!(looper.move_by(5), 2);
    }

    #[test]
    fn type_test() {
        let mut _looper: FixedTweener<i32, i32, Looper<Linear>> = FixedTweener::new(0, 2, 2, Looper::new(Linear), 2);
    }
}