Add method to check if all inputs are pressed (#11010)

# Objective

- Provide way to check whether multiple inputs are pressed.

## Solution

- Add `all_pressed` method that checks if all inputs are currently being
pressed.
This commit is contained in:
Mateusz Wachowiak 2023-12-18 02:45:43 +01:00 committed by GitHub
parent 9a89fc44f4
commit 2c7eab1b4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -86,6 +86,11 @@ where
inputs.into_iter().any(|it| self.pressed(it)) 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`. /// Registers a release for the given `input`.
pub fn release(&mut self, input: T) { pub fn release(&mut self, input: T) {
// Returns `true` if the `input` was pressed. // Returns `true` if the `input` was pressed.
@ -217,6 +222,19 @@ mod test {
assert!(input.any_pressed([DummyInput::Input1, DummyInput::Input2])); 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] #[test]
fn test_release() { fn test_release() {
let mut input = ButtonInput::default(); let mut input = ButtonInput::default();