From 97975a314dbe2d05f193bc21ea41c8317e9ac0e3 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Thu, 2 Nov 2017 13:40:27 +0100 Subject: [PATCH] Unified mouse movement handling --- base/vulkanexamplebase.cpp | 130 ++++++++++++------------------------- base/vulkanexamplebase.h | 1 + 2 files changed, 44 insertions(+), 87 deletions(-) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 1ecc7e97..5c69e350 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1160,38 +1160,7 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR } case WM_MOUSEMOVE: { - bool handled = false; - - ImGuiIO& io = ImGui::GetIO(); - handled = io.WantCaptureMouse; - - 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 (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 (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; - } + handleMouseMove(LOWORD(lParam), HIWORD(lParam)); break; } case WM_SIZE: @@ -1416,39 +1385,9 @@ void* VulkanExampleBase::setupWindow(void* view) VulkanExampleBase *self = reinterpret_cast(data); self->pointerMotion(pointer, time, sx, sy); } -void VulkanExampleBase::pointerMotion(wl_pointer *pointer, uint32_t time, - wl_fixed_t sx, wl_fixed_t sy) +void VulkanExampleBase::pointerMotion(wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) { - double x = wl_fixed_to_double(sx); - double y = wl_fixed_to_double(sy); - - double dx = mousePos.x - x; - double dy = mousePos.y - y; - - if (mouseButtons.left) - { - rotation.x += dy * 1.25f * rotationSpeed; - rotation.y -= dx * 1.25f * rotationSpeed; - camera.rotate(glm::vec3( - dy * camera.rotationSpeed, - -dx * camera.rotationSpeed, - 0.0f)); - viewUpdated = true; - } - if (mouseButtons.right) - { - zoom += dy * .005f * zoomSpeed; - camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f * zoomSpeed)); - viewUpdated = true; - } - if (mouseButtons.middle) - { - cameraPos.x -= dx * 0.01f; - cameraPos.y -= dy * 0.01f; - camera.translate(glm::vec3(-dx * 0.01f, -dy * 0.01f, 0.0f)); - viewUpdated = true; - } - mousePos = glm::vec2(x, y); + handleMouseMove(wl_fixed_to_int(sx), wl_fixed_to_int(sy)); } /*static*/void VulkanExampleBase::pointerButtonCb(void *data, @@ -1795,29 +1734,8 @@ void VulkanExampleBase::handleEvent(const xcb_generic_event_t *event) case XCB_MOTION_NOTIFY: { xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *)event; - if (mouseButtons.left) - { - rotation.x += (mousePos.y - (float)motion->event_y) * 1.25f; - rotation.y -= (mousePos.x - (float)motion->event_x) * 1.25f; - camera.rotate(glm::vec3((mousePos.y - (float)motion->event_y) * camera.rotationSpeed, -(mousePos.x - (float)motion->event_x) * camera.rotationSpeed, 0.0f)); - viewUpdated = true; - } - if (mouseButtons.right) - { - zoom += (mousePos.y - (float)motion->event_y) * .005f; - camera.translate(glm::vec3(-0.0f, 0.0f, (mousePos.y - (float)motion->event_y) * .005f * zoomSpeed)); - viewUpdated = true; - } - if (mouseButtons.middle) - { - cameraPos.x -= (mousePos.x - (float)motion->event_x) * 0.01f; - cameraPos.y -= (mousePos.y - (float)motion->event_y) * 0.01f; - camera.translate(glm::vec3(-(mousePos.x - (float)(float)motion->event_x) * 0.01f, -(mousePos.y - (float)motion->event_y) * 0.01f, 0.0f)); - viewUpdated = true; - mousePos.x = (float)motion->event_x; - mousePos.y = (float)motion->event_y; - } - mousePos = glm::vec2((float)motion->event_x, (float)motion->event_y); + handleMouseMove((int32_t)motion->event_x, (int32_t)motion->event_y); + break; } break; case XCB_BUTTON_PRESS: @@ -2131,6 +2049,44 @@ void VulkanExampleBase::windowResize() prepared = true; } +void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y) +{ + int32_t dx = (int32_t)mousePos.x - x; + int32_t dy = (int32_t)mousePos.y - y; + + bool handled = false; + + if (settings.overlay) { + ImGuiIO& io = ImGui::GetIO(); + handled = io.WantCaptureMouse; + } + mouseMoved((float)x, (float)y, handled); + + if (handled) { + mousePos = glm::vec2((float)x, (float)y); + return; + } + + if (mouseButtons.left) { + rotation.x += dy * 1.25f * rotationSpeed; + rotation.y -= dx * 1.25f * rotationSpeed; + camera.rotate(glm::vec3(dy * camera.rotationSpeed, -dx * camera.rotationSpeed, 0.0f)); + viewUpdated = true; + } + if (mouseButtons.right) { + zoom += dy * .005f * zoomSpeed; + camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f * zoomSpeed)); + viewUpdated = true; + } + if (mouseButtons.middle) { + cameraPos.x -= dx * 0.01f; + cameraPos.y -= dy * 0.01f; + camera.translate(glm::vec3(-dx * 0.01f, -dy * 0.01f, 0.0f)); + viewUpdated = true; + } + mousePos = glm::vec2((float)x, (float)y); +} + void VulkanExampleBase::windowResized() { // Can be overriden in derived class diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index aafa5965..8e6ccb0b 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -69,6 +69,7 @@ private: vks::UIOverlay *UIOverlay = nullptr; // Called if the window is resized and some resources have to be recreatesd void windowResize(); + void handleMouseMove(int32_t x, int32_t y); protected: // Frame counter to display fps uint32_t frameCounter = 0;