Skip to content

Commit cff8c05

Browse files
committed
Add gamepad events
1 parent 3b8acd3 commit cff8c05

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
/// An axis event on a gamepad.
4+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
5+
#[serde(tag = "control", rename_all = "camelCase")]
6+
pub struct GamepadAxisEvent {
7+
/// The axis index.
8+
index: usize,
9+
/// The value of the axis (between -1.0 and 1.0, modeled after the Web Gamepad API).
10+
value: f64,
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
/// A button event on a gamepad.
4+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
5+
#[serde(tag = "control", rename_all = "camelCase")]
6+
pub struct GamepadButtonEvent {
7+
/// The button index.
8+
index: usize,
9+
/// Whether the button is pressed.
10+
down: bool,
11+
/// The value of the button (between 0.0 and 1.0, modeled after the Web Gamepad API).
12+
value: f64,
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use super::{GamepadAxisEvent, GamepadButtonEvent};
4+
5+
/// A control-specific event on a gamepad.
6+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
7+
#[serde(tag = "control", rename_all = "camelCase")]
8+
pub enum GamepadControlEvent {
9+
Button(GamepadButtonEvent),
10+
Axis(GamepadAxisEvent),
11+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use serde::{Deserialize, Serialize};
22

3-
use super::EventSource;
3+
use super::{EventSource, GamepadControlEvent};
44

55
/// A gamepad/controller event.
6-
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
6+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
77
#[serde(rename_all = "camelCase")]
88
pub struct GamepadEvent {
99
/// The client identifier. Also unique per gamepad.
1010
pub source: EventSource,
11-
// TODO: Add remaining
11+
/// The control-specific info.
12+
#[serde(flatten)]
13+
pub control: GamepadControlEvent,
1214
}

lighthouse-protocol/src/input/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
mod event_source;
2+
mod gamepad_axis_event;
3+
mod gamepad_button_event;
4+
mod gamepad_control_event;
25
mod gamepad_event;
36
mod input_event;
47
mod key_event;
@@ -7,6 +10,9 @@ mod mouse_button;
710
mod mouse_event;
811

912
pub use event_source::*;
13+
pub use gamepad_axis_event::*;
14+
pub use gamepad_button_event::*;
15+
pub use gamepad_control_event::*;
1016
pub use gamepad_event::*;
1117
pub use input_event::*;
1218
pub use key_event::*;

0 commit comments

Comments
 (0)