From aae3555b63c964e6a484bdcd0dfcecac83b18b92 Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Mon, 27 Jun 2016 21:01:06 +0100 Subject: [PATCH] update TouchscreenDevice * fix threading issue with zoom gesture * KeyboardMouseDevice touchpad disabled to prevent interference * device supported based on QTouchDevice::devices().count() --- interface/src/Application.cpp | 13 +- .../src/input-plugins/KeyboardMouseDevice.cpp | 117 +++++++++--------- .../src/input-plugins/KeyboardMouseDevice.h | 4 +- .../src/input-plugins/TouchscreenDevice.cpp | 21 ++-- .../src/input-plugins/TouchscreenDevice.h | 6 +- 5 files changed, 84 insertions(+), 77 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7858679583..7680112462 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -961,7 +961,10 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) : // Setup the _keyboardMouseDevice, _touchscreenDevice and the user input mapper with the default bindings userInputMapper->registerDevice(_keyboardMouseDevice->getInputDevice()); - userInputMapper->registerDevice(_touchscreenDevice->getInputDevice()); + // if the _touchscreenDevice is not supported it will not be registered + if (_touchscreenDevice) { + userInputMapper->registerDevice(_touchscreenDevice->getInputDevice()); + } // force the model the look at the correct directory (weird order of operations issue) scriptEngines->setScriptsLocation(scriptEngines->getScriptsLocation()); @@ -2712,7 +2715,7 @@ void Application::touchUpdateEvent(QTouchEvent* event) { if (_keyboardMouseDevice->isActive()) { _keyboardMouseDevice->touchUpdateEvent(event); } - if (Menu::getInstance()->isOptionChecked(TouchscreenDevice::NAME)) { + if (_touchscreenDevice->isActive()) { _touchscreenDevice->touchUpdateEvent(event); } } @@ -2733,7 +2736,7 @@ void Application::touchBeginEvent(QTouchEvent* event) { if (_keyboardMouseDevice->isActive()) { _keyboardMouseDevice->touchBeginEvent(event); } - if (Menu::getInstance()->isOptionChecked(TouchscreenDevice::NAME)) { + if (_touchscreenDevice->isActive()) { _touchscreenDevice->touchBeginEvent(event); } @@ -2753,7 +2756,7 @@ void Application::touchEndEvent(QTouchEvent* event) { if (_keyboardMouseDevice->isActive()) { _keyboardMouseDevice->touchEndEvent(event); } - if (Menu::getInstance()->isOptionChecked(TouchscreenDevice::NAME)) { + if (_touchscreenDevice->isActive()) { _touchscreenDevice->touchEndEvent(event); } @@ -2761,7 +2764,7 @@ void Application::touchEndEvent(QTouchEvent* event) { } void Application::touchGestureEvent(QGestureEvent* event) { - if (Menu::getInstance()->isOptionChecked(TouchscreenDevice::NAME)) { + if (_touchscreenDevice->isActive()) { _touchscreenDevice->touchGestureEvent(event); } } diff --git a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp index cc4117ffc0..550d127198 100755 --- a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp +++ b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.cpp @@ -19,7 +19,7 @@ #include const QString KeyboardMouseDevice::NAME = "Keyboard/Mouse"; -bool KeyboardMouseDevice::_enableMouse = true; +bool KeyboardMouseDevice::_enableTouchpad = true; void KeyboardMouseDevice::pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) { auto userInputMapper = DependencyManager::get(); @@ -62,32 +62,28 @@ void KeyboardMouseDevice::keyReleaseEvent(QKeyEvent* event) { } void KeyboardMouseDevice::mousePressEvent(QMouseEvent* event) { - if (_enableMouse) { - auto input = _inputDevice->makeInput((Qt::MouseButton) event->button()); - auto result = _inputDevice->_buttonPressedMap.insert(input.getChannel()); - if (!result.second) { - // key pressed again ? without catching the release event ? - } - _lastCursor = event->pos(); - _mousePressTime = usecTimestampNow(); - _mouseMoved = false; - - eraseMouseClicked(); + auto input = _inputDevice->makeInput((Qt::MouseButton) event->button()); + auto result = _inputDevice->_buttonPressedMap.insert(input.getChannel()); + if (!result.second) { + // key pressed again ? without catching the release event ? } + _lastCursor = event->pos(); + _mousePressTime = usecTimestampNow(); + _mouseMoved = false; + + eraseMouseClicked(); } void KeyboardMouseDevice::mouseReleaseEvent(QMouseEvent* event) { - if (_enableMouse) { - auto input = _inputDevice->makeInput((Qt::MouseButton) event->button()); - _inputDevice->_buttonPressedMap.erase(input.getChannel()); + auto input = _inputDevice->makeInput((Qt::MouseButton) event->button()); + _inputDevice->_buttonPressedMap.erase(input.getChannel()); - // if we pressed and released at the same location within a small time window, then create a "_CLICKED" - // input for this button we might want to add some small tolerance to this so if you do a small drag it - // still counts as a click. - static const int CLICK_TIME = USECS_PER_MSEC * 500; // 500 ms to click - if (!_mouseMoved && (usecTimestampNow() - _mousePressTime < CLICK_TIME)) { - _inputDevice->_buttonPressedMap.insert(_inputDevice->makeInput((Qt::MouseButton) event->button(), true).getChannel()); - } + // if we pressed and released at the same location within a small time window, then create a "_CLICKED" + // input for this button we might want to add some small tolerance to this so if you do a small drag it + // still counts as a click. + static const int CLICK_TIME = USECS_PER_MSEC * 500; // 500 ms to click + if (!_mouseMoved && (usecTimestampNow() - _mousePressTime < CLICK_TIME)) { + _inputDevice->_buttonPressedMap.insert(_inputDevice->makeInput((Qt::MouseButton) event->button(), true).getChannel()); } } @@ -98,24 +94,22 @@ void KeyboardMouseDevice::eraseMouseClicked() { } void KeyboardMouseDevice::mouseMoveEvent(QMouseEvent* event) { - if (_enableMouse) { - QPoint currentPos = event->pos(); - QPoint currentMove = currentPos - _lastCursor; + QPoint currentPos = event->pos(); + QPoint currentMove = currentPos - _lastCursor; - _inputDevice->_axisStateMap[MOUSE_AXIS_X_POS] = (currentMove.x() > 0 ? currentMove.x() : 0.0f); - _inputDevice->_axisStateMap[MOUSE_AXIS_X_NEG] = (currentMove.x() < 0 ? -currentMove.x() : 0.0f); - // Y mouse is inverted positive is pointing up the screen - _inputDevice->_axisStateMap[MOUSE_AXIS_Y_POS] = (currentMove.y() < 0 ? -currentMove.y() : 0.0f); - _inputDevice->_axisStateMap[MOUSE_AXIS_Y_NEG] = (currentMove.y() > 0 ? currentMove.y() : 0.0f); + _inputDevice->_axisStateMap[MOUSE_AXIS_X_POS] = (currentMove.x() > 0 ? currentMove.x() : 0.0f); + _inputDevice->_axisStateMap[MOUSE_AXIS_X_NEG] = (currentMove.x() < 0 ? -currentMove.x() : 0.0f); + // Y mouse is inverted positive is pointing up the screen + _inputDevice->_axisStateMap[MOUSE_AXIS_Y_POS] = (currentMove.y() < 0 ? -currentMove.y() : 0.0f); + _inputDevice->_axisStateMap[MOUSE_AXIS_Y_NEG] = (currentMove.y() > 0 ? currentMove.y() : 0.0f); - // FIXME - this has the characteristic that it will show large jumps when you move the cursor - // outside of the application window, because we don't get MouseEvents when the cursor is outside - // of the application window. - _lastCursor = currentPos; - _mouseMoved = true; + // FIXME - this has the characteristic that it will show large jumps when you move the cursor + // outside of the application window, because we don't get MouseEvents when the cursor is outside + // of the application window. + _lastCursor = currentPos; + _mouseMoved = true; - eraseMouseClicked(); - } + eraseMouseClicked(); } void KeyboardMouseDevice::wheelEvent(QWheelEvent* event) { @@ -139,34 +133,41 @@ glm::vec2 evalAverageTouchPoints(const QList& points) { } void KeyboardMouseDevice::touchBeginEvent(const QTouchEvent* event) { - _isTouching = event->touchPointStates().testFlag(Qt::TouchPointPressed); - _lastTouch = evalAverageTouchPoints(event->touchPoints()); - _lastTouchTime = _clock.now(); + if (_enableTouchpad) { + _isTouching = event->touchPointStates().testFlag(Qt::TouchPointPressed); + _lastTouch = evalAverageTouchPoints(event->touchPoints()); + _lastTouchTime = _clock.now(); + } } void KeyboardMouseDevice::touchEndEvent(const QTouchEvent* event) { - _isTouching = false; - _lastTouch = evalAverageTouchPoints(event->touchPoints()); - _lastTouchTime = _clock.now(); + if (_enableTouchpad) { + _isTouching = false; + _lastTouch = evalAverageTouchPoints(event->touchPoints()); + _lastTouchTime = _clock.now(); + } } void KeyboardMouseDevice::touchUpdateEvent(const QTouchEvent* event) { - auto currentPos = evalAverageTouchPoints(event->touchPoints()); - _lastTouchTime = _clock.now(); - - if (!_isTouching) { - _isTouching = event->touchPointStates().testFlag(Qt::TouchPointPressed); - } else { - auto currentMove = currentPos - _lastTouch; - - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_X_POS).getChannel()] = (currentMove.x > 0 ? currentMove.x : 0.0f); - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_X_NEG).getChannel()] = (currentMove.x < 0 ? -currentMove.x : 0.0f); - // Y mouse is inverted positive is pointing up the screen - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_Y_POS).getChannel()] = (currentMove.y < 0 ? -currentMove.y : 0.0f); - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_Y_NEG).getChannel()] = (currentMove.y > 0 ? currentMove.y : 0.0f); - } + if (_enableTouchpad) { + auto currentPos = evalAverageTouchPoints(event->touchPoints()); + _lastTouchTime = _clock.now(); - _lastTouch = currentPos; + if (!_isTouching) { + _isTouching = event->touchPointStates().testFlag(Qt::TouchPointPressed); + } + else { + auto currentMove = currentPos - _lastTouch; + + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_X_POS).getChannel()] = (currentMove.x > 0 ? currentMove.x : 0.0f); + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_X_NEG).getChannel()] = (currentMove.x < 0 ? -currentMove.x : 0.0f); + // Y mouse is inverted positive is pointing up the screen + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_Y_POS).getChannel()] = (currentMove.y < 0 ? -currentMove.y : 0.0f); + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_Y_NEG).getChannel()] = (currentMove.y > 0 ? currentMove.y : 0.0f); + } + + _lastTouch = currentPos; + } } controller::Input KeyboardMouseDevice::InputDevice::makeInput(Qt::Key code) const { diff --git a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h index fc2ad44a6d..d88c410ade 100644 --- a/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h +++ b/libraries/input-plugins/src/input-plugins/KeyboardMouseDevice.h @@ -85,7 +85,7 @@ public: void wheelEvent(QWheelEvent* event); - static void enableMouse(bool enableMouse) { _enableMouse = enableMouse; } + static void enableTouchpad(bool enableTouchpad) { _enableTouchpad = enableTouchpad; } static const QString NAME; @@ -125,7 +125,7 @@ protected: std::chrono::high_resolution_clock _clock; std::chrono::high_resolution_clock::time_point _lastTouchTime; - static bool _enableMouse; + static bool _enableTouchpad; }; #endif // hifi_KeyboardMouseDevice_h diff --git a/libraries/input-plugins/src/input-plugins/TouchscreenDevice.cpp b/libraries/input-plugins/src/input-plugins/TouchscreenDevice.cpp index 9430f1d23a..8e3eea4cf6 100644 --- a/libraries/input-plugins/src/input-plugins/TouchscreenDevice.cpp +++ b/libraries/input-plugins/src/input-plugins/TouchscreenDevice.cpp @@ -51,6 +51,13 @@ void TouchscreenDevice::pluginUpdate(float deltaTime, const controller::InputCal distanceScaleY = (_currentTouchVec.y - _firstTouchVec.y) / DPI_SCALE_Y; _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_AXIS_Y_NEG).getChannel()] = distanceScaleY; } + } else if (_touchPointCount == 2) { + if (_scaleFactor > _lastPinchScale && _scaleFactor != 0) { + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_POS).getChannel()] = 1.0f; + } else if (_scaleFactor != 0) { + _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_NEG).getChannel()] = 1.0f; + } + _lastPinchScale = _scaleFactor; } } @@ -64,12 +71,12 @@ void TouchscreenDevice::InputDevice::focusOutEvent() { void TouchscreenDevice::touchBeginEvent(const QTouchEvent* event) { const QTouchEvent::TouchPoint& point = event->touchPoints().at(0); _firstTouchVec = glm::vec2(point.pos().x(), point.pos().y()); - KeyboardMouseDevice::enableMouse(false); + KeyboardMouseDevice::enableTouchpad(false); } void TouchscreenDevice::touchEndEvent(const QTouchEvent* event) { _touchPointCount = 0; - KeyboardMouseDevice::enableMouse(true); + KeyboardMouseDevice::enableTouchpad(true); } void TouchscreenDevice::touchUpdateEvent(const QTouchEvent* event) { @@ -81,13 +88,7 @@ void TouchscreenDevice::touchUpdateEvent(const QTouchEvent* event) { void TouchscreenDevice::touchGestureEvent(const QGestureEvent* event) { if (QGesture* gesture = event->gesture(Qt::PinchGesture)) { QPinchGesture* pinch = static_cast(gesture); - qreal scaleFactor = pinch->totalScaleFactor(); - if (scaleFactor > _lastPinchScale && scaleFactor != 0) { - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_POS).getChannel()] = 1.0f; - } else if (scaleFactor != 0) { - _inputDevice->_axisStateMap[_inputDevice->makeInput(TOUCH_GESTURE_PINCH_NEG).getChannel()] = 1.0f; - } - _lastPinchScale = scaleFactor; + _scaleFactor = pinch->totalScaleFactor(); } } @@ -118,4 +119,4 @@ controller::Input::NamedVector TouchscreenDevice::InputDevice::getAvailableInput QString TouchscreenDevice::InputDevice::getDefaultMappingConfig() const { static const QString MAPPING_JSON = PathUtils::resourcesPath() + "/controllers/touchscreen.json"; return MAPPING_JSON; -} \ No newline at end of file +} diff --git a/libraries/input-plugins/src/input-plugins/TouchscreenDevice.h b/libraries/input-plugins/src/input-plugins/TouchscreenDevice.h index 772776348f..53dba19b00 100644 --- a/libraries/input-plugins/src/input-plugins/TouchscreenDevice.h +++ b/libraries/input-plugins/src/input-plugins/TouchscreenDevice.h @@ -14,6 +14,7 @@ #include #include "InputPlugin.h" +#include class QTouchEvent; class QGestureEvent; @@ -32,10 +33,10 @@ public: enum TouchGestureAxisChannel { TOUCH_GESTURE_PINCH_POS = TOUCH_AXIS_Y_NEG + 1, TOUCH_GESTURE_PINCH_NEG, - }; + }; // Plugin functions - virtual bool isSupported() const override { return true; } + virtual bool isSupported() const override { return QTouchDevice::devices().count() > 0; } virtual const QString& getName() const override { return NAME; } virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); } @@ -71,6 +72,7 @@ public: protected: qreal _lastPinchScale; + qreal _scaleFactor; glm::vec2 _firstTouchVec; glm::vec2 _currentTouchVec; int _touchPointCount;