aboutsummaryrefslogtreecommitdiff
path: root/src/input/systems.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-06-04 15:00:16 -0400
committerLibravatar Silas Bartha <[email protected]>2024-06-04 15:00:16 -0400
commita002e4d738535e6ca779c71231f7b84864b9a8d0 (patch)
tree8ec521b535f3820328f9ea6f2511ca29c630b82f /src/input/systems.rs
parent56aafda8495243fa939bdce01f36d4adbf4ec556 (diff)
Refactored + Renamed + Added Docs
Diffstat (limited to 'src/input/systems.rs')
-rw-r--r--src/input/systems.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/input/systems.rs b/src/input/systems.rs
new file mode 100644
index 0000000..3b7f11d
--- /dev/null
+++ b/src/input/systems.rs
@@ -0,0 +1,46 @@
+use bevy::prelude::*;
+use crossterm::event::{read, Event, KeyEventKind};
+
+use super::{events::TerminalInputEvent, resources::{EventQueue, TerminalInput}};
+
+/// Initializes event queue and thread
+pub fn setup_input(event_queue: Res<EventQueue>) {
+ let event_queue = event_queue.0.clone();
+ std::thread::spawn(move || {
+ loop {
+ // `read()` blocks until an `Event` is available
+ match read() {
+ Ok(event) => {
+ event_queue.lock().unwrap().push(event);
+ }
+ Err(err) => {
+ panic!("Error reading input events: {:?}", err);
+ }
+ }
+ }
+ });
+}
+
+/// Reads events from queue and broadcasts corresponding `TerminalInputEvent`s
+pub fn input_handling(
+ event_queue: Res<EventQueue>,
+ mut input: ResMut<TerminalInput>,
+ mut event_writer: EventWriter<TerminalInputEvent>,
+) {
+ input.clear_released();
+ let mut event_queue = event_queue.0.lock().unwrap();
+ while let Some(event) = event_queue.pop() {
+ if let Event::Key(event) = event {
+ match event.kind {
+ KeyEventKind::Press => {
+ input.press(event.code);
+ }
+ KeyEventKind::Release => {
+ input.release(event.code);
+ }
+ _ => (),
+ }
+ }
+ event_writer.send(TerminalInputEvent(event));
+ }
+}