Skip to content

Commit f764601

Browse files
committed
Add support for parsing direction from orientation events
1 parent be85579 commit f764601

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lighthouse-protocol/src/input/input_event.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ impl InputEvent {
3434

3535
/// Parses the input event as an arbitrary direction.
3636
pub fn direction(&self) -> Option<Direction> {
37-
self.left_direction().or_else(|| self.right_direction())
37+
match self {
38+
InputEvent::Orientation(orientation) => orientation.direction(),
39+
_ => self.left_direction().or_else(|| self.right_direction()),
40+
}
3841
}
3942

4043
/// The direction if the input event represents a WASD key, D-pad or left stick.

lighthouse-protocol/src/input/orientation_event.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

3+
use crate::{Direction, Vec2};
4+
35
use super::EventSource;
46

57
/// A device orientation event.
@@ -17,3 +19,18 @@ pub struct OrientationEvent {
1719
/// The motion of the device around the y-axis (left to right motion), in degrees from -90 (inclusive) to 90 (exclusive).
1820
pub gamma: Option<f64>,
1921
}
22+
23+
impl 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.max(gamma) < deadzone_radius {
31+
return None;
32+
}
33+
34+
Direction::approximate_from(Vec2::new(gamma, beta))
35+
}
36+
}

0 commit comments

Comments
 (0)