mirror of
https://github.com/bevyengine/bevy
synced 2024-12-21 18:43:07 +00:00
e788e3bc83
# Objective - Significantly improve the ergonomics of gamepads and allow new features Gamepads are a bit unergonomic to work with, they use resources but unlike other inputs, they are not limited to a single gamepad, to get around this it uses an identifier (Gamepad) to interact with anything causing all sorts of issues. 1. There are too many: Gamepads, GamepadSettings, GamepadInfo, ButtonInput<T>, 2 Axis<T>. 2. ButtonInput/Axis generic methods become really inconvenient to use e.g. any_pressed() 3. GamepadButton/Axis structs are unnecessary boilerplate: ```rust for gamepad in gamepads.iter() { if button_inputs.just_pressed(GamepadButton::new(gamepad, GamepadButtonType::South)) { info!("{:?} just pressed South", gamepad); } else if button_inputs.just_released(GamepadButton::new(gamepad, GamepadButtonType::South)) { info!("{:?} just released South", gamepad); } } ``` 4. Projects often need to create resources to store the selected gamepad and have to manually check if their gamepad is still valid anyways. - Previously attempted by #3419 and #12674 ## Solution - Implement gamepads as entities. Using entities solves all the problems above and opens new possibilities. 1. Reduce boilerplate and allows iteration ```rust let is_pressed = gamepads_buttons.iter().any(|buttons| buttons.pressed(GamepadButtonType::South)) ``` 2. ButtonInput/Axis generic methods become ergonomic again ```rust gamepad_buttons.any_just_pressed([GamepadButtonType::Start, GamepadButtonType::Select]) ``` 3. Reduces the number of public components significantly (Gamepad, GamepadSettings, GamepadButtons, GamepadAxes) 4. Components are highly convenient. Gamepad optional features could now be expressed naturally (`Option<Rumble> or Option<Gyro>`), allows devs to attach their own components and filter them, so code like this becomes possible: ```rust fn move_player<const T: usize>( player: Query<&Transform, With<Player<T>>>, gamepads_buttons: Query<&GamepadButtons, With<Player<T>>>, ) { if let Ok(gamepad_buttons) = gamepads_buttons.get_single() { if gamepad_buttons.pressed(GamepadButtonType::South) { // move player } } } ``` --- ## Follow-up - [ ] Run conditions? - [ ] Rumble component # Changelog ## Added TODO ## Changed TODO ## Removed TODO ## Migration Guide TODO --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
79 lines
2.9 KiB
Rust
79 lines
2.9 KiB
Rust
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
#![forbid(unsafe_code)]
|
|
#![doc(
|
|
html_logo_url = "https://bevyengine.org/assets/icon.png",
|
|
html_favicon_url = "https://bevyengine.org/assets/icon.png"
|
|
)]
|
|
|
|
//! Systems and type definitions for gamepad handling in Bevy.
|
|
//!
|
|
//! This crate is built on top of [GilRs](gilrs), a library
|
|
//! that handles abstracting over platform-specific gamepad APIs.
|
|
|
|
mod converter;
|
|
mod gilrs_system;
|
|
mod rumble;
|
|
|
|
use bevy_app::{App, Plugin, PostUpdate, PreStartup, PreUpdate};
|
|
use bevy_ecs::entity::EntityHashMap;
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_input::InputSystem;
|
|
use bevy_utils::{synccell::SyncCell, tracing::error, HashMap};
|
|
use gilrs::GilrsBuilder;
|
|
use gilrs_system::{gilrs_event_startup_system, gilrs_event_system};
|
|
use rumble::{play_gilrs_rumble, RunningRumbleEffects};
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), derive(Resource))]
|
|
pub(crate) struct Gilrs(pub SyncCell<gilrs::Gilrs>);
|
|
|
|
/// A [`resource`](Resource) with the mapping of connected [`gilrs::GamepadId`] and their [`Entity`].
|
|
#[derive(Debug, Default, Resource)]
|
|
pub(crate) struct GilrsGamepads {
|
|
/// Mapping of [`Entity`] to [`gilrs::GamepadId`].
|
|
pub(crate) entity_to_id: EntityHashMap<gilrs::GamepadId>,
|
|
/// Mapping of [`gilrs::GamepadId`] to [`Entity`].
|
|
pub(crate) id_to_entity: HashMap<gilrs::GamepadId, Entity>,
|
|
}
|
|
|
|
impl GilrsGamepads {
|
|
/// Returns the [`Entity`] assigned to a connected [`gilrs::GamepadId`].
|
|
pub fn get_entity(&self, gamepad_id: gilrs::GamepadId) -> Option<Entity> {
|
|
self.id_to_entity.get(&gamepad_id).copied()
|
|
}
|
|
|
|
/// Returns the [`gilrs::GamepadId`] assigned to a gamepad [`Entity`].
|
|
pub fn get_gamepad_id(&self, entity: Entity) -> Option<gilrs::GamepadId> {
|
|
self.entity_to_id.get(&entity).copied()
|
|
}
|
|
}
|
|
|
|
/// Plugin that provides gamepad handling to an [`App`].
|
|
#[derive(Default)]
|
|
pub struct GilrsPlugin;
|
|
|
|
/// Updates the running gamepad rumble effects.
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
|
|
pub struct RumbleSystem;
|
|
|
|
impl Plugin for GilrsPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
match GilrsBuilder::new()
|
|
.with_default_filters(false)
|
|
.set_update_state(false)
|
|
.build()
|
|
{
|
|
Ok(gilrs) => {
|
|
#[cfg(target_arch = "wasm32")]
|
|
app.insert_non_send_resource(Gilrs(SyncCell::new(gilrs)));
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
app.insert_resource(Gilrs(SyncCell::new(gilrs)));
|
|
app.init_resource::<GilrsGamepads>();
|
|
app.init_resource::<RunningRumbleEffects>()
|
|
.add_systems(PreStartup, gilrs_event_startup_system)
|
|
.add_systems(PreUpdate, gilrs_event_system.before(InputSystem))
|
|
.add_systems(PostUpdate, play_gilrs_rumble.in_set(RumbleSystem));
|
|
}
|
|
Err(err) => error!("Failed to start Gilrs. {}", err),
|
|
}
|
|
}
|
|
}
|