1use crate::utils::stats::ViewStats;
2use std::fmt::Debug;
3use std::hash::Hash;
4use tokio::time::Instant;
5
6pub trait EzState: Clone + Debug + 'static {}
8
9#[derive(PartialEq, Eq, Clone, Debug, Hash)]
11pub struct NoClientState;
12impl EzState for NoClientState {}
13
14#[derive(Debug, Clone, Default)]
16pub struct State<CS>
17where
18 CS: EzState,
19{
20 should_redraw: bool,
21 should_quit: bool,
22 stats: ViewStats,
23 client: CS,
24}
25
26impl Default for State<NoClientState> {
27 fn default() -> Self {
28 Self {
29 should_redraw: false,
30 should_quit: false,
31 stats: ViewStats::default(),
32 client: NoClientState,
33 }
34 }
35}
36
37impl<CS> State<CS>
38where
39 CS: EzState,
40{
41 pub(crate) fn with_client(client_state: CS) -> Self {
42 Self {
43 should_redraw: false,
44 should_quit: false,
45 stats: ViewStats::default(),
46 client: client_state,
47 }
48 }
49 pub(crate) fn set_client(&mut self, client_state: CS) {
50 self.client = client_state;
51 }
52
53 pub(crate) fn log_tick(&mut self, initial_instant: Instant) {
56 self.stats.ticks.add(initial_instant.elapsed().as_millis());
57 }
58 pub(crate) fn tick_cnt(&self) -> u64 {
59 self.stats.ticks.total()
60 }
61
62 pub(crate) fn log_frame(&mut self, initial_instant: Instant) {
65 self.stats.frames.add(initial_instant.elapsed().as_millis());
66 }
67 pub(crate) fn frame_cnt(&mut self) -> u64 {
68 self.stats.frames.total()
69 }
70
71 pub(crate) fn log_loop(&mut self, initial_instant: Instant) {
74 self.stats.loops.add(initial_instant.elapsed().as_millis());
75 }
76 pub(crate) fn loop_cnt(&mut self) -> u64 {
77 self.stats.loops.total()
78 }
79
80 pub(crate) fn unflag_draw(&mut self) {
83 self.should_redraw = false;
84 }
85 pub(crate) fn initiate_draw(&mut self) {
86 self.should_redraw = true;
87 }
88 pub(crate) fn should_redraw(&mut self) -> bool {
89 self.should_redraw
90 }
91
92 pub(crate) fn quit(&mut self) {
95 self.should_quit = true;
96 }
97 pub(crate) fn should_quit(&self) -> bool {
98 self.should_quit
99 }
100
101 pub fn client(&self) -> &CS {
105 &self.client
106 }
107
108 pub fn client_mut(&mut self) -> &mut CS {
110 &mut self.client
111 }
112}