This repository has been archived on 2024-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
rustboyadvance-ng/platform/rustboyadvance-sdl2/src/input.rs
Michel Heily b21cd1e3d9 Refactor project workspace and fix build
Former-commit-id: 8e264cdbc9e5290654c29e6893a121bce73c29c9
2020-04-10 00:53:41 +03:00

52 lines
1.4 KiB
Rust

use sdl2::keyboard::Keycode;
use rustboyadvance_ng::core::keypad as gba_keypad;
use rustboyadvance_ng::InputInterface;
use bit;
use bit::BitIndex;
pub struct Sdl2Input {
keyinput: u16,
}
impl InputInterface for Sdl2Input {
fn poll(&mut self) -> u16 {
self.keyinput
}
}
impl Sdl2Input {
pub fn on_keyboard_key_down(&mut self, keycode: Keycode) {
if let Some(key) = keycode_to_keypad(keycode) {
self.keyinput.set_bit(key as usize, false);
}
}
pub fn on_keyboard_key_up(&mut self, keycode: Keycode) {
if let Some(key) = keycode_to_keypad(keycode) {
self.keyinput.set_bit(key as usize, true);
}
}
}
fn keycode_to_keypad(keycode: Keycode) -> Option<gba_keypad::Keys> {
match keycode {
Keycode::Up => Some(gba_keypad::Keys::Up),
Keycode::Down => Some(gba_keypad::Keys::Down),
Keycode::Left => Some(gba_keypad::Keys::Left),
Keycode::Right => Some(gba_keypad::Keys::Right),
Keycode::Z => Some(gba_keypad::Keys::ButtonB),
Keycode::X => Some(gba_keypad::Keys::ButtonA),
Keycode::Return => Some(gba_keypad::Keys::Start),
Keycode::Backspace => Some(gba_keypad::Keys::Select),
Keycode::A => Some(gba_keypad::Keys::ButtonL),
Keycode::S => Some(gba_keypad::Keys::ButtonR),
_ => None,
}
}
pub fn create_input() -> Sdl2Input {
Sdl2Input {
keyinput: gba_keypad::KEYINPUT_ALL_RELEASED,
}
}