From 36e5f7a150562432573f45a2e9a5484cfa77db8c Mon Sep 17 00:00:00 2001 From: scawful Date: Mon, 18 Nov 2024 14:34:08 -0500 Subject: [PATCH] Refactor input handling in Emulator and Controller for improved key event management --- src/app/core/controller.cc | 133 +++++-------------------------------- src/app/emu/emulator.cc | 96 +++++++++++++++++++++++++- src/app/emu/emulator.h | 17 +++++ 3 files changed, 128 insertions(+), 118 deletions(-) diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index 4b020110..bef46801 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -48,119 +48,6 @@ void NewMasterFrame() { } } -void HandleKeyDown(SDL_Event &event, editor::EditorManager &editor) { - ImGuiIO &io = ImGui::GetIO(); - io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); - io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); - io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); - io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); - - switch (event.key.keysym.sym) { - case SDLK_z: - editor.emulator().snes().SetButtonState(1, 0, true); - break; - case SDLK_a: - editor.emulator().snes().SetButtonState(1, 1, true); - break; - case SDLK_RSHIFT: - editor.emulator().snes().SetButtonState(1, 2, true); - break; - case SDLK_RETURN: - editor.emulator().snes().SetButtonState(1, 3, true); - break; - case SDLK_UP: - editor.emulator().snes().SetButtonState(1, 4, true); - break; - case SDLK_DOWN: - editor.emulator().snes().SetButtonState(1, 5, true); - break; - case SDLK_LEFT: - editor.emulator().snes().SetButtonState(1, 6, true); - break; - case SDLK_RIGHT: - editor.emulator().snes().SetButtonState(1, 7, true); - break; - case SDLK_x: - editor.emulator().snes().SetButtonState(1, 8, true); - break; - case SDLK_s: - editor.emulator().snes().SetButtonState(1, 9, true); - break; - case SDLK_d: - editor.emulator().snes().SetButtonState(1, 10, true); - break; - case SDLK_c: - editor.emulator().snes().SetButtonState(1, 11, true); - break; - default: - break; - } -} - -void HandleKeyUp(SDL_Event &event, editor::EditorManager &editor) { - ImGuiIO &io = ImGui::GetIO(); - int key = event.key.keysym.scancode; - io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); - io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); - io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); - io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); - - switch (event.key.keysym.sym) { - case SDLK_z: - editor.emulator().snes().SetButtonState(1, 0, false); - break; - case SDLK_a: - editor.emulator().snes().SetButtonState(1, 1, false); - break; - case SDLK_RSHIFT: - editor.emulator().snes().SetButtonState(1, 2, false); - break; - case SDLK_RETURN: - editor.emulator().snes().SetButtonState(1, 3, false); - break; - case SDLK_UP: - editor.emulator().snes().SetButtonState(1, 4, false); - break; - case SDLK_DOWN: - editor.emulator().snes().SetButtonState(1, 5, false); - break; - case SDLK_LEFT: - editor.emulator().snes().SetButtonState(1, 6, false); - break; - case SDLK_RIGHT: - editor.emulator().snes().SetButtonState(1, 7, false); - break; - case SDLK_x: - editor.emulator().snes().SetButtonState(1, 8, false); - break; - case SDLK_s: - editor.emulator().snes().SetButtonState(1, 9, false); - break; - case SDLK_d: - editor.emulator().snes().SetButtonState(1, 10, false); - break; - case SDLK_c: - editor.emulator().snes().SetButtonState(1, 11, false); - break; - default: - break; - } -} - -void HandleMouseMovement(int &wheel) { - ImGuiIO &io = ImGui::GetIO(); - int mouseX; - int mouseY; - const int buttons = SDL_GetMouseState(&mouseX, &mouseY); - - io.DeltaTime = 1.0f / 60.0f; - io.MousePos = ImVec2(static_cast(mouseX), static_cast(mouseY)); - io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT); - io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT); - io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE); - io.MouseWheel = static_cast(wheel); -} - } // namespace absl::Status Controller::OnEntry(std::string filename) { @@ -195,10 +82,12 @@ void Controller::OnInput() { ImGui_ImplSDL2_ProcessEvent(&event); switch (event.type) { case SDL_KEYDOWN: - HandleKeyDown(event, editor_manager_); - break; case SDL_KEYUP: - HandleKeyUp(event, editor_manager_); + ImGuiIO &io = ImGui::GetIO(); + io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); + io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); + io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); + io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); break; case SDL_WINDOWEVENT: switch (event.window.event) { @@ -218,7 +107,17 @@ void Controller::OnInput() { } } - HandleMouseMovement(wheel); + ImGuiIO &io = ImGui::GetIO(); + int mouseX; + int mouseY; + const int buttons = SDL_GetMouseState(&mouseX, &mouseY); + + io.DeltaTime = 1.0f / 60.0f; + io.MousePos = ImVec2(static_cast(mouseX), static_cast(mouseY)); + io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT); + io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT); + io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE); + io.MouseWheel = static_cast(wheel); } absl::Status Controller::OnLoad() { diff --git a/src/app/emu/emulator.cc b/src/app/emu/emulator.cc index bafec57f..ac912ac3 100644 --- a/src/app/emu/emulator.cc +++ b/src/app/emu/emulator.cc @@ -261,7 +261,101 @@ void Emulator::RenderNavBar() { void Emulator::HandleEvents() { // Handle user input events - // ... + if (ImGui::IsKeyPressed(keybindings_.a_button)) { + snes_.SetButtonState(1, 0, true); + } + + if (ImGui::IsKeyPressed(keybindings_.b_button)) { + snes_.SetButtonState(1, 1, true); + } + + if (ImGui::IsKeyPressed(keybindings_.select_button)) { + snes_.SetButtonState(1, 2, true); + } + + if (ImGui::IsKeyPressed(keybindings_.start_button)) { + snes_.SetButtonState(1, 3, true); + } + + if (ImGui::IsKeyPressed(keybindings_.up_button)) { + snes_.SetButtonState(1, 4, true); + } + + if (ImGui::IsKeyPressed(keybindings_.down_button)) { + snes_.SetButtonState(1, 5, true); + } + + if (ImGui::IsKeyPressed(keybindings_.left_button)) { + snes_.SetButtonState(1, 6, true); + } + + if (ImGui::IsKeyPressed(keybindings_.right_button)) { + snes_.SetButtonState(1, 7, true); + } + + if (ImGui::IsKeyPressed(keybindings_.x_button)) { + snes_.SetButtonState(1, 8, true); + } + + if (ImGui::IsKeyPressed(keybindings_.y_button)) { + snes_.SetButtonState(1, 9, true); + } + + if (ImGui::IsKeyPressed(keybindings_.l_button)) { + snes_.SetButtonState(1, 10, true); + } + + if (ImGui::IsKeyPressed(keybindings_.r_button)) { + snes_.SetButtonState(1, 11, true); + } + + if (ImGui::IsKeyReleased(keybindings_.a_button)) { + snes_.SetButtonState(1, 0, false); + } + + if (ImGui::IsKeyReleased(keybindings_.b_button)) { + snes_.SetButtonState(1, 1, false); + } + + if (ImGui::IsKeyReleased(keybindings_.select_button)) { + snes_.SetButtonState(1, 2, false); + } + + if (ImGui::IsKeyReleased(keybindings_.start_button)) { + snes_.SetButtonState(1, 3, false); + } + + if (ImGui::IsKeyReleased(keybindings_.up_button)) { + snes_.SetButtonState(1, 4, false); + } + + if (ImGui::IsKeyReleased(keybindings_.down_button)) { + snes_.SetButtonState(1, 5, false); + } + + if (ImGui::IsKeyReleased(keybindings_.left_button)) { + snes_.SetButtonState(1, 6, false); + } + + if (ImGui::IsKeyReleased(keybindings_.right_button)) { + snes_.SetButtonState(1, 7, false); + } + + if (ImGui::IsKeyReleased(keybindings_.x_button)) { + snes_.SetButtonState(1, 8, false); + } + + if (ImGui::IsKeyReleased(keybindings_.y_button)) { + snes_.SetButtonState(1, 9, false); + } + + if (ImGui::IsKeyReleased(keybindings_.l_button)) { + snes_.SetButtonState(1, 10, false); + } + + if (ImGui::IsKeyReleased(keybindings_.r_button)) { + snes_.SetButtonState(1, 11, false); + } } void Emulator::RenderBreakpointList() { diff --git a/src/app/emu/emulator.h b/src/app/emu/emulator.h index 8255109e..72891261 100644 --- a/src/app/emu/emulator.h +++ b/src/app/emu/emulator.h @@ -19,6 +19,21 @@ namespace app { */ namespace emu { +struct EmulatorKeybindings { + ImGuiKey a_button = ImGuiKey_Z; + ImGuiKey b_button = ImGuiKey_A; + ImGuiKey x_button = ImGuiKey_S; + ImGuiKey y_button = ImGuiKey_X; + ImGuiKey l_button = ImGuiKey_Q; + ImGuiKey r_button = ImGuiKey_W; + ImGuiKey start_button = ImGuiKey_RETURN; + ImGuiKey select_button = ImGuiKey_BACKSPACE; + ImGuiKey up_button = ImGuiKey_UP; + ImGuiKey down_button = ImGuiKey_DOWN; + ImGuiKey left_button = ImGuiKey_LEFT; + ImGuiKey right_button = ImGuiKey_RIGHT; +}; + /** * @class Emulator * @brief A class for emulating and debugging SNES games. @@ -145,6 +160,8 @@ class Emulator : public SharedRom { std::vector rom_data_; + EmulatorKeybindings keybindings_; + gui::zeml::Node emulator_node_; };