From eace519ea1c22989f185d96615814559d37434b4 Mon Sep 17 00:00:00 2001 From: oxmox Date: Sat, 11 Mar 2023 12:43:57 +0100 Subject: [PATCH] broken SDL input handling (WIP) --- src/doompanning.cc | 87 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/src/doompanning.cc b/src/doompanning.cc index df3b191..ebe4378 100644 --- a/src/doompanning.cc +++ b/src/doompanning.cc @@ -584,6 +584,9 @@ int doom_controller_loop(ControllerContext &ctx) auto &io = ImGui::GetIO(); s32 mouseWheel = 0.0; + static const float ScaleStep = 0.01f; + static const s32 PanStep = 2; + while (SDL_PollEvent(&event)) { ImGui_ImplSDL2_ProcessEvent(&event); @@ -606,9 +609,67 @@ int doom_controller_loop(ControllerContext &ctx) if (auto doomEvent = doomevent_from_sdlevent(event)) post_doom_event(ctx, *doomEvent); } + + if (!io.WantCaptureKeyboard && (event.type == SDL_KEYDOWN)) + { + // FIXME: it doesn't work like this. use buttonstate and update it in here, then handle everything later like was done with imgui io + const auto sym = event.key.keysym.sym; + const auto mod = event.key.keysym.mod; + const auto repeat = event.key.repeat; + + // Ctrl-Q to quit + if (mod & KMOD_CTRL && sym == SDLK_q) + ctx.quit = true; + + // keyboard zoom with - and =, reset with 0 + if (sym == SDLK_EQUALS) + ctx.scaleFactor += ScaleStep; + if (sym == SDLK_MINUS) + ctx.scaleFactor -= ScaleStep; + if (sym == SDLK_0) + ctx.scaleFactor = 1.0; + + s32 PanFactor = mod & KMOD_CTRL ? 10 : 1; + + // hjkl scrolling and reset with g + if (sym == SDLK_h) + ctx.offsetX -= PanStep; + if (sym == SDLK_j) + ctx.offsetY += PanStep; + if (sym == SDLK_k) + ctx.offsetY -= PanStep; + if (sym == SDLK_l) + ctx.offsetX += PanStep; + if (sym == SDLK_g) + ctx.offsetX = ctx.offsetY = 0; + + // Alt-Enter fullscreen toggle + if (mod == KMOD_ALT && sym == SDLK_RETURN && !repeat) + { + u32 flag = (ctx.isFullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP); + SDL_SetWindowFullscreen(ctx.window, flag); + ctx.isFullscreen = !ctx.isFullscreen; + } + + // TODO: F1 - help + + // F2 - toggle publish inputs to dooms + if (sym == SDLK_F2 && !repeat) + { + ctx.publishInputsMode = !ctx.publishInputsMode; + if (SDL_SetRelativeMouseMode(ctx.publishInputsMode ? SDL_TRUE : SDL_FALSE) < 0) + log_warn("SDL_SetRelativeMouseMode: %s", SDL_GetError()); + io.SetAppAcceptingEvents(!ctx.publishInputsMode); + } + + // F3 to toggle ImGui visibility (skips the call to run_ui() if true) + if (sym == SDLK_F3 && !repeat) + ctx.uiVisible = !ctx.uiVisible; + } } // Process input events not consumed by ImGui + #if 0 if (!io.WantCaptureKeyboard) { // TODO: make scaling scale with the current scale factor @@ -647,23 +708,21 @@ int doom_controller_loop(ControllerContext &ctx) ctx.isFullscreen = !ctx.isFullscreen; } - // F10 to toggle ImGui visibility (skips the call to run_ui() if true) - if (ImGui::IsKeyPressed(ImGuiKey_F10, false)) + // TODO: F1 - help + + // F2 - toggle publish inputs to dooms + if (ImGui::IsKeyPressed(ImGuiKey_F2, false)) { - ctx.uiVisible = !ctx.uiVisible; - // FIXME: sadly this has issues: after toggling the ui off and - // on again the mouse cursor is invisible. Even using - // SDL_ShowCursor() unconditionally did not fix it. I suspect - // it's caused by some interaction between SDL and ImGui. - //if (SDL_SetRelativeMouseMode(ctx.uiVisible ? SDL_TRUE : SDL_FALSE) < 0) - // log_warn("SDL_SetRelativeMouseMode: %s", SDL_GetError()); + ctx.publishInputsMode = !ctx.publishInputsMode; + if (SDL_SetRelativeMouseMode(ctx.publishInputsMode ? SDL_TRUE : SDL_FALSE) < 0) + log_warn("SDL_SetRelativeMouseMode: %s", SDL_GetError()); } - // Ctrl-A to toggle sending input events to dooms. - if (io.KeyCtrl && ImGui::IsKeyPressed(ImGuiKey_A, false)) - ctx.publishInputsMode = !ctx.publishInputsMode; - + // F3 to toggle ImGui visibility (skips the call to run_ui() if true) + if (ImGui::IsKeyPressed(ImGuiKey_F3, false)) + ctx.uiVisible = !ctx.uiVisible; } + #endif static ImVec2 panStartPos; @@ -681,7 +740,7 @@ int doom_controller_loop(ControllerContext &ctx) if (ImGui::IsKeyReleased(ImGuiKey_MouseLeft)) ctx.isMousePanning = false; - if (ctx.isMousePanning) + if (ctx.isMousePanning && !ctx.publishInputsMode) { auto curPos = io.MousePos; auto dx = curPos.x - panStartPos.x;