freya_hooks/use_animation/
anim_sequential.rs

1use std::ops::Deref;
2
3use super::{
4    AnimDirection,
5    AnimatedValue,
6};
7
8/// Chain a sequence of animated values.
9#[derive(Clone)]
10pub struct AnimSequential<Animated: AnimatedValue, const N: usize> {
11    values: [Animated; N],
12    curr_value: usize,
13    acc_index: u128,
14}
15
16impl<Animated: AnimatedValue, const N: usize> AnimSequential<Animated, N> {
17    pub fn new(values: [Animated; N]) -> Self {
18        Self {
19            values,
20            curr_value: 0,
21            acc_index: 0,
22        }
23    }
24}
25
26impl<Animated: AnimatedValue, const N: usize> Deref for AnimSequential<Animated, N> {
27    type Target = [Animated; N];
28
29    fn deref(&self) -> &Self::Target {
30        &self.values
31    }
32}
33
34impl<Animated: AnimatedValue, const N: usize> AnimatedValue for AnimSequential<Animated, N> {
35    fn advance(&mut self, index: u128, direction: AnimDirection) {
36        if let Some(value) = self.values.get_mut(self.curr_value) {
37            let index = index - self.acc_index;
38            value.advance(index, direction);
39
40            if value.is_finished(index, direction) {
41                self.curr_value += 1;
42                self.acc_index += index;
43            }
44        }
45    }
46
47    fn is_finished(&self, index: u128, direction: AnimDirection) -> bool {
48        if let Some(value) = self.values.get(self.curr_value) {
49            value.is_finished(index, direction)
50        } else {
51            true
52        }
53    }
54
55    fn prepare(&mut self, direction: AnimDirection) {
56        self.acc_index = 0;
57        self.curr_value = 0;
58        for val in &mut self.values {
59            val.prepare(direction);
60        }
61    }
62
63    fn finish(&mut self, direction: AnimDirection) {
64        for value in &mut self.values {
65            value.finish(direction);
66        }
67    }
68}