gsa 0.2.1

Game development library modelled after an imaginary console
Documentation
use gilrs::Button;
use winit::event::ScanCode;

/// UP on dpad
pub const DPAD_UP: Buttons = 0x0001;
/// DOWN on dpad
pub const DPAD_DOWN: Buttons = 0x0002;
/// LEFT on dpad
pub const DPAD_LEFT: Buttons = 0x0004;
/// RIGHT on dpad
pub const DPAD_RIGHT: Buttons = 0x008;
/// X on nintendo, Y on microsoft, TRIANGLE on sony
pub const FACE_UP: Buttons = 0x0010;
/// B on nintendo, A on microsoft, CROSS on sony
pub const FACE_DOWN: Buttons = 0x0020;
/// Y on nintendo, X on microsoft, SQUARE on sony
pub const FACE_LEFT: Buttons = 0x0040;
/// A on nintendo, B on microsoft, CIRCLE on sony
pub const FACE_RIGHT: Buttons = 0x0080;
/// left shoulder button
pub const L: Buttons = 0x0100;
/// right shoulder button
pub const R: Buttons = 0x0200;
/// start button
pub const START: Buttons = 0x0400;
/// select button
pub const SELECT: Buttons = 0x0800;

/// Bitmask of button states
///
/// GSA pretends all input is a snes-like gamepad
/// d-pad, 4 face buttons, l and r shoulder buttons, start and select
pub type Buttons = u16;

pub(crate) fn button_from_scancode(scancode: ScanCode) -> Buttons {
    //todo: currently windows only? check
    match scancode {
        0x0010 => L,
        0x0011 => FACE_UP,
        0x0012 => R,
        0x001C => START,
        0x001E => FACE_LEFT,
        0x001F => FACE_DOWN,
        0x0020 => FACE_RIGHT,
        0x0039 => SELECT,
        0xE048 => DPAD_UP,
        0xE04B => DPAD_LEFT,
        0xE04D => DPAD_RIGHT,
        0xE050 => DPAD_DOWN,
        _ => 0,
    }
}

pub(crate) fn button_from_gilrs(button: Button) -> Buttons {
    match button {
        Button::South => FACE_DOWN,
        Button::East => FACE_RIGHT,
        Button::North => FACE_UP,
        Button::West => FACE_LEFT,
        Button::LeftTrigger | Button::LeftTrigger2 => L,
        Button::RightTrigger | Button::RightTrigger2 => R,
        Button::Select => SELECT,
        Button::Start => START,
        Button::DPadUp => DPAD_UP,
        Button::DPadDown => DPAD_DOWN,
        Button::DPadLeft => DPAD_LEFT,
        Button::DPadRight => DPAD_RIGHT,
        _ => 0,
    }
}