From cb1e7ae28497c87943fed79eb7fb53d30821f22a Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 14 Jul 2023 23:56:24 -0400 Subject: [PATCH] add just pressed input events --- src/input.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/input.rs b/src/input.rs index 0223fbb..b31d24a 100755 --- a/src/input.rs +++ b/src/input.rs @@ -101,6 +101,7 @@ impl Input { // convert state let buttonev = Box::new(match input.state { + ElementState::Pressed if self.is_pressed(code) => ButtonEvent::Pressed(code), ElementState::Pressed => ButtonEvent::JustPressed(code), ElementState::Released => ButtonEvent::Released(code), }); @@ -127,9 +128,8 @@ impl Input { false } - /// TODO: was_just_pressed - - pub fn is_pressed(&self, code: VirtualKeyCode) -> bool { + pub fn was_just_pressed(&self, code: VirtualKeyCode) -> bool { + // get a hash of the key code let mut hasher = DefaultHasher::new(); code.hash(&mut hasher); let inner_hash = hasher.finish(); @@ -137,17 +137,36 @@ impl Input { if let Some(e) = self.events.get(&inner_hash) { if let Some(ev) = e.downcast_ref::>() { match ev { - ButtonEvent::Pressed(v) => { - if v.clone() == code { - return true; - } - }, ButtonEvent::JustPressed(v) => { if v.clone() == code { return true; } }, - ButtonEvent::Released(_) => { + _ => { + return false; + } + } + } + } + + false + } + + pub fn is_pressed(&self, code: VirtualKeyCode) -> bool { + // get a hash of the key code + let mut hasher = DefaultHasher::new(); + code.hash(&mut hasher); + let inner_hash = hasher.finish(); + + if let Some(e) = self.events.get(&inner_hash) { + if let Some(ev) = e.downcast_ref::>() { + match ev { + ButtonEvent::Pressed(v) | ButtonEvent::JustPressed(v) => { + if v.clone() == code { + return true; + } + }, + _ => { return false; } }