1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "mousedevice_p.h"
41
42#include <Qt3DInput/qmousedevice.h>
43#include <Qt3DCore/qentity.h>
44#include <Qt3DCore/qnode.h>
45
46#include <Qt3DInput/private/inputmanagers_p.h>
47#include <Qt3DInput/private/inputhandler_p.h>
48#include <Qt3DInput/private/qmousedevice_p.h>
49
50QT_BEGIN_NAMESPACE
51
52namespace Qt3DInput {
53namespace Input {
54
55MouseDevice::MouseDevice()
56 : QAbstractPhysicalDeviceBackendNode(ReadOnly)
57 , m_inputHandler(nullptr)
58 , m_wasPressed(false)
59 , m_sensitivity(0.1f)
60 , m_updateAxesContinuously(false)
61{
62}
63
64MouseDevice::~MouseDevice()
65{
66}
67
68void MouseDevice::setInputHandler(InputHandler *handler)
69{
70 m_inputHandler = handler;
71}
72
73InputHandler *MouseDevice::inputHandler() const
74{
75 return m_inputHandler;
76}
77
78float MouseDevice::axisValue(int axisIdentifier) const
79{
80 switch (axisIdentifier) {
81 case QMouseDevice::X:
82 return m_mouseState.xAxis;
83 case QMouseDevice::Y:
84 return m_mouseState.yAxis;
85 case QMouseDevice::WheelX:
86 return m_mouseState.wXAxis;
87 case QMouseDevice::WheelY:
88 return m_mouseState.wYAxis;
89 default:
90 break;
91 }
92 return 0.0f;
93}
94
95bool MouseDevice::isButtonPressed(int buttonIdentifier) const
96{
97 switch (buttonIdentifier) {
98 case QMouseEvent::LeftButton:
99 return m_mouseState.leftPressed;
100 case QMouseEvent::MiddleButton:
101 return m_mouseState.centerPressed;
102 case QMouseEvent::RightButton:
103 return m_mouseState.rightPressed;
104 default:
105 break;
106 }
107 return false;
108}
109
110MouseDevice::MouseState MouseDevice::mouseState() const
111{
112 return m_mouseState;
113}
114
115QPointF MouseDevice::previousPos() const
116{
117 return m_previousPos;
118}
119
120bool MouseDevice::wasPressed() const
121{
122 return m_wasPressed;
123}
124
125float MouseDevice::sensitivity() const
126{
127 return m_sensitivity;
128}
129
130bool MouseDevice::updateAxesContinuously() const
131{
132 return m_updateAxesContinuously;
133}
134
135#if QT_CONFIG(wheelevent)
136void MouseDevice::updateWheelEvents(const QList<QT_PREPEND_NAMESPACE (QWheelEvent)> &events)
137{
138 // Reset axis values before we accumulate new values for this frame
139 m_mouseState.wXAxis = 0.0f;
140 m_mouseState.wYAxis = 0.0f;
141 if (!events.isEmpty()) {
142 for (const QT_PREPEND_NAMESPACE(QWheelEvent) &e : events) {
143 m_mouseState.wXAxis += m_sensitivity * e.angleDelta().x();
144 m_mouseState.wYAxis += m_sensitivity * e.angleDelta().y();
145 }
146 }
147}
148#endif
149
150// Main Thread
151void MouseDevice::updateMouseEvents(const QList<QT_PREPEND_NAMESPACE(QMouseEvent)> &events)
152{
153 // Reset axis values before we accumulate new values for this frame
154 m_mouseState.xAxis = 0.0f;
155 m_mouseState.yAxis = 0.0f;
156
157 if (!events.isEmpty()) {
158 for (const QT_PREPEND_NAMESPACE(QMouseEvent) &e : events) {
159 m_mouseState.leftPressed = e.buttons() & (Qt::LeftButton);
160 m_mouseState.centerPressed = e.buttons() & (Qt::MiddleButton);
161 m_mouseState.rightPressed = e.buttons() & (Qt::RightButton);
162 const bool pressed = m_mouseState.leftPressed || m_mouseState.centerPressed || m_mouseState.rightPressed;
163 if (m_updateAxesContinuously || (m_wasPressed && pressed)) {
164 m_mouseState.xAxis += m_sensitivity * (e.screenPos().x() - m_previousPos.x());
165 m_mouseState.yAxis += m_sensitivity * (m_previousPos.y() - e.screenPos().y());
166 }
167 m_wasPressed = pressed;
168 m_previousPos = e.screenPos();
169 }
170 }
171}
172
173void MouseDevice::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
174{
175 QAbstractPhysicalDeviceBackendNode::syncFromFrontEnd(frontEnd, firstTime);
176 const Qt3DInput::QMouseDevice *node = qobject_cast<const Qt3DInput::QMouseDevice *>(object: frontEnd);
177 if (!node)
178 return;
179
180 m_sensitivity = node->sensitivity();
181 m_updateAxesContinuously = node->updateAxesContinuously();
182}
183
184MouseDeviceFunctor::MouseDeviceFunctor(QInputAspect *inputAspect, InputHandler *handler)
185 : m_inputAspect(inputAspect)
186 , m_handler(handler)
187{
188}
189
190Qt3DCore::QBackendNode *MouseDeviceFunctor::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const
191{
192 MouseDevice *controller = m_handler->mouseDeviceManager()->getOrCreateResource(id: change->subjectId());
193 controller->setInputAspect(m_inputAspect);
194 controller->setInputHandler(m_handler);
195 m_handler->appendMouseDevice(device: m_handler->mouseDeviceManager()->lookupHandle(id: change->subjectId()));
196 return controller;
197}
198
199Qt3DCore::QBackendNode *MouseDeviceFunctor::get(Qt3DCore::QNodeId id) const
200{
201 return m_handler->mouseDeviceManager()->lookupResource(id);
202}
203
204void MouseDeviceFunctor::destroy(Qt3DCore::QNodeId id) const
205{
206 m_handler->removeMouseDevice(device: m_handler->mouseDeviceManager()->lookupHandle(id));
207 m_handler->mouseDeviceManager()->releaseResource(id);
208}
209
210} // namespace Input
211} // namespace Qt3DInput
212
213QT_END_NAMESPACE
214

source code of qt3d/src/input/backend/mousedevice.cpp