Skip to content

Commit f79e3b4

Browse files
authored
Merge pull request #30 from ProjectLighthouseCAU/orientation-events
Add support for orientation events
2 parents 6e4e908 + 18dc2dc commit f79e3b4

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lighthouse-protocol/src/input/input_event.rs

Lines changed: 25 additions & 2 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, GamepadEvent, KeyEvent, MidiEvent, MouseEvent, UnknownEvent};
5+
use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MouseEvent, OrientationEvent, UnknownEvent};
66

77
/// A user input event, as generated by the new frontend (LUNA).
88
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
@@ -12,6 +12,7 @@ pub enum InputEvent {
1212
Mouse(MouseEvent),
1313
Gamepad(GamepadEvent),
1414
Midi(MidiEvent),
15+
Orientation(OrientationEvent),
1516
#[serde(untagged)]
1617
Unknown(UnknownEvent),
1718
}
@@ -23,6 +24,7 @@ impl InputEvent {
2324
InputEvent::Key(KeyEvent { source, .. }) => source,
2425
InputEvent::Mouse(MouseEvent { source, .. }) => source,
2526
InputEvent::Gamepad(GamepadEvent { source, .. }) => source,
27+
InputEvent::Orientation(OrientationEvent { source, .. }) => source,
2628
InputEvent::Midi(MidiEvent { source, .. }) => source,
2729
InputEvent::Unknown(UnknownEvent { source, .. }) => source,
2830
}
@@ -58,7 +60,7 @@ impl InputEvent {
5860
mod tests {
5961
use serde_json::json;
6062

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

6365
#[test]
6466
fn key_event() {
@@ -180,6 +182,27 @@ mod tests {
180182
);
181183
}
182184

185+
#[test]
186+
fn orientation_event() {
187+
assert_eq!(
188+
serde_json::from_value::<InputEvent>(json!({
189+
"type": "orientation",
190+
"source": 1,
191+
"absolute": false,
192+
"alpha": null,
193+
"beta": 3.0,
194+
"gamma": -10.0,
195+
})).unwrap(),
196+
InputEvent::Orientation(OrientationEvent {
197+
source: EventSource::Int(1),
198+
absolute: Some(false),
199+
alpha: None,
200+
beta: Some(3.0),
201+
gamma: Some(-10.0),
202+
})
203+
)
204+
}
205+
183206
#[test]
184207
fn unknown_event() {
185208
assert_eq!(

lighthouse-protocol/src/input/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod legacy_input_event;
1111
mod midi_event;
1212
mod mouse_button;
1313
mod mouse_event;
14+
mod orientation_event;
1415
mod unknown_event;
1516

1617
pub use event_source::*;
@@ -26,4 +27,5 @@ pub use legacy_input_event::*;
2627
pub use midi_event::*;
2728
pub use mouse_button::*;
2829
pub use mouse_event::*;
30+
pub use orientation_event::*;
2931
pub use unknown_event::*;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use super::EventSource;
4+
5+
/// A device orientation event.
6+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7+
pub struct OrientationEvent {
8+
/// The client identifier.
9+
pub source: EventSource,
10+
// /// Whether the device provides absolute orientation data.
11+
pub absolute: Option<bool>,
12+
/// The motion of the device around the z-axis, in degrees from 0 (inclusive) to 360 (exclusive).
13+
pub alpha: Option<f64>,
14+
/// The motion of the device around the x-axis (front to back motion), in degrees from -180 (inclusive) to 180 (exclusive).
15+
pub beta: Option<f64>,
16+
/// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive).
17+
pub gamma: Option<f64>,
18+
}

0 commit comments

Comments
 (0)