diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index c5e2f4d6..eadf4b38 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1125,11 +1125,26 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR } } break; - case WM_RBUTTONDOWN: case WM_LBUTTONDOWN: + mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); + mouseButtons.left = true; + break; + case WM_RBUTTONDOWN: + mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); + mouseButtons.right = true; + break; case WM_MBUTTONDOWN: - mousePos.x = (float)LOWORD(lParam); - mousePos.y = (float)HIWORD(lParam); + mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); + mouseButtons.middle = true; + break; + case WM_LBUTTONUP: + mouseButtons.left = false; + break; + case WM_RBUTTONUP: + mouseButtons.right = false; + break; + case WM_MBUTTONUP: + mouseButtons.middle = false; break; case WM_MOUSEWHEEL: { @@ -1140,37 +1155,37 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR break; } case WM_MOUSEMOVE: - if (wParam & MK_RBUTTON) - { - int32_t posx = LOWORD(lParam); - int32_t posy = HIWORD(lParam); + { + bool handled = false; + int32_t posx = LOWORD(lParam); + int32_t posy = HIWORD(lParam); + mouseMoved((float)posx, (float)posy, handled); + if (handled) { + mousePos = glm::vec2((float)posx, (float)posy); + break; + } + if (mouseButtons.right) { zoom += (mousePos.y - (float)posy) * .005f * zoomSpeed; camera.translate(glm::vec3(-0.0f, 0.0f, (mousePos.y - (float)posy) * .005f * zoomSpeed)); mousePos = glm::vec2((float)posx, (float)posy); viewUpdated = true; } - if (wParam & MK_LBUTTON) - { - int32_t posx = LOWORD(lParam); - int32_t posy = HIWORD(lParam); + if (mouseButtons.left) { rotation.x += (mousePos.y - (float)posy) * 1.25f * rotationSpeed; rotation.y -= (mousePos.x - (float)posx) * 1.25f * rotationSpeed; camera.rotate(glm::vec3((mousePos.y - (float)posy) * camera.rotationSpeed, -(mousePos.x - (float)posx) * camera.rotationSpeed, 0.0f)); mousePos = glm::vec2((float)posx, (float)posy); viewUpdated = true; } - if (wParam & MK_MBUTTON) - { - int32_t posx = LOWORD(lParam); - int32_t posy = HIWORD(lParam); + if (mouseButtons.middle) { cameraPos.x -= (mousePos.x - (float)posx) * 0.01f; cameraPos.y -= (mousePos.y - (float)posy) * 0.01f; camera.translate(glm::vec3(-(mousePos.x - (float)posx) * 0.01f, -(mousePos.y - (float)posy) * 0.01f, 0.0f)); + mousePos = glm::vec2((float)posx, (float)posy); viewUpdated = true; - mousePos.x = (float)posx; - mousePos.y = (float)posy; } break; + } case WM_SIZE: if ((prepared) && (wParam != SIZE_MINIMIZED)) { @@ -1899,6 +1914,8 @@ void VulkanExampleBase::viewChanged() {} void VulkanExampleBase::keyPressed(uint32_t) {} +void VulkanExampleBase::mouseMoved(double x, double y, bool & handled) {} + void VulkanExampleBase::buildCommandBuffers() {} void VulkanExampleBase::createCommandPool() diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index db31c551..381a33cf 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -189,13 +189,17 @@ public: VkImageView view; } depthStencil; - // Gamepad state (only one pad supported) - struct - { + struct { glm::vec2 axisLeft = glm::vec2(0.0f); glm::vec2 axisRight = glm::vec2(0.0f); } gamePadState; + struct { + bool left = false; + bool right = false; + bool middle = false; + } mouseButtons; + // OS specific #if defined(_WIN32) HWND window; @@ -225,19 +229,10 @@ public: wl_surface *surface = nullptr; wl_shell_surface *shell_surface = nullptr; bool quit = false; - struct { - bool left = false; - bool right = false; - bool middle = false; - } mouseButtons; + #elif defined(_DIRECT2DISPLAY) bool quit = false; #elif defined(VK_USE_PLATFORM_XCB_KHR) - struct { - bool left = false; - bool right = false; - bool middle = false; - } mouseButtons; bool quit = false; xcb_connection_t *connection; xcb_screen_t *screen; @@ -325,9 +320,10 @@ public: // Can be overriden in derived class to e.g. update uniform buffers // Containing view dependant matrices virtual void viewChanged(); - // Called if a key is pressed /** @brief (Virtual) Called after a key was pressed, can be used to do custom key handling */ virtual void keyPressed(uint32_t); + /** @brief (Virtual) Called after th mouse cursor moved and before internal events (like camera rotation) is handled */ + virtual void mouseMoved(double x, double y, bool &handled); // Called when the window has been resized // Can be overriden in derived class to recreate or rebuild resources attached to the frame buffer / swapchain virtual void windowResized();