[nostalgia/core/glfw] Fix keyboard support for ImGui

This commit is contained in:
Gary Talent 2022-02-01 02:15:49 -06:00
parent 87c03c0d98
commit 0d76129a0e
2 changed files with 20 additions and 20 deletions

View File

@ -15,24 +15,6 @@ namespace nostalgia::core {
void draw(Context *ctx) noexcept; void draw(Context *ctx) noexcept;
static void handleKeyPress(Context *ctx, int key) {
switch (key) {
case GLFW_KEY_ESCAPE:
case GLFW_KEY_Q:
shutdown(ctx);
break;
default:
break;
}
}
static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) {
const auto ctx = static_cast<Context*>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS) {
handleKeyPress(ctx, key);
}
}
ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName) noexcept { ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName) noexcept {
auto ctx = ox::make_unique<Context>(); auto ctx = ox::make_unique<Context>();
ctx->rom = std::move(fs); ctx->rom = std::move(fs);
@ -43,7 +25,6 @@ ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const
id->startTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); id->startTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
glfwInit(); glfwInit();
oxReturnError(initGfx(ctx.get())); oxReturnError(initGfx(ctx.get()));
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
return ctx; return ctx;
} }

View File

@ -15,7 +15,7 @@ namespace nostalgia::core {
constexpr auto Scale = 5; constexpr auto Scale = 5;
static void handleGlfwError(int err, const char *desc) { static void handleGlfwError(int err, const char *desc) noexcept {
oxErrf("GLFW error ({}): {}\n", err, desc); oxErrf("GLFW error ({}): {}\n", err, desc);
} }
@ -23,6 +23,24 @@ void ImGui_Impl_NewFrame() noexcept {
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
} }
static void handleKeyPress(Context *ctx, int key) noexcept {
switch (key) {
case GLFW_KEY_ESCAPE:
case GLFW_KEY_Q:
shutdown(ctx);
break;
default:
break;
}
}
static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) noexcept {
const auto ctx = static_cast<Context *>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS) {
handleKeyPress(ctx, key);
}
}
ox::Error initGfx(Context *ctx) noexcept { ox::Error initGfx(Context *ctx) noexcept {
auto id = ctx->windowerData<GlfwImplData>(); auto id = ctx->windowerData<GlfwImplData>();
glfwSetErrorCallback(handleGlfwError); glfwSetErrorCallback(handleGlfwError);
@ -37,6 +55,7 @@ ox::Error initGfx(Context *ctx) noexcept {
if (id->window == nullptr) { if (id->window == nullptr) {
return OxError(1, "Could not open GLFW window"); return OxError(1, "Could not open GLFW window");
} }
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
glfwSetWindowUserPointer(id->window, ctx); glfwSetWindowUserPointer(id->window, ctx);
glfwMakeContextCurrent(id->window); glfwMakeContextCurrent(id->window);
if constexpr(config::ImGuiEnabled) { if constexpr(config::ImGuiEnabled) {