#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GamepadId {
pub id: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GamepadButton {
South,
East,
North,
West,
LeftBumper,
RightBumper,
LeftTrigger,
RightTrigger,
Select,
Start,
Mode,
LeftThumb,
RightThumb,
DPadUp,
DPadDown,
DPadLeft,
DPadRight,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GamepadAxis {
LeftStickX,
LeftStickY,
RightStickX,
RightStickY,
LeftZ,
RightZ,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GamepadState {
pub id: GamepadId,
pub connected: bool,
pub buttons: u32,
pub left_stick_x: f32,
pub left_stick_y: f32,
pub right_stick_x: f32,
pub right_stick_y: f32,
pub left_z: f32,
pub right_z: f32,
}
impl GamepadButton {
pub fn bit(self) -> u32 {
1u32 << (self as u32)
}
}
impl GamepadState {
pub fn empty(id: GamepadId) -> Self {
Self {
id,
connected: false,
buttons: 0,
left_stick_x: 0.0,
left_stick_y: 0.0,
right_stick_x: 0.0,
right_stick_y: 0.0,
left_z: 0.0,
right_z: 0.0,
}
}
pub fn is_pressed(&self, button: GamepadButton) -> bool {
self.buttons & button.bit() != 0
}
pub fn axis(&self, axis: GamepadAxis) -> f32 {
match axis {
GamepadAxis::LeftStickX => self.left_stick_x,
GamepadAxis::LeftStickY => self.left_stick_y,
GamepadAxis::RightStickX => self.right_stick_x,
GamepadAxis::RightStickY => self.right_stick_y,
GamepadAxis::LeftZ => self.left_z,
GamepadAxis::RightZ => self.right_z,
}
}
}
impl_option!(
GamepadState,
OptionGamepadState,
[Debug, Clone, Copy, PartialEq]
);