From 6e742e5d45a11689e5cbe4231c5d53350d9d7b03 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 24 Mar 2022 01:00:45 -0500 Subject: [PATCH] [nostalgia/core/glfw] Cleanup key input system --- src/nostalgia/core/glfw/gfx.cpp | 41 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index 2fa1e092..28d20fbe 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -29,31 +29,28 @@ static auto setKeyDownStatus(GlfwImplData *id, Key key, bool down) noexcept { } static void handleKeyPress(Context *ctx, int key, bool down) noexcept { + static constexpr auto keyMap = [] { + ox::Array map = {}; + for (auto i = 0u; i < 26; ++i) { + map[GLFW_KEY_A + i] = static_cast(static_cast(Key::Alpha_A) + i); + } + for (auto i = 0u; i < 10; ++i) { + map[GLFW_KEY_0 + i] = static_cast(static_cast(Key::Num_0) + i); + } + map[GLFW_KEY_LEFT_ALT] = Key::Mod_Alt; + map[GLFW_KEY_RIGHT_ALT] = Key::Mod_Alt; + map[GLFW_KEY_LEFT_CONTROL] = Key::Mod_Ctrl; + map[GLFW_KEY_RIGHT_CONTROL] = Key::Mod_Ctrl; + map[GLFW_KEY_LEFT_SUPER] = Key::Mod_Super; + map[GLFW_KEY_RIGHT_SUPER] = Key::Mod_Super; + return map; + }(); const auto eventHandler = keyEventHandler(ctx); const auto id = ctx->windowerData(); + const auto k = keyMap[static_cast(key)]; + setKeyDownStatus(id, k, down); if (eventHandler) { - if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { - const auto k = static_cast(Key::Alpha_A + (key - GLFW_KEY_A)); - setKeyDownStatus(id, k, down); - eventHandler(ctx, k, down); - } else if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9) { - const auto k = static_cast(Key::Num_0 + (key - GLFW_KEY_0)); - setKeyDownStatus(id, k, down); - eventHandler(ctx, k, down); - } else { - switch (key) { - case GLFW_KEY_LEFT_SHIFT: - case GLFW_KEY_RIGHT_SHIFT: - setKeyDownStatus(id, Key::Mod_Shift, down); - eventHandler(ctx, Key::Mod_Shift, down); - break; - case GLFW_KEY_LEFT_CONTROL: - case GLFW_KEY_RIGHT_CONTROL: - setKeyDownStatus(id, Key::Mod_Ctrl, down); - eventHandler(ctx, Key::Mod_Ctrl, down); - break; - } - } + eventHandler(ctx, k, down); } //if constexpr(ox::defines::Debug) { // switch (key) {