Skip to content

Commit 734673b

Browse files
authored
Merge pull request #22 from ProjectLighthouseCAU/axis2d
Add axis 2D event for gamepad events
2 parents 7b8ddbf + f3028e2 commit 734673b

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::Vec2;
4+
5+
/// A 2D axis event on a gamepad.
6+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7+
#[serde(tag = "control", rename_all = "camelCase")]
8+
pub struct GamepadAxis2DEvent {
9+
/// The 2D axes index (0 is the left stick, 1 is the right stick).
10+
pub index: usize,
11+
/// The value of the axis (each component is between -1.0 and 1.0, modeled after the Web Gamepad API).
12+
pub value: Vec2<f64>,
13+
}

lighthouse-protocol/src/input/gamepad_axis_event.rs

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

3-
/// An axis event on a gamepad.
3+
/// A 1D axis event on a gamepad.
44
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
55
#[serde(tag = "control", rename_all = "camelCase")]
66
pub struct GamepadAxisEvent {
7-
/// The axis index.
7+
/// The 1D axis index.
88
pub index: usize,
99
/// The value of the axis (between -1.0 and 1.0, modeled after the Web Gamepad API).
1010
pub value: f64,
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use serde::{Deserialize, Serialize};
22

3-
use super::{GamepadAxisEvent, GamepadButtonEvent};
3+
use super::{GamepadAxis2DEvent, GamepadAxisEvent, GamepadButtonEvent};
44

55
/// A control-specific event on a gamepad.
66
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
77
#[serde(tag = "control", rename_all = "camelCase")]
88
pub enum GamepadControlEvent {
99
Button(GamepadButtonEvent),
1010
Axis(GamepadAxisEvent),
11+
#[serde(rename = "axis2d")]
12+
Axis2D(GamepadAxis2DEvent),
1113
}

lighthouse-protocol/src/input/input_event.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum InputEvent {
1515
mod tests {
1616
use serde_json::json;
1717

18-
use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, KeyModifiers, MouseButton, MouseEvent, Pos};
18+
use crate::{EventSource, GamepadAxis2DEvent, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, KeyModifiers, MouseButton, MouseEvent, Pos, Vec2};
1919

2020
#[test]
2121
fn key_event() {
@@ -104,4 +104,27 @@ mod tests {
104104
})
105105
);
106106
}
107+
108+
#[test]
109+
fn gamepad_axis_2d_event() {
110+
assert_eq!(
111+
serde_json::from_value::<InputEvent>(json!({
112+
"type": "gamepad",
113+
"source": 1,
114+
"control": "axis2d",
115+
"index": 42,
116+
"value": {
117+
"x": 0.2,
118+
"y": -0.2,
119+
},
120+
})).unwrap(),
121+
InputEvent::Gamepad(GamepadEvent {
122+
source: EventSource::Int(1),
123+
control: GamepadControlEvent::Axis2D(GamepadAxis2DEvent {
124+
index: 42,
125+
value: Vec2::new(0.2, -0.2),
126+
}),
127+
})
128+
);
129+
}
107130
}

lighthouse-protocol/src/input/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod event_source;
2+
mod gamepad_axis_2d_event;
23
mod gamepad_axis_event;
34
mod gamepad_button_event;
45
mod gamepad_control_event;
@@ -11,6 +12,7 @@ mod mouse_button;
1112
mod mouse_event;
1213

1314
pub use event_source::*;
15+
pub use gamepad_axis_2d_event::*;
1416
pub use gamepad_axis_event::*;
1517
pub use gamepad_button_event::*;
1618
pub use gamepad_control_event::*;

0 commit comments

Comments
 (0)