mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
e94215c4c6
# Objective - Remove redundant gamepad events - Simplify consuming gamepad events. - Refactor: Separate handling of gamepad events into multiple systems. ## Solution - Removed `GamepadEventRaw`, and `GamepadEventType`. - Added bespoke `GamepadConnectionEvent`, `GamepadAxisChangedEvent`, and `GamepadButtonChangedEvent`. - Refactored `gamepad_event_system`. - Added `gamepad_button_event_system`, `gamepad_axis_event_system`, and `gamepad_connection_system`, which update the `Input` and `Axis` resources using their corresponding event type. Gamepad events are now handled in their own systems and have their own types. This allows for querying for gamepad events without having to match on `GamepadEventType` and makes creating handlers for specific gamepad event types, like a `GamepadConnectionEvent` or `GamepadButtonChangedEvent` possible. We remove `GamepadEventRaw` by filtering the gamepad events, using `GamepadSettings`, _at the source_, in `bevy_gilrs`. This way we can create `GamepadEvent`s directly and avoid creating `GamepadEventRaw` which do not pass the user defined filters. We expose ordered `GamepadEvent`s and we can respond to individual gamepad event types. ## Migration Guide - Replace `GamepadEvent` and `GamepadEventRaw` types with their specific gamepad event type.
51 lines
1.7 KiB
Rust
51 lines
1.7 KiB
Rust
//! Iterates and prints gamepad input and connection events.
|
|
|
|
use bevy::{
|
|
input::gamepad::{
|
|
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnectionEvent, GamepadEvent,
|
|
},
|
|
prelude::*,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_system(gamepad_events)
|
|
.add_system(gamepad_ordered_events)
|
|
.run();
|
|
}
|
|
|
|
fn gamepad_events(
|
|
mut gamepad_connection_events: EventReader<GamepadConnectionEvent>,
|
|
mut gamepad_axis_events: EventReader<GamepadAxisChangedEvent>,
|
|
mut gamepad_button_events: EventReader<GamepadButtonChangedEvent>,
|
|
) {
|
|
for connection_event in gamepad_connection_events.iter() {
|
|
info!("{:?}", connection_event);
|
|
}
|
|
for axis_event in gamepad_axis_events.iter() {
|
|
info!(
|
|
"{:?} of {:?} is changed to {}",
|
|
axis_event.axis_type, axis_event.gamepad, axis_event.value
|
|
);
|
|
}
|
|
for button_event in gamepad_button_events.iter() {
|
|
info!(
|
|
"{:?} of {:?} is changed to {}",
|
|
button_event.button_type, button_event.gamepad, button_event.value
|
|
);
|
|
}
|
|
}
|
|
|
|
// If you require in-frame relative event ordering, you can also read the `Gamepad` event
|
|
// stream directly. For standard use-cases, reading the events individually or using the
|
|
// `Input<T>` or `Axis<T>` resources is preferable.
|
|
fn gamepad_ordered_events(mut gamepad_events: EventReader<GamepadEvent>) {
|
|
for gamepad_event in gamepad_events.iter() {
|
|
match gamepad_event {
|
|
GamepadEvent::Connection(connection_event) => info!("{:?}", connection_event),
|
|
GamepadEvent::Button(button_event) => info!("{:?}", button_event),
|
|
GamepadEvent::Axis(axis_event) => info!("{:?}", axis_event),
|
|
}
|
|
}
|
|
}
|