1use bitmask::bitmask;
2
3use crate::db_internal::{gamepad_readState, gamepad_setRumble, gamepad_isConnected};
4
5#[repr(C)]
6#[derive(Clone, Copy)]
7pub enum GamepadSlot {
8 SlotA,
9 SlotB,
10 SlotC,
11 SlotD,
12}
13
14bitmask! {
15 #[repr(C)]
16 pub mask GamepadButtonMask: u16 where flags GamepadButton {
17 A = 1,
18 B = (1 << 1),
19 X = (1 << 2),
20 Y = (1 << 3),
21 Up = (1 << 4),
22 Down = (1 << 5),
23 Left = (1 << 6),
24 Right = (1 << 7),
25 L1 = (1 << 8),
26 L2 = (1 << 9),
27 L3 = (1 << 10),
28 R1 = (1 << 11),
29 R2 = (1 << 12),
30 R3 = (1 << 13),
31 Select = (1 << 14),
32 Start = (1 << 15),
33 }
34}
35
36#[repr(C)]
37#[derive(Clone, Copy, Debug)]
38pub struct GamepadState {
39 pub button_mask: GamepadButtonMask,
40 pub left_stick_x: i16,
41 pub left_stick_y: i16,
42 pub right_stick_x: i16,
43 pub right_stick_y: i16,
44}
45
46impl GamepadState {
47 pub fn is_pressed(self, button: GamepadButton) -> bool {
49 return self.button_mask.contains(button);
50 }
51}
52
53pub struct Gamepad {
54 pub slot: GamepadSlot,
55}
56
57impl Gamepad {
58 pub const fn new(slot: GamepadSlot) -> Gamepad {
60 return Gamepad { slot: slot };
61 }
62
63 pub fn is_connected(&self) -> bool {
65 unsafe { return gamepad_isConnected(self.slot); }
66 }
67
68 pub fn read_state(&self) -> GamepadState {
70 let mut state = GamepadState { button_mask: GamepadButtonMask::none(), left_stick_x: 0, left_stick_y: 0, right_stick_x: 0, right_stick_y: 0 };
71 unsafe { gamepad_readState(self.slot, &mut state); }
72 return state;
73 }
74
75 pub fn set_rumble(&self, enable: bool) {
77 unsafe { gamepad_setRumble(self.slot, enable); }
78 }
79}