From cd6f9a774de6f6928ee7b441c51b2666bd283ce8 Mon Sep 17 00:00:00 2001 From: dante ruiz Date: Thu, 31 Oct 2019 14:38:14 -0700 Subject: [PATCH] implement gesture pinch to camera zoom --- .../resources/controllers/keyboardMouse.json | 2 ++ interface/src/Application.cpp | 5 ++- .../src/input-plugins/KeyboardMouseDevice.cpp | 33 +++++++++++++++++++ .../src/input-plugins/KeyboardMouseDevice.h | 5 +++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/interface/resources/controllers/keyboardMouse.json b/interface/resources/controllers/keyboardMouse.json index d6ecc540c2..11c80a4dd2 100644 --- a/interface/resources/controllers/keyboardMouse.json +++ b/interface/resources/controllers/keyboardMouse.json @@ -269,6 +269,8 @@ { "from": "Keyboard.MouseWheelDown", "to": "Actions.LATERAL_LEFT" }, { "from": "Keyboard.MouseWheelLeft", "to": "Actions.BOOM_OUT", "filters": [ { "type": "scale", "scale": 0.02 } ]}, { "from": "Keyboard.MouseWheelRight", "to": "Actions.BOOM_IN", "filters": [ { "type": "scale", "scale": 0.02 } ]}, + { "from": "Keyboard.GesturePinchOut", "to": "Actions.BOOM_OUT"}, + { "from": "Keyboard.GesturePinchIn", "to": "Actions.BOOM_IN"}, { "from": "Keyboard.Space", "to": "Actions.VERTICAL_UP" }, { "from": "Keyboard.R", "to": "Actions.ACTION1" }, diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 28e3368b30..221ab211f4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4871,6 +4871,9 @@ void Application::touchEndEvent(QTouchEvent* event) { } void Application::touchGestureEvent(QGestureEvent* event) { + if (_keyboardMouseDevice->isActive()) { + _keyboardMouseDevice->touchGestureEvent(event); + } if (_touchscreenDevice && _touchscreenDevice->isActive()) { _touchscreenDevice->touchGestureEvent(event); } @@ -6262,7 +6265,7 @@ void Application::update(float deltaTime) { myAvatar->setDriveKey(MyAvatar::TRANSLATE_Z, -1.0f * userInputMapper->getActionState(controller::Action::TRANSLATE_Z)); myAvatar->setDriveKey(MyAvatar::TRANSLATE_Y, userInputMapper->getActionState(controller::Action::TRANSLATE_Y)); myAvatar->setDriveKey(MyAvatar::TRANSLATE_X, userInputMapper->getActionState(controller::Action::TRANSLATE_X)); - if (deltaTime > FLT_EPSILON) { + if (deltaTime > FLT_EPSILON && userInputMapper->getActionState(controller::Action::TRANSLATE_CAMERA_Z) == 0.0f) { myAvatar->setDriveKey(MyAvatar::PITCH, -1.0f * userInputMapper->getActionState(controller::Action::PITCH)); myAvatar->setDriveKey(MyAvatar::YAW, -1.0f * userInputMapper->getActionState(controller::Action::YAW)); myAvatar->setDriveKey(MyAvatar::DELTA_PITCH, -1.0f * userInputMapper->getActionState(controller::Action::DELTA_PITCH)); diff --git a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp index b1746951bb..50c9f6aa3b 100755 --- a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp +++ b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -145,6 +146,36 @@ glm::vec2 evalAverageTouchPoints(const QList& points) { return averagePoint; } +void KeyboardMouseDevice::touchGestureEvent(const QGestureEvent* event) { + QPinchGesture* pinchGesture = (QPinchGesture*) event->gesture(Qt::PinchGesture); + + if (pinchGesture) { + switch (pinchGesture->state()) { + case Qt::GestureStarted: + _lastTotalScaleFactor = pinchGesture->totalScaleFactor(); + break; + + case Qt::GestureUpdated: { + qreal totalScaleFactor = pinchGesture->totalScaleFactor(); + qreal scaleFactorDelta = totalScaleFactor - _lastTotalScaleFactor; + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_POS).getChannel()].value = (float) (scaleFactorDelta > 0 ? scaleFactorDelta : 0.0f); + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_NEG).getChannel()].value = (float) (scaleFactorDelta < 0 ? -scaleFactorDelta : 0.0f); + _lastTotalScaleFactor = totalScaleFactor; + break; + } + + case Qt::GestureFinished: { + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_POS).getChannel()].value = 0.0f; + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_NEG).getChannel()].value = 0.0f; + break; + } + + default: + break; + } + } +} + void KeyboardMouseDevice::touchBeginEvent(const QTouchEvent* event) { if (_enableTouch) { _isTouching = event->touchPointStates().testFlag(Qt::TouchPointPressed); @@ -344,6 +375,8 @@ controller::Input::NamedVector KeyboardMouseDevice::InputDevice::getAvailableInp availableInputs.append(Input::NamedPair(makeInput(TOUCH_AXIS_X_NEG), "TouchpadLeft")); availableInputs.append(Input::NamedPair(makeInput(TOUCH_AXIS_Y_POS), "TouchpadUp")); availableInputs.append(Input::NamedPair(makeInput(TOUCH_AXIS_Y_NEG), "TouchpadDown")); + availableInputs.append(Input::NamedPair(makeInput(TOUCH_GESTURE_PINCH_POS), "GesturePinchOut")); + availableInputs.append(Input::NamedPair(makeInput(TOUCH_GESTURE_PINCH_NEG), "GesturePinchIn")); }); return availableInputs; } diff --git a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h index f6921c8e23..f53190b6ad 100644 --- a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h +++ b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h @@ -23,6 +23,7 @@ class QTouchEvent; class QKeyEvent; class QMouseEvent; class QWheelEvent; +class QGestureEvent; class KeyboardMouseDevice : public InputPlugin { Q_OBJECT @@ -60,6 +61,8 @@ public: TOUCH_AXIS_X_NEG, TOUCH_AXIS_Y_POS, TOUCH_AXIS_Y_NEG, + TOUCH_GESTURE_PINCH_POS, + TOUCH_GESTURE_PINCH_NEG, }; enum TouchButtonChannel { @@ -81,6 +84,7 @@ public: void mouseReleaseEvent(QMouseEvent* event); void eraseMouseClicked(); + void touchGestureEvent(const QGestureEvent* event); void touchBeginEvent(const QTouchEvent* event); void touchEndEvent(const QTouchEvent* event); void touchUpdateEvent(const QTouchEvent* event); @@ -121,6 +125,7 @@ protected: QPoint _previousCursor; QPoint _mousePressPos; quint64 _mousePressTime; + qreal _lastTotalScaleFactor; bool _clickDeadspotActive; glm::vec2 _lastTouch; std::shared_ptr _inputDevice { std::make_shared() };