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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::phase::Phase;
use core::Scalar;
use serde::{Deserialize, Serialize};
use std::ops::Range;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Transition<T> {
    from: T,
    to: T,
    #[serde(default)]
    pub phase: Phase,
    #[serde(default)]
    pub playing: bool,
    #[serde(default)]
    time: Scalar,
}

impl<T> Transition<T>
where
    T: Clone,
{
    pub fn new(from: T, to: T, phase: Phase) -> Self {
        Self {
            from,
            to,
            playing: false,
            time: phase.time_frame().start,
            phase,
        }
    }

    pub fn instant(value: T) -> Self {
        Self::new(
            value.clone(),
            value,
            Phase::point(1.0).expect("Could not create point phase for instant transition"),
        )
    }

    pub fn time(&self) -> Scalar {
        self.time
    }

    pub fn start(&mut self) {
        self.time = self.phase.time_frame().start;
    }

    pub fn end(&mut self) {
        self.time = self.phase.time_frame().end;
    }

    pub fn set_time(&mut self, time: Scalar) {
        self.time = time
            .max(self.phase.time_frame().start)
            .min(self.phase.time_frame().end);
    }

    pub fn in_progress(&self) -> bool {
        self.time < self.phase.time_frame().end
    }

    pub fn is_complete(&self) -> bool {
        !self.in_progress()
    }

    pub fn from(&self) -> &T {
        &self.from
    }

    pub fn from_mut(&mut self) -> &mut T {
        &mut self.from
    }

    pub fn to(&self) -> &T {
        &self.to
    }

    pub fn to_mut(&mut self) -> &mut T {
        &mut self.to
    }

    pub fn time_frame(&self) -> Range<Scalar> {
        self.phase.time_frame()
    }

    pub fn duration(&self) -> Scalar {
        self.phase.duration()
    }

    pub fn phase(&self) -> Scalar {
        self.sample(self.time)
    }

    pub fn sample(&self, time: Scalar) -> Scalar {
        self.phase.sample(time)
    }

    pub fn set(&mut self, value: T) {
        self.from = self.to.clone();
        self.to = value;
        self.time = 0.0;
    }

    pub fn process(&mut self, delta_time: Scalar) {
        if self.playing {
            let time_frame = self.phase.time_frame();
            let time = self.time + delta_time;
            if time >= time_frame.end {
                self.playing = false;
            }
            self.time = time.max(time_frame.start).min(time_frame.end);
        }
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SwitchTransition {
    value: bool,
    #[serde(default)]
    pub phase: Phase,
    #[serde(default)]
    pub playing: bool,
    #[serde(default)]
    time: Scalar,
}

impl SwitchTransition {
    pub fn new(value: bool, phase: Phase) -> Self {
        let time = if value {
            phase.time_frame().end
        } else {
            phase.time_frame().start
        };
        Self {
            value,
            phase,
            playing: false,
            time,
        }
    }

    pub fn instant(value: bool) -> Self {
        Self::new(
            value,
            Phase::point(1.0).expect("Could not create point phase for instant switch"),
        )
    }

    pub fn time(&self) -> Scalar {
        self.time
    }

    pub fn start(&mut self) {
        self.time = if self.value {
            self.phase.time_frame().start
        } else {
            self.phase.time_frame().end
        };
    }

    pub fn end(&mut self) {
        self.time = if self.value {
            self.phase.time_frame().end
        } else {
            self.phase.time_frame().start
        };
    }

    pub fn set_time(&mut self, time: Scalar) {
        self.time = time
            .max(self.phase.time_frame().start)
            .min(self.phase.time_frame().end);
    }

    pub fn in_progress(&self) -> bool {
        if self.value {
            self.time < self.phase.time_frame().end
        } else {
            self.time > self.phase.time_frame().start
        }
    }

    pub fn is_complete(&self) -> bool {
        !self.in_progress()
    }

    pub fn value(&self) -> bool {
        self.value
    }

    pub fn duration(&self) -> Scalar {
        self.phase.duration()
    }

    pub fn phase(&self) -> Scalar {
        self.sample(self.time)
    }

    pub fn sample(&self, time: Scalar) -> Scalar {
        self.phase.sample(time)
    }

    pub fn set(&mut self, value: bool) {
        self.value = value;
    }

    pub fn process(&mut self, delta_time: Scalar) {
        if self.playing {
            let time_frame = self.phase.time_frame();
            let time = if self.value {
                let time = self.time + delta_time;
                if time >= time_frame.end {
                    self.playing = false;
                }
                time
            } else {
                let time = self.time - delta_time;
                if time <= time_frame.start {
                    self.playing = false;
                }
                time
            };
            self.time = time.max(time_frame.start).min(time_frame.end);
        }
    }
}