vizia_core/events/
timer.rs1use std::{cmp::Ordering, rc::Rc};
2
3use web_time::{Duration, Instant};
4
5use crate::{context::EventContext, entity::Entity};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum TimerAction {
14 Start,
16 Tick(Duration),
18 Stop,
20}
21
22#[derive(Clone)]
23pub struct TimerState {
24 pub(crate) entity: Entity,
25 pub(crate) id: Timer,
26 pub(crate) time: Instant,
27 pub(crate) interval: Duration,
28 pub(crate) duration: Option<Duration>,
29 pub(crate) start_time: Instant,
30 pub(crate) callback: Rc<dyn Fn(&mut EventContext, TimerAction)>,
31 pub(crate) ticking: bool,
32 pub(crate) stopping: bool,
33}
34
35impl TimerState {
36 pub fn start_time(&self) -> Instant {
37 self.start_time
38 }
39
40 pub fn end_time(&self) -> Option<Instant> {
41 self.duration.map(|duration| self.start_time + duration)
42 }
43
44 pub fn set_interval(&mut self, interval: Duration) -> &mut Self {
46 self.interval = interval;
47
48 self
49 }
50
51 pub fn set_duration(&mut self, duration: Option<Duration>) -> &mut Self {
53 self.duration = duration;
54
55 self
56 }
57
58 pub fn duration(&self) -> Option<Duration> {
60 self.duration
61 }
62
63 pub fn interval(&self) -> Duration {
65 self.interval
66 }
67
68 pub fn progress(&self) -> Option<f32> {
70 self.duration.map(|duration| {
71 ((self.time - self.start_time).as_secs_f32() / duration.as_secs_f32()).clamp(0.0, 1.0)
72 })
73 }
74}
75
76impl PartialEq<Self> for TimerState {
77 fn eq(&self, other: &Self) -> bool {
78 self.time.eq(&other.time)
79 }
80}
81
82impl Eq for TimerState {}
83
84impl PartialOrd for TimerState {
85 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
86 Some(self.cmp(other))
87 }
88}
89
90impl Ord for TimerState {
91 fn cmp(&self, other: &Self) -> Ordering {
92 self.time.cmp(&other.time).reverse()
93 }
94}
95
96#[derive(Debug, PartialEq, Eq, Copy, Clone)]
98pub struct Timer(pub usize);