Skip to content

Add support for motion events #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 23, 2025
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
4 changes: 3 additions & 1 deletion 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, OrientationEvent, UnknownEvent};
use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MotionEvent, MouseEvent, OrientationEvent, UnknownEvent};

/// A user input event, as generated by the new frontend (LUNA).
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
Expand All @@ -13,6 +13,7 @@ pub enum InputEvent {
Gamepad(GamepadEvent),
Midi(MidiEvent),
Orientation(OrientationEvent),
Motion(MotionEvent),
#[serde(untagged)]
Unknown(UnknownEvent),
}
Expand All @@ -25,6 +26,7 @@ impl InputEvent {
InputEvent::Mouse(MouseEvent { source, .. }) => source,
InputEvent::Gamepad(GamepadEvent { source, .. }) => source,
InputEvent::Orientation(OrientationEvent { source, .. }) => source,
InputEvent::Motion(MotionEvent { source, .. }) => source,
InputEvent::Midi(MidiEvent { source, .. }) => source,
InputEvent::Unknown(UnknownEvent { source, .. }) => source,
}
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 @@ -9,6 +9,7 @@ mod key_event;
mod key_modifiers;
mod legacy_input_event;
mod midi_event;
mod motion_event;
mod mouse_button;
mod mouse_event;
mod orientation_event;
Expand All @@ -25,6 +26,7 @@ pub use key_event::*;
pub use key_modifiers::*;
pub use legacy_input_event::*;
pub use midi_event::*;
pub use motion_event::*;
pub use mouse_button::*;
pub use mouse_event::*;
pub use orientation_event::*;
Expand Down
21 changes: 21 additions & 0 deletions lighthouse-protocol/src/input/motion_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};

use crate::{Rot3, Vec3};

use super::EventSource;

/// A device motion event.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MotionEvent {
/// The client identifier.
pub source: EventSource,
/// The acceleration in 3D space in m/s^2.
pub acceleration: Option<Vec3<Option<f64>>>,
/// The acceleration in 3D space (including gravity) in m/s^2.
pub acceleration_including_gravity: Option<Vec3<Option<f64>>>,
/// The rotation rate in deg/s on the three rotation axes.
pub rotation_rate: Option<Rot3<Option<f64>>>,
/// The granularity of these events in ms.
pub interval: f64,
}
1 change: 1 addition & 0 deletions lighthouse-protocol/src/input/orientation_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::EventSource;

/// A device orientation event.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct OrientationEvent {
/// The client identifier.
pub source: EventSource,
Expand Down
4 changes: 4 additions & 0 deletions lighthouse-protocol/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ mod color;
mod direction;
mod rect;
mod rem_euclid;
mod rot3;
mod rotation;
mod sqrt;
mod unity;
mod vec2;
mod vec3;
mod zero;

pub use color::*;
pub use direction::*;
pub use rect::*;
pub use rem_euclid::*;
pub use rot3::*;
pub use rotation::*;
pub use sqrt::*;
pub use unity::*;
pub use vec2::*;
pub use vec3::*;
pub use zero::*;
32 changes: 32 additions & 0 deletions lighthouse-protocol/src/utils/rot3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};

use super::Zero;

/// A 3D rotation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Rot3<T> {
pub alpha: T,
pub beta: T,
pub gamma: T,
}

impl<T> Rot3<T> {
/// Creates a mew position.
pub const fn new(alpha: T, beta: T, gamma: T) -> Self {
Self { alpha, beta, gamma }
}

/// Maps a function over the vector.
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Rot3<U> {
Rot3 {
alpha: f(self.alpha),
beta: f(self.beta),
gamma: f(self.gamma),
}
}
}

impl<T> Zero for Rot3<T> where T: Zero {
/// The origin.
const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO);
}
87 changes: 87 additions & 0 deletions lighthouse-protocol/src/utils/vec3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::{fmt, ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign}};

use serde::{Deserialize, Serialize};

use super::{Sqrt, Zero};

/// A 3D vector.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Vec3<T> {
pub x: T,
pub y: T,
pub z: T,
}

impl<T> Vec3<T> {
/// Creates a mew position.
pub const fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}

/// Maps a function over the vector.
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Vec3<U> {
Vec3 {
x: f(self.x),
y: f(self.y),
z: f(self.z),
}
}
}

impl<T> Zero for Vec3<T> where T: Zero {
/// The origin.
const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO);
}

impl<T> Vec3<T> where T: Add<Output = T> + Mul<Output = T> + Sqrt + Copy {
/// The vector's length.
pub fn length(&self) -> T {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}

impl<T> fmt::Display for Vec3<T> where T: fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {})", self.x, self.y, self.z)
}
}

impl<T> Add for Vec3<T> where T: Add<Output = T> {
type Output = Self;

fn add(self, rhs: Vec3<T>) -> Self {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}

impl<T> Neg for Vec3<T> where T: Neg<Output = T> {
type Output = Self;

fn neg(self) -> Self {
Self::new(-self.x, -self.y, -self.z)
}
}

impl<T> Sub for Vec3<T> where T: Sub<Output = T> {
type Output = Self;

fn sub(self, rhs: Vec3<T>) -> Self {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}

impl<T> AddAssign<Self> for Vec3<T> where T: AddAssign<T> {
fn add_assign(&mut self, rhs: Vec3<T>) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}

impl<T> SubAssign<Self> for Vec3<T> where T: SubAssign<T> {
fn sub_assign(&mut self, rhs: Vec3<T>) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}