lighthouse_protocol/input/
gamepad_button_event.rs

1use serde::{Deserialize, Serialize};
2
3use crate::Direction;
4
5/// A button event on a gamepad.
6#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7#[serde(tag = "control", rename_all = "camelCase")]
8pub struct GamepadButtonEvent {
9    /// The button index.
10    pub index: usize,
11    /// Whether the button is pressed.
12    pub down: bool,
13    /// The value of the button (between 0.0 and 1.0, modeled after the Web Gamepad API).
14    pub value: f64,
15}
16
17impl GamepadButtonEvent {
18    /// The direction if one of the D-pad buttons was pressed.
19    /// See https://www.w3.org/TR/gamepad/#dfn-standard-gamepad
20    pub fn d_pad_direction(&self) -> Option<Direction> {
21        match self.index {
22            12 => Some(Direction::Up),
23            13 => Some(Direction::Down),
24            14 => Some(Direction::Left),
25            15 => Some(Direction::Right),
26            _ => None,
27        }
28    }
29}