11use crate :: {
22 converter:: { convert_axis, convert_button} ,
3- Gilrs , GilrsGamepads ,
3+ GilrsGamepads , GILRS ,
44} ;
55use bevy_ecs:: event:: EventWriter ;
66use bevy_ecs:: prelude:: Commands ;
7- #[ cfg( target_arch = "wasm32" ) ]
8- use bevy_ecs:: system:: NonSendMut ;
97use bevy_ecs:: system:: ResMut ;
108use bevy_input:: gamepad:: {
119 GamepadConnection , GamepadConnectionEvent , RawGamepadAxisChangedEvent ,
@@ -15,101 +13,102 @@ use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter};
1513
1614pub fn gilrs_event_startup_system (
1715 mut commands : Commands ,
18- #[ cfg( target_arch = "wasm32" ) ] mut gilrs : NonSendMut < Gilrs > ,
19- #[ cfg( not( target_arch = "wasm32" ) ) ] mut gilrs : ResMut < Gilrs > ,
2016 mut gamepads : ResMut < GilrsGamepads > ,
2117 mut events : EventWriter < GamepadConnectionEvent > ,
2218) {
23- for ( id, gamepad) in gilrs. 0 . get ( ) . gamepads ( ) {
24- // Create entity and add to mapping
25- let entity = commands. spawn_empty ( ) . id ( ) ;
26- gamepads. id_to_entity . insert ( id, entity) ;
27- gamepads. entity_to_id . insert ( entity, id) ;
19+ GILRS . with ( |gilrs_ref_cell| {
20+ for ( id, gamepad) in gilrs_ref_cell. borrow ( ) . as_ref ( ) . expect ( "gilrs should have been initialized" ) . gamepads ( ) {
21+ // Create entity and add to mapping
22+ let entity = commands. spawn_empty ( ) . id ( ) ;
23+ gamepads. id_to_entity . insert ( id, entity) ;
24+ gamepads. entity_to_id . insert ( entity, id) ;
2825
29- events. send ( GamepadConnectionEvent {
30- gamepad : entity,
31- connection : GamepadConnection :: Connected {
32- name : gamepad. name ( ) . to_string ( ) ,
33- vendor_id : gamepad. vendor_id ( ) ,
34- product_id : gamepad. product_id ( ) ,
35- } ,
36- } ) ;
37- }
26+ events. send ( GamepadConnectionEvent {
27+ gamepad : entity,
28+ connection : GamepadConnection :: Connected {
29+ name : gamepad. name ( ) . to_string ( ) ,
30+ vendor_id : gamepad. vendor_id ( ) ,
31+ product_id : gamepad. product_id ( ) ,
32+ } ,
33+ } ) ;
34+ }
35+ } ) ;
3836}
3937
4038pub fn gilrs_event_system (
4139 mut commands : Commands ,
42- #[ cfg( target_arch = "wasm32" ) ] mut gilrs : NonSendMut < Gilrs > ,
43- #[ cfg( not( target_arch = "wasm32" ) ) ] mut gilrs : ResMut < Gilrs > ,
4440 mut gamepads : ResMut < GilrsGamepads > ,
4541 mut events : EventWriter < RawGamepadEvent > ,
4642 mut connection_events : EventWriter < GamepadConnectionEvent > ,
4743 mut button_events : EventWriter < RawGamepadButtonChangedEvent > ,
4844 mut axis_event : EventWriter < RawGamepadAxisChangedEvent > ,
4945) {
50- let gilrs = gilrs. 0 . get ( ) ;
51- while let Some ( gilrs_event) = gilrs. next_event ( ) . filter_ev ( & axis_dpad_to_button, gilrs) {
52- gilrs. update ( & gilrs_event) ;
53- match gilrs_event. event {
54- EventType :: Connected => {
55- let pad = gilrs. gamepad ( gilrs_event. id ) ;
56- let entity = gamepads. get_entity ( gilrs_event. id ) . unwrap_or_else ( || {
57- let entity = commands. spawn_empty ( ) . id ( ) ;
58- gamepads. id_to_entity . insert ( gilrs_event. id , entity) ;
59- gamepads. entity_to_id . insert ( entity, gilrs_event. id ) ;
60- entity
61- } ) ;
46+ GILRS . with ( |gilrs_ref_cell| {
47+ let mut gilrs_ref = gilrs_ref_cell. borrow_mut ( ) ;
48+ let gilrs = gilrs_ref. as_mut ( ) . expect ( "" ) ;
49+ while let Some ( gilrs_event) = gilrs. next_event ( ) . filter_ev ( & axis_dpad_to_button, gilrs) {
50+ gilrs. update ( & gilrs_event) ;
51+ match gilrs_event. event {
52+ EventType :: Connected => {
53+ let pad = gilrs. gamepad ( gilrs_event. id ) ;
54+ let entity = gamepads. get_entity ( gilrs_event. id ) . unwrap_or_else ( || {
55+ let entity = commands. spawn_empty ( ) . id ( ) ;
56+ gamepads. id_to_entity . insert ( gilrs_event. id , entity) ;
57+ gamepads. entity_to_id . insert ( entity, gilrs_event. id ) ;
58+ entity
59+ } ) ;
6260
63- let event = GamepadConnectionEvent :: new (
64- entity,
65- GamepadConnection :: Connected {
66- name : pad. name ( ) . to_string ( ) ,
67- vendor_id : pad. vendor_id ( ) ,
68- product_id : pad. product_id ( ) ,
69- } ,
70- ) ;
61+ let event = GamepadConnectionEvent :: new (
62+ entity,
63+ GamepadConnection :: Connected {
64+ name : pad. name ( ) . to_string ( ) ,
65+ vendor_id : pad. vendor_id ( ) ,
66+ product_id : pad. product_id ( ) ,
67+ } ,
68+ ) ;
7169
72- events. send ( event. clone ( ) . into ( ) ) ;
73- connection_events. send ( event) ;
74- }
75- EventType :: Disconnected => {
76- let gamepad = gamepads
77- . id_to_entity
78- . get ( & gilrs_event. id )
79- . copied ( )
80- . expect ( "mapping should exist from connection" ) ;
81- let event = GamepadConnectionEvent :: new ( gamepad, GamepadConnection :: Disconnected ) ;
82- events. send ( event. clone ( ) . into ( ) ) ;
83- connection_events. send ( event) ;
84- }
85- EventType :: ButtonChanged ( gilrs_button, raw_value, _) => {
86- let Some ( button) = convert_button ( gilrs_button) else {
87- continue ;
88- } ;
89- let gamepad = gamepads
90- . id_to_entity
91- . get ( & gilrs_event. id )
92- . copied ( )
93- . expect ( "mapping should exist from connection" ) ;
94- events. send ( RawGamepadButtonChangedEvent :: new ( gamepad, button, raw_value) . into ( ) ) ;
95- button_events. send ( RawGamepadButtonChangedEvent :: new (
96- gamepad, button, raw_value,
97- ) ) ;
98- }
99- EventType :: AxisChanged ( gilrs_axis, raw_value, _) => {
100- let Some ( axis) = convert_axis ( gilrs_axis) else {
101- continue ;
102- } ;
103- let gamepad = gamepads
104- . id_to_entity
105- . get ( & gilrs_event. id )
106- . copied ( )
107- . expect ( "mapping should exist from connection" ) ;
108- events. send ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) . into ( ) ) ;
109- axis_event. send ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) ) ;
110- }
111- _ => ( ) ,
112- } ;
113- }
114- gilrs. inc ( ) ;
70+ events. send ( event. clone ( ) . into ( ) ) ;
71+ connection_events. send ( event) ;
72+ }
73+ EventType :: Disconnected => {
74+ let gamepad = gamepads
75+ . id_to_entity
76+ . get ( & gilrs_event. id )
77+ . copied ( )
78+ . expect ( "mapping should exist from connection" ) ;
79+ let event = GamepadConnectionEvent :: new ( gamepad, GamepadConnection :: Disconnected ) ;
80+ events. send ( event. clone ( ) . into ( ) ) ;
81+ connection_events. send ( event) ;
82+ }
83+ EventType :: ButtonChanged ( gilrs_button, raw_value, _) => {
84+ let Some ( button) = convert_button ( gilrs_button) else {
85+ continue ;
86+ } ;
87+ let gamepad = gamepads
88+ . id_to_entity
89+ . get ( & gilrs_event. id )
90+ . copied ( )
91+ . expect ( "mapping should exist from connection" ) ;
92+ events. send ( RawGamepadButtonChangedEvent :: new ( gamepad, button, raw_value) . into ( ) ) ;
93+ button_events. send ( RawGamepadButtonChangedEvent :: new (
94+ gamepad, button, raw_value,
95+ ) ) ;
96+ }
97+ EventType :: AxisChanged ( gilrs_axis, raw_value, _) => {
98+ let Some ( axis) = convert_axis ( gilrs_axis) else {
99+ continue ;
100+ } ;
101+ let gamepad = gamepads
102+ . id_to_entity
103+ . get ( & gilrs_event. id )
104+ . copied ( )
105+ . expect ( "mapping should exist from connection" ) ;
106+ events. send ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) . into ( ) ) ;
107+ axis_event. send ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) ) ;
108+ }
109+ _ => ( ) ,
110+ } ;
111+ }
112+ gilrs. inc ( ) ;
113+ } ) ;
115114}
0 commit comments