sm64_binds/sm64/
game_pad.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
4pub struct GamePad {
5 pub button: u16,
6 pub stick_x: i8,
7 pub stick_y: i8,
8}
9
10impl GamePad {
11 pub const fn new(button: u16, stick_x: i8, stick_y: i8) -> Self {
12 GamePad { button, stick_x, stick_y }
13 }
14
15 pub const fn default() -> Self {
16 GamePad::new(0,0,0)
17 }
18
19 pub fn equals(&self, pad: &GamePad) -> bool {
20 self.button == pad.button && self.stick_x == pad.stick_x && self.stick_y == pad.stick_y
21 }
22
23 pub fn from_bytes(data: &[u8;4]) -> Self {
24 let button = u16::from_le_bytes(data[0..2].try_into().unwrap());
25 let stick_x = data[2] as i8;
26 let stick_y = data[3] as i8;
27 GamePad::new(button, stick_x, stick_y)
28 }
29
30 pub const fn is_pressed(&self, button_mask: u16) -> bool {
31 (self.button & button_mask) != 0
32 }
33
34 pub fn enable_button(&mut self, button_mask: u16) {
35 self.button |= button_mask;
36 }
37
38 pub fn disable_button(&mut self, button_mask: u16) {
39 self.button &= !button_mask;
40 }
41}