From 2c7eab1b4c4ec6c533b6b609d5ddf8a7282f2c4f Mon Sep 17 00:00:00 2001 From: Mateusz Wachowiak Date: Mon, 18 Dec 2023 02:45:43 +0100 Subject: [PATCH] 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. --- crates/bevy_input/src/button_input.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/bevy_input/src/button_input.rs b/crates/bevy_input/src/button_input.rs index 189f714355..8e4c05e4b9 100644 --- a/crates/bevy_input/src/button_input.rs +++ b/crates/bevy_input/src/button_input.rs @@ -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) -> 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. @@ -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();