From 200367913084351a1d536f77edcf089b4e91f73b Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sat, 22 Jul 2023 21:43:15 -0700 Subject: [PATCH 1/7] Adding KeyPressed --- Walnut/src/Walnut/Input/Input.cpp | 7 +++++++ Walnut/src/Walnut/Input/Input.h | 1 + 2 files changed, 8 insertions(+) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index 6f2340e2..395b8e37 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -13,6 +13,13 @@ namespace Walnut { return state == GLFW_PRESS || state == GLFW_REPEAT; } + bool Input::IsKeyPressed(KeyCode keycode) + { + GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); + int state = glfwGetKey(windowHandle, (int)keycode); + return state == GLFW_PRESS; + } + bool Input::IsMouseButtonDown(MouseButton button) { GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index de4192c6..80d668b1 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -10,6 +10,7 @@ namespace Walnut { { public: static bool IsKeyDown(KeyCode keycode); + static bool IsKeyPressed(KeyCode keycode); static bool IsMouseButtonDown(MouseButton button); static glm::vec2 GetMousePosition(); From 27d8d1b32f92566b81c31f9fa34f7949f6ad9eaf Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sat, 22 Jul 2023 22:46:52 -0700 Subject: [PATCH 2/7] Adding Key callback event --- Walnut/src/Walnut/Input/Input.cpp | 28 +++++++++++++++++++++++----- Walnut/src/Walnut/Input/Input.h | 14 ++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index 395b8e37..d956b314 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -6,18 +6,36 @@ namespace Walnut { - bool Input::IsKeyDown(KeyCode keycode) + void Input::InitKeysCallBack() { GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); - int state = glfwGetKey(windowHandle, (int)keycode); - return state == GLFW_PRESS || state == GLFW_REPEAT; + glfwSetKeyCallback(windowHandle, KeyCallback); + } + + void Input::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) + { + if (action == GLFW_PRESS) + { + KeyCode keycode = (KeyCode)key; + + auto funcIter = KEY_CALLBACK_MAP.find(keycode); + if (funcIter != KEY_CALLBACK_MAP.end()) + { + funcIter->second(); + } + } } - bool Input::IsKeyPressed(KeyCode keycode) + void Input::SetKeyCallback(KeyCode keycode, std::function& func) + { + KEY_CALLBACK_MAP[keycode] = func; + } + + bool Input::IsKeyDown(KeyCode keycode) { GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); int state = glfwGetKey(windowHandle, (int)keycode); - return state == GLFW_PRESS; + return state == GLFW_PRESS || state == GLFW_REPEAT; } bool Input::IsMouseButtonDown(MouseButton button) diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index 80d668b1..3bcbea57 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -1,7 +1,9 @@ #pragma once #include "KeyCodes.h" - +#include +#include +#include #include namespace Walnut { @@ -9,13 +11,21 @@ namespace Walnut { class Input { public: + static void InitKeysCallBack(); + + static void SetKeyCallback(KeyCode keycode, std::function& func); + static bool IsKeyDown(KeyCode keycode); - static bool IsKeyPressed(KeyCode keycode); + static bool IsMouseButtonDown(MouseButton button); static glm::vec2 GetMousePosition(); static void SetCursorMode(CursorMode mode); + + static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); + + static std::unordered_map> KEY_CALLBACK_MAP; }; } From d37b7e0ef8bc717a99e5e748c769688824fba71d Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sat, 22 Jul 2023 23:08:28 -0700 Subject: [PATCH 3/7] map of functions --- Walnut/src/Walnut/Input/Input.cpp | 22 ++++++++++++---------- Walnut/src/Walnut/Input/Input.h | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index d956b314..ccdbeca7 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -6,27 +6,29 @@ namespace Walnut { - void Input::InitKeysCallBack() - { - GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); - glfwSetKeyCallback(windowHandle, KeyCallback); - } - - void Input::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) + void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { KeyCode keycode = (KeyCode)key; - auto funcIter = KEY_CALLBACK_MAP.find(keycode); - if (funcIter != KEY_CALLBACK_MAP.end()) + auto funcIter = Input::KEY_CALLBACK_MAP.find(keycode); + if (funcIter != Input::KEY_CALLBACK_MAP.end()) { funcIter->second(); } } } - void Input::SetKeyCallback(KeyCode keycode, std::function& func) + void Input::InitKeysCallBack() + { + GLFWwindow* windowHandle = Application::Get().GetWindowHandle(); + glfwSetKeyCallback(windowHandle, KeyCallback); + } + + + + void Input::SetKeyCallback(KeyCode keycode, std::function func) { KEY_CALLBACK_MAP[keycode] = func; } diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index 3bcbea57..f03c206f 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -13,7 +13,7 @@ namespace Walnut { public: static void InitKeysCallBack(); - static void SetKeyCallback(KeyCode keycode, std::function& func); + static void SetKeyCallback(KeyCode keycode, std::function func); static bool IsKeyDown(KeyCode keycode); @@ -23,8 +23,6 @@ namespace Walnut { static void SetCursorMode(CursorMode mode); - static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); - static std::unordered_map> KEY_CALLBACK_MAP; }; From 41767c2180e6dbf5d1344be517dd385f1cdb8d16 Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sat, 22 Jul 2023 23:13:36 -0700 Subject: [PATCH 4/7] Fixed Input Key callback --- Walnut/src/Walnut/Input/Input.cpp | 8 ++++---- Walnut/src/Walnut/Input/Input.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index ccdbeca7..1113da88 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -6,13 +6,13 @@ namespace Walnut { + std::unordered_map> Input::KEY_CALLBACK_MAP; + void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { - KeyCode keycode = (KeyCode)key; - - auto funcIter = Input::KEY_CALLBACK_MAP.find(keycode); + auto funcIter = Input::KEY_CALLBACK_MAP.find(key); if (funcIter != Input::KEY_CALLBACK_MAP.end()) { funcIter->second(); @@ -30,7 +30,7 @@ namespace Walnut { void Input::SetKeyCallback(KeyCode keycode, std::function func) { - KEY_CALLBACK_MAP[keycode] = func; + KEY_CALLBACK_MAP[(int)keycode] = func; } bool Input::IsKeyDown(KeyCode keycode) diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index f03c206f..9fcf0297 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -23,7 +23,7 @@ namespace Walnut { static void SetCursorMode(CursorMode mode); - static std::unordered_map> KEY_CALLBACK_MAP; + static std::unordered_map> KEY_CALLBACK_MAP; }; } From a31263b888483e71bd2c12ac6337a67cd0144715 Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sat, 22 Jul 2023 23:26:35 -0700 Subject: [PATCH 5/7] Support KeyPress repeat too --- Walnut/src/Walnut/Input/Input.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index 1113da88..3d6b19ef 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -10,7 +10,7 @@ namespace Walnut { void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (action == GLFW_PRESS) + if (action == GLFW_PRESS || action == GLFW_REPEAT) { auto funcIter = Input::KEY_CALLBACK_MAP.find(key); if (funcIter != Input::KEY_CALLBACK_MAP.end()) @@ -26,8 +26,6 @@ namespace Walnut { glfwSetKeyCallback(windowHandle, KeyCallback); } - - void Input::SetKeyCallback(KeyCode keycode, std::function func) { KEY_CALLBACK_MAP[(int)keycode] = func; From 7b3ba4f1b0ce910adac5542cd384cea4ca7cb3eb Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sun, 23 Jul 2023 02:27:48 -0700 Subject: [PATCH 6/7] Fixup include ordering --- Walnut/src/Walnut/Input/Input.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index 9fcf0297..4400bdf7 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -1,9 +1,9 @@ #pragma once #include "KeyCodes.h" -#include -#include + #include +#include #include namespace Walnut { From 1f4fa1dc8c7e1e0b86545c854f399d0b7acd2326 Mon Sep 17 00:00:00 2001 From: Jason Chionh Date: Sun, 23 Jul 2023 12:58:21 -0700 Subject: [PATCH 7/7] Rename static variable to follow proper case --- Walnut/src/Walnut/Input/Input.cpp | 8 ++++---- Walnut/src/Walnut/Input/Input.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Walnut/src/Walnut/Input/Input.cpp b/Walnut/src/Walnut/Input/Input.cpp index 3d6b19ef..359e6002 100644 --- a/Walnut/src/Walnut/Input/Input.cpp +++ b/Walnut/src/Walnut/Input/Input.cpp @@ -6,14 +6,14 @@ namespace Walnut { - std::unordered_map> Input::KEY_CALLBACK_MAP; + std::unordered_map> Input::s_KeyCallbackMap; void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS || action == GLFW_REPEAT) { - auto funcIter = Input::KEY_CALLBACK_MAP.find(key); - if (funcIter != Input::KEY_CALLBACK_MAP.end()) + auto funcIter = Input::s_KeyCallbackMap.find(key); + if (funcIter != Input::s_KeyCallbackMap.end()) { funcIter->second(); } @@ -28,7 +28,7 @@ namespace Walnut { void Input::SetKeyCallback(KeyCode keycode, std::function func) { - KEY_CALLBACK_MAP[(int)keycode] = func; + s_KeyCallbackMap[(int)keycode] = func; } bool Input::IsKeyDown(KeyCode keycode) diff --git a/Walnut/src/Walnut/Input/Input.h b/Walnut/src/Walnut/Input/Input.h index 4400bdf7..133a336d 100644 --- a/Walnut/src/Walnut/Input/Input.h +++ b/Walnut/src/Walnut/Input/Input.h @@ -23,7 +23,7 @@ namespace Walnut { static void SetCursorMode(CursorMode mode); - static std::unordered_map> KEY_CALLBACK_MAP; + static std::unordered_map> s_KeyCallbackMap; }; }