feat(input): implement Gamepad button translation and Android keycodes
This commit is contained in:
parent
8a7a05ec9a
commit
98328f6014
3 changed files with 99 additions and 0 deletions
73
crates/molotov-core/src/input/gamepad.rs
Normal file
73
crates/molotov-core/src/input/gamepad.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum GamepadType {
|
||||||
|
Xbox,
|
||||||
|
PlayStation,
|
||||||
|
NintendoSwitch,
|
||||||
|
Generic,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum GamepadButton {
|
||||||
|
South, // A (Xbox) / Cross (PS) / B (Switch)
|
||||||
|
East, // B (Xbox) / Circle (PS) / A (Switch)
|
||||||
|
West, // X (Xbox) / Square (PS) / Y (Switch)
|
||||||
|
North, // Y (Xbox) / Triangle (PS) / X (Switch)
|
||||||
|
Back,
|
||||||
|
Guide,
|
||||||
|
Start,
|
||||||
|
LeftStick,
|
||||||
|
RightStick,
|
||||||
|
LeftShoulder,
|
||||||
|
RightShoulder,
|
||||||
|
DpadUp,
|
||||||
|
DpadDown,
|
||||||
|
DpadLeft,
|
||||||
|
DpadRight,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(i32)]
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum AndroidKeyCode {
|
||||||
|
DpadUp = 19,
|
||||||
|
DpadDown = 20,
|
||||||
|
DpadLeft = 21,
|
||||||
|
DpadRight = 22,
|
||||||
|
DpadCenter = 23,
|
||||||
|
ButtonA = 96,
|
||||||
|
ButtonB = 97,
|
||||||
|
ButtonC = 98,
|
||||||
|
ButtonX = 99,
|
||||||
|
ButtonY = 100,
|
||||||
|
ButtonZ = 101,
|
||||||
|
ButtonL1 = 102,
|
||||||
|
ButtonR1 = 103,
|
||||||
|
ButtonL2 = 104,
|
||||||
|
ButtonR2 = 105,
|
||||||
|
ButtonThumbL = 106,
|
||||||
|
ButtonThumbR = 107,
|
||||||
|
ButtonStart = 108,
|
||||||
|
ButtonSelect = 109,
|
||||||
|
ButtonMode = 110,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_button_to_android(btn: GamepadButton, _gamepad_type: GamepadType) -> AndroidKeyCode {
|
||||||
|
match btn {
|
||||||
|
GamepadButton::South => AndroidKeyCode::ButtonA,
|
||||||
|
GamepadButton::East => AndroidKeyCode::ButtonB,
|
||||||
|
GamepadButton::West => AndroidKeyCode::ButtonX,
|
||||||
|
GamepadButton::North => AndroidKeyCode::ButtonY,
|
||||||
|
GamepadButton::Back => AndroidKeyCode::ButtonSelect,
|
||||||
|
GamepadButton::Guide => AndroidKeyCode::ButtonMode,
|
||||||
|
GamepadButton::Start => AndroidKeyCode::ButtonStart,
|
||||||
|
GamepadButton::LeftStick => AndroidKeyCode::ButtonThumbL,
|
||||||
|
GamepadButton::RightStick => AndroidKeyCode::ButtonThumbR,
|
||||||
|
GamepadButton::LeftShoulder => AndroidKeyCode::ButtonL1,
|
||||||
|
GamepadButton::RightShoulder => AndroidKeyCode::ButtonR1,
|
||||||
|
GamepadButton::DpadUp => AndroidKeyCode::DpadUp,
|
||||||
|
GamepadButton::DpadDown => AndroidKeyCode::DpadDown,
|
||||||
|
GamepadButton::DpadLeft => AndroidKeyCode::DpadLeft,
|
||||||
|
GamepadButton::DpadRight => AndroidKeyCode::DpadRight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
pub mod gamepad;
|
||||||
pub mod ime;
|
pub mod ime;
|
||||||
|
|
|
||||||
25
crates/molotov-core/tests/gamepad_mapping_test.rs
Normal file
25
crates/molotov-core/tests/gamepad_mapping_test.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
use molotov_core::input::gamepad::{
|
||||||
|
translate_button_to_android, AndroidKeyCode, GamepadButton, GamepadType,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_xbox_and_playstation_button_mapping() {
|
||||||
|
// Xbox 'A' button -> Android KEYCODE_BUTTON_A
|
||||||
|
assert_eq!(
|
||||||
|
translate_button_to_android(GamepadButton::South, GamepadType::Xbox),
|
||||||
|
AndroidKeyCode::ButtonA
|
||||||
|
);
|
||||||
|
|
||||||
|
// PlayStation 'Cross' button -> Android KEYCODE_BUTTON_A
|
||||||
|
assert_eq!(
|
||||||
|
translate_button_to_android(GamepadButton::South, GamepadType::PlayStation),
|
||||||
|
AndroidKeyCode::ButtonA
|
||||||
|
);
|
||||||
|
|
||||||
|
// D-Pad Up -> Android KEYCODE_DPAD_UP
|
||||||
|
assert_eq!(
|
||||||
|
translate_button_to_android(GamepadButton::DpadUp, GamepadType::Xbox),
|
||||||
|
AndroidKeyCode::DpadUp
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in a new issue