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
//! Animation group support for coordinating multiple animations
use std::time::{Duration, Instant};
use super::{AnimationState, KeyframeAnimation};
/// Mode for animation group execution
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum GroupMode {
/// Run all animations simultaneously
#[default]
Parallel,
/// Run animations one after another
Sequential,
}
/// Group of animations that can run in parallel or sequence
#[derive(Clone)]
pub struct AnimationGroup {
animations: Vec<KeyframeAnimation>,
mode: GroupMode,
state: AnimationState,
start_time: Option<Instant>,
}
impl AnimationGroup {
/// Create a parallel animation group
pub fn parallel() -> Self {
Self {
animations: Vec::new(),
mode: GroupMode::Parallel,
state: AnimationState::Pending,
start_time: None,
}
}
/// Create a sequential animation group
pub fn sequential() -> Self {
Self {
animations: Vec::new(),
mode: GroupMode::Sequential,
state: AnimationState::Pending,
start_time: None,
}
}
/// Add an animation to the group
pub fn with_animation(mut self, animation: KeyframeAnimation) -> Self {
self.animations.push(animation);
self
}
/// Get total duration of the group
pub fn total_duration(&self) -> Duration {
match self.mode {
GroupMode::Parallel => self
.animations
.iter()
.map(|a| a.delay + a.duration)
.max()
.unwrap_or(Duration::ZERO),
GroupMode::Sequential => self.animations.iter().map(|a| a.delay + a.duration).sum(),
}
}
/// Start all animations
pub fn start(&mut self) {
self.start_time = Some(Instant::now());
self.state = AnimationState::Running;
match self.mode {
GroupMode::Parallel => {
for anim in &mut self.animations {
anim.start();
}
}
GroupMode::Sequential => {
// Start only the first animation; others will be started as they complete
if let Some(first) = self.animations.first_mut() {
first.start();
}
}
}
}
/// Update all animations
pub fn update(&mut self) {
if self.state != AnimationState::Running {
return;
}
match self.mode {
GroupMode::Parallel => {
// Check if all are completed
if self.animations.iter().all(|a| a.is_completed()) {
self.state = AnimationState::Completed;
}
}
GroupMode::Sequential => {
// Find current running animation
let mut should_start_next = false;
let mut next_idx = 0;
for (i, anim) in self.animations.iter().enumerate() {
if anim.is_running() {
break;
}
if anim.is_completed()
&& i + 1 < self.animations.len()
&& !self.animations[i + 1].is_running()
&& !self.animations[i + 1].is_completed()
{
should_start_next = true;
next_idx = i + 1;
}
}
if should_start_next {
self.animations[next_idx].start();
}
// Check if all are completed
if self.animations.iter().all(|a| a.is_completed()) {
self.state = AnimationState::Completed;
}
}
}
}
/// Check if all animations are completed
pub fn is_completed(&self) -> bool {
self.state == AnimationState::Completed
}
/// Get mutable reference to animations
pub fn animations_mut(&mut self) -> &mut [KeyframeAnimation] {
&mut self.animations
}
}