mraphics_core/animation/
action.rs1pub struct Action {
2 pub start_time: f32,
3 pub duration: f32,
4
5 pub on_start: Box<dyn FnMut()>,
6 pub on_stop: Box<dyn FnMut()>,
7 pub on_execute: Box<dyn FnMut()>,
8 pub on_update: Box<dyn FnMut(f32, f32)>,
9
10 started: bool,
11 stopped: bool,
12}
13
14impl Action {
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
58 pub fn is_stopped(&self) -> bool {
59 self.stopped
60 }
61}