ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.93k stars 10.29k forks source link

Capturing gamepad input only when my dear imgui window is focused #7403

Open MacroController opened 7 months ago

MacroController commented 7 months ago

Version/Branch of Dear ImGui:

Version 1.90.4, Branch: master

Back-ends:

imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp

Compiler, OS:

Windows 10 + MSVC 2019

Full config/build information:

Dear ImGui 1.90.4 (19040)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1929
define: _MSVC_LANG=201703
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00000006
 HasMouseCursors
 HasSetMousePos
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 3840.00,2004.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I launch the examples/*/main.cpp corresponding to my backends; with the addition of the MCVE I provided. This MCVE calls ImGui::IsKeyPressed(...) with either keyboard or gamepad input, and display information based on the return value of the call.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

        // I added this bit of code in the examples/*/main.cpp, after the 3. Show another simple window, and before the Rendering and Cleanup sections of code
        // 4. Show my problem.
        {
            ImGui::Begin("My problem");
            if (ImGui::IsKeyPressed(ImGuiKey_GamepadL1))
            {
                ImGui::Text("The gamepad input is captured regardless of what window from my OS is focused");
                ImGui::SameLine();
                ImGui::Text("Gamepad L1");
            }
            if (ImGui::IsKeyPressed(ImGuiKey_Q))
            {
                ImGui::Text("The keyboard input is captured only if my dear imgui window is focused");
                ImGui::SameLine();
                ImGui::Text("Keyboard Q");
            }
            ImGui::End();
        }
ocornut commented 6 months ago

Since gamepad is handled by platform backend we could decide to program backends this way. IMHO it should be an option.

ImGuiIO:
[...]
bool ConfigGamepadNeedAppFocus;

The setting could be honored by dear imgui by filtering Gamepad_xxx events based on io.AppFocusLost OR be honored by backend code. I think the earlier would be simpler to implement.