Skip to content

Commit

Permalink
Add method to check if all inputs are pressed (#11010)
Browse files Browse the repository at this point in the history
# Objective

- Provide way to check whether multiple inputs are pressed.

## Solution

- Add `all_pressed` method that checks if all inputs are currently being
pressed.
  • Loading branch information
matiqo15 committed Dec 18, 2023
1 parent 9a89fc4 commit 2c7eab1
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crates/bevy_input/src/button_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ where
inputs.into_iter().any(|it| self.pressed(it))
}

/// Returns `true` if all items in `inputs` have been pressed.
pub fn all_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().all(|it| self.pressed(it))
}

/// Registers a release for the given `input`.
pub fn release(&mut self, input: T) {
// Returns `true` if the `input` was pressed.
Expand Down Expand Up @@ -217,6 +222,19 @@ mod test {
assert!(input.any_pressed([DummyInput::Input1, DummyInput::Input2]));
}

#[test]
fn test_all_pressed() {
let mut input = ButtonInput::default();
assert!(!input.all_pressed([DummyInput::Input1]));
assert!(!input.all_pressed([DummyInput::Input2]));
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input1);
assert!(input.all_pressed([DummyInput::Input1]));
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input2);
assert!(input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
}

#[test]
fn test_release() {
let mut input = ButtonInput::default();
Expand Down

0 comments on commit 2c7eab1

Please sign in to comment.