diff options
author | Silas Bartha <[email protected]> | 2024-06-04 15:00:16 -0400 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2024-06-04 15:00:16 -0400 |
commit | a002e4d738535e6ca779c71231f7b84864b9a8d0 (patch) | |
tree | 8ec521b535f3820328f9ea6f2511ca29c630b82f /src/input/resources.rs | |
parent | 56aafda8495243fa939bdce01f36d4adbf4ec556 (diff) |
Refactored + Renamed + Added Docs
Diffstat (limited to 'src/input/resources.rs')
-rw-r--r-- | src/input/resources.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/input/resources.rs b/src/input/resources.rs new file mode 100644 index 0000000..afe6353 --- /dev/null +++ b/src/input/resources.rs @@ -0,0 +1,48 @@ +use bevy::{prelude::*, utils::HashSet}; +use crossterm::event::{Event, KeyCode}; +use std::sync::{Arc, Mutex}; + +/// Resource containing currently pressed and released keys +#[derive(Resource, Default)] +pub struct TerminalInput { + pressed_keys: HashSet<KeyCode>, + released_keys: HashSet<KeyCode>, +} + +impl TerminalInput { + /// Gets whether the given key is pressed + pub fn is_pressed(&self, code: KeyCode) -> bool { + self.pressed_keys.contains(&code) + } + + /// Gets whether the given key is released + pub fn is_released(&self, code: KeyCode) -> bool { + self.released_keys.contains(&code) + } + + /// Sets given key to pressed + pub(super) fn press(&mut self, code: KeyCode) { + if !self.is_pressed(code) { + self.pressed_keys.insert(code); + } + } + + /// Sets given key to released and removes pressed state + 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); + } + } + + /// Clears all released keys + pub(super) fn clear_released(&mut self) { + self.released_keys.clear(); + } +} + +/// Event queue for crossterm input event thread +#[derive(Resource, Default)] +pub(crate) struct EventQueue(pub(super) Arc<Mutex<Vec<Event>>>); |