StudioCherno / Walnut

Walnut is a simple application framework for Vulkan and Dear ImGui apps
MIT License
1.99k stars 367 forks source link

Walnut::Input to support key callbacks #52

Open jchionh opened 1 year ago

jchionh commented 1 year ago

Existing Input.cpp class only supports checking of if key is pressed down.

This is a proposal to add support for callbacks via: https://www.glfw.org/docs/3.3/input_guide.html#input_key

Additions to the Input class:

// this method will set a private KeysCallback() function by glfwSetKeyCallback(window, KeysCallback);
static void InitKeysCallBack();

// this method adds func to a unordered_map using KeyCode as the key
static void SetKeyCallback(KeyCode keycode, std::function<void()> func);

// this function gets called by GLFW when a key is pressed, and on keyboard's repeat timing
// we will lookup the unordered_map to see if a std::function is set in the map, if it is, invoke it.
void KeysCallback(GLFWwindow* window, int key, int scancode, int action, int mods);

// this is the undorderd_map that stored custom key callbacks.
static std::unordered_map<int, std::function<void()>> KEY_CALLBACK_MAP;
jchionh commented 1 year ago

Proposed PR: https://github.com/StudioCherno/Walnut/pull/53