diff --git a/base/camera.hpp b/base/camera.hpp index 6358f88b..160c26e9 100644 --- a/base/camera.hpp +++ b/base/camera.hpp @@ -169,10 +169,9 @@ public: position -= glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed; if (keys.right) position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed; - - updateViewMatrix(); } } + updateViewMatrix(); }; // Update camera passing separate axis data (gamepad) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 35c43f5c..c73a4656 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1344,6 +1344,8 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR resizing = false; break; } + + OnHandleMessage(hWnd, uMsg, wParam, lParam); } #elif defined(VK_USE_PLATFORM_ANDROID_KHR) int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* event) @@ -2934,3 +2936,7 @@ void VulkanExampleBase::setupSwapChain() } void VulkanExampleBase::OnUpdateUIOverlay(vks::UIOverlay *overlay) {} + +#if defined(_WIN32) +void VulkanExampleBase::OnHandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {}; +#endif diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 9785164a..abdd61f5 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -391,6 +391,10 @@ public: /** @brief (Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay */ virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay); + +#if defined(_WIN32) + virtual void OnHandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); +#endif }; // OS specific macros for the example main entry points diff --git a/examples/imgui/main.cpp b/examples/imgui/main.cpp index 91a3b1e0..e06a63b3 100644 --- a/examples/imgui/main.cpp +++ b/examples/imgui/main.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - imGui (https://github.com/ocornut/imgui) * -* Copyright (C) 2017 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2017-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -97,6 +97,16 @@ public: ImGuiIO& io = ImGui::GetIO(); io.DisplaySize = ImVec2(width, height); io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f); +#if defined(_WIN32) + // If we directly work with os specific key codes, we need to map special key types like tab + io.KeyMap[ImGuiKey_Tab] = VK_TAB; + io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT; + io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT; + io.KeyMap[ImGuiKey_UpArrow] = VK_UP; + io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN; + io.KeyMap[ImGuiKey_Backspace] = VK_BACK; + io.KeyMap[ImGuiKey_Enter] = VK_RETURN; +#endif } // Initialize all Vulkan resources used by the ui @@ -560,7 +570,6 @@ public: renderPassBeginInfo.pClearValues = clearValues; imGui->newFrame(this, (frameCounter == 0)); - imGui->updateBuffers(); for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) @@ -743,6 +752,8 @@ public: if (!prepared) return; + updateUniformBuffers(); + // Update imGui ImGuiIO& io = ImGui::GetIO(); @@ -755,14 +766,6 @@ public: io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible; draw(); - - if (uiSettings.animateLight) - updateUniformBuffers(); - } - - virtual void viewChanged() - { - updateUniformBuffers(); } virtual void mouseMoved(double x, double y, bool &handled) @@ -771,6 +774,29 @@ public: handled = io.WantCaptureMouse && UIOverlay.visible; } +// Input handling is platform specific, to show how it's basically done this sample implements it for Windows +#if defined(_WIN32) + virtual void OnHandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + ImGuiIO& io = ImGui::GetIO(); + // Only react to keyboard input if ImGui is active + if (io.WantCaptureKeyboard) { + // Character input + if (uMsg == WM_CHAR) { + if (wParam > 0 && wParam < 0x10000) { + io.AddInputCharacter((unsigned short)wParam); + } + } + // Special keys (tab, cursor, etc.) + if ((wParam < 256) && (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN)) { + io.KeysDown[wParam] = true; + } + if ((wParam < 256) && (uMsg == WM_KEYUP || uMsg == WM_SYSKEYUP)) { + io.KeysDown[wParam] = false; + } + } + } +#endif + }; VULKAN_EXAMPLE_MAIN()