|
1 | 1 | use crate::converter::{convert_axis, convert_button, convert_gamepad_id};
|
2 | 2 | use bevy_ecs::event::EventWriter;
|
3 |
| -use bevy_ecs::system::{NonSend, NonSendMut}; |
4 |
| -use bevy_input::gamepad::GamepadInfo; |
5 |
| -use bevy_input::{gamepad::GamepadEventRaw, prelude::*}; |
| 3 | +use bevy_ecs::system::{NonSend, NonSendMut, Res}; |
| 4 | +use bevy_input::gamepad::{ |
| 5 | + GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadConnection, GamepadConnectionEvent, |
| 6 | + GamepadSettings, |
| 7 | +}; |
| 8 | +use bevy_input::gamepad::{GamepadEvent, GamepadInfo}; |
| 9 | +use bevy_input::prelude::{GamepadAxis, GamepadButton}; |
| 10 | +use bevy_input::Axis; |
6 | 11 | use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs};
|
7 | 12 |
|
8 |
| -pub fn gilrs_event_startup_system(gilrs: NonSend<Gilrs>, mut events: EventWriter<GamepadEventRaw>) { |
| 13 | +pub fn gilrs_event_startup_system( |
| 14 | + gilrs: NonSend<Gilrs>, |
| 15 | + mut connection_events: EventWriter<GamepadConnectionEvent>, |
| 16 | +) { |
9 | 17 | for (id, gamepad) in gilrs.gamepads() {
|
10 | 18 | let info = GamepadInfo {
|
11 | 19 | name: gamepad.name().into(),
|
12 | 20 | };
|
13 | 21 |
|
14 |
| - events.send(GamepadEventRaw::new( |
15 |
| - convert_gamepad_id(id), |
16 |
| - GamepadEventType::Connected(info), |
17 |
| - )); |
| 22 | + connection_events.send(GamepadConnectionEvent { |
| 23 | + gamepad: convert_gamepad_id(id), |
| 24 | + connection: GamepadConnection::Connected(info), |
| 25 | + }); |
18 | 26 | }
|
19 | 27 | }
|
20 | 28 |
|
21 |
| -pub fn gilrs_event_system(mut gilrs: NonSendMut<Gilrs>, mut events: EventWriter<GamepadEventRaw>) { |
| 29 | +pub fn gilrs_event_system( |
| 30 | + mut gilrs: NonSendMut<Gilrs>, |
| 31 | + mut events: EventWriter<GamepadEvent>, |
| 32 | + gamepad_axis: Res<Axis<GamepadAxis>>, |
| 33 | + gamepad_buttons: Res<Axis<GamepadButton>>, |
| 34 | + gamepad_settings: Res<GamepadSettings>, |
| 35 | +) { |
22 | 36 | while let Some(gilrs_event) = gilrs
|
23 | 37 | .next_event()
|
24 | 38 | .filter_ev(&axis_dpad_to_button, &mut gilrs)
|
25 | 39 | {
|
26 | 40 | gilrs.update(&gilrs_event);
|
27 | 41 |
|
| 42 | + let gamepad = convert_gamepad_id(gilrs_event.id); |
28 | 43 | match gilrs_event.event {
|
29 | 44 | EventType::Connected => {
|
30 | 45 | let pad = gilrs.gamepad(gilrs_event.id);
|
31 | 46 | let info = GamepadInfo {
|
32 | 47 | name: pad.name().into(),
|
33 | 48 | };
|
34 | 49 |
|
35 |
| - events.send(GamepadEventRaw::new( |
36 |
| - convert_gamepad_id(gilrs_event.id), |
37 |
| - GamepadEventType::Connected(info), |
38 |
| - )); |
| 50 | + events.send( |
| 51 | + GamepadConnectionEvent::new(gamepad, GamepadConnection::Connected(info)).into(), |
| 52 | + ); |
39 | 53 | }
|
40 |
| - EventType::Disconnected => { |
41 |
| - events.send(GamepadEventRaw::new( |
42 |
| - convert_gamepad_id(gilrs_event.id), |
43 |
| - GamepadEventType::Disconnected, |
44 |
| - )); |
45 |
| - } |
46 |
| - EventType::ButtonChanged(gilrs_button, value, _) => { |
| 54 | + EventType::Disconnected => events |
| 55 | + .send(GamepadConnectionEvent::new(gamepad, GamepadConnection::Disconnected).into()), |
| 56 | + EventType::ButtonChanged(gilrs_button, raw_value, _) => { |
47 | 57 | if let Some(button_type) = convert_button(gilrs_button) {
|
48 |
| - events.send(GamepadEventRaw::new( |
49 |
| - convert_gamepad_id(gilrs_event.id), |
50 |
| - GamepadEventType::ButtonChanged(button_type, value), |
51 |
| - )); |
| 58 | + let button = GamepadButton::new(gamepad, button_type); |
| 59 | + let old_value = gamepad_buttons.get(button); |
| 60 | + let button_settings = gamepad_settings.get_button_axis_settings(button); |
| 61 | + |
| 62 | + // Only send events that pass the user-defined change threshold |
| 63 | + if let Some(filtered_value) = button_settings.filter(raw_value, old_value) { |
| 64 | + events.send( |
| 65 | + GamepadButtonChangedEvent::new(gamepad, button_type, filtered_value) |
| 66 | + .into(), |
| 67 | + ); |
| 68 | + } |
52 | 69 | }
|
53 | 70 | }
|
54 |
| - EventType::AxisChanged(gilrs_axis, value, _) => { |
| 71 | + EventType::AxisChanged(gilrs_axis, raw_value, _) => { |
55 | 72 | if let Some(axis_type) = convert_axis(gilrs_axis) {
|
56 |
| - events.send(GamepadEventRaw::new( |
57 |
| - convert_gamepad_id(gilrs_event.id), |
58 |
| - GamepadEventType::AxisChanged(axis_type, value), |
59 |
| - )); |
| 73 | + let axis = GamepadAxis::new(gamepad, axis_type); |
| 74 | + let old_value = gamepad_axis.get(axis); |
| 75 | + let axis_settings = gamepad_settings.get_axis_settings(axis); |
| 76 | + |
| 77 | + // Only send events that pass the user-defined change threshold |
| 78 | + if let Some(filtered_value) = axis_settings.filter(raw_value, old_value) { |
| 79 | + events.send( |
| 80 | + GamepadAxisChangedEvent::new(gamepad, axis_type, filtered_value).into(), |
| 81 | + ); |
| 82 | + } |
60 | 83 | }
|
61 | 84 | }
|
62 | 85 | _ => (),
|
|
0 commit comments