add just pressed input events
This commit is contained in:
parent
f5bfa93f63
commit
cb1e7ae284
37
src/input.rs
37
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::<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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue