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
use std::ops::Deref;

use super::{
    AnimDirection,
    AnimatedValue,
};

/// Chain a sequence of animated values.
#[derive(Clone)]
pub struct AnimSequential<Animated: AnimatedValue, const N: usize> {
    values: [Animated; N],
    curr_value: usize,
    acc_index: u128,
}

impl<Animated: AnimatedValue, const N: usize> AnimSequential<Animated, N> {
    pub fn new(values: [Animated; N]) -> Self {
        Self {
            values,
            curr_value: 0,
            acc_index: 0,
        }
    }
}

impl<Animated: AnimatedValue, const N: usize> Deref for AnimSequential<Animated, N> {
    type Target = [Animated; N];

    fn deref(&self) -> &Self::Target {
        &self.values
    }
}

impl<Animated: AnimatedValue, const N: usize> AnimatedValue for AnimSequential<Animated, N> {
    fn advance(&mut self, index: u128, direction: AnimDirection) {
        if let Some(value) = self.values.get_mut(self.curr_value) {
            let index = index - self.acc_index;
            value.advance(index, direction);

            if value.is_finished(index, direction) {
                self.curr_value += 1;
                self.acc_index += index;
            }
        }
    }

    fn is_finished(&self, index: u128, direction: AnimDirection) -> bool {
        if let Some(value) = self.values.get(self.curr_value) {
            value.is_finished(index, direction)
        } else {
            true
        }
    }

    fn prepare(&mut self, direction: AnimDirection) {
        self.acc_index = 0;
        self.curr_value = 0;
        for val in &mut self.values {
            val.prepare(direction);
        }
    }

    fn finish(&mut self, direction: AnimDirection) {
        for value in &mut self.values {
            value.finish(direction);
        }
    }
}