From 8ee5269fa2b7dea9665a6c88fc7fa54a8bfc1af1 Mon Sep 17 00:00:00 2001 From: Alex Melville Date: Tue, 17 Mar 2020 13:17:04 -0400 Subject: [PATCH] add get_keys_released method to Window (#151) --- .gitignore | 2 ++ examples/noise.rs | 10 ++++++++++ src/key_handler.rs | 19 +++++++++++++++++++ src/lib.rs | 23 +++++++++++++++++++++++ src/os/macos/mod.rs | 5 +++++ src/os/redox/mod.rs | 4 ++++ src/os/unix/mod.rs | 7 +++++++ src/os/unix/x11.rs | 5 +++++ src/os/windows/mod.rs | 5 +++++ 9 files changed, 80 insertions(+) diff --git a/.gitignore b/.gitignore index a9d37c5..1647e48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target Cargo.lock +*.idea +*.iml diff --git a/examples/noise.rs b/examples/noise.rs index 8a0a82d..7dc42f2 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -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(); diff --git a/src/key_handler.rs b/src/key_handler.rs index 4c41f28..bbb30ba 100644 --- a/src/key_handler.rs +++ b/src/key_handler.rs @@ -91,6 +91,20 @@ impl KeyHandler { Some(keys) } + pub fn get_keys_released(&self) -> Option> { + let mut keys: Vec = 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]; } } diff --git a/src/lib.rs b/src/lib.rs index 531f889..6c44046 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { + self.0.get_keys_released() + } + /// /// Check if a single key is down. /// diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 83e4aac..3e5b16b 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -468,6 +468,11 @@ impl Window { self.key_handler.get_keys_pressed(repeat) } + #[inline] + pub fn get_keys_released(&self) -> Option> { + self.key_handler.get_keys_released() + } + #[inline] pub fn is_key_down(&self, key: Key) -> bool { self.key_handler.is_key_down(key) diff --git a/src/os/redox/mod.rs b/src/os/redox/mod.rs index 0cee3ab..6e09c06 100644 --- a/src/os/redox/mod.rs +++ b/src/os/redox/mod.rs @@ -189,6 +189,10 @@ impl Window { self.key_handler.get_keys_pressed(repeat) } + pub fn get_keys_released(&self) -> Option> { + self.key_handler.get_keys_released() + } + pub fn is_key_down(&self, key: Key) -> bool { self.key_handler.is_key_down(key) } diff --git a/src/os/unix/mod.rs b/src/os/unix/mod.rs index c610786..87ca428 100644 --- a/src/os/unix/mod.rs +++ b/src/os/unix/mod.rs @@ -155,6 +155,13 @@ impl Window { } } + pub fn get_keys_released(&self) -> Option> { + 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), diff --git a/src/os/unix/x11.rs b/src/os/unix/x11.rs index 515f583..492d7df 100644 --- a/src/os/unix/x11.rs +++ b/src/os/unix/x11.rs @@ -590,6 +590,11 @@ impl Window { self.key_handler.get_keys_pressed(repeat) } + #[inline] + pub fn get_keys_released(&self) -> Option> { + self.key_handler.get_keys_released() + } + #[inline] pub fn is_key_down(&self, key: Key) -> bool { self.key_handler.is_key_down(key) diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 3d1a788..0c3ebc6 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -700,6 +700,11 @@ impl Window { self.key_handler.get_keys_pressed(repeat) } + #[inline] + pub fn get_keys_released(&self) -> Option> { + self.key_handler.get_keys_released() + } + #[inline] pub fn is_key_down(&self, key: Key) -> bool { self.key_handler.is_key_down(key)