summaryrefslogtreecommitdiffstats
path: root/src/input
diff options
context:
space:
mode:
authorRĂ©mi Benoit <remi.benoit@kdab.com>2015-07-08 18:45:56 +0200
committerSean Harmer <sean.harmer@kdab.com>2015-08-03 14:44:07 +0000
commit9b4816a068ebd17e561d72fe9e12c78279e87c32 (patch)
tree7e150886332ad2864c7e3ec4317e14e0cb55af74 /src/input
parent6a5f72d17fd198ac29fa09dbdd54138faeccef8d (diff)
Filter and dispatch mouse events
For now events are sent to all MouseInputs regardless of their state (active/inactive) or if the event is targeted at an entity. Change-Id: I06d58bc72f994d211404c1f8a9ec862a2c65d300 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/input')
-rw-r--r--src/input/input.pri8
-rw-r--r--src/input/inputhandler.cpp56
-rw-r--r--src/input/inputhandler_p.h8
-rw-r--r--src/input/mousecontroller.cpp5
-rw-r--r--src/input/mousecontroller_p.h2
-rw-r--r--src/input/mouseeventdispatcherjob.cpp76
-rw-r--r--src/input/mouseeventdispatcherjob_p.h71
-rw-r--r--src/input/mouseeventfilter.cpp82
-rw-r--r--src/input/mouseeventfilter.h71
-rw-r--r--src/input/qinputaspect.cpp8
10 files changed, 382 insertions, 5 deletions
diff --git a/src/input/input.pri b/src/input/input.pri
index a16677905..93c031947 100644
--- a/src/input/input.pri
+++ b/src/input/input.pri
@@ -21,7 +21,9 @@ HEADERS += \
$$PWD/qmousecontroller_p.h \
$$PWD/q3dmouseevent.h \
$$PWD/mousecontroller_p.h \
- $$PWD/mouseinput_p.h
+ $$PWD/mouseinput_p.h \
+ $$PWD/mouseeventfilter.h \
+ $$PWD/mouseeventdispatcherjob_p.h
SOURCES += \
$$PWD/cameracontroller.cpp \
@@ -39,6 +41,8 @@ SOURCES += \
$$PWD/qmousecontroller.cpp \
$$PWD/q3dmouseevent.cpp \
$$PWD/mousecontroller.cpp \
- $$PWD/mouseinput.cpp
+ $$PWD/mouseinput.cpp \
+ $$PWD/mouseeventfilter.cpp \
+ $$PWD/mouseeventdispatcherjob.cpp
INCLUDEPATH += $$PWD
diff --git a/src/input/inputhandler.cpp b/src/input/inputhandler.cpp
index 56daf8ed6..2021694f1 100644
--- a/src/input/inputhandler.cpp
+++ b/src/input/inputhandler.cpp
@@ -37,8 +37,10 @@
#include "inputhandler_p.h"
#include "inputmanagers_p.h"
#include "keyboardeventfilter_p.h"
+#include "mouseeventfilter.h"
#include "assignkeyboardfocusjob_p.h"
#include "keyeventdispatcherjob_p.h"
+#include "mouseeventdispatcherjob_p.h"
QT_BEGIN_NAMESPACE
@@ -53,20 +55,29 @@ InputHandler::InputHandler()
, m_mouseInputManager(new MouseInputManager())
, m_eventSource(Q_NULLPTR)
, m_keyboardEventFilter(new KeyboardEventFilter())
+ , m_mouseEventFilter(new MouseEventFilter())
{
m_keyboardEventFilter->setInputHandler(this);
+ m_mouseEventFilter->setInputHandler(this);
}
// Called in MainThread
void InputHandler::setEventSource(QObject *object)
{
if (object != m_eventSource) {
- if (m_eventSource)
+ if (m_eventSource) {
m_eventSource->removeEventFilter(m_keyboardEventFilter);
+ m_eventSource->removeEventFilter(m_mouseEventFilter);
+ }
+
clearPendingKeyEvents();
+ clearPendingMouseEvents();
+
m_eventSource = object;
- if (m_eventSource)
+ if (m_eventSource) {
m_eventSource->installEventFilter(m_keyboardEventFilter);
+ m_eventSource->installEventFilter(m_mouseEventFilter);
+ }
}
}
@@ -93,6 +104,26 @@ void InputHandler::clearPendingKeyEvents()
m_pendingEvents.clear();
}
+void InputHandler::appendMouseEvent(const QMouseEvent &event)
+{
+ QMutexLocker lock(&m_mutex);
+ m_pendingMouseEvents.append(event);
+}
+
+QList<QMouseEvent> InputHandler::pendingMouseEvents()
+{
+ QMutexLocker lock(&m_mutex);
+ QList<QMouseEvent> pendingEvents = m_pendingMouseEvents;
+ m_pendingMouseEvents.clear();
+ return pendingEvents;
+}
+
+void InputHandler::clearPendingMouseEvents()
+{
+ QMutexLocker lock(&m_mutex);
+ m_pendingMouseEvents.clear();
+}
+
void InputHandler::appendKeyboardController(HKeyboardController controller)
{
m_activeKeyboardControllers.append(controller);
@@ -145,6 +176,27 @@ QVector<QAspectJobPtr> InputHandler::keyboardJobs()
return jobs;
}
+QVector<QAspectJobPtr> InputHandler::mouseJobs()
+{
+ QVector<QAspectJobPtr> jobs;
+ const QList<QMouseEvent> events = pendingMouseEvents();
+
+ Q_FOREACH (const HMouseController cHandle, m_activeMouseControllers) {
+ MouseController *controller = m_mouseControllerManager->data(cHandle);
+
+ // Event dispacthing job
+ if (!events.isEmpty()) {
+ Q_FOREACH (const QNodeId &input, controller->mouseInputs()) {
+ MouseEventDispatcherJob *job = new MouseEventDispatcherJob(input, events);
+ job->setInputHandler(this);
+ jobs.append(QAspectJobPtr(job));
+ }
+ }
+ }
+
+ return jobs;
+}
+
} // Input
} // Qt3D
diff --git a/src/input/inputhandler_p.h b/src/input/inputhandler_p.h
index 84bad456c..477c61e99 100644
--- a/src/input/inputhandler_p.h
+++ b/src/input/inputhandler_p.h
@@ -54,6 +54,7 @@ class KeyboardControllerManager;
class KeyboardEventFilter;
class MouseControllerManager;
class MouseInputManager;
+class MouseEventFilter;
class InputHandler
{
@@ -72,6 +73,10 @@ public:
QList<QKeyEvent> pendingKeyEvents();
void clearPendingKeyEvents();
+ void appendMouseEvent(const QMouseEvent &event);
+ QList<QMouseEvent> pendingMouseEvents();
+ void clearPendingMouseEvents();
+
void appendKeyboardController(HKeyboardController controller);
void removeKeyboardController(HKeyboardController controller);
@@ -79,6 +84,7 @@ public:
void removeMouseController(HMouseController controller);
QVector<QAspectJobPtr> keyboardJobs();
+ QVector<QAspectJobPtr> mouseJobs();
private:
KeyboardControllerManager *m_keyboardControllerManager;
@@ -90,6 +96,8 @@ private:
QObject *m_eventSource;
KeyboardEventFilter *m_keyboardEventFilter;
QList<QKeyEvent> m_pendingEvents;
+ MouseEventFilter *m_mouseEventFilter;
+ QList<QMouseEvent> m_pendingMouseEvents;
mutable QMutex m_mutex;
};
diff --git a/src/input/mousecontroller.cpp b/src/input/mousecontroller.cpp
index 968f21ce0..d41ad1eb4 100644
--- a/src/input/mousecontroller.cpp
+++ b/src/input/mousecontroller.cpp
@@ -79,6 +79,11 @@ void MouseController::removeMouseInput(const QNodeId &input)
m_mouseInputs.removeOne(input);
}
+QVector<QNodeId> MouseController::mouseInputs() const
+{
+ return m_mouseInputs;
+}
+
void MouseController::sceneChangeEvent(const QSceneChangePtr &e)
{
Q_UNUSED(e);
diff --git a/src/input/mousecontroller_p.h b/src/input/mousecontroller_p.h
index 6ff8198d8..1d8ecddeb 100644
--- a/src/input/mousecontroller_p.h
+++ b/src/input/mousecontroller_p.h
@@ -59,6 +59,8 @@ public:
void addMouseInput(const QNodeId &input);
void removeMouseInput(const QNodeId &input);
+ QVector<QNodeId> mouseInputs() const;
+
protected:
void sceneChangeEvent(const QSceneChangePtr &e) Q_DECL_OVERRIDE;
diff --git a/src/input/mouseeventdispatcherjob.cpp b/src/input/mouseeventdispatcherjob.cpp
new file mode 100644
index 000000000..6104f2093
--- /dev/null
+++ b/src/input/mouseeventdispatcherjob.cpp
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mouseeventdispatcherjob_p.h"
+#include "inputhandler_p.h"
+#include "mouseinput_p.h"
+#include "inputmanagers_p.h"
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3D {
+
+namespace Input {
+
+MouseEventDispatcherJob::MouseEventDispatcherJob(const QNodeId &input, const QList<QMouseEvent> &events)
+ : QAspectJob()
+ , m_inputHandler(Q_NULLPTR)
+ , m_mouseInput(input)
+ , m_events(events)
+{
+}
+
+void MouseEventDispatcherJob::setInputHandler(InputHandler *handler)
+{
+ m_inputHandler = handler;
+}
+
+void MouseEventDispatcherJob::run()
+{
+ MouseInput *input = m_inputHandler->mouseInputManager()->lookupResource(m_mouseInput);
+ if (input) {
+ Q_FOREACH (const QMouseEvent &e, m_events) {
+ // Send events to frontend
+ input->mouseEvent(Q3DMouseEventPtr(new Q3DMouseEvent(e)));
+ }
+ }
+}
+
+} // Input
+
+} // Qt3D
+
+QT_END_NAMESPACE
diff --git a/src/input/mouseeventdispatcherjob_p.h b/src/input/mouseeventdispatcherjob_p.h
new file mode 100644
index 000000000..bee74eafa
--- /dev/null
+++ b/src/input/mouseeventdispatcherjob_p.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3D_INPUT_MOUSEEVENTDISPATCHERJOB_P_H
+#define QT3D_INPUT_MOUSEEVENTDISPATCHERJOB_P_H
+
+#include <Qt3DCore/qaspectjob.h>
+#include <Qt3DCore/qnodeid.h>
+#include <QMouseEvent>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3D {
+
+namespace Input {
+
+class InputHandler;
+
+class MouseEventDispatcherJob : public QAspectJob
+{
+public:
+ explicit MouseEventDispatcherJob(const QNodeId &input, const QList<QMouseEvent> &events);
+ void setInputHandler(InputHandler *handler);
+ void run() Q_DECL_FINAL;
+
+private:
+ InputHandler *m_inputHandler;
+ const QNodeId m_mouseInput;
+ const QList<QMouseEvent> m_events;
+};
+
+} // Input
+
+} // Mouse
+
+QT_END_NAMESPACE
+
+#endif // QT3D_INPUT_MOUSEEVENTDISPATCHERJOB_P_H
diff --git a/src/input/mouseeventfilter.cpp b/src/input/mouseeventfilter.cpp
new file mode 100644
index 000000000..ab0519623
--- /dev/null
+++ b/src/input/mouseeventfilter.cpp
@@ -0,0 +1,82 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "mouseeventfilter.h"
+#include "inputhandler_p.h"
+#include <QEvent>
+#include <QKeyEvent>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3D {
+
+namespace Input {
+
+// The MouseEventFilter must be in the same thread as the view it will be monitoring
+
+MouseEventFilter::MouseEventFilter(QObject *parent)
+ : QObject(parent)
+{
+}
+
+void MouseEventFilter::setInputHandler(InputHandler *handler)
+{
+ m_inputHandler = handler;
+}
+
+// Triggered in the view thread (usually the main thread)
+bool MouseEventFilter::eventFilter(QObject *obj, QEvent *e)
+{
+ switch (e->type()) {
+ case QEvent::MouseButtonPress:
+ case QEvent::MouseButtonRelease:
+ case QEvent::MouseMove:
+ // Store event to be processed later on in an InputAspect job
+ m_inputHandler->appendMouseEvent(QMouseEvent(*static_cast<QMouseEvent *>(e)));
+ break;
+
+ default:
+ break;
+ }
+
+ return QObject::eventFilter(obj, e);
+}
+
+} // Input
+
+} // Qt3D
+
+QT_END_NAMESPACE
diff --git a/src/input/mouseeventfilter.h b/src/input/mouseeventfilter.h
new file mode 100644
index 000000000..730245240
--- /dev/null
+++ b/src/input/mouseeventfilter.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3D_INPUT_MOUSEEVENTFILTER_P_H
+#define QT3D_INPUT_MOUSEEVENTFILTER_P_H
+
+#include <QObject>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3D {
+
+namespace Input {
+
+class InputHandler;
+
+class MouseEventFilter : public QObject
+{
+ Q_OBJECT
+public:
+ explicit MouseEventFilter(QObject *parent = 0);
+ void setInputHandler(InputHandler *handler);
+ inline InputHandler *inputHandler() const { return m_inputHandler; }
+
+protected:
+ bool eventFilter(QObject *obj, QEvent *e) Q_DECL_OVERRIDE;
+
+private:
+ InputHandler *m_inputHandler;
+};
+
+} // Input
+
+} // Qt3D
+
+QT_END_NAMESPACE
+
+#endif // QT3D_INPUT_MOUSEEVENTFILTER_P_H
diff --git a/src/input/qinputaspect.cpp b/src/input/qinputaspect.cpp
index 5f052ca43..84638239b 100644
--- a/src/input/qinputaspect.cpp
+++ b/src/input/qinputaspect.cpp
@@ -40,11 +40,15 @@
#include "inputhandler_p.h"
#include "keyboardcontroller_p.h"
#include "keyboardinput_p.h"
+#include "mousecontroller_p.h"
+#include "mouseinput_p.h"
#include <Qt3DCore/qaspectfactory.h>
#include <Qt3DCore/qnodevisitor.h>
#include <Qt3DCore/qscenepropertychange.h>
#include <Qt3DInput/qkeyboardcontroller.h>
#include <Qt3DInput/qkeyboardinput.h>
+#include <Qt3DInput/qmousecontroller.h>
+#include <Qt3DInput/qmouseinput.h>
QT_BEGIN_NAMESPACE
@@ -73,6 +77,8 @@ QInputAspect::QInputAspect(QObject *parent)
{
registerBackendType<QKeyboardController>(QBackendNodeFunctorPtr(new Input::KeyboardControllerFunctor(d_func()->m_inputHandler.data())));
registerBackendType<QKeyboardInput>(QBackendNodeFunctorPtr(new Input::KeyboardInputFunctor(d_func()->m_inputHandler.data())));
+ registerBackendType<QMouseController>(QBackendNodeFunctorPtr(new Input::MouseControllerFunctor(d_func()->m_inputHandler.data())));
+ registerBackendType<QMouseInput>(QBackendNodeFunctorPtr(new Input::MouseInputFunctor(d_func()->m_inputHandler.data())));
}
QCamera *QInputAspect::camera() const
@@ -94,7 +100,7 @@ QVector<QAspectJobPtr> QInputAspect::jobsToExecute(qint64 time)
QVector<QAspectJobPtr> jobs;
jobs.append(d->m_inputHandler->keyboardJobs());
- // One job for Mouse events
+ jobs.append(d->m_inputHandler->mouseJobs());
return jobs;
}