Skip to main content

mraphics_core/animation/
action.rs

1pub struct Action<'res> {
2    pub start_time: f32,
3    pub duration: f32,
4
5    pub on_start: Box<dyn FnMut() + 'res>,
6    pub on_stop: Box<dyn FnMut() + 'res>,
7    pub on_execute: Box<dyn FnMut() + 'res>,
8    pub on_update: Box<dyn FnMut(f32, f32) + 'res>,
9
10    pub started: bool,
11    pub stopped: bool,
12}
13
14impl<'res> Action<'res> {
15    pub fn new() -> Self {
16        Self {
17            start_time: 0.0,
18            duration: 1.0,
19            on_start: Box::new(|| {}),
20            on_stop: Box::new(|| {}),
21            on_execute: Box::new(|| {}),
22            on_update: Box::new(|_, _| {}),
23
24            started: false,
25            stopped: false,
26        }
27    }
28
29    pub fn execute(&mut self, progress: f32, elapsed_time: f32) {
30        (self.on_execute)();
31
32        if self.stopped || progress < 0.0 {
33            return;
34        }
35
36        if !self.started {
37            (self.on_start)();
38            self.started = true;
39        }
40
41        if progress == 0.0 {
42            return;
43        }
44
45        if progress > 1.0 {
46            if !self.stopped {
47                (self.on_update)(1.0, self.duration);
48                (self.on_stop)();
49                self.stopped = true;
50            }
51
52            return;
53        }
54
55        (self.on_update)(progress, elapsed_time);
56    }
57}