@@ -4,8 +4,6 @@ use crate::{
4
4
} ;
5
5
use bevy_ecs:: event:: EventWriter ;
6
6
use bevy_ecs:: prelude:: Commands ;
7
- #[ cfg( target_arch = "wasm32" ) ]
8
- use bevy_ecs:: system:: NonSendMut ;
9
7
use bevy_ecs:: system:: ResMut ;
10
8
use bevy_input:: gamepad:: {
11
9
GamepadConnection , GamepadConnectionEvent , RawGamepadAxisChangedEvent ,
@@ -15,101 +13,103 @@ use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter};
15
13
16
14
pub fn gilrs_event_startup_system (
17
15
mut commands : Commands ,
18
- #[ cfg( target_arch = "wasm32" ) ] mut gilrs : NonSendMut < Gilrs > ,
19
- #[ cfg( not( target_arch = "wasm32" ) ) ] mut gilrs : ResMut < Gilrs > ,
16
+ mut gilrs : ResMut < Gilrs > ,
20
17
mut gamepads : ResMut < GilrsGamepads > ,
21
18
mut events : EventWriter < GamepadConnectionEvent > ,
22
19
) {
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) ;
28
-
29
- events. write ( 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
- }
20
+ gilrs. with ( |gilrs| {
21
+ for ( id, gamepad) in gilrs. gamepads ( ) {
22
+ // Create entity and add to mapping
23
+ let entity = commands. spawn_empty ( ) . id ( ) ;
24
+ gamepads. id_to_entity . insert ( id, entity) ;
25
+ gamepads. entity_to_id . insert ( entity, id) ;
26
+ events. write ( 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
+ } ) ;
38
36
}
39
37
40
38
pub fn gilrs_event_system (
41
39
mut commands : Commands ,
42
- #[ cfg( target_arch = "wasm32" ) ] mut gilrs : NonSendMut < Gilrs > ,
43
- #[ cfg( not( target_arch = "wasm32" ) ) ] mut gilrs : ResMut < Gilrs > ,
40
+ mut gilrs : ResMut < Gilrs > ,
44
41
mut gamepads : ResMut < GilrsGamepads > ,
45
42
mut events : EventWriter < RawGamepadEvent > ,
46
43
mut connection_events : EventWriter < GamepadConnectionEvent > ,
47
44
mut button_events : EventWriter < RawGamepadButtonChangedEvent > ,
48
45
mut axis_event : EventWriter < RawGamepadAxisChangedEvent > ,
49
46
) {
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
- } ) ;
62
-
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
- ) ;
47
+ gilrs. with ( |gilrs| {
48
+ while let Some ( gilrs_event) = gilrs. next_event ( ) . filter_ev ( & axis_dpad_to_button, gilrs) {
49
+ gilrs. update ( & gilrs_event) ;
50
+ match gilrs_event. event {
51
+ EventType :: Connected => {
52
+ let pad = gilrs. gamepad ( gilrs_event. id ) ;
53
+ let entity = gamepads. get_entity ( gilrs_event. id ) . unwrap_or_else ( || {
54
+ let entity = commands. spawn_empty ( ) . id ( ) ;
55
+ gamepads. id_to_entity . insert ( gilrs_event. id , entity) ;
56
+ gamepads. entity_to_id . insert ( entity, gilrs_event. id ) ;
57
+ entity
58
+ } ) ;
71
59
72
- events. write ( event. clone ( ) . into ( ) ) ;
73
- connection_events. write ( 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. write ( event. clone ( ) . into ( ) ) ;
83
- connection_events. write ( 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. write ( RawGamepadButtonChangedEvent :: new ( gamepad, button, raw_value) . into ( ) ) ;
95
- button_events. write ( 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. write ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) . into ( ) ) ;
109
- axis_event. write ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) ) ;
110
- }
111
- _ => ( ) ,
112
- } ;
113
- }
114
- gilrs. inc ( ) ;
60
+ let event = GamepadConnectionEvent :: new (
61
+ entity,
62
+ GamepadConnection :: Connected {
63
+ name : pad. name ( ) . to_string ( ) ,
64
+ vendor_id : pad. vendor_id ( ) ,
65
+ product_id : pad. product_id ( ) ,
66
+ } ,
67
+ ) ;
68
+ events. write ( event. clone ( ) . into ( ) ) ;
69
+ connection_events. write ( event) ;
70
+ }
71
+ EventType :: Disconnected => {
72
+ let gamepad = gamepads
73
+ . id_to_entity
74
+ . get ( & gilrs_event. id )
75
+ . copied ( )
76
+ . expect ( "mapping should exist from connection" ) ;
77
+ let event =
78
+ GamepadConnectionEvent :: new ( gamepad, GamepadConnection :: Disconnected ) ;
79
+ events. write ( event. clone ( ) . into ( ) ) ;
80
+ connection_events. write ( event) ;
81
+ }
82
+ EventType :: ButtonChanged ( gilrs_button, raw_value, _) => {
83
+ let Some ( button) = convert_button ( gilrs_button) else {
84
+ continue ;
85
+ } ;
86
+ let gamepad = gamepads
87
+ . id_to_entity
88
+ . get ( & gilrs_event. id )
89
+ . copied ( )
90
+ . expect ( "mapping should exist from connection" ) ;
91
+ events. write (
92
+ RawGamepadButtonChangedEvent :: new ( gamepad, button, raw_value) . into ( ) ,
93
+ ) ;
94
+ button_events. write ( RawGamepadButtonChangedEvent :: new (
95
+ gamepad, button, raw_value,
96
+ ) ) ;
97
+ }
98
+ EventType :: AxisChanged ( gilrs_axis, raw_value, _) => {
99
+ let Some ( axis) = convert_axis ( gilrs_axis) else {
100
+ continue ;
101
+ } ;
102
+ let gamepad = gamepads
103
+ . id_to_entity
104
+ . get ( & gilrs_event. id )
105
+ . copied ( )
106
+ . expect ( "mapping should exist from connection" ) ;
107
+ events. write ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) . into ( ) ) ;
108
+ axis_event. write ( RawGamepadAxisChangedEvent :: new ( gamepad, axis, raw_value) ) ;
109
+ }
110
+ _ => ( ) ,
111
+ } ;
112
+ }
113
+ gilrs. inc ( ) ;
114
+ } ) ;
115
115
}
0 commit comments