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
27 changes: 25 additions & 2 deletions lighthouse-protocol/src/input/input_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::Direction;

use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MouseEvent, UnknownEvent};
use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MouseEvent, OrientationEvent, UnknownEvent};

/// A user input event, as generated by the new frontend (LUNA).
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
Expand All @@ -12,6 +12,7 @@ pub enum InputEvent {
Mouse(MouseEvent),
Gamepad(GamepadEvent),
Midi(MidiEvent),
Orientation(OrientationEvent),
#[serde(untagged)]
Unknown(UnknownEvent),
}
Expand All @@ -23,6 +24,7 @@ impl InputEvent {
InputEvent::Key(KeyEvent { source, .. }) => source,
InputEvent::Mouse(MouseEvent { source, .. }) => source,
InputEvent::Gamepad(GamepadEvent { source, .. }) => source,
InputEvent::Orientation(OrientationEvent { source, .. }) => source,
InputEvent::Midi(MidiEvent { source, .. }) => source,
InputEvent::Unknown(UnknownEvent { source, .. }) => source,
}
Expand Down Expand Up @@ -58,7 +60,7 @@ impl InputEvent {
mod tests {
use serde_json::json;

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

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

#[test]
fn orientation_event() {
assert_eq!(
serde_json::from_value::<InputEvent>(json!({
"type": "orientation",
"source": 1,
"absolute": false,
"alpha": null,
"beta": 3.0,
"gamma": -10.0,
})).unwrap(),
InputEvent::Orientation(OrientationEvent {
source: EventSource::Int(1),
absolute: Some(false),
alpha: None,
beta: Some(3.0),
gamma: Some(-10.0),
})
)
}

#[test]
fn unknown_event() {
assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-protocol/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod legacy_input_event;
mod midi_event;
mod mouse_button;
mod mouse_event;
mod orientation_event;
mod unknown_event;

pub use event_source::*;
Expand All @@ -26,4 +27,5 @@ pub use legacy_input_event::*;
pub use midi_event::*;
pub use mouse_button::*;
pub use mouse_event::*;
pub use orientation_event::*;
pub use unknown_event::*;
18 changes: 18 additions & 0 deletions lighthouse-protocol/src/input/orientation_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};

use super::EventSource;

/// A device orientation event.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct OrientationEvent {
/// The client identifier.
pub source: EventSource,
// /// Whether the device provides absolute orientation data.
pub absolute: Option<bool>,
/// The motion of the device around the z-axis, in degrees from 0 (inclusive) to 360 (exclusive).
pub alpha: Option<f64>,
/// The motion of the device around the x-axis (front to back motion), in degrees from -180 (inclusive) to 180 (exclusive).
pub beta: Option<f64>,
/// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive).
pub gamma: Option<f64>,
}