bevy/examples/input/gamepad_input_events.rs
dataphract bcc33f6757 feat: add GamepadInfo, expose gamepad names (#6342)
# Objective

Fixes #6339.

## Solution

This PR adds a new type, `GamepadInfo`, which holds metadata associated with a particular `Gamepad`. The `Gamepads` resource now holds a `HashMap<Gamepad, GamepadInfo>`. The `GamepadInfo` is created when the gamepad backend (by default `bevy_gilrs`) emits a "gamepad connected" event.

The `gamepad_viewer` example has been updated to showcase the new functionality.

Before:

![bevy-gamepad-old](https://user-images.githubusercontent.com/86984145/197359427-2130a3c0-bd8a-4683-ae24-2a9eaa98b586.png)

After:

![bevy-gamepad-new](https://user-images.githubusercontent.com/86984145/197359429-f7963163-df26-4906-af7f-6186fe3bd338.png)


---

## Changelog

### Added

- Added `GamepadInfo`.
- Added `Gamepads::name()`, which returns the name of the specified gamepad if it exists.

### Changed

- `GamepadEventType::Connected` is now a tuple variant with a single field of type `GamepadInfo`.
- Since `GamepadInfo` is not `Copy`, `GamepadEventType` is no longer `Copy`. The same is true of `GamepadEvent` and `GamepadEventRaw`.

## Migration Guide

- Pattern matches on `GamepadEventType::Connected` will need to be updated, as the form of the variant has changed.
- Code that requires `GamepadEvent`, `GamepadEventRaw` or `GamepadEventType` to be `Copy` will need to be updated.
2022-10-24 14:33:50 +00:00

38 lines
1.1 KiB
Rust

//! Iterates and prints gamepad input and connection events.
use bevy::{
input::gamepad::{GamepadEvent, GamepadEventType},
prelude::*,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_system(gamepad_events)
.run();
}
fn gamepad_events(mut gamepad_event: EventReader<GamepadEvent>) {
for event in gamepad_event.iter() {
match event.event_type {
GamepadEventType::Connected(_) => {
info!("{:?} Connected", event.gamepad);
}
GamepadEventType::Disconnected => {
info!("{:?} Disconnected", event.gamepad);
}
GamepadEventType::ButtonChanged(button_type, value) => {
info!(
"{:?} of {:?} is changed to {}",
button_type, event.gamepad, value
);
}
GamepadEventType::AxisChanged(axis_type, value) => {
info!(
"{:?} of {:?} is changed to {}",
axis_type, event.gamepad, value
);
}
}
}
}