add just pressed input events

This commit is contained in:
SeanOMik 2023-07-14 23:56:24 -04:00
parent f5bfa93f63
commit cb1e7ae284
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
1 changed files with 28 additions and 9 deletions

View File

@ -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::<ButtonEvent<KeyCode>>() {
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::<ButtonEvent<KeyCode>>() {
match ev {
ButtonEvent::Pressed(v) | ButtonEvent::JustPressed(v) => {
if v.clone() == code {
return true;
}
},
_ => {
return false;
}
}