Skip to content

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

Closed
DJMcNab wants to merge 2 commits into
bevyengine:mainfrom
DJMcNab:any_pressed
Closed

DJMcNab wants to merge 2 commits into
bevyengine:mainfrom
DJMcNab:any_pressed

Conversation

@DJMcNab

@DJMcNab DJMcNab commented Aug 31, 2021

Copy link
Copy Markdown
Member

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:

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:

if keyboard.any_pressed([KeyCode::Space, KeyCode::Up, KeyCode::W]) {

@github-actions github-actions Bot added the S-Needs-Triage This issue needs to be labelled label Aug 31, 2021
@DJMcNab DJMcNab added A-Input Player input via keyboard, mouse, gamepad, and more C-Feature A new feature, making something new possible and removed S-Needs-Triage This issue needs to be labelled labels Aug 31, 2021
@alice-i-cecile

Copy link
Copy Markdown
Member

Can you add all_pressed as well? This is particularly useful for modifier keys.

@DJMcNab

DJMcNab commented Sep 1, 2021

Copy link
Copy Markdown
Member Author

Maybe? I feel like that is less useful, because all the modifier keys have at least two versions - e.g. left control and right control.

I definitely would add that if there's consensus that it's useful though.

@mockersf mockersf added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Sep 1, 2021
@cart

cart commented Sep 1, 2021

Copy link
Copy Markdown
Member

Definitely looks useful! Some "due diligence" questions:

  1. Would we need this if/when we add a proper "map-able action system"? Ex: action = Jump, mapped_keys = Keyboard::A, Joystick::X. We should reflexively push back against multiple apis that accomplish the same goal.
  2. Should we just build an action system instead? Should we merge this now and then remove it when the action system is built? Should we keep both?

@TheRawMeatball

Copy link
Copy Markdown
Member

I think merge now, think about action system later is the most reasonable choice.

@DJMcNab

DJMcNab commented Sep 1, 2021

Copy link
Copy Markdown
Member Author
  1. If the action system would remove the Input struct to force you to use it, then this would naturally slide off.
    If someone would use this method, the alternative that they're going to do is an or of multiple calls to this, which gets messy.
  2. This is solving a pain point now, instead of in (indeterminate time in the future).

@cart

cart commented Sep 1, 2021

Copy link
Copy Markdown
Member

Cool I agree that merging this now is probably the right choice, just didn't want to bias things when introducing the questions :). I think all_pressed would also be a useful addition.

@cart

cart commented Sep 1, 2021

Copy link
Copy Markdown
Member

bors r+

bors Bot pushed a commit that referenced this pull request Sep 1, 2021
# 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]) {
```
@bors bors Bot changed the title Add convenience methods for checking a set of inputs [Merged by Bors] - Add convenience methods for checking a set of inputs Sep 1, 2021
@bors bors Bot closed this Sep 1, 2021
@DJMcNab
DJMcNab deleted the any_pressed branch September 2, 2021 07:38
DJMcNab pushed a commit to DJMcNab/bevy-series1 that referenced this pull request Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Input Player input via keyboard, mouse, gamepad, and more C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants