Skip to content

Commit

Permalink
Add convenience methods for checking a set of inputs (#2760)
Browse files Browse the repository at this point in the history
# Objective

Make it easier to check if some set of inputs matches a key, such as if you want to allow all of space or up or w for jumping.

Currently, this requires:
```rust
if keyboard.pressed(KeyCode::Space)
            || keyboard.pressed(KeyCode::Up)
            || keyboard.pressed(KeyCode::W) {
    // ...
```

## Solution

Add an implementation of the helper methods, which very simply iterate through the items, used as:
```rust
if keyboard.any_pressed([KeyCode::Space, KeyCode::Up, KeyCode::W]) {
```
  • Loading branch information
DJMcNab committed Sep 1, 2021
1 parent af20cad commit 321d998
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit 321d998

Please sign in to comment.