Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lighthouse-protocol/src/input/gamepad_axis_2d_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};

use crate::Vec2;

/// A 2D axis event on a gamepad.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(tag = "control", rename_all = "camelCase")]
pub struct GamepadAxis2DEvent {
/// The 2D axes index (0 is the left stick, 1 is the right stick).
pub index: usize,
/// The value of the axis (each component is between -1.0 and 1.0, modeled after the Web Gamepad API).
pub value: Vec2<f64>,
}
4 changes: 2 additions & 2 deletions lighthouse-protocol/src/input/gamepad_axis_event.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};

/// An axis event on a gamepad.
/// A 1D axis event on a gamepad.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(tag = "control", rename_all = "camelCase")]
pub struct GamepadAxisEvent {
/// The axis index.
/// The 1D axis index.
pub index: usize,
/// The value of the axis (between -1.0 and 1.0, modeled after the Web Gamepad API).
pub value: f64,
Expand Down
4 changes: 3 additions & 1 deletion lighthouse-protocol/src/input/gamepad_control_event.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use serde::{Deserialize, Serialize};

use super::{GamepadAxisEvent, GamepadButtonEvent};
use super::{GamepadAxis2DEvent, GamepadAxisEvent, GamepadButtonEvent};

/// A control-specific event on a gamepad.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(tag = "control", rename_all = "camelCase")]
pub enum GamepadControlEvent {
Button(GamepadButtonEvent),
Axis(GamepadAxisEvent),
#[serde(rename = "axis2d")]
Axis2D(GamepadAxis2DEvent),
}
25 changes: 24 additions & 1 deletion lighthouse-protocol/src/input/input_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum InputEvent {
mod tests {
use serde_json::json;

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

#[test]
fn key_event() {
Expand Down Expand Up @@ -104,4 +104,27 @@ mod tests {
})
);
}

#[test]
fn gamepad_axis_2d_event() {
assert_eq!(
serde_json::from_value::<InputEvent>(json!({
"type": "gamepad",
"source": 1,
"control": "axis2d",
"index": 42,
"value": {
"x": 0.2,
"y": -0.2,
},
})).unwrap(),
InputEvent::Gamepad(GamepadEvent {
source: EventSource::Int(1),
control: GamepadControlEvent::Axis2D(GamepadAxis2DEvent {
index: 42,
value: Vec2::new(0.2, -0.2),
}),
})
);
}
}
2 changes: 2 additions & 0 deletions lighthouse-protocol/src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod event_source;
mod gamepad_axis_2d_event;
mod gamepad_axis_event;
mod gamepad_button_event;
mod gamepad_control_event;
Expand All @@ -11,6 +12,7 @@ mod mouse_button;
mod mouse_event;

pub use event_source::*;
pub use gamepad_axis_2d_event::*;
pub use gamepad_axis_event::*;
pub use gamepad_button_event::*;
pub use gamepad_control_event::*;
Expand Down