diff options
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/input/resources.rs | 40 | ||||
-rw-r--r-- | src/input/systems.rs | 5 |
4 files changed, 29 insertions, 21 deletions
@@ -1,9 +1,10 @@ [package] name = "bevy_terminal_display" -version = "0.3.0" +version = "0.4.0" edition = "2021" license = "0BSD OR MIT OR Apache-2.0" description = "A plugin for the Bevy game engine which enables rendering to a terminal using unicode braille characters." +repository = "https://github.com/exvacuum/bevy_terminal_display" [dependencies] crossbeam-channel = "0.5" @@ -25,7 +25,7 @@ Features Include: | Crate Version | Bevy Version | |--- |--- | -| 0.3 | 0.14 | +| 0.3-0.4 | 0.14 | | 0.2 | 0.13 | ## Installation diff --git a/src/input/resources.rs b/src/input/resources.rs index afe6353..ba2f6dd 100644 --- a/src/input/resources.rs +++ b/src/input/resources.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*, utils::HashSet}; +use bevy::{prelude:: *, utils::HashSet}; use crossterm::event::{Event, KeyCode}; use std::sync::{Arc, Mutex}; @@ -6,7 +6,8 @@ use std::sync::{Arc, Mutex}; #[derive(Resource, Default)] pub struct TerminalInput { pressed_keys: HashSet<KeyCode>, - released_keys: HashSet<KeyCode>, + just_pressed_keys: HashSet<KeyCode>, + just_released_keys: HashSet<KeyCode>, } impl TerminalInput { @@ -15,31 +16,36 @@ impl TerminalInput { 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) + /// Gets whether the given key was just pressed + pub fn just_pressed(&self, code: KeyCode) -> bool { + self.just_pressed_keys.contains(&code) + } + + /// Gets whether the given key was just released + pub fn just_released(&self, code: KeyCode) -> bool { + self.just_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); - } + self.pressed_keys.insert(code); + self.just_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); - } + self.pressed_keys.remove(&code); + self.just_released_keys.insert(code); } - /// Clears all released keys - pub(super) fn clear_released(&mut self) { - self.released_keys.clear(); + /// Clears all just released keys + pub(super) fn clear_just_released(&mut self) { + self.just_released_keys.clear(); + } + + /// Clears all just pressed keys + pub(super) fn clear_just_pressed(&mut self) { + self.just_pressed_keys.clear(); } } diff --git a/src/input/systems.rs b/src/input/systems.rs index 9560e1e..9be9f25 100644 --- a/src/input/systems.rs +++ b/src/input/systems.rs @@ -29,7 +29,8 @@ pub fn input_handling( mut input: ResMut<TerminalInput>, mut event_writer: EventWriter<TerminalInputEvent>, ) { - input.clear_released(); + input.clear_just_released(); + input.clear_just_pressed(); let mut event_queue = event_queue.0.lock().unwrap(); let mut key_events = Vec::<KeyEvent>::new(); while let Some(event) = event_queue.pop() { @@ -51,4 +52,4 @@ pub fn input_handling( _ => (), } } -}
\ No newline at end of file +} |