From 2d45defe15ca9a94f6eba992af3d544fcd5dfb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20H=C3=B6ltt=C3=A4?= Date: Fri, 30 Nov 2018 14:37:49 +0100 Subject: Add find-function find-function is similar to the move, but instead of moving the cursor, it returns the item that cursor would move to if move was called with the same arguments --- DemoApplication/pages/Page4.qml | 20 +++++++++++++++ plugin/cursornavigation.cpp | 50 +++++++++++++++++++++++-------------- plugin/cursornavigation.h | 7 +++++- plugin/cursornavigationattached.cpp | 16 ++++++++++++ plugin/cursornavigationattached.h | 5 ++++ plugin/inputadapter.cpp | 2 +- plugin/spatialnavigation4dir.cpp | 2 +- 7 files changed, 80 insertions(+), 22 deletions(-) diff --git a/DemoApplication/pages/Page4.qml b/DemoApplication/pages/Page4.qml index cc74876..a77ce7b 100644 --- a/DemoApplication/pages/Page4.qml +++ b/DemoApplication/pages/Page4.qml @@ -12,6 +12,14 @@ Item { repeat: false } + Rectangle { + id: pointerRect + border.color: "orange" + border.width: 1 + visible: false + color: "transparent" + } + Gamepad { deviceId: GamepadManager.connectedGamepads.length > 0 ? GamepadManager.connectedGamepads[0] : -1 @@ -21,6 +29,18 @@ Item { //console.log("handle joystick move, v=" + v) parent.CursorNavigation.move(Qt.vector2d(axisLeftX, axisLeftY), 10) cooldownTimer.start() + } else if (v.length() >= 0.1) { + var item = parent.CursorNavigation.find(Qt.vector2d(axisLeftX, axisLeftY), 10) + //cooldownTimer.start() + if (item != undefined) { + pointerRect.x = item.x + pointerRect.y = item.y + pointerRect.width = item.width + pointerRect.height = item.height + pointerRect.visible = true + } + } else { + pointerRect.visible = false } } diff --git a/plugin/cursornavigation.cpp b/plugin/cursornavigation.cpp index 1a81f76..e35a939 100644 --- a/plugin/cursornavigation.cpp +++ b/plugin/cursornavigation.cpp @@ -26,14 +26,30 @@ void CursorNavigation::move(qreal angle, qreal tolerance, bool discrete) { qreal a = qDegreesToRadians(angle); qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); - handleMove(a, t, discrete); + _move(a, t, discrete); } void CursorNavigation::move(const QVector2D& vector, qreal tolerance, bool discrete) { qreal a = qAtan2(vector.y(), vector.x()); qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); - handleMove(a, t, discrete); + _move(a, t, discrete); +} + +CursorNavigationAttached *CursorNavigation::find(qreal angle, qreal tolerance, bool discrete) +{ + qreal a = qDegreesToRadians(angle); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); + + return _find(a,t,discrete); +} + +CursorNavigationAttached *CursorNavigation::find(const QVector2D& vector, qreal tolerance, bool discrete) +{ + qreal a = qAtan2(vector.y(), vector.x()); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); + + return _find(a,t,discrete); } void CursorNavigation::action(Action action) @@ -47,7 +63,7 @@ void CursorNavigation::action(Action action) case Activate: break; case Escape: { - /* if item has escapeTrgate defined, set focus to that. otherwise leave + /* if item has escapeTarget defined, set focus to that. otherwise leave * scope, ie. go back to parent's parent in the hierarchy and set focus * (back) to it (setting the focus further to one of its children * depends on the focus mechanism). @@ -184,11 +200,20 @@ void CursorNavigation::unregisterItem(CursorNavigationAttached* item) item->m_parentNavigable->m_children.removeOne(item); } -bool CursorNavigation::handleMove(qreal angle, qreal tolerance, bool discrete) +void CursorNavigation::_move(qreal angle, qreal tolerance, bool discrete) +{ + CursorNavigationAttached *nextItem = _find(angle, tolerance, discrete); + + if (nextItem) { + setCursorOnItem(nextItem); + } +} + +CursorNavigationAttached *CursorNavigation::_find(qreal angle, qreal tolerance, bool discrete) { CursorNavigationAttached *nextItem = nullptr; - qWarning() << "handleMove, angle = " << angle << " tolerance = " << tolerance << " discrete = " << discrete; + qWarning() << "find next item, angle = " << angle << " tolerance = " << tolerance << " discrete = " << discrete; CursorNavigationCommand cmd(angle, tolerance); QList &candidates = m_currentItem ? @@ -201,18 +226,5 @@ bool CursorNavigation::handleMove(qreal angle, qreal tolerance, bool discrete) nextItem = m_navigation360.getNextCandidate(candidates, m_currentItem, cmd); } - if (nextItem) { - setCursorOnItem(nextItem); - } - -/* for (auto alg : m_algorithms) { - nextItem = alg->getNextCandidate(candidates, m_currentItem, cmd); - if (nextItem) { - setCursorOnItem(nextItem); - break; - } - }*/ - - return true; + return nextItem; } - diff --git a/plugin/cursornavigation.h b/plugin/cursornavigation.h index 5c7d7f8..34dd794 100644 --- a/plugin/cursornavigation.h +++ b/plugin/cursornavigation.h @@ -20,8 +20,12 @@ class CursorNavigation : public QObject public: CursorNavigation(QQuickWindow *parent); + //move the cursor void move(qreal angle, qreal tolerance, bool discrete); void move(const QVector2D& vector, qreal tolerance, bool discrete); + //find the next item without moving the cursor + CursorNavigationAttached *find(qreal angle, qreal tolerance, bool discrete); + CursorNavigationAttached *find(const QVector2D& vector, qreal tolerance, bool discrete); void action(Action action); static CursorNavigationAttached *qmlAttachedProperties(QObject *object); @@ -36,7 +40,8 @@ private: void registerItem(CursorNavigationAttached* item); void unregisterItem(CursorNavigationAttached* item); - bool handleMove(qreal angle, qreal tolerance, bool discrete); + void _move(qreal angle, qreal tolerance, bool discrete); + CursorNavigationAttached *_find(qreal angle, qreal tolerance, bool discrete); private: static const char windowPropertyName[]; diff --git a/plugin/cursornavigationattached.cpp b/plugin/cursornavigationattached.cpp index 0f7afdc..af34bfd 100644 --- a/plugin/cursornavigationattached.cpp +++ b/plugin/cursornavigationattached.cpp @@ -80,6 +80,22 @@ void CursorNavigationAttached::move(QVector2D vector, qreal tolerance) m_cursorNavigation->move(vector, tolerance, false); } +QQuickItem *CursorNavigationAttached::find(qreal angle, qreal tolerance) +{ + CursorNavigationAttached *item = m_cursorNavigation->find(angle, tolerance, false); + if (item) + return item->item(); + return nullptr; +} + +QQuickItem *CursorNavigationAttached::find(QVector2D vector, qreal tolerance) +{ + CursorNavigationAttached *item = m_cursorNavigation->find(vector, tolerance, false); + if (item) + return item->item(); + return nullptr; +} + void CursorNavigationAttached::moveUp() { m_cursorNavigation->move(-90, 0, true); diff --git a/plugin/cursornavigationattached.h b/plugin/cursornavigationattached.h index ee32f76..1ae2119 100644 --- a/plugin/cursornavigationattached.h +++ b/plugin/cursornavigationattached.h @@ -42,6 +42,11 @@ public slots: void move(qreal angle, qreal tolerance = 0); void move(QVector2D vector, qreal tolerance = 0); + + //find the next item with this move, without moving + QQuickItem *find(qreal angle, qreal tolerance = 0); + QQuickItem *find(QVector2D vector, qreal tolerance = 0); + void moveUp(); void moveDown(); void moveRight(); diff --git a/plugin/inputadapter.cpp b/plugin/inputadapter.cpp index 89fa1a8..feb1373 100644 --- a/plugin/inputadapter.cpp +++ b/plugin/inputadapter.cpp @@ -44,7 +44,7 @@ bool InputAdapter::handleKeyEvent(QKeyEvent *event) m_cursorNavigation->move(0, 0, true); break; case Qt::Key_Up: - m_cursorNavigation->move(270, 0, true); + m_cursorNavigation->move(-90, 0, true); break; case Qt::Key_Down: m_cursorNavigation->move(90, 0, true); diff --git a/plugin/spatialnavigation4dir.cpp b/plugin/spatialnavigation4dir.cpp index 0ccf041..ac72e4e 100644 --- a/plugin/spatialnavigation4dir.cpp +++ b/plugin/spatialnavigation4dir.cpp @@ -30,7 +30,7 @@ CursorNavigationAttached* SpatialNavigation4Dir::getNextCandidate( if (candidates.isEmpty()) return nullptr; - qDebug() << "spatial chooser called, no of candidates=" << candidates.count(); + qDebug() << "4-way algortihm called, no of candidates=" << candidates.count(); if (!currentItem && candidates.size()) { qDebug() << "the spatial chooser falling back to first child" << candidates.first(); -- cgit v1.2.3