Skip to content

Commit 04f3b75

Browse files
authored
Merge pull request #31 from ProjectLighthouseCAU/motion-events
Add support for motion events
2 parents b47284f + 029f4a6 commit 04f3b75

File tree

7 files changed

+150
-1
lines changed

7 files changed

+150
-1
lines changed

lighthouse-protocol/src/input/input_event.rs

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

77
/// A user input event, as generated by the new frontend (LUNA).
88
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
@@ -13,6 +13,7 @@ pub enum InputEvent {
1313
Gamepad(GamepadEvent),
1414
Midi(MidiEvent),
1515
Orientation(OrientationEvent),
16+
Motion(MotionEvent),
1617
#[serde(untagged)]
1718
Unknown(UnknownEvent),
1819
}
@@ -25,6 +26,7 @@ impl InputEvent {
2526
InputEvent::Mouse(MouseEvent { source, .. }) => source,
2627
InputEvent::Gamepad(GamepadEvent { source, .. }) => source,
2728
InputEvent::Orientation(OrientationEvent { source, .. }) => source,
29+
InputEvent::Motion(MotionEvent { source, .. }) => source,
2830
InputEvent::Midi(MidiEvent { source, .. }) => source,
2931
InputEvent::Unknown(UnknownEvent { source, .. }) => source,
3032
}

lighthouse-protocol/src/input/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod key_event;
99
mod key_modifiers;
1010
mod legacy_input_event;
1111
mod midi_event;
12+
mod motion_event;
1213
mod mouse_button;
1314
mod mouse_event;
1415
mod orientation_event;
@@ -25,6 +26,7 @@ pub use key_event::*;
2526
pub use key_modifiers::*;
2627
pub use legacy_input_event::*;
2728
pub use midi_event::*;
29+
pub use motion_event::*;
2830
pub use mouse_button::*;
2931
pub use mouse_event::*;
3032
pub use orientation_event::*;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::{Rot3, Vec3};
4+
5+
use super::EventSource;
6+
7+
/// A device motion event.
8+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
9+
#[serde(rename_all = "camelCase")]
10+
pub struct MotionEvent {
11+
/// The client identifier.
12+
pub source: EventSource,
13+
/// The acceleration in 3D space in m/s^2.
14+
pub acceleration: Option<Vec3<Option<f64>>>,
15+
/// The acceleration in 3D space (including gravity) in m/s^2.
16+
pub acceleration_including_gravity: Option<Vec3<Option<f64>>>,
17+
/// The rotation rate in deg/s on the three rotation axes.
18+
pub rotation_rate: Option<Rot3<Option<f64>>>,
19+
/// The granularity of these events in ms.
20+
pub interval: f64,
21+
}

lighthouse-protocol/src/input/orientation_event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::EventSource;
44

55
/// A device orientation event.
66
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7+
#[serde(rename_all = "camelCase")]
78
pub struct OrientationEvent {
89
/// The client identifier.
910
pub source: EventSource,

lighthouse-protocol/src/utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ mod color;
22
mod direction;
33
mod rect;
44
mod rem_euclid;
5+
mod rot3;
56
mod rotation;
67
mod sqrt;
78
mod unity;
89
mod vec2;
10+
mod vec3;
911
mod zero;
1012

1113
pub use color::*;
1214
pub use direction::*;
1315
pub use rect::*;
1416
pub use rem_euclid::*;
17+
pub use rot3::*;
1518
pub use rotation::*;
1619
pub use sqrt::*;
1720
pub use unity::*;
1821
pub use vec2::*;
22+
pub use vec3::*;
1923
pub use zero::*;

lighthouse-protocol/src/utils/rot3.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use super::Zero;
4+
5+
/// A 3D rotation.
6+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7+
pub struct Rot3<T> {
8+
pub alpha: T,
9+
pub beta: T,
10+
pub gamma: T,
11+
}
12+
13+
impl<T> Rot3<T> {
14+
/// Creates a mew position.
15+
pub const fn new(alpha: T, beta: T, gamma: T) -> Self {
16+
Self { alpha, beta, gamma }
17+
}
18+
19+
/// Maps a function over the vector.
20+
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Rot3<U> {
21+
Rot3 {
22+
alpha: f(self.alpha),
23+
beta: f(self.beta),
24+
gamma: f(self.gamma),
25+
}
26+
}
27+
}
28+
29+
impl<T> Zero for Rot3<T> where T: Zero {
30+
/// The origin.
31+
const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO);
32+
}

lighthouse-protocol/src/utils/vec3.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::{fmt, ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign}};
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use super::{Sqrt, Zero};
6+
7+
/// A 3D vector.
8+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9+
pub struct Vec3<T> {
10+
pub x: T,
11+
pub y: T,
12+
pub z: T,
13+
}
14+
15+
impl<T> Vec3<T> {
16+
/// Creates a mew position.
17+
pub const fn new(x: T, y: T, z: T) -> Self {
18+
Self { x, y, z }
19+
}
20+
21+
/// Maps a function over the vector.
22+
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Vec3<U> {
23+
Vec3 {
24+
x: f(self.x),
25+
y: f(self.y),
26+
z: f(self.z),
27+
}
28+
}
29+
}
30+
31+
impl<T> Zero for Vec3<T> where T: Zero {
32+
/// The origin.
33+
const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO);
34+
}
35+
36+
impl<T> Vec3<T> where T: Add<Output = T> + Mul<Output = T> + Sqrt + Copy {
37+
/// The vector's length.
38+
pub fn length(&self) -> T {
39+
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
40+
}
41+
}
42+
43+
impl<T> fmt::Display for Vec3<T> where T: fmt::Display {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45+
write!(f, "({}, {}, {})", self.x, self.y, self.z)
46+
}
47+
}
48+
49+
impl<T> Add for Vec3<T> where T: Add<Output = T> {
50+
type Output = Self;
51+
52+
fn add(self, rhs: Vec3<T>) -> Self {
53+
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
54+
}
55+
}
56+
57+
impl<T> Neg for Vec3<T> where T: Neg<Output = T> {
58+
type Output = Self;
59+
60+
fn neg(self) -> Self {
61+
Self::new(-self.x, -self.y, -self.z)
62+
}
63+
}
64+
65+
impl<T> Sub for Vec3<T> where T: Sub<Output = T> {
66+
type Output = Self;
67+
68+
fn sub(self, rhs: Vec3<T>) -> Self {
69+
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
70+
}
71+
}
72+
73+
impl<T> AddAssign<Self> for Vec3<T> where T: AddAssign<T> {
74+
fn add_assign(&mut self, rhs: Vec3<T>) {
75+
self.x += rhs.x;
76+
self.y += rhs.y;
77+
self.z += rhs.z;
78+
}
79+
}
80+
81+
impl<T> SubAssign<Self> for Vec3<T> where T: SubAssign<T> {
82+
fn sub_assign(&mut self, rhs: Vec3<T>) {
83+
self.x -= rhs.x;
84+
self.y -= rhs.y;
85+
self.z -= rhs.z;
86+
}
87+
}

0 commit comments

Comments
 (0)