2022-05-16 13:53:20 +00:00
|
|
|
//! Iterates and prints gamepad input and connection events.
|
|
|
|
|
2020-11-10 03:26:08 +00:00
|
|
|
use bevy::{
|
Gamepad events refactor (#6965)
# 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.
2023-01-09 19:24:52 +00:00
|
|
|
input::gamepad::{
|
|
|
|
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnectionEvent, GamepadEvent,
|
|
|
|
},
|
2020-11-10 03:26:08 +00:00
|
|
|
prelude::*,
|
|
|
|
};
|
2020-10-21 17:27:00 +00:00
|
|
|
|
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2023-03-18 01:45:34 +00:00
|
|
|
.add_systems(Update, (gamepad_events, gamepad_ordered_events))
|
2020-10-21 17:27:00 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
Gamepad events refactor (#6965)
# 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.
2023-01-09 19:24:52 +00:00
|
|
|
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),
|
2020-10-21 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|