lighthouse_protocol/input/
gamepad_event.rs1use serde::{Deserialize, Serialize};
2
3use crate::Direction;
4
5use super::{EventSource, GamepadControlEvent};
6
7#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
9#[serde(rename_all = "camelCase")]
10pub struct GamepadEvent {
11 pub source: EventSource,
13 #[serde(flatten)]
15 pub control: GamepadControlEvent,
16}
17
18impl GamepadEvent {
19 pub fn direction(&self) -> Option<Direction> {
21 self.left_direction().or_else(|| self.right_direction())
22 }
23
24 pub fn left_direction(&self) -> Option<Direction> {
27 match &self.control {
28 GamepadControlEvent::Button(button) => button.d_pad_direction(),
29 GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 0 => axis2d.direction(),
30 _ => None,
31 }
32 }
33
34 pub fn right_direction(&self) -> Option<Direction> {
37 match &self.control {
38 GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 1 => axis2d.direction(),
39 _ => None,
40 }
41 }
42}