diff --git a/include/recomp_input.h b/include/recomp_input.h index b27faa0..fbbbef7 100644 --- a/include/recomp_input.h +++ b/include/recomp_input.h @@ -74,7 +74,9 @@ namespace recomp { }; void start_scanning_input(InputDevice device); + void stop_scanning_input(); void finish_scanning_input(InputField scanned_field); + void cancel_scanning_input(); InputField get_scanned_input(); struct DefaultN64Mappings { diff --git a/src/game/input.cpp b/src/game/input.cpp index a1f8aa4..66b816d 100644 --- a/src/game/input.cpp +++ b/src/game/input.cpp @@ -60,6 +60,10 @@ void recomp::start_scanning_input(recomp::InputDevice device) { scanning_device.store(device); } +void recomp::stop_scanning_input() { + scanning_device.store(recomp::InputDevice::COUNT); +} + void queue_if_enabled(SDL_Event* event) { if (!recomp::all_input_disabled()) { recomp::queue_event(*event); @@ -81,8 +85,12 @@ bool sdl_event_filter(void* userdata, SDL_Event* event) { if (keyevent->keysym.scancode == SDL_Scancode::SDL_SCANCODE_RETURN && (keyevent->keysym.mod & SDL_Keymod::KMOD_ALT)) { RT64ChangeWindow(); } - if (scanning_device == recomp::InputDevice::Keyboard) { - set_scanned_input({(uint32_t)InputType::Keyboard, keyevent->keysym.scancode}); + if (scanning_device != recomp::InputDevice::COUNT) { + if (keyevent->keysym.scancode == SDL_Scancode::SDL_SCANCODE_ESCAPE) { + recomp::cancel_scanning_input(); + } else if (scanning_device == recomp::InputDevice::Keyboard) { + set_scanned_input({(uint32_t)InputType::Keyboard, keyevent->keysym.scancode}); + } } else { queue_if_enabled(event); } @@ -123,9 +131,13 @@ bool sdl_event_filter(void* userdata, SDL_Event* event) { queue_if_enabled(event); break; case SDL_EventType::SDL_CONTROLLERBUTTONDOWN: - if (scanning_device == recomp::InputDevice::Controller) { - SDL_ControllerButtonEvent* button_event = &event->cbutton; - set_scanned_input({(uint32_t)InputType::ControllerDigital, button_event->button}); + if (scanning_device != recomp::InputDevice::COUNT) { + if (event->cbutton.button == SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_BACK) { + recomp::cancel_scanning_input(); + } else if (scanning_device == recomp::InputDevice::Controller) { + SDL_ControllerButtonEvent* button_event = &event->cbutton; + set_scanned_input({(uint32_t)InputType::ControllerDigital, button_event->button}); + } } else { queue_if_enabled(event); } diff --git a/src/ui/ui_config.cpp b/src/ui/ui_config.cpp index b9a8d01..458d2e5 100644 --- a/src/ui/ui_config.cpp +++ b/src/ui/ui_config.cpp @@ -75,6 +75,15 @@ void recomp::finish_scanning_input(recomp::InputField scanned_field) { controls_model_handle.DirtyVariable("active_binding_slot"); } +void recomp::cancel_scanning_input() { + recomp::stop_scanning_input(); + scanned_input_index = -1; + scanned_binding_index = -1; + controls_model_handle.DirtyVariable("inputs"); + controls_model_handle.DirtyVariable("active_binding_input"); + controls_model_handle.DirtyVariable("active_binding_slot"); +} + void close_config_menu() { recomp::save_config();