From b2928de6143d6a76a77b55ff8b3392b7a2070693 Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Sun, 3 May 2020 22:03:57 +0200 Subject: [PATCH] Use positional keyboard mapping instead of symbolic in the SDL2 frontend Former-commit-id: 778b79f4afd5d290399e518f9b1a45470c5ad3a9 Former-commit-id: 58b2e0dd75acbe01fe0f4e73c0fa73af8caa88c2 --- platform/rustboyadvance-sdl2/src/input.rs | 34 +++++++++++------------ platform/rustboyadvance-sdl2/src/main.rs | 22 +++++++-------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/platform/rustboyadvance-sdl2/src/input.rs b/platform/rustboyadvance-sdl2/src/input.rs index 8a169a4..df2207c 100644 --- a/platform/rustboyadvance-sdl2/src/input.rs +++ b/platform/rustboyadvance-sdl2/src/input.rs @@ -1,4 +1,4 @@ -use sdl2::keyboard::Keycode; +use sdl2::keyboard::Scancode; use rustboyadvance_core::keypad as gba_keypad; use rustboyadvance_core::InputInterface; @@ -17,30 +17,30 @@ impl InputInterface for Sdl2Input { } impl Sdl2Input { - pub fn on_keyboard_key_down(&mut self, keycode: Keycode) { - if let Some(key) = keycode_to_keypad(keycode) { + pub fn on_keyboard_key_down(&mut self, scancode: Scancode) { + if let Some(key) = scancode_to_keypad(scancode) { 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) { + pub fn on_keyboard_key_up(&mut self, scancode: Scancode) { + if let Some(key) = scancode_to_keypad(scancode) { self.keyinput.set_bit(key as usize, true); } } } -fn keycode_to_keypad(keycode: Keycode) -> Option { - 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), +fn scancode_to_keypad(scancode: Scancode) -> Option { + match scancode { + Scancode::Up => Some(gba_keypad::Keys::Up), + Scancode::Down => Some(gba_keypad::Keys::Down), + Scancode::Left => Some(gba_keypad::Keys::Left), + Scancode::Right => Some(gba_keypad::Keys::Right), + Scancode::Z => Some(gba_keypad::Keys::ButtonB), + Scancode::X => Some(gba_keypad::Keys::ButtonA), + Scancode::Return => Some(gba_keypad::Keys::Start), + Scancode::Backspace => Some(gba_keypad::Keys::Select), + Scancode::A => Some(gba_keypad::Keys::ButtonL), + Scancode::S => Some(gba_keypad::Keys::ButtonR), _ => None, } } diff --git a/platform/rustboyadvance-sdl2/src/main.rs b/platform/rustboyadvance-sdl2/src/main.rs index 1d17f27..f38fc5a 100644 --- a/platform/rustboyadvance-sdl2/src/main.rs +++ b/platform/rustboyadvance-sdl2/src/main.rs @@ -1,7 +1,7 @@ use sdl2; use sdl2::event::Event; use sdl2::image::{InitFlag, LoadTexture}; -use sdl2::keyboard::Keycode; +use sdl2::keyboard::Scancode; use sdl2::messagebox::*; use sdl2::pixels::Color; use sdl2::rect::Rect; @@ -255,18 +255,18 @@ fn main() -> Result<(), Box> { for event in event_pump.poll_iter() { match event { Event::KeyDown { - keycode: Some(keycode), + scancode: Some(scancode), .. - } => match keycode { - Keycode::Space => frame_limiter = false, + } => match scancode { + Scancode::Space => frame_limiter = false, k => input.borrow_mut().on_keyboard_key_down(k), }, Event::KeyUp { - keycode: Some(keycode), + scancode: Some(scancode), .. - } => match keycode { + } => match scancode { #[cfg(feature = "debugger")] - Keycode::F1 => { + Scancode::F1 => { let mut debugger = Debugger::new(gba); info!("starting debugger..."); debugger.repl(matches.value_of("script_file")).unwrap(); @@ -274,8 +274,8 @@ fn main() -> Result<(), Box> { info!("ending debugger...") } #[cfg(feature = "gdb")] - Keycode::F2 => spawn_and_run_gdb_server(&mut gba, DEFAULT_GDB_SERVER_ADDR)?, - Keycode::F5 => { + Scancode::F2 => spawn_and_run_gdb_server(&mut gba, DEFAULT_GDB_SERVER_ADDR)?, + Scancode::F5 => { info!("Saving state ..."); let save = gba.save_state()?; write_bin_file(&savestate_path, &save)?; @@ -285,7 +285,7 @@ fn main() -> Result<(), Box> { bytesize::ByteSize::b(save.len() as u64) ); } - Keycode::F9 => { + Scancode::F9 => { if savestate_path.is_file() { let save = read_bin_file(&savestate_path)?; info!("Restoring state from {:?}...", savestate_path); @@ -295,7 +295,7 @@ fn main() -> Result<(), Box> { info!("Savestate not created, please create one by pressing F5"); } } - Keycode::Space => frame_limiter = true, + Scancode::Space => frame_limiter = true, k => input.borrow_mut().on_keyboard_key_up(k), }, Event::Quit { .. } => break 'running,