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 "qlogicaldevice.h"
41#include "qlogicaldevice_p.h"
42
43#include <Qt3DCore/qnodecreatedchange.h>
44#include <Qt3DInput/qaction.h>
45#include <Qt3DInput/qaxis.h>
46
47QT_BEGIN_NAMESPACE
48
49namespace Qt3DInput {
50
51QLogicalDevicePrivate::QLogicalDevicePrivate()
52 : Qt3DCore::QComponentPrivate()
53{
54}
55
56QLogicalDevicePrivate::~QLogicalDevicePrivate()
57{
58}
59
60/*!
61 \class Qt3DInput::QLogicalDevice
62 \inmodule Qt3DInput
63 \inherits Qt3DCore::QNode
64 \brief QLogicalDevice allows the user to define a set of actions that they wish to use within an application.
65
66 \since 5.6
67*/
68
69/*!
70 \qmltype LogicalDevice
71 \inqmlmodule Qt3D.Input
72 \instantiates Qt3DInput::QLogicalDevice
73 \brief QML frontend for the Qt3DInput::QLogicalDevice C++ class.
74
75 Allows the user to define a set of actions that they wish to use within an application.
76
77 \qml
78 LogicalDevice {
79 id: keyboardLogicalDevice
80
81 actions: [
82 Action {
83 name: "fire"
84 inputs: [
85 ActionInput {
86 sourceDevice: keyboardSourceDevice
87 keys: [Qt.Key_Space]
88 },
89 InputChord {
90 tolerance: 10
91 inputs: [
92 ActionInput {
93 sourceDevice: keyboardSourceDevice
94 keys: [Qt.Key_A]
95 },
96 ActionInput {
97 sourceDevice: keyboardSourceDevice
98 keys: [Qt.Key_S]
99 }
100 ]
101 }
102 ]
103 },
104 Action {
105 name: "reload"
106 inputs: [
107 ActionInput {
108 sourceDevice: keyboardSourceDevice
109 keys: [Qt.Key_Alt]
110 }
111 ]
112 },
113 Action {
114 name: "combo"
115 inputs: [
116 InputSequence {
117 interval: 1000
118 timeout: 10000
119 inputs: [
120 ActionInput {
121 sourceDevice: keyboardSourceDevice
122 keys: [Qt.Key_G]
123 },
124 ActionInput {
125 sourceDevice: keyboardSourceDevice
126 keys: [Qt.Key_D]
127 },
128 ActionInput {
129 sourceDevice: keyboardSourceDevice
130 keys: [Qt.Key_J]
131 }
132 ]
133 }
134 ]
135 }
136 ]
137 }
138 \endqml
139
140 \since 5.6
141*/
142
143/*!
144 Constructs a new QLogicalDevice instance with parent \a parent.
145 */
146QLogicalDevice::QLogicalDevice(Qt3DCore::QNode *parent)
147 : Qt3DCore::QComponent(*new QLogicalDevicePrivate(), parent)
148{
149}
150
151QLogicalDevice::~QLogicalDevice()
152{
153}
154
155/*!
156 \qmlproperty list<Action> Qt3D.Input::LogicalDevice::actions
157
158 The actions used by this Logical Device
159*/
160
161/*!
162 Add an \a action to the list of actions.
163 */
164void QLogicalDevice::addAction(QAction *action)
165{
166 Q_D(QLogicalDevice);
167 if (!d->m_actions.contains(t: action)) {
168 d->m_actions.push_back(t: action);
169 // Force creation in backend by setting parent
170 if (!action->parent())
171 action->setParent(this);
172
173 // Ensures proper bookkeeping
174 d->registerDestructionHelper(node: action, func: &QLogicalDevice::removeAction, d->m_actions);
175
176 d->updateNode(node: action, property: "action", change: Qt3DCore::PropertyValueAdded);
177 }
178}
179
180/*!
181 Remove an \a action from the list of actions.
182 */
183void QLogicalDevice::removeAction(QAction *action)
184{
185 Q_D(QLogicalDevice);
186 if (d->m_actions.contains(t: action)) {
187 d->updateNode(node: action, property: "action", change: Qt3DCore::PropertyValueRemoved);
188
189 d->m_actions.removeOne(t: action);
190
191 // Remove bookkeeping connection
192 d->unregisterDestructionHelper(node: action);
193 }
194}
195
196/*!
197 Returns the list of actions.
198 */
199QVector<QAction *> QLogicalDevice::actions() const
200{
201 Q_D(const QLogicalDevice);
202 return d->m_actions;
203}
204
205/*!
206 \qmlproperty list<Axis> Qt3D.Input::LogicalDevice::axis
207
208 The axis used by this Logical Device
209*/
210
211/*!
212 Add an \a axis to the list of axis.
213 */
214void QLogicalDevice::addAxis(QAxis *axis)
215{
216 Q_D(QLogicalDevice);
217 if (!d->m_axes.contains(t: axis)) {
218 d->m_axes.push_back(t: axis);
219
220 // Force creation in backend by setting parent
221 if (!axis->parent())
222 axis->setParent(this);
223
224 // Ensures proper bookkeeping
225 d->registerDestructionHelper(node: axis, func: &QLogicalDevice::removeAxis, d->m_axes);
226
227 d->updateNode(node: axis, property: "axis", change: Qt3DCore::PropertyValueAdded);
228 }
229}
230
231/*!
232 Remove an \a axis drom the list of axis.
233 */
234void QLogicalDevice::removeAxis(QAxis *axis)
235{
236 Q_D(QLogicalDevice);
237 if (d->m_axes.contains(t: axis)) {
238 d->updateNode(node: axis, property: "axis", change: Qt3DCore::PropertyValueRemoved);
239
240 d->m_axes.removeOne(t: axis);
241
242 // Remove bookkeeping connection
243 d->unregisterDestructionHelper(node: axis);
244 }
245}
246
247/*!
248 Returns the list of axis.
249 */
250QVector<QAxis *> QLogicalDevice::axes() const
251{
252 Q_D(const QLogicalDevice);
253 return d->m_axes;
254}
255
256Qt3DCore::QNodeCreatedChangeBasePtr QLogicalDevice::createNodeCreationChange() const
257{
258 auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QLogicalDeviceData>::create(arguments: this);
259 auto &data = creationChange->data;
260 data.actionIds = qIdsForNodes(nodes: actions());
261 data.axisIds = qIdsForNodes(nodes: axes());
262 return creationChange;
263}
264
265} // Qt3DInput
266
267QT_END_NAMESPACE
268

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