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 common run conditions to bevy_input #7806

Conversation

jakobhellermann
Copy link
Contributor

Objective

Common run conditions can be very useful for quick and ergonomic changes to when a system runs.
Specifically what I'd like to be able to do is

use bevy::prelude::*;
use bevy::input::common_conditions::input_toggle_active;

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(
      bevy_inspector_egui::quick::WorldInspectorPlugin::default()
        .run_if(input_toggle_active(true, KeyCode::Escape)
    )
    .run();
}

Solution

  • add bevy_input::common_conditions module with input_toggle_active, input_pressed, input_just_pressed, input_just_released

Changelog

  • added common run conditions for bevy_input
  • you can now use .add_system(jump.run_if(input_just_pressed(KeyCode::Space)))

@jakobhellermann jakobhellermann added C-Enhancement A new feature A-ECS Entities, components, systems, and events A-Input Player input via keyboard, mouse, gamepad, and more labels Feb 24, 2023
@alice-i-cecile
Copy link
Member

Quick and dirty! Convenient though.

I'll look into adding the same in LWIM too.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are well-made, well-documented and useful.

I'd like to see bevy_window::close_on_esc use these instead of having the keycode baked in, but I think that should be a seperate PR.

Co-authored-by: Carter Weinberg <weinbergcarter@gmail.com>
/// .run();
/// }
///
/// fn update_pause_state(mut paused: ResMut<Paused>, input: Input<KeyCode>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I think I see what you are going for, but it's a little confusing because I don't think update_pause_state is ever getting called. Should that be in the schedule somewhere?

{
let mut active = default;
move |inputs: Res<Input<T>>| {
active ^= inputs.just_pressed(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind explaining what this symbol is ^= for my smooth brain pls lol. I think I understand but it'd be worth clarifying haha.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's xor assign. So basically if just_pressed { active = !active }.
Reading this I had to think for a minute to know why this works so I should probably just write the simpler clearer way lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because

inactive ^ not pressed -> inactive
inactive ^ pressed -> active
active ^ not pressed -> active
active ^ pressed -> inactive

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, okay I think that makes sense to me lol.

/// }
///
/// ```
pub fn input_toggle_active<T>(default: bool, input: T) -> impl FnMut(Res<Input<T>>) -> bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Where is this function getting called?

Copy link
Contributor

@Carter0 Carter0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it mostly looks good! I'd approve once I understand what's happening with input_toggle_active.

@alice-i-cecile alice-i-cecile 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 Feb 25, 2023
@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Feb 25, 2023
# Objective

Common run conditions can be very useful for quick and ergonomic changes to when a system runs.
Specifically what I'd like to be able to do is
```rust
use bevy::prelude::*;
use bevy::input::common_conditions::input_toggle_active;

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(
      bevy_inspector_egui::quick::WorldInspectorPlugin::default()
        .run_if(input_toggle_active(true, KeyCode::Escape)
    )
    .run();
}
```

## Solution

- add `bevy_input::common_conditions` module with `input_toggle_active`, `input_pressed`, `input_just_pressed`, `input_just_released`

## Changelog

- added common run conditions for `bevy_input`
- you can now use `.add_system(jump.run_if(input_just_pressed(KeyCode::Space)))`
@bors bors bot changed the title add common run conditions to bevy_input [Merged by Bors] - add common run conditions to bevy_input Feb 25, 2023
@bors bors bot closed this Feb 25, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
# Objective

Common run conditions can be very useful for quick and ergonomic changes to when a system runs.
Specifically what I'd like to be able to do is
```rust
use bevy::prelude::*;
use bevy::input::common_conditions::input_toggle_active;

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_plugin(
      bevy_inspector_egui::quick::WorldInspectorPlugin::default()
        .run_if(input_toggle_active(true, KeyCode::Escape)
    )
    .run();
}
```

## Solution

- add `bevy_input::common_conditions` module with `input_toggle_active`, `input_pressed`, `input_just_pressed`, `input_just_released`

## Changelog

- added common run conditions for `bevy_input`
- you can now use `.add_system(jump.run_if(input_just_pressed(KeyCode::Space)))`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Input Player input via keyboard, mouse, gamepad, and more C-Enhancement A new feature 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.

None yet

3 participants