Add a release_all function to Input. (#5011)

Adds a `release_all` function to `Input` that releases all of the currently pressed inputs and marks them as just released.
This commit is contained in:
Hoidigan 2022-06-20 17:35:56 +00:00
parent 3217f216aa
commit 2ec5ff9652

View file

@ -82,6 +82,12 @@ where
}
}
/// Registers a release for all currently pressed inputs.
pub fn release_all(&mut self) {
// Move all items from pressed into just_released
self.just_released.extend(self.pressed.drain());
}
/// Returns `true` if the `input` has just been pressed.
pub fn just_pressed(&self, input: T) -> bool {
self.just_pressed.contains(&input)
@ -197,6 +203,17 @@ mod test {
assert!(input.just_released.contains(&DummyInput::Input1));
}
#[test]
fn test_release_all() {
let mut input = Input::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release_all();
assert!(input.pressed.is_empty());
assert!(input.just_released.contains(&DummyInput::Input1));
assert!(input.just_released.contains(&DummyInput::Input2));
}
#[test]
fn test_just_pressed() {
let mut input = Input::default();