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
//! Animations module for create simple tick based animations for one or more properties
 
use std::collections::HashMap;
use std::hash::Hash;

mod easing;
mod action;
mod errors;
mod task;

pub use interpolation::EaseFunction;
pub use self::easing::*;
pub use self::action::*;
pub use self::errors::*;
pub use self::task::*;

/// Aniumations simple queue with transition state
#[derive(Clone, Debug, Default)]
pub struct Animation<T, S> where T: Eq + Clone + Hash, S: Eq + Clone + Hash {
    /// Single transformation tasks
    singles: HashMap<S, SingleTask>, 
    /// Complex tasks for transition properties
    tasks: HashMap<T, Task>,
}

impl <T, S>Animation<T, S> where T: Eq + Clone + Hash, S: Eq + Clone + Hash {
    pub fn push_task(&mut self, key: T, task: Task) {
        self.tasks.insert(key, task).is_some();
    }

    pub fn push_single(&mut self, key: S, task: SingleTask) {
        self.singles.insert(key, task).is_some();
    }

    pub fn get_task(&mut self, key: &T) -> Option<&mut Task> {
        self.tasks.get_mut(key)
    }

    pub fn get_single(&mut self, key: &S) -> Option<&mut SingleTask> {
        self.singles.get_mut(key)
    }

    pub fn run(&mut self, delta_time: f32) {
        self.singles.retain(|_, value| !value.finished);
        self.tasks.retain(|_, value| !value.finished);

        // Interate and handle all tasks
        for (key, task) in self.tasks.iter_mut() {
            task.run(delta_time).is_ok();
        }

        for (_, task) in self.singles.iter_mut() {
            task.run(delta_time).is_ok();
        }
    }
}