diff options
Diffstat (limited to 'src/resources.rs')
-rw-r--r-- | src/resources.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..343967d --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,38 @@ +use std::sync::{Arc, Mutex}; + +use bevy::{prelude::*, utils::HashSet}; +use crossterm::event::{Event, KeyCode}; + +#[derive(Resource, Default)] +pub struct TerminalInput { + pressed_keys: HashSet<KeyCode>, + released_keys: HashSet<KeyCode>, +} + +impl TerminalInput { + pub fn is_pressed(&self, code: KeyCode) -> bool { + self.pressed_keys.contains(&code) + } + + pub fn is_released(&self, code: KeyCode) -> bool { + self.released_keys.contains(&code) + } + + pub(super) fn press(&mut self, code: KeyCode) { + if !self.is_pressed(code) { + self.pressed_keys.insert(code); + } + } + + pub(super) fn release(&mut self, code: KeyCode) { + if self.is_pressed(code) { + self.pressed_keys.remove(&code); + } + if !self.is_released(code) { + self.released_keys.insert(code); + } + } +} + +#[derive(Resource, Default)] +pub(super) struct EventQueue(pub(super) Arc<Mutex<Vec<Event>>>); |