From a307d24031db87f07487994f4e471fdb7030eaf6 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 9 Mar 2025 19:11:30 -0400 Subject: [PATCH] Refactor key mapping logic and streamline shortcut execution in CommandManager and ShortcutManager --- src/app/editor/system/command_manager.cc | 63 +---------------------- src/app/editor/system/command_manager.h | 2 - src/app/editor/system/shortcut_manager.cc | 20 ++++--- 3 files changed, 10 insertions(+), 75 deletions(-) diff --git a/src/app/editor/system/command_manager.cc b/src/app/editor/system/command_manager.cc index 99047c15..50b18c79 100644 --- a/src/app/editor/system/command_manager.cc +++ b/src/app/editor/system/command_manager.cc @@ -2,73 +2,12 @@ #include +#include "app/gui/input.h" #include "imgui/imgui.h" namespace yaze { namespace editor { -ImGuiKey MapKeyToImGuiKey(char key) { - switch (key) { - case 'A': - return ImGuiKey_A; - case 'B': - return ImGuiKey_B; - case 'C': - return ImGuiKey_C; - case 'D': - return ImGuiKey_D; - case 'E': - return ImGuiKey_E; - case 'F': - return ImGuiKey_F; - case 'G': - return ImGuiKey_G; - case 'H': - return ImGuiKey_H; - case 'I': - return ImGuiKey_I; - case 'J': - return ImGuiKey_J; - case 'K': - return ImGuiKey_K; - case 'L': - return ImGuiKey_L; - case 'M': - return ImGuiKey_M; - case 'N': - return ImGuiKey_N; - case 'O': - return ImGuiKey_O; - case 'P': - return ImGuiKey_P; - case 'Q': - return ImGuiKey_Q; - case 'R': - return ImGuiKey_R; - case 'S': - return ImGuiKey_S; - case 'T': - return ImGuiKey_T; - case 'U': - return ImGuiKey_U; - case 'V': - return ImGuiKey_V; - case 'W': - return ImGuiKey_W; - case 'X': - return ImGuiKey_X; - case 'Y': - return ImGuiKey_Y; - case 'Z': - return ImGuiKey_Z; - case '/': - return ImGuiKey_Slash; - case '-': - return ImGuiKey_Minus; - default: - return ImGuiKey_COUNT; - } -} // When the player presses Space, a popup will appear fixed to the bottom of the // ImGui window with a list of the available key commands which can be used. diff --git a/src/app/editor/system/command_manager.h b/src/app/editor/system/command_manager.h index 58669be7..07f9576f 100644 --- a/src/app/editor/system/command_manager.h +++ b/src/app/editor/system/command_manager.h @@ -10,8 +10,6 @@ namespace yaze { namespace editor { -ImGuiKey MapKeyToImGuiKey(char key); - class CommandManager { public: CommandManager() = default; diff --git a/src/app/editor/system/shortcut_manager.cc b/src/app/editor/system/shortcut_manager.cc index 6aa25742..1131eb4d 100644 --- a/src/app/editor/system/shortcut_manager.cc +++ b/src/app/editor/system/shortcut_manager.cc @@ -140,29 +140,27 @@ std::vector ParseShortcut(const std::string& shortcut) { void ExecuteShortcuts(const ShortcutManager& shortcut_manager) { // Check for keyboard shortcuts using the shortcut manager for (const auto& shortcut : shortcut_manager.GetShortcuts()) { - bool ctrl_pressed = ImGui::GetIO().KeyCtrl; - bool alt_pressed = ImGui::GetIO().KeyAlt; - bool shift_pressed = ImGui::GetIO().KeyShift; - bool super_pressed = ImGui::GetIO().KeySuper; - bool keys_pressed = false; + bool keys_pressed = true; // Check for all the keys in the shortcut for (const auto& key : shortcut.second.keys) { if (key == ImGuiMod_Ctrl) { - keys_pressed = ctrl_pressed; + keys_pressed &= ImGui::GetIO().KeyCtrl; } else if (key == ImGuiMod_Alt) { - keys_pressed = alt_pressed; + keys_pressed &= ImGui::GetIO().KeyAlt; } else if (key == ImGuiMod_Shift) { - keys_pressed = shift_pressed; + keys_pressed &= ImGui::GetIO().KeyShift; } else if (key == ImGuiMod_Super) { - keys_pressed = super_pressed; + keys_pressed &= ImGui::GetIO().KeySuper; } else { - keys_pressed = ImGui::IsKeyDown(key); + keys_pressed &= ImGui::IsKeyDown(key); } - if (!keys_pressed) { break; } } + if (keys_pressed) { + shortcut.second.callback(); + } } }