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 | #include "qactioninput.h" |
40 | #include "qactioninput_p.h" |
41 | |
42 | #include <Qt3DInput/qabstractphysicaldevice.h> |
43 | #include <Qt3DInput/qabstractactioninput.h> |
44 | #include <Qt3DCore/qnodecreatedchange.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | namespace Qt3DInput { |
49 | |
50 | QActionInputPrivate::QActionInputPrivate() |
51 | : QAbstractActionInputPrivate(), |
52 | m_sourceDevice(nullptr) |
53 | { |
54 | } |
55 | |
56 | /*! |
57 | \class Qt3DInput::QActionInput |
58 | \inmodule Qt3DInput |
59 | \inherits Qt3DInput::QAbstractActionInput |
60 | \brief QActionInput stores Device and Buttons used to trigger an input event. |
61 | |
62 | \since 5.7 |
63 | */ |
64 | |
65 | /*! |
66 | \qmltype ActionInput |
67 | \inqmlmodule Qt3D.Input |
68 | \inherits QAbstractActionInput |
69 | \instantiates Qt3DInput::QActionInput |
70 | \brief QML frontend for the Qt3DInput::QActionInput C++ class. |
71 | |
72 | Links a physical device and selected buttons on it which can trigger this action. |
73 | |
74 | Each Action input can be triggered by one or many buttons on a source device |
75 | \qml |
76 | ActionInput { |
77 | sourceDevice: keyboardSourceDevice |
78 | buttons: [Qt.Key_A] |
79 | } |
80 | \endqml |
81 | \qml |
82 | ActionInput { |
83 | sourceDevice: keyboardSourceDevice |
84 | buttons: [Qt.Key_A,Qt.Key_B] |
85 | } |
86 | \endqml |
87 | \since 5.7 |
88 | */ |
89 | |
90 | /*! |
91 | Constructs a new QActionInput instance with \a parent. |
92 | */ |
93 | QActionInput::QActionInput(Qt3DCore::QNode *parent) |
94 | : Qt3DInput::QAbstractActionInput(*new QActionInputPrivate(), parent) |
95 | { |
96 | |
97 | } |
98 | |
99 | /*! \internal */ |
100 | QActionInput::~QActionInput() |
101 | { |
102 | } |
103 | |
104 | /*! |
105 | \property Qt3DInput::QActionInput::buttons |
106 | |
107 | Holds the buttons that can trigger this Action. |
108 | */ |
109 | QVector<int> QActionInput::buttons() const |
110 | { |
111 | Q_D(const QActionInput); |
112 | return d->m_buttons; |
113 | } |
114 | |
115 | /*! |
116 | \property Qt3DInput::QActionInput::sourceDevice |
117 | |
118 | The current source device of the QActionInput. |
119 | */ |
120 | /*! |
121 | \fn Qt3DInput::QActionInput::sourceDeviceChanged(QAbstractPhysicalDevice *sourceDevice) |
122 | |
123 | This signal is emitted when the source device associated with the action input is changed to \a sourceDevice. |
124 | */ |
125 | |
126 | /*! |
127 | \qmlproperty AbstractPhysicalDevice Qt3D.Input::ActionInput::sourceDevice |
128 | |
129 | The current source device of the ActionInput |
130 | */ |
131 | |
132 | /*! |
133 | \qmlsignal Qt3D.Input::ActionInput::sourceDeviceChanged() |
134 | |
135 | This signal is emitted when the source device associated with the action input is changed. |
136 | |
137 | The corresponding handler is \c onSourceDeviceChanged |
138 | */ |
139 | void QActionInput::setSourceDevice(QAbstractPhysicalDevice *sourceDevice) |
140 | { |
141 | Q_D(QActionInput); |
142 | if (d->m_sourceDevice != sourceDevice) { |
143 | |
144 | if (d->m_sourceDevice) |
145 | d->unregisterDestructionHelper(node: d->m_sourceDevice); |
146 | |
147 | // Check and set parent if needed |
148 | // to force creation in the backend |
149 | if (sourceDevice && !sourceDevice->parent()) |
150 | sourceDevice->setParent(this); |
151 | |
152 | d->m_sourceDevice = sourceDevice; |
153 | |
154 | // Ensures proper bookkeeping |
155 | if (d->m_sourceDevice) |
156 | d->registerDestructionHelper(node: sourceDevice, func: &QActionInput::setSourceDevice, d->m_sourceDevice); |
157 | |
158 | emit sourceDeviceChanged(sourceDevice); |
159 | } |
160 | } |
161 | |
162 | QAbstractPhysicalDevice *QActionInput::sourceDevice() const |
163 | { |
164 | Q_D(const QActionInput); |
165 | return d->m_sourceDevice; |
166 | } |
167 | |
168 | /*! |
169 | \fn Qt3DInput::QActionInput::buttonsChanged(const QVector<int> &buttons) |
170 | |
171 | This signal is emitted when the buttons associated with the action input is changed. |
172 | The buttons changed are \a buttons |
173 | */ |
174 | |
175 | /*! |
176 | \qmlproperty list<int> Qt3D.Input::ActionInput::buttons |
177 | |
178 | The Buttons that can trigger this Action. |
179 | */ |
180 | |
181 | /*! |
182 | \qmlsignal Qt3D.Input::ActionInput::buttonsChanged(const QVector<int> &buttons) |
183 | |
184 | This signal is emitted when the \a buttons associated with the action input is changed. |
185 | |
186 | The corresponding handler is \c onbuttonsChanged |
187 | */ |
188 | |
189 | /*! |
190 | Set the buttons to trigger the QActionInput instance to \a buttons. |
191 | */ |
192 | void QActionInput::setButtons(const QVector<int> &buttons) |
193 | { |
194 | Q_D(QActionInput); |
195 | if (buttons != d->m_buttons) { |
196 | d->m_buttons = buttons; |
197 | emit buttonsChanged(buttons); |
198 | } |
199 | } |
200 | |
201 | Qt3DCore::QNodeCreatedChangeBasePtr QActionInput::createNodeCreationChange() const |
202 | { |
203 | auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QActionInputData>::create(arguments: this); |
204 | auto &data = creationChange->data; |
205 | |
206 | Q_D(const QActionInput); |
207 | data.sourceDeviceId = qIdForNode(node: d->m_sourceDevice); |
208 | data.buttons = d->m_buttons; |
209 | |
210 | return creationChange; |
211 | } |
212 | |
213 | } // Qt3DInput |
214 | |
215 | QT_END_NAMESPACE |
216 | |