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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Timeline system for orchestrating multiple animations
//!
//! This module provides a timeline system that allows sequencing and
//! coordinating multiple tween animations with precise timing control.
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
use slotmap::SlotMap;
use crate::core::{AnimationState, Tween, TweenId};
/// State of a timeline
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimelineState {
/// Timeline has not started
Idle,
/// Timeline is currently playing
Playing,
/// Timeline is paused (can be resumed)
Paused,
/// Timeline has finished playing
Completed,
}
/// A tween within a timeline with timing information
#[derive(Debug, Clone)]
pub struct TimelineTween {
/// Unique ID of the tween
pub id: TweenId,
/// Time at which this tween starts
pub start_time: Duration,
/// The tween animation
pub tween: Tween,
}
/// Timeline for orchestrating multiple tweens
///
/// A timeline manages multiple tween animations, allowing them to be
/// scheduled at specific times and played together or in sequence.
#[derive(Clone)]
pub struct Timeline {
tweens: Rc<RefCell<SlotMap<TweenId, TimelineTween>>>,
state: TimelineState,
current_time: Duration,
total_duration: Duration,
playback_rate: f64,
repeat: Option<u32>,
yoyo: bool,
repeat_count: u32,
direction: crate::core::AnimationDirection,
}
impl Default for Timeline {
fn default() -> Self {
Self::new()
}
}
impl Timeline {
/// Create a new timeline
pub fn new() -> Self {
Self {
tweens: Rc::new(RefCell::new(SlotMap::with_key())),
state: TimelineState::Idle,
current_time: Duration::ZERO,
total_duration: Duration::ZERO,
playback_rate: 1.0,
repeat: None,
yoyo: false,
repeat_count: 0,
direction: crate::core::AnimationDirection::Forward,
}
}
/// Get the current state of the timeline
pub fn state(&self) -> TimelineState {
self.state
}
/// Get the current playback time
pub fn current_time(&self) -> Duration {
self.current_time
}
/// Get the total duration of the timeline
pub fn total_duration(&self) -> Duration {
self.total_duration
}
/// Get the current progress (0.0 to 1.0)
pub fn progress(&self) -> f64 {
if self.total_duration == Duration::ZERO {
0.0
} else {
self.current_time.as_secs_f64() / self.total_duration.as_secs_f64()
}
}
/// Get the playback rate (speed multiplier)
pub fn playback_rate(&self) -> f64 {
self.playback_rate
}
/// Set the playback rate
///
/// # Arguments
/// * `rate` - Playback rate (0.0 to 10.0)
pub fn set_playback_rate(&mut self, rate: f64) {
self.playback_rate = rate.clamp(0.0, 10.0);
}
/// Set the number of repeats
///
/// # Arguments
/// * `count` - Number of times to repeat (None for infinite)
pub fn set_repeat(&mut self, count: Option<u32>) {
self.repeat = count;
}
/// Enable or disable yoyo (reverse after each repeat)
///
/// # Arguments
/// * `enabled` - Whether to enable yoyo
pub fn set_yoyo(&mut self, enabled: bool) {
self.yoyo = enabled;
}
/// Add a tween at a specific start time
///
/// # Arguments
/// * `tween` - The tween to add
/// * `start_time` - Time at which the tween should start
///
/// # Returns
/// ID of the added tween
pub fn add_tween(&mut self, tween: Tween, start_time: Duration) -> TweenId {
let mut tweens = self.tweens.borrow_mut();
let end_time = start_time + tween.duration();
if end_time > self.total_duration {
self.total_duration = end_time;
}
let timeline_tween = TimelineTween {
id: tween.id(),
start_time,
tween,
};
tweens.insert(timeline_tween)
}
/// Add a tween at the current timeline time
///
/// # Arguments
/// * `tween` - The tween to add
///
/// # Returns
/// ID of the added tween
pub fn add_tween_at(&mut self, tween: Tween) -> TweenId {
let start_time = self.current_time;
self.add_tween(tween, start_time)
}
/// Remove a tween from the timeline
///
/// # Arguments
/// * `id` - ID of the tween to remove
///
/// # Returns
/// true if the tween was removed, false otherwise
pub fn remove_tween(&mut self, id: TweenId) -> bool {
self.tweens.borrow_mut().remove(id).is_some()
}
/// Clear all tweens and reset the timeline
pub fn clear(&mut self) {
self.tweens.borrow_mut().clear();
self.current_time = Duration::ZERO;
self.total_duration = Duration::ZERO;
self.repeat_count = 0;
self.state = TimelineState::Idle;
}
/// Start playing the timeline
pub fn play(&mut self) {
if self.state == TimelineState::Completed {
self.restart();
}
self.state = TimelineState::Playing;
}
/// Pause the timeline
pub fn pause(&mut self) {
if self.state == TimelineState::Playing {
self.state = TimelineState::Paused;
}
}
/// Resume a paused timeline
pub fn resume(&mut self) {
if self.state == TimelineState::Paused {
self.state = TimelineState::Playing;
}
}
/// Stop the timeline and reset to beginning
pub fn stop(&mut self) {
self.state = TimelineState::Idle;
self.current_time = Duration::ZERO;
}
/// Restart the timeline from the beginning
pub fn restart(&mut self) {
self.current_time = Duration::ZERO;
self.repeat_count = 0;
self.state = TimelineState::Playing;
let mut tweens = self.tweens.borrow_mut();
for (_, timeline_tween) in tweens.iter_mut() {
timeline_tween.tween.reset();
}
}
/// Reverse the playback direction
pub fn reverse(&mut self) {
self.direction = match self.direction {
crate::core::AnimationDirection::Forward => crate::core::AnimationDirection::Backward,
crate::core::AnimationDirection::Backward => crate::core::AnimationDirection::Forward,
};
}
/// Seek to a specific time in the timeline
///
/// # Arguments
/// * `time` - Time to seek to
pub fn seek(&mut self, time: Duration) {
let target_time = time.clamp(Duration::ZERO, self.total_duration);
self.current_time = target_time;
self.update_tweens_at_time();
}
/// Seek to a specific progress (0.0 to 1.0)
///
/// # Arguments
/// * `progress` - Progress value to seek to
pub fn seek_to_progress(&mut self, progress: f64) {
let clamped = progress.clamp(0.0, 1.0);
let time = Duration::from_secs_f64(clamped * self.total_duration.as_secs_f64());
self.seek(time);
}
/// Advance the timeline by a time delta
///
/// # Arguments
/// * `delta` - Time to advance
pub fn tick(&mut self, delta: Duration) {
if self.state != TimelineState::Playing {
return;
}
let adjusted_delta = Duration::from_secs_f64(delta.as_secs_f64() * self.playback_rate);
match self.direction {
crate::core::AnimationDirection::Forward => {
self.current_time += adjusted_delta;
if self.current_time >= self.total_duration {
self.handle_completion();
}
}
crate::core::AnimationDirection::Backward => {
if self.current_time > adjusted_delta {
self.current_time -= adjusted_delta;
} else {
self.current_time = Duration::ZERO;
self.handle_completion();
}
}
}
self.update_tweens_at_time();
}
/// Update all tweens to match current timeline time
fn update_tweens_at_time(&mut self) {
let tweens = self.tweens.borrow();
let mut updates: Vec<(TweenId, Duration)> = Vec::new();
for (id, timeline_tween) in tweens.iter() {
let tween_local_time = if self.current_time >= timeline_tween.start_time {
self.current_time - timeline_tween.start_time
} else {
Duration::ZERO
};
if tween_local_time <= timeline_tween.tween.duration() {
updates.push((id, tween_local_time));
}
}
drop(tweens);
let mut tweens = self.tweens.borrow_mut();
for (id, local_time) in updates {
if let Some(timeline_tween) = tweens.get_mut(id) {
if timeline_tween.tween.state() == AnimationState::Idle {
timeline_tween.tween.play();
}
timeline_tween.tween.seek(local_time);
}
}
}
/// Handle timeline completion based on repeat settings
fn handle_completion(&mut self) {
if let Some(count) = self.repeat {
if self.repeat_count < count {
self.repeat_count += 1;
if self.yoyo {
self.reverse();
}
self.current_time = Duration::ZERO;
} else {
self.state = TimelineState::Completed;
}
} else if self.yoyo {
self.reverse();
self.current_time = Duration::ZERO;
} else {
self.state = TimelineState::Completed;
}
}
/// Get IDs of all currently active tweens
///
/// # Returns
/// Vector of active tween IDs
pub fn get_active_tweens(&self) -> Vec<TweenId> {
let tweens = self.tweens.borrow();
tweens
.iter()
.filter(|(_, timeline_tween)| {
let tween_end = timeline_tween.start_time + timeline_tween.tween.duration();
self.current_time >= timeline_tween.start_time && self.current_time < tween_end
})
.map(|(id, _)| id)
.collect()
}
/// Check if timeline is currently playing
pub fn is_playing(&self) -> bool {
self.state == TimelineState::Playing
}
/// Check if timeline is paused
pub fn is_paused(&self) -> bool {
self.state == TimelineState::Paused
}
/// Check if timeline has completed
pub fn is_completed(&self) -> bool {
self.state == TimelineState::Completed
}
}