Refactor key mapping logic and streamline shortcut execution in CommandManager and ShortcutManager

This commit is contained in:
scawful
2025-03-09 19:11:30 -04:00
parent 7b2c919745
commit a307d24031
3 changed files with 10 additions and 75 deletions

View File

@@ -2,73 +2,12 @@
#include <fstream>
#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.

View File

@@ -10,8 +10,6 @@
namespace yaze {
namespace editor {
ImGuiKey MapKeyToImGuiKey(char key);
class CommandManager {
public:
CommandManager() = default;

View File

@@ -140,29 +140,27 @@ std::vector<ImGuiKey> 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();
}
}
}