Skip to content

Commit

Permalink
Add a release_all function to Input. (bevyengine#5011)
Browse files Browse the repository at this point in the history
Adds a `release_all` function to `Input` that releases all of the currently pressed inputs and marks them as just released.
  • Loading branch information
Hoidigan authored and ItsDoot committed Feb 1, 2023
1 parent 0ce6eea commit d99ea9f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/bevy_input/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d99ea9f

Please sign in to comment.