From 200a7dbf7974abe8952952a35a02188e23bea735 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Mon, 15 Nov 2021 17:32:18 +0300 Subject: [PATCH] Enabled ImGui navigation with keyboard and game controller. Ref issue #92. --- SpaceCadetPinball/imgui_impl_sdl.cpp | 4 +++- SpaceCadetPinball/winmain.cpp | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/SpaceCadetPinball/imgui_impl_sdl.cpp b/SpaceCadetPinball/imgui_impl_sdl.cpp index 669c7b7..87bbcdd 100644 --- a/SpaceCadetPinball/imgui_impl_sdl.cpp +++ b/SpaceCadetPinball/imgui_impl_sdl.cpp @@ -379,7 +379,9 @@ static void ImGui_ImplSDL2_UpdateGamepads() return; // Get gamepad - SDL_GameController* game_controller = SDL_GameControllerOpen(0); + SDL_GameController* game_controller = nullptr; + if (SDL_NumJoysticks() > 0 && SDL_IsGameController(0)) + game_controller = SDL_GameControllerOpen(0); if (!game_controller) { io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index 09400bd..03375e8 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -144,6 +144,7 @@ int winmain::WinMain(LPCSTR lpCmdLine) ImIO = &io; // ImGui_ImplSDL2_Init is private, we are not actually using ImGui OpenGl backend ImGui_ImplSDL2_InitForOpenGL(window, nullptr); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad; auto prefPath = SDL_GetPrefPath(nullptr, "SpaceCadetPinball"); auto iniPath = std::string(prefPath) + "imgui_pb.ini"; @@ -334,7 +335,7 @@ void winmain::RenderUi() if (ImGui::Begin("main", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_AlwaysAutoResize | - ImGuiWindowFlags_NoMove)) + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoFocusOnAppearing)) { ImGui::PushID(1); ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{}); @@ -347,6 +348,10 @@ void winmain::RenderUi() } ImGui::End(); ImGui::PopStyleVar(); + + // This window can not loose nav focus for some reason, clear it manually. + if (ImGui::IsNavInputDown(ImGuiNavInput_Cancel)) + ImGui::FocusWindow(NULL); } // No demo window in release to save space @@ -611,12 +616,14 @@ int winmain::event_handler(const SDL_Event* event) default: ; } } - if (ImIO->WantCaptureKeyboard) + if (ImIO->WantCaptureKeyboard && !options::WaitingForInput()) { switch (event->type) { case SDL_KEYDOWN: case SDL_KEYUP: + case SDL_CONTROLLERBUTTONDOWN: + case SDL_CONTROLLERBUTTONUP: return 1; default: ; }