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
use std::{cell::RefCell, ops::RangeInclusive};

use tween::{BounceIn, Linear, SineIn, SineInOut, Tweener};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TransitionAnimation {
    BounceIn(i32),
    SineIn(i32),
    SineInOut(i32),
    Linear(i32),
}

impl TransitionAnimation {
    /// New BounceIn [Animation]
    pub fn new_bounce_in(time: i32) -> Self {
        Self::BounceIn(time)
    }

    /// New SineIn [Animation]
    pub fn new_sine_in(time: i32) -> Self {
        Self::SineIn(time)
    }

    /// New SineInOut [Animation]
    pub fn new_sine_in_out(time: i32) -> Self {
        Self::SineInOut(time)
    }

    /// New Linear [Animation]
    pub fn new_linear(time: i32) -> Self {
        Self::Linear(time)
    }

    pub fn to_animation(self, range: RangeInclusive<f64>) -> Animation {
        match self {
            TransitionAnimation::BounceIn(time) => Animation::new_bounce_in(range, time),
            TransitionAnimation::SineIn(time) => Animation::new_sine_in(range, time),
            TransitionAnimation::SineInOut(time) => Animation::new_sine_in_out(range, time),
            TransitionAnimation::Linear(time) => Animation::new_linear(range, time),
        }
    }
}

/// Animation mode and configuration.
#[derive(Clone)]
pub enum Animation {
    BounceIn(RefCell<Tweener<f64, i32, BounceIn>>),
    SineIn(RefCell<Tweener<f64, i32, SineIn>>),
    SineInOut(RefCell<Tweener<f64, i32, SineInOut>>),
    Linear(RefCell<Tweener<f64, i32, Linear>>),
}

impl Animation {
    /// New BounceIn [Animation]
    pub fn new_bounce_in(range: RangeInclusive<f64>, time: i32) -> Self {
        Self::BounceIn(RefCell::new(Tweener::bounce_in(
            *range.start(),
            *range.end(),
            time,
        )))
    }

    /// New SineIn [Animation]
    pub fn new_sine_in(range: RangeInclusive<f64>, time: i32) -> Self {
        Self::SineIn(RefCell::new(Tweener::sine_in(
            *range.start(),
            *range.end(),
            time,
        )))
    }

    /// New SineInOut [Animation]
    pub fn new_sine_in_out(range: RangeInclusive<f64>, time: i32) -> Self {
        Self::SineInOut(RefCell::new(Tweener::sine_in_out(
            *range.start(),
            *range.end(),
            time,
        )))
    }

    /// New Linear [Animation]
    pub fn new_linear(range: RangeInclusive<f64>, time: i32) -> Self {
        Self::Linear(RefCell::new(Tweener::linear(
            *range.start(),
            *range.end(),
            time,
        )))
    }

    /// Get the duration of the animation.
    pub fn duration(&self) -> i32 {
        match self {
            Animation::BounceIn(tween) => tween.borrow().duration,
            Animation::SineIn(tween) => tween.borrow().duration,
            Animation::SineInOut(tween) => tween.borrow().duration,
            Animation::Linear(tween) => tween.borrow().duration,
        }
    }

    /// Get the initial value of the animation.
    #[allow(dead_code)]
    pub fn initial_value(&self) -> f64 {
        match self {
            Animation::BounceIn(tween) => tween.borrow().initial_value(),
            Animation::SineIn(tween) => tween.borrow().initial_value(),
            Animation::SineInOut(tween) => tween.borrow().initial_value(),
            Animation::Linear(tween) => tween.borrow().initial_value(),
        }
    }

    /// Get the final value of the animation.
    #[allow(dead_code)]
    pub fn final_value(&self) -> f64 {
        match self {
            Animation::BounceIn(tween) => tween.borrow().final_value(),
            Animation::SineIn(tween) => tween.borrow().final_value(),
            Animation::SineInOut(tween) => tween.borrow().final_value(),
            Animation::Linear(tween) => tween.borrow().final_value(),
        }
    }

    /// Move the animation to the given index.
    pub fn move_value(&mut self, index: i32) -> f64 {
        match self {
            Animation::BounceIn(ref mut tween) => {
                let tween = tween.get_mut();
                tween.move_to(index)
            }
            Animation::SineIn(ref mut tween) => {
                let tween = tween.get_mut();
                tween.move_to(index)
            }
            Animation::SineInOut(ref mut tween) => {
                let tween = tween.get_mut();
                tween.move_to(index)
            }
            Animation::Linear(ref mut tween) => {
                let tween = tween.get_mut();
                tween.move_to(index)
            }
        }
    }

    #[allow(dead_code)]
    pub fn is_finished(&self) -> bool {
        match self {
            Animation::BounceIn(tween) => tween.borrow().is_finished(),
            Animation::SineIn(tween) => tween.borrow().is_finished(),
            Animation::SineInOut(tween) => tween.borrow().is_finished(),
            Animation::Linear(tween) => tween.borrow().is_finished(),
        }
    }
}