mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 11:21:30 +11:00
add get_keys_released method to Window (#151)
This commit is contained in:
parent
9d5529422c
commit
8ee5269fa2
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,4 @@
|
||||||
target
|
target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
*.idea
|
||||||
|
*.iml
|
||||||
|
|
|
@ -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
|
window
|
||||||
.update_with_buffer(&buffer, new_size.0, new_size.1)
|
.update_with_buffer(&buffer, new_size.0, new_size.1)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -91,6 +91,20 @@ impl KeyHandler {
|
||||||
Some(keys)
|
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]
|
#[inline]
|
||||||
pub fn is_key_down(&self, key: Key) -> bool {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
return self.keys[key as usize];
|
return self.keys[key as usize];
|
||||||
|
@ -134,6 +148,11 @@ impl KeyHandler {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_key_released(&self, key: Key) -> bool {
|
pub fn is_key_released(&self, key: Key) -> bool {
|
||||||
let idx = key as usize;
|
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];
|
return self.keys_prev[idx] && !self.keys[idx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
23
src/lib.rs
23
src/lib.rs
|
@ -561,6 +561,29 @@ impl Window {
|
||||||
self.0.get_keys_pressed(repeat)
|
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.
|
/// Check if a single key is down.
|
||||||
///
|
///
|
||||||
|
|
|
@ -468,6 +468,11 @@ impl Window {
|
||||||
self.key_handler.get_keys_pressed(repeat)
|
self.key_handler.get_keys_pressed(repeat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
|
||||||
|
self.key_handler.get_keys_released()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_key_down(&self, key: Key) -> bool {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
self.key_handler.is_key_down(key)
|
self.key_handler.is_key_down(key)
|
||||||
|
|
|
@ -189,6 +189,10 @@ impl Window {
|
||||||
self.key_handler.get_keys_pressed(repeat)
|
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 {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
self.key_handler.is_key_down(key)
|
self.key_handler.is_key_down(key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Window::X11(ref w) => w.is_key_down(key),
|
Window::X11(ref w) => w.is_key_down(key),
|
||||||
|
|
|
@ -590,6 +590,11 @@ impl Window {
|
||||||
self.key_handler.get_keys_pressed(repeat)
|
self.key_handler.get_keys_pressed(repeat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
|
||||||
|
self.key_handler.get_keys_released()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_key_down(&self, key: Key) -> bool {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
self.key_handler.is_key_down(key)
|
self.key_handler.is_key_down(key)
|
||||||
|
|
|
@ -700,6 +700,11 @@ impl Window {
|
||||||
self.key_handler.get_keys_pressed(repeat)
|
self.key_handler.get_keys_pressed(repeat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_keys_released(&self) -> Option<Vec<Key>> {
|
||||||
|
self.key_handler.get_keys_released()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_key_down(&self, key: Key) -> bool {
|
pub fn is_key_down(&self, key: Key) -> bool {
|
||||||
self.key_handler.is_key_down(key)
|
self.key_handler.is_key_down(key)
|
||||||
|
|
Loading…
Reference in a new issue