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 "qmousedevice.h"
41#include "qmousedevice_p.h"
42
43#include <Qt3DInput/qmouseevent.h>
44#include <Qt3DInput/qphysicaldevicecreatedchange.h>
45#include <Qt3DCore/qentity.h>
46
47QT_BEGIN_NAMESPACE
48
49namespace Qt3DInput {
50
51/*! \internal */
52QMouseDevicePrivate::QMouseDevicePrivate()
53 : QAbstractPhysicalDevicePrivate()
54 , m_sensitivity(0.1f)
55 , m_updateContinuously(false)
56{
57}
58
59/*!
60 \qmltype MouseDevice
61 \instantiates Qt3DInput::QMouseDevice
62 \inqmlmodule Qt3D.Input
63 \since 5.5
64 \brief Delegates mouse events to the attached MouseHandler objects.
65
66 A MouseDevice delegates mouse events from physical mouse device to
67 MouseHandler objects. The sensitivity of the mouse can be controlled
68 with the \l MouseDevice::sensitivity property, which specifies the rate
69 in which the logical mouse coordinates change in response to physical
70 movement of the mouse.
71
72 \sa MouseHandler
73 */
74
75/*!
76 \class Qt3DInput::QMouseDevice
77 \inmodule Qt3DInput
78 \since 5.5
79 \brief Delegates mouse events to the attached MouseHandler objects.
80
81 A QMouseDevice delegates mouse events from physical mouse device to
82 QMouseHandler objects. The sensitivity of the mouse can be controlled
83 with the \l QMouseDevice::sensitivity property, which specifies the rate
84 in which the logical mouse coordinates change in response to physical
85 movement of the mouse.
86
87 \sa QMouseHandler
88 */
89
90/*!
91 \enum QMouseDevice::Axis
92
93 The mouse axis.
94
95 \value X
96 \value Y
97 \value WheelX
98 \value WheelY
99
100 \sa Qt3DInput::QAnalogAxisInput::setAxis
101 */
102
103/*!
104 \qmlproperty real MouseDevice::sensitivity
105
106 Holds the current sensitivity of the mouse device.
107 Default is 0.1.
108 */
109
110/*!
111 \property Qt3DInput::QMouseDevice::updateAxesContinuously
112
113 If \c true, axes will be updated anytime they change regardless of whether
114 any mouse button is being pressed. Otherwise, axes are updated only when
115 one of the mouse buttons is being pressed.
116
117 The default value is \c false.
118 \since 5.15
119*/
120
121/*!
122 \qmlproperty bool MouseDevice::updateAxesContinuously
123
124 If \c true, axes will be updated anytime they change regardless of whether
125 any mouse button is being pressed. Otherwise, axes are updated only when
126 one of the mouse buttons is being pressed.
127
128 The default value is \c false.
129 \since 5.15
130*/
131
132/*!
133 Constructs a new QMouseDevice instance with parent \a parent.
134 */
135QMouseDevice::QMouseDevice(QNode *parent)
136 : QAbstractPhysicalDevice(*new QMouseDevicePrivate, parent)
137{
138}
139
140/*! \internal */
141QMouseDevice::~QMouseDevice()
142{
143}
144
145/*!
146 \return the axis count.
147
148 \note Currently always returns 4.
149 */
150int QMouseDevice::axisCount() const
151{
152 return 4;
153}
154
155/*!
156 \return the button count.
157
158 \note Currently always returns 3.
159 */
160int QMouseDevice::buttonCount() const
161{
162 return 3;
163}
164
165/*!
166 \return the names of the axis.
167
168 \note Currently always returns StringList["X", "Y"]
169 */
170QStringList QMouseDevice::axisNames() const
171{
172 return QStringList()
173 << QStringLiteral("X")
174 << QStringLiteral("Y")
175 << QStringLiteral("WheelX")
176 << QStringLiteral("WheelY");
177}
178
179/*!
180 \return the names of the buttons.
181
182 \note Currently always returns StringList["Left", "Right", "Center"]
183 */
184QStringList QMouseDevice::buttonNames() const
185{
186 return QStringList()
187 << QStringLiteral("Left")
188 << QStringLiteral("Right")
189 << QStringLiteral("Center");
190}
191
192/*!
193 Convert axis \a name to axis identifier.
194 */
195int QMouseDevice::axisIdentifier(const QString &name) const
196{
197 if (name == QLatin1String("X"))
198 return X;
199 else if (name == QLatin1String("Y"))
200 return Y;
201 else if (name == QLatin1String("WheelX"))
202 return WheelX;
203 else if (name == QLatin1String("WheelY"))
204 return WheelY;
205 return -1;
206}
207
208/*!
209 Returns the button identifier that corresponds with
210 the specified \a name. The possible return values are
211 documented in \l {Qt3DInput::}{QMouseEvent::Buttons}.
212*/
213int QMouseDevice::buttonIdentifier(const QString &name) const
214{
215 if (name == QLatin1String("Left"))
216 return Qt3DInput::QMouseEvent::LeftButton;
217 if (name == QLatin1String("Right"))
218 return Qt3DInput::QMouseEvent::RightButton;
219 if (name == QLatin1String("Center"))
220 return Qt3DInput::QMouseEvent::MiddleButton;
221 return -1;
222}
223
224/*!
225 \property Qt3DInput::QMouseDevice::sensitivity
226
227 Holds the sensitivity of the mouse device.
228 The default is 0.1.
229*/
230float QMouseDevice::sensitivity() const
231{
232 Q_D(const QMouseDevice);
233 return d->m_sensitivity;
234}
235
236bool QMouseDevice::updateAxesContinuously() const
237{
238 Q_D(const QMouseDevice);
239 return d->m_updateContinuously;
240}
241
242void QMouseDevice::setSensitivity(float value)
243{
244 Q_D(QMouseDevice);
245 if (qFuzzyCompare(p1: value, p2: d->m_sensitivity))
246 return;
247
248 d->m_sensitivity = value;
249 emit sensitivityChanged(value);
250}
251
252void QMouseDevice::setUpdateAxesContinuously(bool updateAxesContinuously)
253{
254 Q_D(QMouseDevice);
255 if (d->m_updateContinuously == updateAxesContinuously)
256 return;
257
258 d->m_updateContinuously = updateAxesContinuously;
259 emit updateAxesContinuouslyChanged(updateAxesContinuously);
260}
261
262/*! \internal */
263void QMouseDevice::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change)
264{
265 Q_UNUSED(change);
266 // TODO: To be completed as the mouse input aspect takes shape
267}
268
269Qt3DCore::QNodeCreatedChangeBasePtr QMouseDevice::createNodeCreationChange() const
270{
271 auto creationChange = QPhysicalDeviceCreatedChangePtr<QMouseDeviceData>::create(arguments: this);
272 auto &data = creationChange->data;
273
274 Q_D(const QMouseDevice);
275 data.sensitivity = d->m_sensitivity;
276
277 return creationChange;
278}
279
280} // namespace Qt3DInput
281
282QT_END_NAMESPACE
283

source code of qt3d/src/input/frontend/qmousedevice.cpp