apollo_hyper_libretro_bindings/
buttons.rs

1use apollo_hyper_libretro_sys::{
2    DEVICE_ID_JOYPAD_A, DEVICE_ID_JOYPAD_B, DEVICE_ID_JOYPAD_DOWN, DEVICE_ID_JOYPAD_L,
3    DEVICE_ID_JOYPAD_L2, DEVICE_ID_JOYPAD_L3, DEVICE_ID_JOYPAD_LEFT, DEVICE_ID_JOYPAD_R,
4    DEVICE_ID_JOYPAD_R2, DEVICE_ID_JOYPAD_R3, DEVICE_ID_JOYPAD_RIGHT, DEVICE_ID_JOYPAD_SELECT,
5    DEVICE_ID_JOYPAD_START, DEVICE_ID_JOYPAD_UP, DEVICE_ID_JOYPAD_X, DEVICE_ID_JOYPAD_Y,
6};
7
8#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
9pub struct InputPort {
10    pub buttons: Buttons,
11    pub joystick_x: i16,
12    pub joystick_y: i16,
13    pub mouse_x: i16,
14    pub mouse_y: i16,
15    pub mouse_left_down: bool,
16    pub mouse_right_down: bool,
17    pub mouse_middle_down: bool,
18}
19
20impl InputPort {
21    pub fn new() -> Self {
22        InputPort::default()
23    }
24}
25
26#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
27pub struct Buttons(i16);
28
29impl Buttons {
30    pub fn new() -> Self {
31        Buttons::default()
32    }
33    pub fn get(self, id: u32) -> bool {
34        assert!(id < 16);
35        (self.0 & (1 << id)) != 0
36    }
37    fn mask_inout(self, b: bool, id: u32) -> Self {
38        assert!(id < 16);
39        if b {
40            Buttons(self.0 | (1 << id))
41        } else {
42            Buttons(self.0 & !(1 << id))
43        }
44    }
45    pub fn up(self, b: bool) -> Self {
46        self.mask_inout(b, DEVICE_ID_JOYPAD_UP)
47    }
48    pub fn down(self, b: bool) -> Self {
49        self.mask_inout(b, DEVICE_ID_JOYPAD_DOWN)
50    }
51    pub fn left(self, b: bool) -> Self {
52        self.mask_inout(b, DEVICE_ID_JOYPAD_LEFT)
53    }
54    pub fn right(self, b: bool) -> Self {
55        self.mask_inout(b, DEVICE_ID_JOYPAD_RIGHT)
56    }
57
58    pub fn select(self, b: bool) -> Self {
59        self.mask_inout(b, DEVICE_ID_JOYPAD_SELECT)
60    }
61    pub fn start(self, b: bool) -> Self {
62        self.mask_inout(b, DEVICE_ID_JOYPAD_START)
63    }
64    pub fn a(self, b: bool) -> Self {
65        self.mask_inout(b, DEVICE_ID_JOYPAD_A)
66    }
67    pub fn b(self, b: bool) -> Self {
68        self.mask_inout(b, DEVICE_ID_JOYPAD_B)
69    }
70    pub fn y(self, b: bool) -> Self {
71        self.mask_inout(b, DEVICE_ID_JOYPAD_Y)
72    }
73    pub fn x(self, b: bool) -> Self {
74        self.mask_inout(b, DEVICE_ID_JOYPAD_X)
75    }
76    pub fn l1(self, b: bool) -> Self {
77        self.mask_inout(b, DEVICE_ID_JOYPAD_L)
78    }
79    pub fn r1(self, b: bool) -> Self {
80        self.mask_inout(b, DEVICE_ID_JOYPAD_R)
81    }
82    pub fn l2(self, b: bool) -> Self {
83        self.mask_inout(b, DEVICE_ID_JOYPAD_L2)
84    }
85    pub fn r2(self, b: bool) -> Self {
86        self.mask_inout(b, DEVICE_ID_JOYPAD_R2)
87    }
88    pub fn l3(self, b: bool) -> Self {
89        self.mask_inout(b, DEVICE_ID_JOYPAD_L3)
90    }
91    pub fn r3(self, b: bool) -> Self {
92        self.mask_inout(b, DEVICE_ID_JOYPAD_R3)
93    }
94
95    pub fn get_up(self) -> bool {
96        self.get(DEVICE_ID_JOYPAD_UP)
97    }
98    pub fn get_down(self) -> bool {
99        self.get(DEVICE_ID_JOYPAD_DOWN)
100    }
101    pub fn get_left(self) -> bool {
102        self.get(DEVICE_ID_JOYPAD_LEFT)
103    }
104    pub fn get_right(self) -> bool {
105        self.get(DEVICE_ID_JOYPAD_RIGHT)
106    }
107
108    pub fn get_select(self) -> bool {
109        self.get(DEVICE_ID_JOYPAD_SELECT)
110    }
111    pub fn get_start(self) -> bool {
112        self.get(DEVICE_ID_JOYPAD_START)
113    }
114    pub fn get_a(self) -> bool {
115        self.get(DEVICE_ID_JOYPAD_A)
116    }
117    pub fn get_b(self) -> bool {
118        self.get(DEVICE_ID_JOYPAD_B)
119    }
120    pub fn get_y(self) -> bool {
121        self.get(DEVICE_ID_JOYPAD_Y)
122    }
123    pub fn get_x(self) -> bool {
124        self.get(DEVICE_ID_JOYPAD_X)
125    }
126    pub fn get_l1(self) -> bool {
127        self.get(DEVICE_ID_JOYPAD_L)
128    }
129    pub fn get_r1(self) -> bool {
130        self.get(DEVICE_ID_JOYPAD_R)
131    }
132    pub fn get_l2(self) -> bool {
133        self.get(DEVICE_ID_JOYPAD_L2)
134    }
135    pub fn get_r2(self) -> bool {
136        self.get(DEVICE_ID_JOYPAD_R2)
137    }
138    pub fn get_l3(self) -> bool {
139        self.get(DEVICE_ID_JOYPAD_L3)
140    }
141    pub fn get_r3(self) -> bool {
142        self.get(DEVICE_ID_JOYPAD_R3)
143    }
144}