From 4b49f03a40a4aadfa8d586c184d098eda539f5bf Mon Sep 17 00:00:00 2001 From: omar Date: Mon, 5 Feb 2018 23:16:40 +0100 Subject: [PATCH] Examples: SDL+GL3: Added Navigation keyboard mapping. (#787) --- .../imgui_impl_sdl_gl3.cpp | 24 +++++++++++++++++++ .../sdl_opengl3_example/imgui_impl_sdl_gl3.h | 1 + examples/sdl_opengl3_example/main.cpp | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp index 06d62867..0619b6ad 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp @@ -4,6 +4,7 @@ // Implemented features: // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. +// [X] Keyboard navigation mapping. Enable with 'io.NavFlags |= ImGuiNavFlags_EnableKeyboard'. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). @@ -412,6 +413,29 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window) // Hide OS mouse cursor if ImGui is drawing it SDL_ShowCursor(io.MouseDrawCursor ? 0 : 1); + // 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, SDL_SCANCODE_LEFT); + MAP_KEY(ImGuiNavInput_KeyRight, SDL_SCANCODE_RIGHT); + MAP_KEY(ImGuiNavInput_KeyUp, SDL_SCANCODE_UP); + MAP_KEY(ImGuiNavInput_KeyDown, SDL_SCANCODE_DOWN); + MAP_KEY(ImGuiNavInput_KeyMenu, SDL_SCANCODE_LALT); + MAP_KEY(ImGuiNavInput_KeyMenu, SDL_SCANCODE_RALT); + MAP_KEY(ImGuiNavInput_PadActivate, SDL_SCANCODE_SPACE); + MAP_KEY(ImGuiNavInput_PadCancel, SDL_SCANCODE_ESCAPE); + MAP_KEY(ImGuiNavInput_PadInput, SDL_SCANCODE_RETURN); + MAP_KEY(ImGuiNavInput_PadTweakSlow, SDL_SCANCODE_LALT); + MAP_KEY(ImGuiNavInput_PadTweakSlow, SDL_SCANCODE_LALT); + MAP_KEY(ImGuiNavInput_PadTweakFast, SDL_SCANCODE_LSHIFT); + MAP_KEY(ImGuiNavInput_PadTweakFast, SDL_SCANCODE_RSHIFT); + #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(); } diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h index 3f76e294..fc7e1e96 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h @@ -4,6 +4,7 @@ // Implemented features: // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. +// [X] Keyboard navigation mapping. Enable with 'io.NavFlags |= ImGuiNavFlags_EnableKeyboard'. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). diff --git a/examples/sdl_opengl3_example/main.cpp b/examples/sdl_opengl3_example/main.cpp index 4bab4916..7cd4ca98 100644 --- a/examples/sdl_opengl3_example/main.cpp +++ b/examples/sdl_opengl3_example/main.cpp @@ -34,7 +34,9 @@ int main(int, char**) gl3wInit(); // Setup ImGui binding + ImGuiIO& io = ImGui::GetIO(); (void)io; ImGui_ImplSdlGL3_Init(window); + //io.NavFlags |= ImGuiNavFlags_EnableKeyboard; // Setup style ImGui::StyleColorsDark(); @@ -47,7 +49,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 'misc/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("../../misc/fonts/Roboto-Medium.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);