amethyst_input/controller.rs
1use serde::{Deserialize, Serialize};
2
3use crate::{bindings::BindingTypes, event::InputEvent};
4
5/// Controller axes matching SDL controller model
6#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
7pub enum ControllerAxis {
8 /// The X axis on the left stick
9 LeftX,
10 /// The Y axis on the left stick
11 LeftY,
12 /// The X axis on the right stick
13 RightX,
14 /// The Y axis on the right stick
15 RightY,
16 /// The analog left trigger, not to be confused with the left bumper.
17 LeftTrigger,
18 /// The analog right trigger, not to be confused with the right bumper.
19 RightTrigger,
20}
21
22/// Controller buttons matching SDL controller model
23#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash, Serialize, Deserialize)]
24pub enum ControllerButton {
25 /// The A button, typically the lower button in the "diamond" of buttons on the right side
26 /// of the controller.
27 A,
28 /// The B button, typically the right button in the "diamond" of buttons on the right side
29 /// of the controller.
30 B,
31 /// The X button, typically the left button in the "diamond" of buttons on the right side
32 /// of the controller.
33 X,
34 /// The Y button, typically the top button in the "diamond" of buttons on the right side
35 /// of the controller.
36 Y,
37 /// The dpad button pointed towards the player
38 DPadDown,
39 /// The dpad button pointed to the player's left
40 DPadLeft,
41 /// The dpad button pointed to the player's right
42 DPadRight,
43 /// The dpad button pointed away from the player.
44 DPadUp,
45 /// The digital left shoulder bumper. Usually located above the left trigger.
46 LeftShoulder,
47 /// The digital right shoulder bumper. Usually located above the right trigger.
48 RightShoulder,
49 /// If your press the left analog stick into the controller this button is pressed.
50 LeftStick,
51 /// If your press the right analog stick into the controller this button is pressed.
52 RightStick,
53 /// The back button is typically a button slightly left of center with a leftward arrow on it.
54 Back,
55 /// The start button is typically a button slightly right of center with a rightward arrow on it.
56 Start,
57 /// The centermost button on the controller. Large and green on an Xbox controller.
58 Guide,
59}
60
61/// Controller events generated by the SDL events system.
62#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
63pub enum ControllerEvent {
64 /// Movement event on a controller axis.
65 ///
66 /// Corresponds to [`SDL_CONTROLLERAXISMOTION`].
67 ///
68 /// [`SDL_CONTROLLERAXISMOTION`]: https://wiki.libsdl.org/SDL_ControllerAxisEvent
69 ControllerAxisMoved {
70 /// The joystick instance id.
71 which: u32,
72 /// The controller axis.
73 axis: ControllerAxis,
74 /// The axis value (range: -32768 to 32767).
75 value: f32,
76 },
77 /// Button press event on a controller.
78 ///
79 /// Corresponds to [`SDL_CONTROLLERBUTTONDOWN`].
80 ///
81 /// [`SDL_CONTROLLERBUTTONDOWN`]: https://wiki.libsdl.org/SDL_ControllerButtonEvent
82 ControllerButtonPressed {
83 /// The joystick instance id.
84 which: u32,
85 /// The controller button.
86 button: ControllerButton,
87 },
88 /// Button press event on a controller.
89 ///
90 /// Corresponds to [`SDL_CONTROLLERBUTTONUP`].
91 ///
92 /// [`SDL_CONTROLLERBUTTONUP`]: https://wiki.libsdl.org/SDL_ControllerButtonEvent
93 ControllerButtonReleased {
94 /// The joystick instance id.
95 which: u32,
96 /// The controller button.
97 button: ControllerButton,
98 },
99 /// Controller disconnect event.
100 ///
101 /// Corresponds to [`SDL_CONTROLLERDEVICEREMOVED`].
102 ///
103 /// [`SDL_CONTROLLERDEVICEREMOVED`]: https://wiki.libsdl.org/SDL_ControllerDeviceEvent
104 ControllerDisconnected {
105 /// The joystick device index for the `SDL_CONTROLLERDEVICEADDED` event or instance id for
106 /// the `SDL_CONTROLLERDEVICEREMOVED` or `SDL_CONTROLLERDEVICEREMAPPED` event
107 which: u32,
108 },
109 /// Controller connected event.
110 ///
111 /// Corresponds to [`SDL_CONTROLLERDEVICEADDED`].
112 ///
113 /// [`SDL_CONTROLLERDEVICEADDED`]: https://wiki.libsdl.org/SDL_ControllerDeviceEvent
114 ControllerConnected {
115 /// The joystick device index for the `SDL_CONTROLLERDEVICEADDED` event or instance id for
116 /// the `SDL_CONTROLLERDEVICEREMOVED` or `SDL_CONTROLLERDEVICEREMAPPED` event
117 which: u32,
118 },
119}
120
121impl<'a, T> Into<InputEvent<T>> for &'a ControllerEvent
122where
123 T: BindingTypes,
124{
125 fn into(self) -> InputEvent<T> {
126 use self::ControllerEvent::*;
127 match *self {
128 ControllerAxisMoved { which, axis, value } => {
129 InputEvent::ControllerAxisMoved { which, axis, value }
130 }
131 ControllerButtonPressed { which, button } => {
132 InputEvent::ControllerButtonPressed { which, button }
133 }
134 ControllerButtonReleased { which, button } => {
135 InputEvent::ControllerButtonReleased { which, button }
136 }
137 ControllerConnected { which } => InputEvent::ControllerConnected { which },
138 ControllerDisconnected { which } => InputEvent::ControllerDisconnected { which },
139 }
140 }
141}