Skip to content

Commit 2b27998

Browse files
zicklagItsDoot
authored andcommitted
Improve Gamepad DPad Button Detection (bevyengine#5220)
# Objective - Enable the `axis_dpad_to_button` gilrs filter to map hats to dpad buttons on supported remotes. - Fixes Leafwing-Studios/leafwing-input-manager#149 - Might have fixed the confusion related to bevyengine#3229 ## Solution - Enables the `axis_dpad_to_button` filter in `gilrs` which will use it's remote mapping information to see if there are hats mapped to dpads for that remote model. I don't really understand the logic it uses exactly, but it is usually enabled by default in gilrs and I believe it probably leads to more intuitive mapping compared to the current situation of dpad buttons being mapped to an axis. - Removes the `GamepadAxisType::DPadX` and `GamepadAxisType::DPadY` enum variants to avoid user confusion. Those variants should never be emitted anyway, for all supported remotes. --- ## Changelog ### Changed - Removed `GamepadAxisType::DPadX` and `GamepadAxisType::DPadY` in favor of using `GamepadButtonType::DPad[Up/Down/Left/Right]` instead. ## Migration Guide If your game reads gamepad events or queries the axis state of `GamePadAxisType::DPadX` or `GamePadAxisType::DPadY`, then you must migrate your code to check whether or not the `GamepadButtonType::DPadUp`, `GamepadButtonType::DPadDown`, etc. buttons were pressed instead.
1 parent 2ba0e86 commit 2b27998

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

crates/bevy_gilrs/src/converter.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ pub fn convert_axis(axis: gilrs::Axis) -> Option<GamepadAxisType> {
3737
gilrs::Axis::RightStickX => Some(GamepadAxisType::RightStickX),
3838
gilrs::Axis::RightStickY => Some(GamepadAxisType::RightStickY),
3939
gilrs::Axis::RightZ => Some(GamepadAxisType::RightZ),
40-
gilrs::Axis::DPadX => Some(GamepadAxisType::DPadX),
41-
gilrs::Axis::DPadY => Some(GamepadAxisType::DPadY),
42-
gilrs::Axis::Unknown => None,
40+
// The `axis_dpad_to_button` gilrs filter should filter out all DPadX and DPadY events. If
41+
// it doesn't then we probably need an entry added to the following repo and an update to
42+
// GilRs to use the updated database: https://github.com/gabomdq/SDL_GameControllerDB
43+
gilrs::Axis::Unknown | gilrs::Axis::DPadX | gilrs::Axis::DPadY => None,
4344
}
4445
}

crates/bevy_gilrs/src/gilrs_system.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::converter::{convert_axis, convert_button, convert_gamepad_id};
22
use bevy_ecs::event::EventWriter;
33
use bevy_ecs::system::{NonSend, NonSendMut};
44
use bevy_input::{gamepad::GamepadEventRaw, prelude::*};
5-
use gilrs::{EventType, Gilrs};
5+
use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs};
66

77
pub fn gilrs_event_startup_system(gilrs: NonSend<Gilrs>, mut events: EventWriter<GamepadEventRaw>) {
88
for (id, _) in gilrs.gamepads() {
@@ -14,7 +14,12 @@ pub fn gilrs_event_startup_system(gilrs: NonSend<Gilrs>, mut events: EventWriter
1414
}
1515

1616
pub fn gilrs_event_system(mut gilrs: NonSendMut<Gilrs>, mut events: EventWriter<GamepadEventRaw>) {
17-
while let Some(gilrs_event) = gilrs.next_event() {
17+
while let Some(gilrs_event) = gilrs
18+
.next_event()
19+
.filter_ev(&axis_dpad_to_button, &mut gilrs)
20+
{
21+
gilrs.update(&gilrs_event);
22+
1823
match gilrs_event.event {
1924
EventType::Connected => {
2025
events.send(GamepadEventRaw::new(

crates/bevy_input/src/gamepad.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ pub enum GamepadAxisType {
135135
RightStickX,
136136
RightStickY,
137137
RightZ,
138-
DPadX,
139-
DPadY,
140138
}
141139

142140
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -412,15 +410,13 @@ const ALL_BUTTON_TYPES: [GamepadButtonType; 19] = [
412410
GamepadButtonType::DPadRight,
413411
];
414412

415-
const ALL_AXIS_TYPES: [GamepadAxisType; 8] = [
413+
const ALL_AXIS_TYPES: [GamepadAxisType; 6] = [
416414
GamepadAxisType::LeftStickX,
417415
GamepadAxisType::LeftStickY,
418416
GamepadAxisType::LeftZ,
419417
GamepadAxisType::RightStickX,
420418
GamepadAxisType::RightStickY,
421419
GamepadAxisType::RightZ,
422-
GamepadAxisType::DPadX,
423-
GamepadAxisType::DPadY,
424420
];
425421

426422
#[cfg(test)]

0 commit comments

Comments
 (0)