Skip to content

Commit 2c7eab1

Browse files
authored
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.
1 parent 9a89fc4 commit 2c7eab1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

crates/bevy_input/src/button_input.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ where
8686
inputs.into_iter().any(|it| self.pressed(it))
8787
}
8888

89+
/// Returns `true` if all items in `inputs` have been pressed.
90+
pub fn all_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
91+
inputs.into_iter().all(|it| self.pressed(it))
92+
}
93+
8994
/// Registers a release for the given `input`.
9095
pub fn release(&mut self, input: T) {
9196
// Returns `true` if the `input` was pressed.
@@ -217,6 +222,19 @@ mod test {
217222
assert!(input.any_pressed([DummyInput::Input1, DummyInput::Input2]));
218223
}
219224

225+
#[test]
226+
fn test_all_pressed() {
227+
let mut input = ButtonInput::default();
228+
assert!(!input.all_pressed([DummyInput::Input1]));
229+
assert!(!input.all_pressed([DummyInput::Input2]));
230+
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
231+
input.press(DummyInput::Input1);
232+
assert!(input.all_pressed([DummyInput::Input1]));
233+
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
234+
input.press(DummyInput::Input2);
235+
assert!(input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
236+
}
237+
220238
#[test]
221239
fn test_release() {
222240
let mut input = ButtonInput::default();

0 commit comments

Comments
 (0)