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
use core::ops::{Add, Div, Mul, Sub};

use num_traits::{float::FloatCore, One};

use crate::{Anim, Fun};

/// An `Anim` together with the duration that it is intended to be played for.
///
/// Explicitly carrying the duration around makes it easier to sequentially
/// compose animations in some places.
#[derive(Clone, Debug)]
pub struct AnimWithDur<F: Fun>(pub Anim<F>, pub F::T);

impl<F> Anim<F>
where
    F: Fun,
{
    /// Tag this animation with the duration that it is intended to be played
    /// for.
    ///
    /// Note that using this tagging is completely optional, but it may
    /// make it easier to combine animations sometimes.
    pub fn dur(self, t: F::T) -> AnimWithDur<F> {
        AnimWithDur(self, t)
    }
}

impl<'a, V> From<&'a [V]> for AnimWithDur<SliceClosure<'a, V>>
where
    V: Clone,
{
    fn from(slice: &'a [V]) -> Self {
        AnimWithDur(Anim(SliceClosure(slice)), slice.len())
    }
}

pub fn slice<'a, V>(slice: &'a [V]) -> AnimWithDur<impl Fun<T = usize, V = V> + 'a>
where
    V: Clone + 'a,
{
    slice.into()
}

#[doc(hidden)]
pub struct SliceClosure<'a, V>(&'a [V]);

impl<'a, V> Fun for SliceClosure<'a, V>
where
    V: Clone,
{
    type T = usize;
    type V = V;

    fn eval(&self, t: Self::T) -> Self::V {
        self.0[t].clone()
    }
}

impl<F> Anim<F>
where
    F: Fun,
    F::T: Clone + FloatCore,
{
    pub fn scale_to_dur(self, dur: F::T) -> AnimWithDur<impl Fun<T = F::T, V = F::V>> {
        self.scale_time(F::T::one() / dur).dur(dur)
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Clone,
{
    pub fn as_ref(&self) -> AnimWithDur<&F> {
        AnimWithDur(self.0.as_ref(), self.1.clone())
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
{
    pub fn transform<G, H>(self, h: H) -> AnimWithDur<G>
    where
        G: Fun<T = F::T>,
        H: FnOnce(Anim<F>) -> Anim<G>,
    {
        AnimWithDur(h(self.0), self.1)
    }

    pub fn map<W>(self, f: impl Fn(F::V) -> W) -> AnimWithDur<impl Fun<T = F::T, V = W>> {
        self.transform(move |anim| anim.map(f))
    }

    pub fn dur(self, t: F::T) -> AnimWithDur<F> {
        AnimWithDur(self.0, t)
    }
}

impl<'a, T, X, Y, F> AnimWithDur<F>
where
    T: 'a + Clone,
    X: 'a,
    Y: 'a,
    F: Fun<T = T, V = (X, Y)>,
{
    pub fn unzip(
        &'a self,
    ) -> (
        AnimWithDur<impl Fun<T = F::T, V = X> + 'a>,
        AnimWithDur<impl Fun<T = F::T, V = Y> + 'a>,
    ) {
        (
            self.as_ref().transform(|anim| anim.fst()),
            self.as_ref().transform(|anim| anim.snd()),
        )
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Copy + PartialOrd + Sub<Output = F::T>,
{
    pub fn seq<G>(self, next: Anim<G>) -> Anim<impl Fun<T = F::T, V = F::V>>
    where
        G: Fun<T = F::T, V = F::V>,
    {
        self.0.seq(self.1, next)
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Copy + PartialOrd + Sub<Output = F::T> + Add<Output = F::T>,
{
    pub fn seq_with_dur<G>(self, next: AnimWithDur<G>) -> AnimWithDur<impl Fun<T = F::T, V = F::V>>
    where
        G: Fun<T = F::T, V = F::V>,
    {
        let dur = self.1.clone() + next.1;
        AnimWithDur(self.seq(next.0), dur)
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Clone + FloatCore,
{
    pub fn repeat(self) -> Anim<impl Fun<T = F::T, V = F::V>> {
        self.0.repeat(self.1)
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Clone + Sub<Output = F::T>,
{
    pub fn backwards(self) -> AnimWithDur<impl Fun<T = F::T, V = F::V>> {
        AnimWithDur(self.0.backwards(self.1.clone()), self.1)
    }
}

impl<F> AnimWithDur<F>
where
    F: Fun,
    F::T: Clone + Mul<Output = F::T> + Div<Output = F::T>,
{
    pub fn scale_time(self, t_scale: F::T) -> AnimWithDur<impl Fun<T = F::T, V = F::V>> {
        AnimWithDur(self.0.scale_time(t_scale.clone()), self.1 / t_scale)
    }
}

#[macro_export]
macro_rules! seq_with_dur {
    (
        $expr:expr $(,)?
    ) => {
        $expr
    };

    (
        $head:expr,
        $($tail:expr $(,)?)+
    ) => {
        $head.seq_with_dur($crate::seq_with_dur!($($tail,)*))
    }
}