1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub enum ErrorType {
    GamepadNotConnected { slot: u8 },
    Unknown, // Uncommon errors not documented by gamepad lib
}

#[derive(Debug, Clone)]
pub struct GamepadError {
    pub msg: String,
    pub error_type: ErrorType,
}
impl GamepadError {
    pub fn new<T: Into<String>>(msg: T, error_type: ErrorType) -> Self {
        GamepadError {
            msg: msg.into(),
            error_type,
        }
    }
}

#[derive(Clone, Debug)]
pub enum GamepadEvent {
    Connected {},
    Disconnected {},
}

#[derive(Clone, Debug)]
pub struct GamepadState {
    pub(crate) buttons: HashMap<Button, ButtonState>,
    pub(crate) joysticks: HashMap<Joystick, JoystickState>,
}
impl GamepadState {
    pub fn new() -> Self {
        GamepadState {
            buttons: HashMap::new(),
            joysticks: HashMap::new(),
        }
    }

    pub fn buttons(&self) -> &HashMap<Button, ButtonState> {
        &self.buttons
    }

    pub fn buttons_mut(&mut self) -> &mut HashMap<Button, ButtonState> {
        &mut self.buttons
    }

    pub fn joystick(&self, joystick: Joystick) -> (f32, f32) {
        if let Some(joystick) = self.joysticks.get(&joystick) {
            return joystick.normalized_value;
        }

        (0.0, 0.0)
    }

    pub fn joystick_raw(&self, joystick: Joystick) -> (i16, i16) {
        if let Some(joystick) = self.joysticks.get(&joystick) {
            return joystick.raw_value;
        }

        (0, 0)
    }

    pub fn joysticks(&self) -> &HashMap<Joystick, JoystickState> {
        &self.joysticks
    }
    pub fn joysticks_mut(&mut self) -> &mut HashMap<Joystick, JoystickState> {
        &mut self.joysticks
    }

    pub fn is_pressed(&self, button: Button) -> bool {
        match self.buttons.get(&button) {
            Some(button_state) => button_state.is_pressed,
            None => false,
        }
    }

    pub fn is_just_pressed(&self, button: Button) -> bool {
        match self.buttons.get(&button) {
            Some(button_state) => button_state.is_pressed && !button_state.was_pressed,
            None => false,
        }
    }

    pub fn is_just_released(&self, button: Button) -> bool {
        match self.buttons.get(&button) {
            Some(button_state) => !button_state.is_pressed && button_state.was_pressed,
            None => false,
        }
    }
}

#[derive(Clone, Debug)]
pub struct JoystickState {
    pub(crate) raw_value: (i16, i16),
    pub(crate) normalized_value: (f32, f32),
}
impl JoystickState {
    pub fn new(raw_value: (i16, i16), normalized_value: (f32, f32)) -> Self {
        JoystickState {
            raw_value,
            normalized_value,
        }
    }
}
impl Default for JoystickState {
    fn default() -> JoystickState {
        JoystickState {
            raw_value: (0, 0),
            normalized_value: (0.0, 0.0),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ButtonState {
    pub(crate) is_pressed: bool,
    pub(crate) was_pressed: bool,
}
impl ButtonState {
    pub fn new(is_pressed: bool, was_pressed: bool) -> Self {
        ButtonState {
            is_pressed,
            was_pressed,
        }
    }
    pub fn is_pressed(&self) -> bool {
        self.is_pressed
    }

    pub fn is_just_pressed(&self) -> bool {
        self.is_pressed && !self.was_pressed
    }

    pub fn is_just_released(&self) -> bool {
        !self.is_pressed && self.was_pressed
    }
}
impl Default for ButtonState {
    fn default() -> ButtonState {
        ButtonState::new(false, false)
    }
}

#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq)]
pub enum Button {
    DPadNorth,
    DPadSouth,
    DPadWest,
    DPadEast,
    North,
    South,
    West,
    East,
    LeftShoulder,
    RightShoulder,
    LeftTrigger,
    RightTrigger,
    RightStick,
    LeftStick,
    Menu,
    Select,
    Start,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Joystick {
    Left,
    Right,
}