mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
Add convenience methods for checking a set of inputs (#2760)
# 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]) { ```
This commit is contained in:
parent
af20cad830
commit
321d998615
2 changed files with 17 additions and 2 deletions
|
@ -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);
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
|
|
@ -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!");
|
||||
|
|
Loading…
Reference in a new issue