From 1943b5be6426b55eae5b48785fd2224a1a4a1c8e Mon Sep 17 00:00:00 2001 From: lauchmelder Date: Sun, 5 Jan 2025 21:02:32 +0100 Subject: [PATCH] improve imgui modifier key handling: --- quark/src/platform/IoTranslate.hpp | 2 +- quark/src/platform/glfw/IoTranslateGlfw.cpp | 18 +++++++------- quark/src/quark/imgui/ImGuiLayer.cpp | 26 ++++++++++----------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/quark/src/platform/IoTranslate.hpp b/quark/src/platform/IoTranslate.hpp index 851d9e9..38d843e 100644 --- a/quark/src/platform/IoTranslate.hpp +++ b/quark/src/platform/IoTranslate.hpp @@ -4,5 +4,5 @@ namespace qk { ImGuiMouseButton TranslateMouseButton(int button); - int TranslateKey(int key); + ImGuiKey TranslateKey(int key); } diff --git a/quark/src/platform/glfw/IoTranslateGlfw.cpp b/quark/src/platform/glfw/IoTranslateGlfw.cpp index 5236d3d..2194de2 100644 --- a/quark/src/platform/glfw/IoTranslateGlfw.cpp +++ b/quark/src/platform/glfw/IoTranslateGlfw.cpp @@ -12,7 +12,7 @@ namespace qk { return button; } - int TranslateKey(int key) { + ImGuiKey TranslateKey(int key) { switch (key) { case GLFW_KEY_TAB: return ImGuiKey_Tab; case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; @@ -29,14 +29,14 @@ namespace qk { case GLFW_KEY_SPACE: return ImGuiKey_Space; case GLFW_KEY_ENTER: return ImGuiKey_Enter; case GLFW_KEY_ESCAPE: return ImGuiKey_Escape; - case GLFW_KEY_LEFT_CONTROL: return (ImGuiKey)(ImGuiKey_LeftCtrl | ImGuiMod_Ctrl); - case GLFW_KEY_LEFT_SHIFT: return (ImGuiKey)(ImGuiKey_LeftShift | ImGuiMod_Shift); - case GLFW_KEY_LEFT_ALT: return (ImGuiKey)(ImGuiKey_LeftAlt | ImGuiMod_Alt); - case GLFW_KEY_LEFT_SUPER: return (ImGuiKey)(ImGuiKey_LeftSuper | ImGuiMod_Super); - case GLFW_KEY_RIGHT_CONTROL: return (ImGuiKey)(ImGuiKey_RightCtrl | ImGuiMod_Ctrl); - case GLFW_KEY_RIGHT_SHIFT: return (ImGuiKey)(ImGuiKey_RightShift | ImGuiMod_Shift); - case GLFW_KEY_RIGHT_ALT: return (ImGuiKey)(ImGuiKey_RightAlt | ImGuiMod_Alt); - case GLFW_KEY_RIGHT_SUPER: return (ImGuiKey)(ImGuiKey_RightSuper | ImGuiMod_Super); + case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl; + case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift; + case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt; + case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper; + case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl; + case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift; + case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt; + case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper; case GLFW_KEY_MENU: return ImGuiKey_Menu; case GLFW_KEY_0: return ImGuiKey_0; case GLFW_KEY_1: return ImGuiKey_1; diff --git a/quark/src/quark/imgui/ImGuiLayer.cpp b/quark/src/quark/imgui/ImGuiLayer.cpp index 6d0279d..b27fd04 100644 --- a/quark/src/quark/imgui/ImGuiLayer.cpp +++ b/quark/src/quark/imgui/ImGuiLayer.cpp @@ -107,52 +107,50 @@ namespace qk { bool ImGuiLayer::OnKeyPressedEvent(KeyPressedEvent& e) { ImGuiIO& io = ImGui::GetIO(); - int key = TranslateKey(e.GetKeyCode()); - int mod = ImGuiMod_Mask_ & key; + ImGuiKey key = TranslateKey(e.GetKeyCode()); - if ((mod & ImGuiMod_Ctrl) != 0) { + if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) { io.AddKeyEvent(ImGuiMod_Ctrl, true); } - if ((mod & ImGuiMod_Shift) != 0) { + if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) { io.AddKeyEvent(ImGuiMod_Shift, true); } - if ((mod & ImGuiMod_Alt) != 0) { + if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) { io.AddKeyEvent(ImGuiMod_Alt, true); } - if ((mod & ImGuiMod_Super) != 0) { + if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) { io.AddKeyEvent(ImGuiMod_Super, true); } - io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), true); + io.AddKeyEvent(key, true); return false; } bool ImGuiLayer::OnKeyReleasedEvent(KeyReleasedEvent& e) { ImGuiIO& io = ImGui::GetIO(); - int key = TranslateKey(e.GetKeyCode()); - int mod = ImGuiMod_Mask_ & key; + ImGuiKey key = TranslateKey(e.GetKeyCode()); - if ((mod & ImGuiMod_Ctrl) != 0) { + if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) { io.AddKeyEvent(ImGuiMod_Ctrl, false); } - if ((mod & ImGuiMod_Shift) != 0) { + if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) { io.AddKeyEvent(ImGuiMod_Shift, false); } - if ((mod & ImGuiMod_Alt) != 0) { + if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) { io.AddKeyEvent(ImGuiMod_Alt, false); } - if ((mod & ImGuiMod_Super) != 0) { + if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) { io.AddKeyEvent(ImGuiMod_Super, false); } - io.AddKeyEvent((ImGuiKey)(key & ~ImGuiMod_Mask_), false); + io.AddKeyEvent(key, false); return false; }