Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add convenience methods for checking a set of inputs #2760

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/bevy_input/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = T>) -> 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);
Expand All @@ -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<Item = T>) -> 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"
Expand All @@ -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<Item = T>) -> 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"
Expand Down
4 changes: 2 additions & 2 deletions examples/input/keyboard_modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn main() {

/// This system prints when Ctrl + Shift + A is pressed
fn keyboard_input_system(input: Res<Input<KeyCode>>) {
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!");
Expand Down