Skip to content

Commit 5431195

Browse files
committed
Factor out direction methods on GamepadEvent
1 parent 75e0bc7 commit 5431195

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

lighthouse-protocol/src/input/gamepad_event.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

3+
use crate::Direction;
4+
35
use super::{EventSource, GamepadControlEvent};
46

57
/// A gamepad/controller event.
@@ -12,3 +14,29 @@ pub struct GamepadEvent {
1214
#[serde(flatten)]
1315
pub control: GamepadControlEvent,
1416
}
17+
18+
impl GamepadEvent {
19+
/// Parses the gamepad event as an arbitrary direction.
20+
pub fn direction(&self) -> Option<Direction> {
21+
self.left_direction().or_else(|| self.right_direction())
22+
}
23+
24+
/// The direction if the gamepad event represents a D-pad or left stick.
25+
/// Commonly used e.g. for movement in games.
26+
pub fn left_direction(&self) -> Option<Direction> {
27+
match &self.control {
28+
GamepadControlEvent::Button(button) => button.d_pad_direction(),
29+
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 0 => axis2d.direction(),
30+
_ => None,
31+
}
32+
}
33+
34+
/// The direction if the gamepad event represents a right stick event.
35+
/// Commonly used e.g. for camera control in games.
36+
pub fn right_direction(&self) -> Option<Direction> {
37+
match &self.control {
38+
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 1 => axis2d.direction(),
39+
_ => None,
40+
}
41+
}
42+
}

lighthouse-protocol/src/input/input_event.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
22

33
use crate::Direction;
44

5-
use super::{EventSource, GamepadControlEvent, GamepadEvent, KeyEvent, MouseEvent};
5+
use super::{EventSource, GamepadEvent, KeyEvent, MouseEvent};
66

77
/// A user input event, as generated by the new frontend (LUNA).
88
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
@@ -33,11 +33,7 @@ impl InputEvent {
3333
pub fn left_direction(&self) -> Option<Direction> {
3434
match self {
3535
InputEvent::Key(key) => key.wasd_direction(),
36-
InputEvent::Gamepad(gamepad) => match &gamepad.control {
37-
GamepadControlEvent::Button(button) => button.d_pad_direction(),
38-
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 0 => axis2d.direction(),
39-
_ => None,
40-
},
36+
InputEvent::Gamepad(gamepad) => gamepad.left_direction(),
4137
_ => None,
4238
}
4339
}
@@ -47,10 +43,7 @@ impl InputEvent {
4743
pub fn right_direction(&self) -> Option<Direction> {
4844
match self {
4945
InputEvent::Key(key) => key.arrow_direction(),
50-
InputEvent::Gamepad(gamepad) => match &gamepad.control {
51-
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 1 => axis2d.direction(),
52-
_ => None,
53-
},
46+
InputEvent::Gamepad(gamepad) => gamepad.right_direction(),
5447
_ => None,
5548
}
5649
}

0 commit comments

Comments
 (0)