1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
4pub enum ErrorType {
5 GamepadNotConnected { slot: u8 },
6 Unknown, }
8
9#[derive(Debug, Clone)]
10pub struct GamepadError {
11 pub msg: String,
12 pub error_type: ErrorType,
13}
14impl GamepadError {
15 pub fn new<T: Into<String>>(msg: T, error_type: ErrorType) -> Self {
16 GamepadError {
17 msg: msg.into(),
18 error_type,
19 }
20 }
21}
22
23#[derive(Clone, Debug)]
24pub enum GamepadEvent {
25 Connected {},
26 Disconnected {},
27}
28
29#[derive(Clone, Debug)]
30pub struct GamepadState {
31 pub(crate) buttons: HashMap<Button, ButtonState>,
32 pub(crate) joysticks: HashMap<Joystick, JoystickState>,
33}
34impl GamepadState {
35 pub fn new() -> Self {
36 GamepadState {
37 buttons: HashMap::new(),
38 joysticks: HashMap::new(),
39 }
40 }
41
42 pub fn buttons(&self) -> &HashMap<Button, ButtonState> {
43 &self.buttons
44 }
45
46 pub fn buttons_mut(&mut self) -> &mut HashMap<Button, ButtonState> {
47 &mut self.buttons
48 }
49
50 pub fn joystick(&self, joystick: Joystick) -> (f32, f32) {
51 if let Some(joystick) = self.joysticks.get(&joystick) {
52 return joystick.normalized_value;
53 }
54
55 (0.0, 0.0)
56 }
57
58 pub fn joystick_raw(&self, joystick: Joystick) -> (i16, i16) {
59 if let Some(joystick) = self.joysticks.get(&joystick) {
60 return joystick.raw_value;
61 }
62
63 (0, 0)
64 }
65
66 pub fn joysticks(&self) -> &HashMap<Joystick, JoystickState> {
67 &self.joysticks
68 }
69 pub fn joysticks_mut(&mut self) -> &mut HashMap<Joystick, JoystickState> {
70 &mut self.joysticks
71 }
72
73 pub fn is_pressed(&self, button: Button) -> bool {
74 match self.buttons.get(&button) {
75 Some(button_state) => button_state.is_pressed,
76 None => false,
77 }
78 }
79
80 pub fn is_just_pressed(&self, button: Button) -> bool {
81 match self.buttons.get(&button) {
82 Some(button_state) => button_state.is_pressed && !button_state.was_pressed,
83 None => false,
84 }
85 }
86
87 pub fn is_just_released(&self, button: Button) -> bool {
88 match self.buttons.get(&button) {
89 Some(button_state) => !button_state.is_pressed && button_state.was_pressed,
90 None => false,
91 }
92 }
93}
94
95#[derive(Clone, Debug)]
96pub struct JoystickState {
97 pub(crate) raw_value: (i16, i16),
98 pub(crate) normalized_value: (f32, f32),
99}
100impl JoystickState {
101 pub fn new(raw_value: (i16, i16), normalized_value: (f32, f32)) -> Self {
102 JoystickState {
103 raw_value,
104 normalized_value,
105 }
106 }
107}
108impl Default for JoystickState {
109 fn default() -> JoystickState {
110 JoystickState {
111 raw_value: (0, 0),
112 normalized_value: (0.0, 0.0),
113 }
114 }
115}
116
117#[derive(Clone, Debug)]
118pub struct ButtonState {
119 pub(crate) is_pressed: bool,
120 pub(crate) was_pressed: bool,
121}
122impl ButtonState {
123 pub fn new(is_pressed: bool, was_pressed: bool) -> Self {
124 ButtonState {
125 is_pressed,
126 was_pressed,
127 }
128 }
129 pub fn is_pressed(&self) -> bool {
130 self.is_pressed
131 }
132
133 pub fn is_just_pressed(&self) -> bool {
134 self.is_pressed && !self.was_pressed
135 }
136
137 pub fn is_just_released(&self) -> bool {
138 !self.is_pressed && self.was_pressed
139 }
140}
141impl Default for ButtonState {
142 fn default() -> ButtonState {
143 ButtonState::new(false, false)
144 }
145}
146
147#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq)]
148pub enum Button {
149 DPadNorth,
150 DPadSouth,
151 DPadWest,
152 DPadEast,
153 North,
154 South,
155 West,
156 East,
157 LeftShoulder,
158 RightShoulder,
159 LeftTrigger,
160 RightTrigger,
161 RightStick,
162 LeftStick,
163 Menu,
164 Select,
165 Start,
166}
167
168#[derive(Clone, Debug, Hash, PartialEq, Eq)]
169pub enum Joystick {
170 Left,
171 Right,
172}