add get_keys_released method to Window (#151)

This commit is contained in:
Alex Melville 2020-03-17 13:17:04 -04:00 committed by GitHub
parent 9d5529422c
commit 8ee5269fa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 80 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
target
Cargo.lock
*.idea
*.iml

View file

@ -58,6 +58,16 @@ fn main() {
}
});
window.get_keys_released().map(|keys| {
for t in keys {
match t {
Key::W => println!("released w!"),
Key::T => println!("released t!"),
_ => (),
}
}
});
window
.update_with_buffer(&buffer, new_size.0, new_size.1)
.unwrap();

View file

@ -91,6 +91,20 @@ impl KeyHandler {
Some(keys)
}
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
let mut keys: Vec<Key> = Vec::new();
for (idx, is_down) in self.keys.iter().enumerate() {
if !(*is_down) && self.is_key_index_released(idx) {
unsafe {
keys.push(mem::transmute(idx as u8));
}
}
}
Some(keys)
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
return self.keys[key as usize];
@ -134,6 +148,11 @@ impl KeyHandler {
#[inline]
pub fn is_key_released(&self, key: Key) -> bool {
let idx = key as usize;
return self.is_key_index_released(idx);
}
#[inline]
fn is_key_index_released(&self, idx: usize) -> bool {
return self.keys_prev[idx] && !self.keys[idx];
}
}

View file

@ -561,6 +561,29 @@ impl Window {
self.0.get_keys_pressed(repeat)
}
///
/// Get the current released keys.
///
/// # Examples
///
/// ```no_run
/// # use minifb::*;
/// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap();
/// window.get_keys_released().map(|keys| {
/// for t in keys {
/// match t {
/// Key::W => println!("released w"),
/// Key::T => println!("released t"),
/// _ => (),
/// }
/// }
/// });
/// ```
#[inline]
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
self.0.get_keys_released()
}
///
/// Check if a single key is down.
///

View file

@ -468,6 +468,11 @@ impl Window {
self.key_handler.get_keys_pressed(repeat)
}
#[inline]
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
self.key_handler.get_keys_released()
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
self.key_handler.is_key_down(key)

View file

@ -189,6 +189,10 @@ impl Window {
self.key_handler.get_keys_pressed(repeat)
}
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
self.key_handler.get_keys_released()
}
pub fn is_key_down(&self, key: Key) -> bool {
self.key_handler.is_key_down(key)
}

View file

@ -155,6 +155,13 @@ impl Window {
}
}
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
match *self {
Window::X11(ref w) => w.get_keys_released(),
Window::Wayland(ref _w) => unimplemented!(),
}
}
pub fn is_key_down(&self, key: Key) -> bool {
match *self {
Window::X11(ref w) => w.is_key_down(key),

View file

@ -590,6 +590,11 @@ impl Window {
self.key_handler.get_keys_pressed(repeat)
}
#[inline]
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
self.key_handler.get_keys_released()
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
self.key_handler.is_key_down(key)

View file

@ -700,6 +700,11 @@ impl Window {
self.key_handler.get_keys_pressed(repeat)
}
#[inline]
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
self.key_handler.get_keys_released()
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
self.key_handler.is_key_down(key)