use super::animation::{AnimationConfig, AnimationDriver, AnimationId};
pub struct AnimationGroup {
name: String,
parallel: Vec<AnimationId>,
sequential: Vec<AnimationConfig>,
current_seq_index: usize,
current_seq_id: Option<AnimationId>,
}
impl AnimationGroup {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
parallel: Vec::new(),
sequential: Vec::new(),
current_seq_index: 0,
current_seq_id: None,
}
}
pub fn add_parallel(&mut self, id: AnimationId) {
self.parallel.push(id);
}
pub fn add_sequential(&mut self, config: AnimationConfig) {
self.sequential.push(config);
}
pub fn name(&self) -> &str {
&self.name
}
pub fn is_completed(&self, driver: &AnimationDriver) -> bool {
let parallel_done = self
.parallel
.iter()
.all(|id| driver.get_progress(*id).map(|p| p >= 1.0).unwrap_or(true));
parallel_done && self.current_seq_index >= self.sequential.len()
}
pub fn len_parallel(&self) -> usize {
self.parallel.len()
}
pub fn len_sequential(&self) -> usize {
self.sequential.len()
}
pub fn current_seq_index(&self) -> usize {
self.current_seq_index
}
pub fn reset(&mut self) {
self.current_seq_index = 0;
self.current_seq_id = None;
}
pub fn advance(&mut self, driver: &mut AnimationDriver) {
if self.current_seq_index >= self.sequential.len() {
return;
}
let parallel_done = self
.parallel
.iter()
.all(|id| driver.get_progress(*id).map(|p| p >= 1.0).unwrap_or(true));
if !parallel_done {
return;
}
if self.current_seq_id.is_none() {
let config = self.sequential[self.current_seq_index].clone();
let id = driver.add(config, |_| {});
self.current_seq_id = Some(id);
return;
}
let done = self
.current_seq_id
.and_then(|id| driver.get_progress(id))
.map(|p| p >= 1.0)
.unwrap_or(true);
if done {
self.current_seq_id = None;
self.current_seq_index += 1;
if self.current_seq_index < self.sequential.len() {
let config = self.sequential[self.current_seq_index].clone();
let id = driver.add(config, |_| {});
self.current_seq_id = Some(id);
}
}
}
}