Added keyboard input handling

Windows only
This commit is contained in:
Sascha Willems 2023-02-25 09:37:08 +01:00
parent 33bb0eb2b8
commit cca1c865d9
4 changed files with 47 additions and 12 deletions

View file

@ -169,10 +169,9 @@ public:
position -= glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed; position -= glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
if (keys.right) if (keys.right)
position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed; position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
}
}
updateViewMatrix(); updateViewMatrix();
}
}
}; };
// Update camera passing separate axis data (gamepad) // Update camera passing separate axis data (gamepad)

View file

@ -1344,6 +1344,8 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
resizing = false; resizing = false;
break; break;
} }
OnHandleMessage(hWnd, uMsg, wParam, lParam);
} }
#elif defined(VK_USE_PLATFORM_ANDROID_KHR) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* event) int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* event)
@ -2934,3 +2936,7 @@ void VulkanExampleBase::setupSwapChain()
} }
void VulkanExampleBase::OnUpdateUIOverlay(vks::UIOverlay *overlay) {} void VulkanExampleBase::OnUpdateUIOverlay(vks::UIOverlay *overlay) {}
#if defined(_WIN32)
void VulkanExampleBase::OnHandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {};
#endif

View file

@ -391,6 +391,10 @@ public:
/** @brief (Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay */ /** @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); 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 // OS specific macros for the example main entry points

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example - imGui (https://github.com/ocornut/imgui) * 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) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -97,6 +97,16 @@ public:
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(width, height); io.DisplaySize = ImVec2(width, height);
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f); 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 // Initialize all Vulkan resources used by the ui
@ -560,7 +570,6 @@ public:
renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.pClearValues = clearValues;
imGui->newFrame(this, (frameCounter == 0)); imGui->newFrame(this, (frameCounter == 0));
imGui->updateBuffers(); imGui->updateBuffers();
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
@ -743,6 +752,8 @@ public:
if (!prepared) if (!prepared)
return; return;
updateUniformBuffers();
// Update imGui // Update imGui
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
@ -755,14 +766,6 @@ public:
io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible; io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible;
draw(); draw();
if (uiSettings.animateLight)
updateUniformBuffers();
}
virtual void viewChanged()
{
updateUniformBuffers();
} }
virtual void mouseMoved(double x, double y, bool &handled) virtual void mouseMoved(double x, double y, bool &handled)
@ -771,6 +774,29 @@ public:
handled = io.WantCaptureMouse && UIOverlay.visible; 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() VULKAN_EXAMPLE_MAIN()