Add "just pressed" input tracking
This commit is contained in:
parent
72da32574e
commit
ef0771d67c
@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bevy_terminal_display"
|
name = "bevy_terminal_display"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "0BSD OR MIT OR Apache-2.0"
|
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."
|
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]
|
[dependencies]
|
||||||
crossbeam-channel = "0.5"
|
crossbeam-channel = "0.5"
|
||||||
|
@ -25,7 +25,7 @@ Features Include:
|
|||||||
|
|
||||||
| Crate Version | Bevy Version |
|
| Crate Version | Bevy Version |
|
||||||
|--- |--- |
|
|--- |--- |
|
||||||
| 0.3 | 0.14 |
|
| 0.3-0.4 | 0.14 |
|
||||||
| 0.2 | 0.13 |
|
| 0.2 | 0.13 |
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
@ -6,7 +6,8 @@ use std::sync::{Arc, Mutex};
|
|||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
pub struct TerminalInput {
|
pub struct TerminalInput {
|
||||||
pressed_keys: HashSet<KeyCode>,
|
pressed_keys: HashSet<KeyCode>,
|
||||||
released_keys: HashSet<KeyCode>,
|
just_pressed_keys: HashSet<KeyCode>,
|
||||||
|
just_released_keys: HashSet<KeyCode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TerminalInput {
|
impl TerminalInput {
|
||||||
@ -15,31 +16,36 @@ impl TerminalInput {
|
|||||||
self.pressed_keys.contains(&code)
|
self.pressed_keys.contains(&code)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets whether the given key is released
|
/// Gets whether the given key was just pressed
|
||||||
pub fn is_released(&self, code: KeyCode) -> bool {
|
pub fn just_pressed(&self, code: KeyCode) -> bool {
|
||||||
self.released_keys.contains(&code)
|
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
|
/// Sets given key to pressed
|
||||||
pub(super) fn press(&mut self, code: KeyCode) {
|
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
|
/// Sets given key to released and removes pressed state
|
||||||
pub(super) fn release(&mut self, code: KeyCode) {
|
pub(super) fn release(&mut self, code: KeyCode) {
|
||||||
if self.is_pressed(code) {
|
|
||||||
self.pressed_keys.remove(&code);
|
self.pressed_keys.remove(&code);
|
||||||
}
|
self.just_released_keys.insert(code);
|
||||||
if !self.is_released(code) {
|
|
||||||
self.released_keys.insert(code);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears all released keys
|
/// Clears all just released keys
|
||||||
pub(super) fn clear_released(&mut self) {
|
pub(super) fn clear_just_released(&mut self) {
|
||||||
self.released_keys.clear();
|
self.just_released_keys.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clears all just pressed keys
|
||||||
|
pub(super) fn clear_just_pressed(&mut self) {
|
||||||
|
self.just_pressed_keys.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,8 @@ pub fn input_handling(
|
|||||||
mut input: ResMut<TerminalInput>,
|
mut input: ResMut<TerminalInput>,
|
||||||
mut event_writer: EventWriter<TerminalInputEvent>,
|
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 event_queue = event_queue.0.lock().unwrap();
|
||||||
let mut key_events = Vec::<KeyEvent>::new();
|
let mut key_events = Vec::<KeyEvent>::new();
|
||||||
while let Some(event) = event_queue.pop() {
|
while let Some(event) = event_queue.pop() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user