lighthouse_protocol/input/
orientation_event.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{Direction, Vec2};
4
5use super::EventSource;
6
7/// A device orientation event.
8#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
9#[serde(rename_all = "camelCase")]
10pub struct OrientationEvent {
11    /// The client identifier.
12    pub source: EventSource,
13    /// Whether the device provides absolute orientation data.
14    pub absolute: Option<bool>,
15    /// The motion of the device around the z-axis, in degrees from 0 (inclusive) to 360 (exclusive).
16    pub alpha: Option<f64>,
17    /// The motion of the device around the x-axis (front to back motion), in degrees from -180 (inclusive) to 180 (exclusive).
18    pub beta: Option<f64>,
19    /// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive).
20    pub gamma: Option<f64>,
21}
22
23impl OrientationEvent {
24    /// The approximate direction (outside of a small deadzone) for a phone tilted against a flat surface.
25    pub fn direction(&self) -> Option<Direction> {
26        let Some(beta) = self.beta else { return None };
27        let Some(gamma) = self.gamma else { return None };
28
29        let deadzone_radius: f64 = 10.0;
30        if beta.abs().max(gamma.abs()) < deadzone_radius {
31            return None;
32        }
33
34        Direction::approximate_from(Vec2::new(gamma, beta))
35    }
36}