input: allow multiple gamepad inputs to be registered for one button in one frame (#9446)

# Objective

- Currently, (AFAIC, accidentally) after registering an event for a
Gilrs button event, we ignore all subsequent events for the same button
in the same frame, because we don't update our filter. This is rare, but
I noticed it while adding gamepad support to a terminal app rendering at
15fps.
- Related to #4664, but does not quite fix it.

## Solution

- Move the edit to the `Axis<GamepadButton>` resource to when we read
the events from Gilrs.
This commit is contained in:
radiish 2023-08-15 23:50:29 +02:00 committed by GitHub
parent f99dcadf8a
commit 632ef0c823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View file

@ -1,6 +1,6 @@
use crate::converter::{convert_axis, convert_button, convert_gamepad_id}; use crate::converter::{convert_axis, convert_button, convert_gamepad_id};
use bevy_ecs::event::EventWriter; use bevy_ecs::event::EventWriter;
use bevy_ecs::system::{NonSend, NonSendMut, Res}; use bevy_ecs::system::{NonSend, NonSendMut, Res, ResMut};
use bevy_input::gamepad::{ use bevy_input::gamepad::{
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnection, GamepadConnectionEvent, GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnection, GamepadConnectionEvent,
GamepadSettings, GamepadSettings,
@ -29,8 +29,8 @@ pub fn gilrs_event_startup_system(
pub fn gilrs_event_system( pub fn gilrs_event_system(
mut gilrs: NonSendMut<Gilrs>, mut gilrs: NonSendMut<Gilrs>,
mut events: EventWriter<GamepadEvent>, mut events: EventWriter<GamepadEvent>,
mut gamepad_buttons: ResMut<Axis<GamepadButton>>,
gamepad_axis: Res<Axis<GamepadAxis>>, gamepad_axis: Res<Axis<GamepadAxis>>,
gamepad_buttons: Res<Axis<GamepadButton>>,
gamepad_settings: Res<GamepadSettings>, gamepad_settings: Res<GamepadSettings>,
) { ) {
while let Some(gilrs_event) = gilrs while let Some(gilrs_event) = gilrs
@ -65,6 +65,9 @@ pub fn gilrs_event_system(
GamepadButtonChangedEvent::new(gamepad, button_type, filtered_value) GamepadButtonChangedEvent::new(gamepad, button_type, filtered_value)
.into(), .into(),
); );
// Update the current value prematurely so that `old_value` is correct in
// future iterations of the loop.
gamepad_buttons.set(button, filtered_value);
} }
} }
} }

View file

@ -1133,7 +1133,6 @@ pub fn gamepad_axis_event_system(
pub fn gamepad_button_event_system( pub fn gamepad_button_event_system(
mut button_events: EventReader<GamepadButtonChangedEvent>, mut button_events: EventReader<GamepadButtonChangedEvent>,
mut button_input: ResMut<Input<GamepadButton>>, mut button_input: ResMut<Input<GamepadButton>>,
mut button_axis: ResMut<Axis<GamepadButton>>,
settings: Res<GamepadSettings>, settings: Res<GamepadSettings>,
) { ) {
for button_event in button_events.iter() { for button_event in button_events.iter() {
@ -1148,8 +1147,6 @@ pub fn gamepad_button_event_system(
} else if button_property.is_pressed(value) { } else if button_property.is_pressed(value) {
button_input.press(button); button_input.press(button);
}; };
button_axis.set(button, value);
} }
} }