From 84da070901e65c0a3cb1a709e1813a061da47817 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 4 Dec 2014 18:27:03 -0800 Subject: [PATCH] Correct mouse position in occulus --- interface/src/Application.cpp | 86 +++++++++++++++++++------ interface/src/Application.h | 11 +++- interface/src/ui/ApplicationOverlay.cpp | 2 +- interface/src/ui/NodeBounds.cpp | 6 +- 4 files changed, 78 insertions(+), 27 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index fe6e81bd79..ca3fcfdd1b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1305,18 +1305,20 @@ void Application::mousePressEvent(QMouseEvent* event, unsigned int deviceID) { if (event->button() == Qt::LeftButton) { _mouseX = event->x(); _mouseY = event->y(); - _mouseDragStartedX = _mouseX; - _mouseDragStartedY = _mouseY; + _mouseDragStartedX = getTrueMouseX(); + _mouseDragStartedY = getTrueMouseY(); _mousePressed = true; - - if (_audio.mousePressEvent(_mouseX, _mouseY)) { - // stop propagation - return; - } - - if (_rearMirrorTools->mousePressEvent(_mouseX, _mouseY)) { - // stop propagation - return; + + if (mouseOnScreen()) { + if (_audio.mousePressEvent(getMouseX(), getMouseY())) { + // stop propagation + return; + } + + if (_rearMirrorTools->mousePressEvent(getMouseX(), getMouseY())) { + // stop propagation + return; + } } // nobody handled this - make it an action event on the _window object @@ -1349,11 +1351,12 @@ void Application::mouseReleaseEvent(QMouseEvent* event, unsigned int deviceID) { _mouseY = event->y(); _mousePressed = false; - checkBandwidthMeterClick(); - if (Menu::getInstance()->isOptionChecked(MenuOption::Stats)) { + if (Menu::getInstance()->isOptionChecked(MenuOption::Stats) && mouseOnScreen()) { // let's set horizontal offset to give stats some margin to mirror int horizontalOffset = MIRROR_VIEW_WIDTH; - Stats::getInstance()->checkClick(_mouseX, _mouseY, _mouseDragStartedX, _mouseDragStartedY, horizontalOffset); + Stats::getInstance()->checkClick(getMouseX(), getMouseY(), + getMouseDragStartedX(), getMouseDragStartedY(), horizontalOffset); + checkBandwidthMeterClick(); } // fire an action end event @@ -1546,13 +1549,13 @@ void Application::idle() { void Application::checkBandwidthMeterClick() { // ... to be called upon button release - if (Menu::getInstance()->isOptionChecked(MenuOption::Bandwidth) && Menu::getInstance()->isOptionChecked(MenuOption::Stats) && Menu::getInstance()->isOptionChecked(MenuOption::UserInterface) && - glm::compMax(glm::abs(glm::ivec2(_mouseX - _mouseDragStartedX, _mouseY - _mouseDragStartedY))) - <= BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH - && _bandwidthMeter.isWithinArea(_mouseX, _mouseY, _glWidget->width(), _glWidget->height())) { + glm::compMax(glm::abs(glm::ivec2(getMouseX() - getMouseDragStartedX(), + getMouseY() - getMouseDragStartedY()))) + <= BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH + && _bandwidthMeter.isWithinArea(getMouseX(), getMouseY(), _glWidget->width(), _glWidget->height())) { // The bandwidth meter is visible, the click didn't get dragged too far and // we actually hit the bandwidth meter @@ -1666,6 +1669,48 @@ glm::vec3 Application::getMouseVoxelWorldCoordinates(const VoxelDetail& mouseVox (mouseVoxel.z + mouseVoxel.s / 2.0f) * TREE_SCALE); } +bool Application::mouseOnScreen() const { + if (OculusManager::isConnected()) { + return getMouseX() >= 0 && getMouseX() <= _glWidget->getDeviceWidth() && + getMouseY() >= 0 && getMouseY() <= _glWidget->getDeviceHeight(); + } + return true; +} + +int Application::getMouseX() const { + if (OculusManager::isConnected()) { + glm::vec2 pos = _applicationOverlay.screenToOverlay(glm::vec2(getTrueMouseX(), getTrueMouseY())); + return pos.x; + } + return getTrueMouseX(); +} + +int Application::getMouseY() const { + if (OculusManager::isConnected()) { + glm::vec2 pos = _applicationOverlay.screenToOverlay(glm::vec2(getTrueMouseX(), getTrueMouseY())); + return pos.y; + } + return getTrueMouseY(); +} + +int Application::getMouseDragStartedX() const { + if (OculusManager::isConnected()) { + glm::vec2 pos = _applicationOverlay.screenToOverlay(glm::vec2(getTrueMouseDragStartedX(), + getTrueMouseDragStartedY())); + return pos.x; + } + return getTrueMouseDragStartedX(); +} + +int Application::getMouseDragStartedY() const { + if (OculusManager::isConnected()) { + glm::vec2 pos = _applicationOverlay.screenToOverlay(glm::vec2(getTrueMouseDragStartedX(), + getTrueMouseDragStartedY())); + return pos.y; + } + return getTrueMouseDragStartedY(); +} + FaceTracker* Application::getActiveFaceTracker() { return (_dde.isActive() ? static_cast(&_dde) : (_faceshift.isActive() ? static_cast(&_faceshift) : @@ -1898,7 +1943,6 @@ void Application::init() { _mouseX = _glWidget->width() / 2; _mouseY = _glWidget->height() / 2; - QCursor::setPos(_mouseX, _mouseY); // TODO: move _myAvatar out of Application. Move relevant code to MyAvataar or AvatarManager _avatarManager.init(); @@ -2076,8 +2120,8 @@ void Application::updateMouseRay() { // if the mouse pointer isn't visible, act like it's at the center of the screen float x = 0.5f, y = 0.5f; if (!_mouseHidden) { - x = _mouseX / (float)_glWidget->width(); - y = _mouseY / (float)_glWidget->height(); + x = getTrueMouseX() / (float)_glWidget->width(); + y = getTrueMouseY() / (float)_glWidget->height(); } PickRay pickRay = _myCamera.computeViewPickRay(x, y); _mouseRayOrigin = pickRay.origin; diff --git a/interface/src/Application.h b/interface/src/Application.h index b737141b40..7c24eaec8e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -213,8 +213,15 @@ public: bool isMouseHidden() const { return _mouseHidden; } const glm::vec3& getMouseRayOrigin() const { return _mouseRayOrigin; } const glm::vec3& getMouseRayDirection() const { return _mouseRayDirection; } - int getMouseX() const { return _mouseX; } - int getMouseY() const { return _mouseY; } + bool mouseOnScreen() const; + int getMouseX() const; + int getMouseY() const; + int getTrueMouseX() const { return _mouseX; } + int getTrueMouseY() const { return _mouseY; } + int getMouseDragStartedX() const; + int getMouseDragStartedY() const; + int getTrueMouseDragStartedX() const { return _mouseDragStartedX; } + int getTrueMouseDragStartedY() const { return _mouseDragStartedY; } bool getLastMouseMoveWasSimulated() const { return _lastMouseMoveWasSimulated;; } Faceshift* getFaceshift() { return &_faceshift; } Visage* getVisage() { return &_visage; } diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index 36babb64e0..287cd60a84 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -523,7 +523,7 @@ void ApplicationOverlay::renderPointers() { //If we are in oculus, render reticle later _reticleActive[MOUSE] = true; _magActive[MOUSE] = true; - _reticulePosition[MOUSE] = glm::vec2(application->getMouseX(), application->getMouseY()); + _reticulePosition[MOUSE] = glm::vec2(application->getTrueMouseX(), application->getTrueMouseY()); _reticleActive[LEFT_CONTROLLER] = false; _reticleActive[RIGHT_CONTROLLER] = false; diff --git a/interface/src/ui/NodeBounds.cpp b/interface/src/ui/NodeBounds.cpp index 631129321c..3c6b4c625a 100644 --- a/interface/src/ui/NodeBounds.cpp +++ b/interface/src/ui/NodeBounds.cpp @@ -38,7 +38,7 @@ void NodeBounds::draw() { // Compute ray to find selected nodes later on. We can't use the pre-computed ray in Application because it centers // itself after the cursor disappears. Application* application = Application::getInstance(); - PickRay pickRay = application->getCamera()->computeViewPickRay(application->getMouseX(), application->getMouseY()); + PickRay pickRay = application->getCamera()->computeViewPickRay(application->getTrueMouseX(), application->getTrueMouseY()); // Variables to keep track of the selected node and properties to draw the cube later if needed Node* selectedNode = NULL; @@ -220,8 +220,8 @@ void NodeBounds::drawOverlay() { const int MOUSE_OFFSET = 10; const int BACKGROUND_BEVEL = 3; - int mouseX = application->getMouseX(), - mouseY = application->getMouseY(), + int mouseX = application->getTrueMouseX(), + mouseY = application->getTrueMouseY(), textWidth = widthText(TEXT_SCALE, 0, _overlayText); glColor4f(0.4f, 0.4f, 0.4f, 0.6f); renderBevelCornersRect(mouseX + MOUSE_OFFSET, mouseY - TEXT_HEIGHT - PADDING,