Parcourir la source

Nav: Examples: DirectX11, Glfw+GL3: Basic code to map keyboard inputs when io.NavFlags & ImGuiNavFlags_EnableKeyboard is set. (will iterate/tweak before spreading to other examples). (#787)

omar il y a 7 ans
Parent
commit
3967ff58b2

+ 20 - 0
examples/directx11_example/imgui_impl_dx11.cpp

@@ -605,6 +605,26 @@ void ImGui_ImplDX11_NewFrame()
     // io.MouseDown : filled by WM_*BUTTON* events
     // io.MouseWheel : filled by WM_MOUSEWHEEL events
 
+    // Gamepad/keyboard navigation mapping [BETA]
+    memset(io.NavInputs, 0, sizeof(io.NavInputs));
+    if (io.NavFlags & ImGuiNavFlags_EnableKeyboard)
+    {
+        // Update keyboard
+        // FIXME-NAV: We are still using some of the ImGuiNavInput_PadXXX enums as keyboard support is incomplete.
+        #define MAP_KEY(NAV_NO, KEY_NO) { if (io.KeysDown[KEY_NO]) io.NavInputs[NAV_NO] = 1.0f; }
+        MAP_KEY(ImGuiNavInput_KeyLeft,      VK_LEFT);
+        MAP_KEY(ImGuiNavInput_KeyRight,     VK_RIGHT);
+        MAP_KEY(ImGuiNavInput_KeyUp,        VK_UP);
+        MAP_KEY(ImGuiNavInput_KeyDown,      VK_DOWN);
+        MAP_KEY(ImGuiNavInput_KeyMenu,      VK_MENU);
+        MAP_KEY(ImGuiNavInput_PadActivate,  VK_SPACE);
+        MAP_KEY(ImGuiNavInput_PadCancel,    VK_ESCAPE);
+        MAP_KEY(ImGuiNavInput_PadInput,     VK_RETURN);
+        MAP_KEY(ImGuiNavInput_PadTweakFast, VK_SHIFT);
+        MAP_KEY(ImGuiNavInput_PadTweakSlow, VK_CONTROL);
+        #undef MAP_KEY
+    }
+
     // Set OS mouse position if requested last frame by io.WantMoveMouse flag (used when io.NavMovesTrue is enabled by user and using directional navigation)
     if (io.WantMoveMouse)
     {

+ 2 - 1
examples/directx11_example/main.cpp

@@ -125,7 +125,9 @@ int main(int, char**)
     UpdateWindow(hwnd);
 
     // Setup ImGui binding
+    ImGuiIO& io = ImGui::GetIO(); (void)io;
     ImGui_ImplDX11_Init(hwnd, g_pd3dDevice, g_pd3dDeviceContext);
+    //io.NavFlags |= ImGuiNavFlags_EnableKeyboard;
 
     // Setup style
     ImGui::StyleColorsClassic();
@@ -138,7 +140,6 @@ int main(int, char**)
     // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
     // - Read 'extra_fonts/README.txt' for more instructions and details.
     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
-    //ImGuiIO& io = ImGui::GetIO();
     //io.Fonts->AddFontDefault();
     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Roboto-Medium.ttf", 16.0f);
     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);

+ 22 - 0
examples/opengl3_example/imgui_impl_glfw_gl3.cpp

@@ -411,6 +411,28 @@ void ImGui_ImplGlfwGL3_NewFrame()
     // Hide OS mouse cursor if ImGui is drawing it
     glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
 
+    // Gamepad/keyboard navigation mapping [BETA]
+    memset(io.NavInputs, 0, sizeof(io.NavInputs));
+    if (io.NavFlags & ImGuiNavFlags_EnableKeyboard)
+    {
+        // Update keyboard
+        // FIXME-NAV: We are still using some of the ImGuiNavInput_PadXXX enums as keyboard support is incomplete.
+        #define MAP_KEY(NAV_NO, KEY_NO) { if (io.KeysDown[KEY_NO]) io.NavInputs[NAV_NO] = 1.0f; }
+        MAP_KEY(ImGuiNavInput_KeyLeft,      GLFW_KEY_LEFT);
+        MAP_KEY(ImGuiNavInput_KeyRight,     GLFW_KEY_RIGHT);
+        MAP_KEY(ImGuiNavInput_KeyUp,        GLFW_KEY_UP);
+        MAP_KEY(ImGuiNavInput_KeyDown,      GLFW_KEY_DOWN);
+        MAP_KEY(ImGuiNavInput_KeyMenu,      GLFW_KEY_LEFT_ALT);
+        MAP_KEY(ImGuiNavInput_PadActivate,  GLFW_KEY_SPACE);
+        MAP_KEY(ImGuiNavInput_PadCancel,    GLFW_KEY_ESCAPE);
+        MAP_KEY(ImGuiNavInput_PadInput,     GLFW_KEY_ENTER);
+        MAP_KEY(ImGuiNavInput_PadTweakSlow, GLFW_KEY_LEFT_ALT);
+        MAP_KEY(ImGuiNavInput_PadTweakSlow, GLFW_KEY_RIGHT_ALT);
+        MAP_KEY(ImGuiNavInput_PadTweakFast, GLFW_KEY_LEFT_SHIFT);
+        MAP_KEY(ImGuiNavInput_PadTweakFast, GLFW_KEY_RIGHT_SHIFT);
+        #undef MAP_KEY
+    }
+
     // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
     ImGui::NewFrame();
 }

+ 2 - 1
examples/opengl3_example/main.cpp

@@ -32,7 +32,9 @@ int main(int, char**)
     gl3wInit();
 
     // Setup ImGui binding
+    ImGuiIO& io = ImGui::GetIO(); (void)io;
     ImGui_ImplGlfwGL3_Init(window, true);
+    //io.NavFlags |= ImGuiNavFlags_EnableKeyboard;
 
     // Setup style
     ImGui::StyleColorsClassic();
@@ -45,7 +47,6 @@ int main(int, char**)
     // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
     // - Read 'extra_fonts/README.txt' for more instructions and details.
     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
-    //ImGuiIO& io = ImGui::GetIO();
     //io.Fonts->AddFontDefault();
     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Roboto-Medium.ttf", 16.0f);
     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);