1use std::cmp::Ordering;
2
3#[derive(Clone, Debug)]
4pub struct GamepadConnectedEvent(pub(crate) GamepadId);
8
9impl GamepadConnectedEvent {
10 pub fn gamepad(&self) -> &GamepadId {
11 &self.0
12 }
13}
14
15#[derive(Clone, Debug)]
16pub struct GamepadDisconnectedEvent(pub(crate) GamepadId);
20
21impl GamepadDisconnectedEvent {
22 pub fn gamepad(&self) -> &GamepadId {
23 &self.0
24 }
25}
26
27#[derive(Clone, Debug)]
28pub struct GamepadButtonEvent {
32 pub(crate) id: GamepadId,
33 pub(crate) button: GamepadButton,
34 pub(crate) is_down: bool,
35 pub(crate) is_repeat: bool,
36}
37
38impl GamepadButtonEvent {
39 pub fn gamepad(&self) -> &GamepadId {
41 &self.id
42 }
43
44 pub fn button(&self) -> GamepadButton {
45 self.button
46 }
47
48 pub fn is_down(&self) -> bool {
50 self.is_down
51 }
52
53 pub fn is_repeat(&self) -> bool {
55 self.is_repeat
56 }
57}
58
59#[derive(Clone, Debug)]
60pub struct GamepadAxisEvent {
64 pub(crate) id: GamepadId,
65 pub(crate) axis: GamepadAxis,
66 pub(crate) value: f32,
67}
68
69impl GamepadAxisEvent {
70 pub fn gamepad(&self) -> &GamepadId {
72 &self.id
73 }
74
75 pub fn axis(&self) -> GamepadAxis {
76 self.axis
77 }
78
79 pub fn value(&self) -> f32 {
80 self.value
81 }
82}
83
84#[derive(Clone, PartialEq, Eq, Debug, Hash)]
85pub struct GamepadId(
87 #[cfg(feature = "gilrs")] pub(crate) gilrs::GamepadId,
88 #[cfg(not(feature = "gilrs"))] usize,
89);
90
91impl PartialOrd for GamepadId {
92 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
93 Some(self.cmp(other))
94 }
95}
96
97impl Ord for GamepadId {
98 fn cmp(&self, other: &Self) -> Ordering {
99 let a: usize = self.0.into();
100 let b: usize = other.0.into();
101 a.cmp(&b)
102 }
103}
104
105#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
106#[cfg_attr(feature = "enum-map", derive(enum_map::Enum))]
107pub enum GamepadButton {
109 Start,
110 Select,
111
112 North,
118 South,
124 East,
130 West,
136
137 LeftStick,
139 RightStick,
141
142 LeftTrigger,
143 RightTrigger,
144
145 LeftShoulder,
146 RightShoulder,
147
148 DPadUp,
149 DPadDown,
150 DPadLeft,
151 DPadRight,
152}
153
154#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
155#[cfg_attr(feature = "enum-map", derive(enum_map::Enum))]
156pub enum GamepadAxis {
158 LeftStickX,
159 LeftStickY,
160
161 RightStickX,
162 RightStickY,
163}
164
165#[cfg(feature = "gilrs")]
166pub(crate) fn convert_gilrs_button(event: gilrs::ev::Button) -> Option<GamepadButton> {
167 use gilrs::ev::Button::*;
168 Some(match event {
169 South => GamepadButton::South,
170 East => GamepadButton::East,
171 North => GamepadButton::North,
172 West => GamepadButton::West,
173 LeftTrigger => GamepadButton::LeftShoulder,
174 LeftTrigger2 => GamepadButton::LeftShoulder,
175 RightTrigger => GamepadButton::RightShoulder,
176 RightTrigger2 => GamepadButton::RightTrigger,
177 Select => GamepadButton::Select,
178 Start => GamepadButton::Start,
179 LeftThumb => GamepadButton::LeftStick,
180 RightThumb => GamepadButton::RightStick,
181 DPadUp => GamepadButton::DPadUp,
182 DPadDown => GamepadButton::DPadDown,
183 DPadLeft => GamepadButton::DPadLeft,
184 DPadRight => GamepadButton::DPadRight,
185
186 C | Z | Unknown | Mode => return None,
187 })
188}
189
190#[cfg(feature = "gilrs")]
191pub(crate) fn convert_gilrs_axis(axis: gilrs::ev::Axis) -> Option<GamepadAxis> {
192 use gilrs::ev::Axis::*;
193
194 Some(match axis {
195 LeftStickX => GamepadAxis::LeftStickX,
196 LeftStickY => GamepadAxis::LeftStickY,
197 RightStickX => GamepadAxis::RightStickX,
198 RightStickY => GamepadAxis::RightStickY,
199
200 LeftZ | RightZ | DPadX | DPadY | Unknown => return None,
201 })
202}