diff --git a/lighthouse-protocol/src/input/input_event.rs b/lighthouse-protocol/src/input/input_event.rs index c581da3..3adf521 100644 --- a/lighthouse-protocol/src/input/input_event.rs +++ b/lighthouse-protocol/src/input/input_event.rs @@ -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)] @@ -13,6 +13,7 @@ pub enum InputEvent { Gamepad(GamepadEvent), Midi(MidiEvent), Orientation(OrientationEvent), + Motion(MotionEvent), #[serde(untagged)] Unknown(UnknownEvent), } @@ -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, } diff --git a/lighthouse-protocol/src/input/mod.rs b/lighthouse-protocol/src/input/mod.rs index a0435bc..05fca69 100644 --- a/lighthouse-protocol/src/input/mod.rs +++ b/lighthouse-protocol/src/input/mod.rs @@ -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; @@ -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::*; diff --git a/lighthouse-protocol/src/input/motion_event.rs b/lighthouse-protocol/src/input/motion_event.rs new file mode 100644 index 0000000..2bfc132 --- /dev/null +++ b/lighthouse-protocol/src/input/motion_event.rs @@ -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>>, + /// The acceleration in 3D space (including gravity) in m/s^2. + pub acceleration_including_gravity: Option>>, + /// The rotation rate in deg/s on the three rotation axes. + pub rotation_rate: Option>>, + /// The granularity of these events in ms. + pub interval: f64, +} diff --git a/lighthouse-protocol/src/input/orientation_event.rs b/lighthouse-protocol/src/input/orientation_event.rs index 8aa748d..8650a69 100644 --- a/lighthouse-protocol/src/input/orientation_event.rs +++ b/lighthouse-protocol/src/input/orientation_event.rs @@ -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, diff --git a/lighthouse-protocol/src/utils/mod.rs b/lighthouse-protocol/src/utils/mod.rs index 822bd4d..eda8e67 100644 --- a/lighthouse-protocol/src/utils/mod.rs +++ b/lighthouse-protocol/src/utils/mod.rs @@ -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::*; diff --git a/lighthouse-protocol/src/utils/rot3.rs b/lighthouse-protocol/src/utils/rot3.rs new file mode 100644 index 0000000..5a82b91 --- /dev/null +++ b/lighthouse-protocol/src/utils/rot3.rs @@ -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 { + pub alpha: T, + pub beta: T, + pub gamma: T, +} + +impl Rot3 { + /// 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(self, mut f: impl FnMut(T) -> U) -> Rot3 { + Rot3 { + alpha: f(self.alpha), + beta: f(self.beta), + gamma: f(self.gamma), + } + } +} + +impl Zero for Rot3 where T: Zero { + /// The origin. + const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO); +} diff --git a/lighthouse-protocol/src/utils/vec3.rs b/lighthouse-protocol/src/utils/vec3.rs new file mode 100644 index 0000000..364449b --- /dev/null +++ b/lighthouse-protocol/src/utils/vec3.rs @@ -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 { + pub x: T, + pub y: T, + pub z: T, +} + +impl Vec3 { + /// 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(self, mut f: impl FnMut(T) -> U) -> Vec3 { + Vec3 { + x: f(self.x), + y: f(self.y), + z: f(self.z), + } + } +} + +impl Zero for Vec3 where T: Zero { + /// The origin. + const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO); +} + +impl Vec3 where T: Add + Mul + 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 fmt::Display for Vec3 where T: fmt::Display { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "({}, {}, {})", self.x, self.y, self.z) + } +} + +impl Add for Vec3 where T: Add { + type Output = Self; + + fn add(self, rhs: Vec3) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Neg for Vec3 where T: Neg { + type Output = Self; + + fn neg(self) -> Self { + Self::new(-self.x, -self.y, -self.z) + } +} + +impl Sub for Vec3 where T: Sub { + type Output = Self; + + fn sub(self, rhs: Vec3) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +impl AddAssign for Vec3 where T: AddAssign { + fn add_assign(&mut self, rhs: Vec3) { + self.x += rhs.x; + self.y += rhs.y; + self.z += rhs.z; + } +} + +impl SubAssign for Vec3 where T: SubAssign { + fn sub_assign(&mut self, rhs: Vec3) { + self.x -= rhs.x; + self.y -= rhs.y; + self.z -= rhs.z; + } +}