mraphics_core/animation/
timeline.rs1use crate::animation::Action;
2
3#[derive(Debug)]
4pub enum TimelineState {
5 PLAYING,
6 PAUSED,
7 WAITING,
8}
9
10pub trait Timeline {
11 fn start_time(&self) -> f32;
12 fn stop_time(&self) -> f32;
13 fn current_time(&self) -> f32;
14 fn state(&self) -> &TimelineState;
15
16 fn start(&mut self);
17 fn forward(&mut self);
18 fn pause(&mut self);
19
20 fn actions(&self) -> &Vec<Action>;
21 fn add_action(&mut self, action: Action);
22 fn add_infinite_action(&mut self, action: Action);
23}
24
25pub struct LogicalTimeline {
26 pub state: TimelineState,
27 pub start_time: f32,
28 pub stop_time: f32,
29 pub logical_fps: f32,
30 pub current_frame: i32,
31
32 actions: Vec<Action>,
33 infinite_actions: Vec<Action>,
34}
35
36impl LogicalTimeline {
37 pub fn new() -> Self {
38 Self {
39 state: TimelineState::WAITING,
40 start_time: 0.0,
41 stop_time: 0.0,
42 logical_fps: 60.0,
43 current_frame: 0,
44 actions: Vec::new(),
45 infinite_actions: Vec::new(),
46 }
47 }
48
49 fn process(&mut self) {
50 let current_time = self.current_time();
51 for action in &mut self.actions {
52 let elapsed = current_time - action.start_time;
53 let progress = elapsed / action.duration;
54 action.execute(progress, elapsed);
55 }
56
57 for action in &mut self.infinite_actions {
58 action.execute(0.0, current_time);
59 }
60 }
61}
62
63impl Timeline for LogicalTimeline {
64 fn current_time(&self) -> f32 {
65 (self.current_frame as f32) * (1.0 / self.logical_fps)
66 }
67
68 fn start_time(&self) -> f32 {
69 self.start_time
70 }
71
72 fn stop_time(&self) -> f32 {
73 self.stop_time
74 }
75
76 fn state(&self) -> &TimelineState {
77 &self.state
78 }
79
80 fn start(&mut self) {
81 self.state = TimelineState::PLAYING;
82 self.process();
83 }
84
85 fn forward(&mut self) {
86 self.current_frame += 1;
87 self.process();
88 }
89
90 fn pause(&mut self) {
91 self.state = TimelineState::PAUSED;
92 }
93
94 fn actions(&self) -> &Vec<Action> {
95 &self.actions
96 }
97
98 fn add_action(&mut self, action: Action) {
99 if action.duration + action.start_time > self.stop_time {
100 self.stop_time = action.duration + action.start_time;
101 }
102
103 self.actions.push(action);
104 }
105
106 fn add_infinite_action(&mut self, action: Action) {
107 self.infinite_actions.push(action);
108 }
109}
110
111pub struct PhysicalTimeline {}