Enabled ImGui navigation with keyboard and game controller.

Ref issue #92.
This commit is contained in:
Muzychenko Andrey 2021-11-15 17:32:18 +03:00
parent 8ab50ea7b7
commit 200a7dbf79
2 changed files with 12 additions and 3 deletions

View File

@ -379,7 +379,9 @@ static void ImGui_ImplSDL2_UpdateGamepads()
return; return;
// Get gamepad // 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) if (!game_controller)
{ {
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;

View File

@ -144,6 +144,7 @@ int winmain::WinMain(LPCSTR lpCmdLine)
ImIO = &io; ImIO = &io;
// ImGui_ImplSDL2_Init is private, we are not actually using ImGui OpenGl backend // ImGui_ImplSDL2_Init is private, we are not actually using ImGui OpenGl backend
ImGui_ImplSDL2_InitForOpenGL(window, nullptr); ImGui_ImplSDL2_InitForOpenGL(window, nullptr);
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
auto prefPath = SDL_GetPrefPath(nullptr, "SpaceCadetPinball"); auto prefPath = SDL_GetPrefPath(nullptr, "SpaceCadetPinball");
auto iniPath = std::string(prefPath) + "imgui_pb.ini"; auto iniPath = std::string(prefPath) + "imgui_pb.ini";
@ -334,7 +335,7 @@ void winmain::RenderUi()
if (ImGui::Begin("main", nullptr, if (ImGui::Begin("main", nullptr,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoMove)) ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoFocusOnAppearing))
{ {
ImGui::PushID(1); ImGui::PushID(1);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{}); ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{});
@ -347,6 +348,10 @@ void winmain::RenderUi()
} }
ImGui::End(); ImGui::End();
ImGui::PopStyleVar(); 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 // No demo window in release to save space
@ -611,12 +616,14 @@ int winmain::event_handler(const SDL_Event* event)
default: ; default: ;
} }
} }
if (ImIO->WantCaptureKeyboard) if (ImIO->WantCaptureKeyboard && !options::WaitingForInput())
{ {
switch (event->type) switch (event->type)
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
return 1; return 1;
default: ; default: ;
} }