diff --git a/lighthouse-protocol/src/input/gamepad_axis_2d_event.rs b/lighthouse-protocol/src/input/gamepad_axis_2d_event.rs new file mode 100644 index 0000000..e421258 --- /dev/null +++ b/lighthouse-protocol/src/input/gamepad_axis_2d_event.rs @@ -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, +} diff --git a/lighthouse-protocol/src/input/gamepad_axis_event.rs b/lighthouse-protocol/src/input/gamepad_axis_event.rs index bd2f4db..5efed6e 100644 --- a/lighthouse-protocol/src/input/gamepad_axis_event.rs +++ b/lighthouse-protocol/src/input/gamepad_axis_event.rs @@ -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, diff --git a/lighthouse-protocol/src/input/gamepad_control_event.rs b/lighthouse-protocol/src/input/gamepad_control_event.rs index 9006bd2..ec52988 100644 --- a/lighthouse-protocol/src/input/gamepad_control_event.rs +++ b/lighthouse-protocol/src/input/gamepad_control_event.rs @@ -1,6 +1,6 @@ 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)] @@ -8,4 +8,6 @@ use super::{GamepadAxisEvent, GamepadButtonEvent}; pub enum GamepadControlEvent { Button(GamepadButtonEvent), Axis(GamepadAxisEvent), + #[serde(rename = "axis2d")] + Axis2D(GamepadAxis2DEvent), } diff --git a/lighthouse-protocol/src/input/input_event.rs b/lighthouse-protocol/src/input/input_event.rs index ffe9062..82cd48c 100644 --- a/lighthouse-protocol/src/input/input_event.rs +++ b/lighthouse-protocol/src/input/input_event.rs @@ -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() { @@ -104,4 +104,27 @@ mod tests { }) ); } + + #[test] + fn gamepad_axis_2d_event() { + assert_eq!( + serde_json::from_value::(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), + }), + }) + ); + } } diff --git a/lighthouse-protocol/src/input/mod.rs b/lighthouse-protocol/src/input/mod.rs index e6e2a33..463661c 100644 --- a/lighthouse-protocol/src/input/mod.rs +++ b/lighthouse-protocol/src/input/mod.rs @@ -1,4 +1,5 @@ mod event_source; +mod gamepad_axis_2d_event; mod gamepad_axis_event; mod gamepad_button_event; mod gamepad_control_event; @@ -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::*;