[nostalgia/core] Add key event handler

This commit is contained in:
2022-03-20 01:42:08 -05:00
parent 2223fe7863
commit ea318bb6c8
5 changed files with 120 additions and 24 deletions
+47 -11
View File
@@ -20,19 +20,53 @@ static void handleGlfwError(int err, const char *desc) noexcept {
oxErrf("GLFW error ({}): {}\n", err, desc);
}
static void handleKeyPress(Context *ctx, int key) noexcept {
switch (key) {
case GLFW_KEY_ESCAPE:
case GLFW_KEY_Q:
if constexpr(ox::defines::Debug) {
oxIgnoreError(shutdown(ctx));
}
break;
default:
break;
static auto setKeyDownStatus(GlfwImplData *id, Key key, bool down) noexcept {
if (down) {
id->keysDown |= 1llu << static_cast<int>(key);
} else {
id->keysDown &= ~(1llu << static_cast<int>(key));
}
}
static void handleKeyPress(Context *ctx, int key, bool down) noexcept {
const auto eventHandler = keyEventHandler(ctx);
const auto id = ctx->windowerData<GlfwImplData>();
if (eventHandler) {
if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) {
const auto k = static_cast<Key>(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>(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;
}
}
}
//if constexpr(ox::defines::Debug) {
// switch (key) {
// case GLFW_KEY_ESCAPE:
// case GLFW_KEY_Q:
// oxIgnoreError(shutdown(ctx));
// break;
// default:
// break;
// }
//}
}
static void handleGlfwCursorPosEvent(GLFWwindow*, double, double) noexcept {
}
@@ -48,7 +82,9 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int
const auto ctx = static_cast<Context*>(glfwGetWindowUserPointer(window));
ctx->uninterruptedRefreshes = 2;
if (action == GLFW_PRESS) {
handleKeyPress(ctx, key);
handleKeyPress(ctx, key, true);
} else if (action == GLFW_RELEASE) {
handleKeyPress(ctx, key, false);
}
}