From 826ea05bd3310a7d9dabf947d04390106d500e83 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 31 Aug 2021 23:01:35 +0100 Subject: [PATCH 1/2] Add convenience methods for checking a set of inputs --- crates/bevy_input/src/input.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index c1bea3f77b88a..09aeea7b88d97 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -63,6 +63,11 @@ where self.pressed.contains(&input) } + /// Check if any item in `inputs` has been pressed. + pub fn any_pressed(&self, inputs: impl IntoIterator) -> bool { + inputs.into_iter().any(|it| self.pressed(it)) + } + /// Register a release for input `input`. pub fn release(&mut self, input: T) { self.pressed.remove(&input); @@ -74,6 +79,11 @@ where self.just_pressed.contains(&input) } + /// Check if any item in `inputs` has just been pressed. + pub fn any_just_pressed(&self, inputs: impl IntoIterator) -> bool { + inputs.into_iter().any(|it| self.just_pressed(it)) + } + /// Clear the "just pressed" state of `input`. Future calls to [`Input::just_pressed`] for the /// given input will return false until a new press event occurs. /// Returns true if `input` is currently "just pressed" @@ -86,6 +96,11 @@ where self.just_released.contains(&input) } + /// Check if any item in `inputs` has just been released. + pub fn any_just_released(&self, inputs: impl IntoIterator) -> bool { + inputs.into_iter().any(|it| self.just_released(it)) + } + /// Clear the "just released" state of `input`. Future calls to [`Input::just_released`] for the /// given input will return false until a new release event occurs. /// Returns true if `input` is currently "just released" From a65bc9c71347ed6ed5ed0f9cd6b738d5fc02f535 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 31 Aug 2021 23:08:34 +0100 Subject: [PATCH 2/2] Use `any_pressed`` in the example --- examples/input/keyboard_modifiers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/input/keyboard_modifiers.rs b/examples/input/keyboard_modifiers.rs index a22275bb0a2ee..5b6ebfa3afce2 100644 --- a/examples/input/keyboard_modifiers.rs +++ b/examples/input/keyboard_modifiers.rs @@ -12,8 +12,8 @@ fn main() { /// This system prints when Ctrl + Shift + A is pressed fn keyboard_input_system(input: Res>) { - let shift = input.pressed(KeyCode::LShift) || input.pressed(KeyCode::RShift); - let ctrl = input.pressed(KeyCode::LControl) || input.pressed(KeyCode::RControl); + let shift = input.any_pressed([KeyCode::LShift, KeyCode::RShift]); + let ctrl = input.any_pressed([KeyCode::LControl, KeyCode::RControl]); if ctrl && shift && input.just_pressed(KeyCode::A) { info!("Just pressed Ctrl + Shift + A!");