Skip to content

Commit 7eb3ba2

Browse files
KDecayexjam
KDecay
authored andcommitted
Change gamepad.rs tuples to normal structs (bevyengine#4519)
# Objective - Part of the splitting process of bevyengine#3692. ## Solution - Remove / change the tuple structs inside of `gamepad.rs` of `bevy_input` to normal structs. ## Reasons - It made the `gamepad_connection_system` cleaner. - It made the `gamepad_input_events.rs` example cleaner (which is probably the most notable change for the user facing API). - Tuple structs are not descriptive (`.0`, `.1`). - Using tuple structs for more than 1 field is a bad idea (This means that the `Gamepad` type might be fine as a tuple struct, but I still prefer normal structs over tuple structs). Feel free to discuss this change as this is more or less just a matter of taste. ## Changelog ### Changed - The `Gamepad`, `GamepadButton`, `GamepadAxis`, `GamepadEvent` and `GamepadEventRaw` types are now normal structs instead of tuple structs and have a `new()` function. ## Migration Guide - The `Gamepad`, `GamepadButton`, `GamepadAxis`, `GamepadEvent` and `GamepadEventRaw` types are now normal structs instead of tuple structs and have a `new()` function. To migrate change every instantiation to use the `new()` function instead and use the appropriate field names instead of `.0` and `.1`.
1 parent b123390 commit 7eb3ba2

File tree

6 files changed

+117
-53
lines changed

6 files changed

+117
-53
lines changed

crates/bevy_gilrs/src/converter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bevy_input::gamepad::{Gamepad, GamepadAxisType, GamepadButtonType};
22

33
pub fn convert_gamepad_id(gamepad_id: gilrs::GamepadId) -> Gamepad {
4-
Gamepad(gamepad_id.into())
4+
Gamepad::new(gamepad_id.into())
55
}
66

77
pub fn convert_button(button: gilrs::Button) -> Option<GamepadButtonType> {

crates/bevy_gilrs/src/gilrs_system.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use gilrs::{EventType, Gilrs};
66

77
pub fn gilrs_event_startup_system(gilrs: NonSend<Gilrs>, mut events: EventWriter<GamepadEventRaw>) {
88
for (id, _) in gilrs.gamepads() {
9-
events.send(GamepadEventRaw(
9+
events.send(GamepadEventRaw::new(
1010
convert_gamepad_id(id),
1111
GamepadEventType::Connected,
1212
));
@@ -17,28 +17,28 @@ pub fn gilrs_event_system(mut gilrs: NonSendMut<Gilrs>, mut events: EventWriter<
1717
while let Some(gilrs_event) = gilrs.next_event() {
1818
match gilrs_event.event {
1919
EventType::Connected => {
20-
events.send(GamepadEventRaw(
20+
events.send(GamepadEventRaw::new(
2121
convert_gamepad_id(gilrs_event.id),
2222
GamepadEventType::Connected,
2323
));
2424
}
2525
EventType::Disconnected => {
26-
events.send(GamepadEventRaw(
26+
events.send(GamepadEventRaw::new(
2727
convert_gamepad_id(gilrs_event.id),
2828
GamepadEventType::Disconnected,
2929
));
3030
}
3131
EventType::ButtonChanged(gilrs_button, value, _) => {
3232
if let Some(button_type) = convert_button(gilrs_button) {
33-
events.send(GamepadEventRaw(
33+
events.send(GamepadEventRaw::new(
3434
convert_gamepad_id(gilrs_event.id),
3535
GamepadEventType::ButtonChanged(button_type, value),
3636
));
3737
}
3838
}
3939
EventType::AxisChanged(gilrs_axis, value, _) => {
4040
if let Some(axis_type) = convert_axis(gilrs_axis) {
41-
events.send(GamepadEventRaw(
41+
events.send(GamepadEventRaw::new(
4242
convert_gamepad_id(gilrs_event.id),
4343
GamepadEventType::AxisChanged(axis_type, value),
4444
));

crates/bevy_input/src/axis.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ mod tests {
7777
];
7878

7979
for (value, expected) in cases {
80-
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
80+
let gamepad_button =
81+
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger);
8182
let mut axis = Axis::<GamepadButton>::default();
8283

8384
axis.set(gamepad_button, value);
@@ -92,7 +93,8 @@ mod tests {
9293
let cases = [-1.0, -0.9, -0.1, 0.0, 0.1, 0.9, 1.0];
9394

9495
for value in cases {
95-
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
96+
let gamepad_button =
97+
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger);
9698
let mut axis = Axis::<GamepadButton>::default();
9799

98100
axis.set(gamepad_button, value);

crates/bevy_input/src/gamepad.rs

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ use bevy_utils::{tracing::info, HashMap, HashSet};
55

66
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
77
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
8-
pub struct Gamepad(pub usize);
8+
pub struct Gamepad {
9+
pub id: usize,
10+
}
11+
12+
impl Gamepad {
13+
pub fn new(id: usize) -> Self {
14+
Self { id }
15+
}
16+
}
917

1018
#[derive(Default)]
1119
/// Container of unique connected [`Gamepad`]s
@@ -48,11 +56,35 @@ pub enum GamepadEventType {
4856

4957
#[derive(Debug, Clone, PartialEq)]
5058
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
51-
pub struct GamepadEvent(pub Gamepad, pub GamepadEventType);
59+
pub struct GamepadEvent {
60+
pub gamepad: Gamepad,
61+
pub event_type: GamepadEventType,
62+
}
63+
64+
impl GamepadEvent {
65+
pub fn new(gamepad: Gamepad, event_type: GamepadEventType) -> Self {
66+
Self {
67+
gamepad,
68+
event_type,
69+
}
70+
}
71+
}
5272

5373
#[derive(Debug, Clone, PartialEq)]
5474
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
55-
pub struct GamepadEventRaw(pub Gamepad, pub GamepadEventType);
75+
pub struct GamepadEventRaw {
76+
pub gamepad: Gamepad,
77+
pub event_type: GamepadEventType,
78+
}
79+
80+
impl GamepadEventRaw {
81+
pub fn new(gamepad: Gamepad, event_type: GamepadEventType) -> Self {
82+
Self {
83+
gamepad,
84+
event_type,
85+
}
86+
}
87+
}
5688

5789
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
5890
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
@@ -80,7 +112,19 @@ pub enum GamepadButtonType {
80112

81113
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
82114
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
83-
pub struct GamepadButton(pub Gamepad, pub GamepadButtonType);
115+
pub struct GamepadButton {
116+
pub gamepad: Gamepad,
117+
pub button_type: GamepadButtonType,
118+
}
119+
120+
impl GamepadButton {
121+
pub fn new(gamepad: Gamepad, button_type: GamepadButtonType) -> Self {
122+
Self {
123+
gamepad,
124+
button_type,
125+
}
126+
}
127+
}
84128

85129
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
86130
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
@@ -97,7 +141,16 @@ pub enum GamepadAxisType {
97141

98142
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
99143
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
100-
pub struct GamepadAxis(pub Gamepad, pub GamepadAxisType);
144+
pub struct GamepadAxis {
145+
pub gamepad: Gamepad,
146+
pub axis_type: GamepadAxisType,
147+
}
148+
149+
impl GamepadAxis {
150+
pub fn new(gamepad: Gamepad, axis_type: GamepadAxisType) -> Self {
151+
Self { gamepad, axis_type }
152+
}
153+
}
101154

102155
#[derive(Default, Debug)]
103156
pub struct GamepadSettings {
@@ -251,14 +304,14 @@ pub fn gamepad_connection_system(
251304
mut gamepad_event: EventReader<GamepadEvent>,
252305
) {
253306
for event in gamepad_event.iter() {
254-
match &event {
255-
GamepadEvent(gamepad, GamepadEventType::Connected) => {
256-
gamepads.register(*gamepad);
257-
info!("{:?} Connected", gamepad);
307+
match event.event_type {
308+
GamepadEventType::Connected => {
309+
gamepads.register(event.gamepad);
310+
info!("{:?} Connected", event.gamepad);
258311
}
259-
GamepadEvent(gamepad, GamepadEventType::Disconnected) => {
260-
gamepads.deregister(gamepad);
261-
info!("{:?} Disconnected", gamepad);
312+
GamepadEventType::Disconnected => {
313+
gamepads.deregister(&event.gamepad);
314+
info!("{:?} Disconnected", event.gamepad);
262315
}
263316
_ => (),
264317
}
@@ -275,62 +328,61 @@ pub fn gamepad_event_system(
275328
) {
276329
button_input.clear();
277330
for event in raw_events.iter() {
278-
let (gamepad, event) = (event.0, &event.1);
279-
match event {
331+
match event.event_type {
280332
GamepadEventType::Connected => {
281-
events.send(GamepadEvent(gamepad, event.clone()));
333+
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
282334
for button_type in &ALL_BUTTON_TYPES {
283-
let gamepad_button = GamepadButton(gamepad, *button_type);
335+
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
284336
button_input.reset(gamepad_button);
285337
button_axis.set(gamepad_button, 0.0);
286338
}
287339
for axis_type in &ALL_AXIS_TYPES {
288-
axis.set(GamepadAxis(gamepad, *axis_type), 0.0);
340+
axis.set(GamepadAxis::new(event.gamepad, *axis_type), 0.0);
289341
}
290342
}
291343
GamepadEventType::Disconnected => {
292-
events.send(GamepadEvent(gamepad, event.clone()));
344+
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
293345
for button_type in &ALL_BUTTON_TYPES {
294-
let gamepad_button = GamepadButton(gamepad, *button_type);
346+
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
295347
button_input.reset(gamepad_button);
296348
button_axis.remove(gamepad_button);
297349
}
298350
for axis_type in &ALL_AXIS_TYPES {
299-
axis.remove(GamepadAxis(gamepad, *axis_type));
351+
axis.remove(GamepadAxis::new(event.gamepad, *axis_type));
300352
}
301353
}
302354
GamepadEventType::AxisChanged(axis_type, value) => {
303-
let gamepad_axis = GamepadAxis(gamepad, *axis_type);
355+
let gamepad_axis = GamepadAxis::new(event.gamepad, axis_type);
304356
if let Some(filtered_value) = settings
305357
.get_axis_settings(gamepad_axis)
306-
.filter(*value, axis.get(gamepad_axis))
358+
.filter(value, axis.get(gamepad_axis))
307359
{
308360
axis.set(gamepad_axis, filtered_value);
309-
events.send(GamepadEvent(
310-
gamepad,
311-
GamepadEventType::AxisChanged(*axis_type, filtered_value),
361+
events.send(GamepadEvent::new(
362+
event.gamepad,
363+
GamepadEventType::AxisChanged(axis_type, filtered_value),
312364
));
313365
}
314366
}
315367
GamepadEventType::ButtonChanged(button_type, value) => {
316-
let gamepad_button = GamepadButton(gamepad, *button_type);
368+
let gamepad_button = GamepadButton::new(event.gamepad, button_type);
317369
if let Some(filtered_value) = settings
318370
.get_button_axis_settings(gamepad_button)
319-
.filter(*value, button_axis.get(gamepad_button))
371+
.filter(value, button_axis.get(gamepad_button))
320372
{
321373
button_axis.set(gamepad_button, filtered_value);
322-
events.send(GamepadEvent(
323-
gamepad,
324-
GamepadEventType::ButtonChanged(*button_type, filtered_value),
374+
events.send(GamepadEvent::new(
375+
event.gamepad,
376+
GamepadEventType::ButtonChanged(button_type, filtered_value),
325377
));
326378
}
327379

328380
let button_property = settings.get_button_settings(gamepad_button);
329381
if button_input.pressed(gamepad_button) {
330-
if button_property.is_released(*value) {
382+
if button_property.is_released(value) {
331383
button_input.release(gamepad_button);
332384
}
333-
} else if button_property.is_pressed(*value) {
385+
} else if button_property.is_pressed(value) {
334386
button_input.press(gamepad_button);
335387
}
336388
}

examples/input/gamepad_input.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ fn gamepad_system(
1414
axes: Res<Axis<GamepadAxis>>,
1515
) {
1616
for gamepad in gamepads.iter().cloned() {
17-
if button_inputs.just_pressed(GamepadButton(gamepad, GamepadButtonType::South)) {
17+
if button_inputs.just_pressed(GamepadButton::new(gamepad, GamepadButtonType::South)) {
1818
info!("{:?} just pressed South", gamepad);
19-
} else if button_inputs.just_released(GamepadButton(gamepad, GamepadButtonType::South)) {
19+
} else if button_inputs.just_released(GamepadButton::new(gamepad, GamepadButtonType::South))
20+
{
2021
info!("{:?} just released South", gamepad);
2122
}
2223

2324
let right_trigger = button_axes
24-
.get(GamepadButton(gamepad, GamepadButtonType::RightTrigger2))
25+
.get(GamepadButton::new(
26+
gamepad,
27+
GamepadButtonType::RightTrigger2,
28+
))
2529
.unwrap();
2630
if right_trigger.abs() > 0.01 {
2731
info!("{:?} RightTrigger2 value is {}", gamepad, right_trigger);
2832
}
2933

3034
let left_stick_x = axes
31-
.get(GamepadAxis(gamepad, GamepadAxisType::LeftStickX))
35+
.get(GamepadAxis::new(gamepad, GamepadAxisType::LeftStickX))
3236
.unwrap();
3337
if left_stick_x.abs() > 0.01 {
3438
info!("{:?} LeftStickX value is {}", gamepad, left_stick_x);

examples/input/gamepad_input_events.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ fn main() {
1212

1313
fn gamepad_events(mut gamepad_event: EventReader<GamepadEvent>) {
1414
for event in gamepad_event.iter() {
15-
match &event {
16-
GamepadEvent(gamepad, GamepadEventType::Connected) => {
17-
info!("{:?} Connected", gamepad);
15+
match event.event_type {
16+
GamepadEventType::Connected => {
17+
info!("{:?} Connected", event.gamepad);
1818
}
19-
GamepadEvent(gamepad, GamepadEventType::Disconnected) => {
20-
info!("{:?} Disconnected", gamepad);
19+
GamepadEventType::Disconnected => {
20+
info!("{:?} Disconnected", event.gamepad);
2121
}
22-
GamepadEvent(gamepad, GamepadEventType::ButtonChanged(button_type, value)) => {
23-
info!("{:?} of {:?} is changed to {}", button_type, gamepad, value);
22+
GamepadEventType::ButtonChanged(button_type, value) => {
23+
info!(
24+
"{:?} of {:?} is changed to {}",
25+
button_type, event.gamepad, value
26+
);
2427
}
25-
GamepadEvent(gamepad, GamepadEventType::AxisChanged(axis_type, value)) => {
26-
info!("{:?} of {:?} is changed to {}", axis_type, gamepad, value);
28+
GamepadEventType::AxisChanged(axis_type, value) => {
29+
info!(
30+
"{:?} of {:?} is changed to {}",
31+
axis_type, event.gamepad, value
32+
);
2733
}
2834
}
2935
}

0 commit comments

Comments
 (0)