aboutsummaryrefslogtreecommitdiff
path: root/src/resources.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-04-26 01:27:13 -0400
committerLibravatar Silas Bartha <[email protected]>2024-04-26 01:27:13 -0400
commit646db8328611f21a5850cc9834b6c72bfdf0c829 (patch)
tree0d65f0c20b70da007aab141cfe6b123119e8c909 /src/resources.rs
Initial Commitv0.1.0
Diffstat (limited to 'src/resources.rs')
-rw-r--r--src/resources.rs38
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>>>);