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#include <Qt3DCore/qpropertyupdatedchange.h>
46
47#include <Qt3DInput/private/inputmanagers_p.h>
48#include <Qt3DInput/private/inputhandler_p.h>
49#include <Qt3DInput/private/qmousedevice_p.h>
50
51QT_BEGIN_NAMESPACE
52
53namespace Qt3DInput {
54namespace Input {
55
56MouseDevice::MouseDevice()
57 : QAbstractPhysicalDeviceBackendNode(ReadOnly)
58 , m_inputHandler(nullptr)
59 , m_wasPressed(false)
60 , m_sensitivity(0.1f)
61{
62}
63
64MouseDevice::~MouseDevice()
65{
66}
67
68void MouseDevice::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
69{
70 const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QMouseDeviceData>>(change);
71 const auto &data = typedChange->data;
72 m_sensitivity = data.sensitivity;
73 QAbstractPhysicalDeviceBackendNode::initializeFromPeer(change);
74}
75
76void MouseDevice::setInputHandler(InputHandler *handler)
77{
78 m_inputHandler = handler;
79}
80
81InputHandler *MouseDevice::inputHandler() const
82{
83 return m_inputHandler;
84}
85
86float MouseDevice::axisValue(int axisIdentifier) const
87{
88 switch (axisIdentifier) {
89 case QMouseDevice::X:
90 return m_mouseState.xAxis;
91 case QMouseDevice::Y:
92 return m_mouseState.yAxis;
93 case QMouseDevice::WheelX:
94 return m_mouseState.wXAxis;
95 case QMouseDevice::WheelY:
96 return m_mouseState.wYAxis;
97 default:
98 break;
99 }
100 return 0.0f;
101}
102
103bool MouseDevice::isButtonPressed(int buttonIdentifier) const
104{
105 switch (buttonIdentifier) {
106 case QMouseEvent::LeftButton:
107 return m_mouseState.leftPressed;
108 case QMouseEvent::MiddleButton:
109 return m_mouseState.centerPressed;
110 case QMouseEvent::RightButton:
111 return m_mouseState.rightPressed;
112 default:
113 break;
114 }
115 return false;
116}
117
118MouseDevice::MouseState MouseDevice::mouseState() const
119{
120 return m_mouseState;
121}
122
123QPointF MouseDevice::previousPos() const
124{
125 return m_previousPos;
126}
127
128bool MouseDevice::wasPressed() const
129{
130 return m_wasPressed;
131}
132
133float MouseDevice::sensitivity() const
134{
135 return m_sensitivity;
136}
137
138#if QT_CONFIG(wheelevent)
139void MouseDevice::updateWheelEvents(const QList<QT_PREPEND_NAMESPACE (QWheelEvent)> &events)
140{
141 // Reset axis values before we accumulate new values for this frame
142 m_mouseState.wXAxis = 0.0f;
143 m_mouseState.wYAxis = 0.0f;
144 if (!events.isEmpty()) {
145 for (const QT_PREPEND_NAMESPACE(QWheelEvent) &e : events) {
146 m_mouseState.wXAxis += m_sensitivity * e.angleDelta().x();
147 m_mouseState.wYAxis += m_sensitivity * e.angleDelta().y();
148 }
149 }
150}
151#endif
152
153void MouseDevice::updateMouseEvents(const QList<QT_PREPEND_NAMESPACE(QMouseEvent)> &events)
154{
155 // Reset axis values before we accumulate new values for this frame
156 m_mouseState.xAxis = 0.0f;
157 m_mouseState.yAxis = 0.0f;
158
159 if (!events.isEmpty()) {
160 for (const QT_PREPEND_NAMESPACE(QMouseEvent) &e : events) {
161 m_mouseState.leftPressed = e.buttons() & (Qt::LeftButton);
162 m_mouseState.centerPressed = e.buttons() & (Qt::MiddleButton);
163 m_mouseState.rightPressed = e.buttons() & (Qt::RightButton);
164 bool pressed = m_mouseState.leftPressed || m_mouseState.centerPressed || m_mouseState.rightPressed;
165 if (m_wasPressed && pressed) {
166 m_mouseState.xAxis += m_sensitivity * (e.screenPos().x() - m_previousPos.x());
167 m_mouseState.yAxis += m_sensitivity * (m_previousPos.y() - e.screenPos().y());
168 }
169 m_wasPressed = pressed;
170 m_previousPos = e.screenPos();
171 }
172 }
173}
174
175void MouseDevice::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
176{
177 if (e->type() == Qt3DCore::PropertyUpdated) {
178 Qt3DCore::QPropertyUpdatedChangePtr propertyChange = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
179 if (propertyChange->propertyName() == QByteArrayLiteral("sensitivity"))
180 m_sensitivity = propertyChange->value().toFloat();
181 }
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(change->subjectId());
193 controller->setInputAspect(m_inputAspect);
194 controller->setInputHandler(m_handler);
195 m_handler->appendMouseDevice(m_handler->mouseDeviceManager()->lookupHandle(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(m_handler->mouseDeviceManager()->lookupHandle(id));
207 m_handler->mouseDeviceManager()->releaseResource(id);
208}
209
210} // namespace Input
211} // namespace Qt3DInput
212
213QT_END_NAMESPACE
214