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

Add GamepadButtonInput event #9008

Merged
merged 7 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 25 additions & 1 deletion crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Axis, Input};
use crate::{Axis, ButtonState, Input};
use bevy_ecs::event::{Event, EventReader, EventWriter};
use bevy_ecs::{
change_detection::DetectChangesMut,
Expand Down Expand Up @@ -253,6 +253,21 @@ impl GamepadButton {
}
}

/// A gamepad button input event.
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct GamepadButtonInput {
/// The gamepad button assigned to the event.
pub button: GamepadButton,
/// The pressed state of the button.
pub state: ButtonState,
}

/// A type of a [`GamepadAxis`].
///
/// ## Usage
Expand Down Expand Up @@ -1134,6 +1149,7 @@ pub fn gamepad_button_event_system(
mut button_events: EventReader<GamepadButtonChangedEvent>,
mut button_input: ResMut<Input<GamepadButton>>,
mut button_axis: ResMut<Axis<GamepadButton>>,
mut button_input_events: EventWriter<GamepadButtonInput>,
settings: Res<GamepadSettings>,
) {
for button_event in button_events.iter() {
Expand All @@ -1145,8 +1161,16 @@ pub fn gamepad_button_event_system(
// We don't have to check if the button was previously pressed
// because that check is performed within Input<T>::release()
button_input.release(button);
button_input_events.send(GamepadButtonInput {
button,
state: ButtonState::Released,
});
} else if button_property.is_pressed(value) {
button_input.press(button);
button_input_events.send(GamepadButtonInput {
button,
state: ButtonState::Pressed,
});
};

button_axis.set(button, value);
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use gamepad::{
gamepad_axis_event_system, gamepad_button_event_system, gamepad_connection_system,
gamepad_event_system, AxisSettings, ButtonAxisSettings, ButtonSettings, Gamepad, GamepadAxis,
GamepadAxisChangedEvent, GamepadAxisType, GamepadButton, GamepadButtonChangedEvent,
GamepadButtonType, GamepadConnection, GamepadConnectionEvent, GamepadEvent,
GamepadButtonInput, GamepadButtonType, GamepadConnection, GamepadConnectionEvent, GamepadEvent,
GamepadRumbleRequest, GamepadSettings, Gamepads,
};

Expand Down Expand Up @@ -74,6 +74,7 @@ impl Plugin for InputPlugin {
// gamepad
.add_event::<GamepadConnectionEvent>()
.add_event::<GamepadButtonChangedEvent>()
.add_event::<GamepadButtonInput>()
Copy link
Member

Choose a reason for hiding this comment

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

I don't like it, but almost all other events have their name ending with Event.

Could you either rename that new event to GamepadButtonInputEvent, or renamed all the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GamepadButtonInput was chosen to match the naming of KeyboardInput; I agree the inconsistent naming isn't great, so it might be good to rename both of them to end with Event.

.add_event::<GamepadAxisChangedEvent>()
.add_event::<GamepadEvent>()
.add_event::<GamepadRumbleRequest>()
Expand Down Expand Up @@ -130,6 +131,7 @@ impl Plugin for InputPlugin {
.register_type::<GamepadConnection>()
.register_type::<GamepadButtonType>()
.register_type::<GamepadButton>()
.register_type::<GamepadButtonInput>()
.register_type::<GamepadAxisType>()
.register_type::<GamepadAxis>()
.register_type::<GamepadSettings>()
Expand Down
25 changes: 16 additions & 9 deletions examples/input/gamepad_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use bevy::{
input::gamepad::{
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnectionEvent, GamepadEvent,
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadButtonInput,
GamepadConnectionEvent, GamepadEvent,
},
prelude::*,
};
Expand All @@ -15,25 +16,31 @@ fn main() {
}

fn gamepad_events(
mut gamepad_connection_events: EventReader<GamepadConnectionEvent>,
mut gamepad_axis_events: EventReader<GamepadAxisChangedEvent>,
mut gamepad_button_events: EventReader<GamepadButtonChangedEvent>,
mut connection_events: EventReader<GamepadConnectionEvent>,
mut axis_changed_events: EventReader<GamepadAxisChangedEvent>,
mut button_changed_events: EventReader<GamepadButtonChangedEvent>,
mut button_input_events: EventReader<GamepadButtonInput>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would benefit from a comment explaining the difference between GamepadButtonInput and GamepadButtonChangedEvent

) {
for connection_event in gamepad_connection_events.iter() {
for connection_event in connection_events.iter() {
info!("{:?}", connection_event);
}
for axis_event in gamepad_axis_events.iter() {
for axis_changed_event in axis_changed_events.iter() {
info!(
"{:?} of {:?} is changed to {}",
axis_event.axis_type, axis_event.gamepad, axis_event.value
axis_changed_event.axis_type, axis_changed_event.gamepad, axis_changed_event.value
);
}
for button_event in gamepad_button_events.iter() {
for button_changed_event in button_changed_events.iter() {
info!(
"{:?} of {:?} is changed to {}",
button_event.button_type, button_event.gamepad, button_event.value
button_changed_event.button_type,
button_changed_event.gamepad,
button_changed_event.value
);
}
for button_input_event in button_input_events.iter() {
info!("{:?}", button_input_event);
}
}

// If you require in-frame relative event ordering, you can also read the `Gamepad` event
Expand Down