Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for custom Key Callbacks in Input class #53

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
map of functions
jchionh committed Jul 23, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d37b7e0ef8bc717a99e5e748c769688824fba71d
22 changes: 12 additions & 10 deletions Walnut/src/Walnut/Input/Input.cpp
Original file line number Diff line number Diff line change
@@ -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<void()>& func)
void Input::InitKeysCallBack()
{
GLFWwindow* windowHandle = Application::Get().GetWindowHandle();
glfwSetKeyCallback(windowHandle, KeyCallback);
}



void Input::SetKeyCallback(KeyCode keycode, std::function<void()> func)
{
KEY_CALLBACK_MAP[keycode] = func;
}
4 changes: 1 addition & 3 deletions Walnut/src/Walnut/Input/Input.h
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ namespace Walnut {
public:
static void InitKeysCallBack();

static void SetKeyCallback(KeyCode keycode, std::function<void()>& func);
static void SetKeyCallback(KeyCode keycode, std::function<void()> func);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to pass the KeyCode to the callback, so that one function could be used for multiple keys.


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<KeyCode, std::function<void()>> KEY_CALLBACK_MAP;
};