summaryrefslogtreecommitdiffstats
path: root/src/input/frontend/qaxisaccumulator.cpp
blob: a7bb1444123730a6aaf669abee8331de75b19547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qaxisaccumulator.h"
#include "qaxisaccumulator_p.h"

#include <Qt3DInput/qaxis.h>
#include <Qt3DCore/qnodecreatedchange.h>

QT_BEGIN_NAMESPACE

namespace Qt3DInput {

/*!
    Constructs a new QAxisAccumulator instance with \a parent.
    \class Qt3DInput::QAxisAccumulator
    \inmodule Qt3DInput
    \inherits Qt3DCore::QComponent
    \brief QAxisAccumulator processes velocity or acceleration data from a QAxis.
    \since 5.8

    A Qt3DInput::QAxis reports the current position of an axis on an input
    device. When the axis is returned to its neutral position the value of that
    axis returns to 0. Often, it is required to have the input from an axis
    control a variable in other ways, for example treating the value from the
    Qt3DInput::QAxis as a velocity (first derivative with respect to time) or
    as an acceleration (second derivative with respect to time). This can be
    done with user code or with a Qt3DLogic::QFrameAction but those approached
    are not ideal as they add more work to the main thread and are inherently
    imperative. The Qt3DInput::QAxisAccumulator class allows for this common
    task to be performed on the Qt 3D backend and be specified in a declarative
    manner.
*/

/*!
    \qmltype AxisAccumulator
    \inqmlmodule Qt3D.Input
    \instantiates Qt3DInput::QAxisAccumulator
    \brief QML frontend for the Qt3DInput::QAxisAccumulator C++ class.
    \since 5.8

    An Axis reports the current position of an axis on an input device. When the
    axis is returned to its neutral position the value of that axis returns to 0.
    Often, it is required to have the input from an axis control a variable in
    other ways, for example treating the value from the Axis as a velocity (first
    derivative with respect to time) or as an acceleration (second derivative with
    respect to time). This can be done with user code or with a FrameAction but
    those approached are not ideal as they add more work to the main thread and
    are inherently imperative. The AxisAccumulator class allows for this common
    task to be performed on the Qt 3D backend and be specified in a declarative
    manner.
*/

/*!
    \qmlproperty int Qt3D.Input::Axis::value
    \readonly

    Holds the value accumulated from the sourceAxis.
*/

/*!
 * \enum Qt3DInput::QAxisAccumulator::SourceAxisType
 *
 * \value Velocity
 * \value Acceleration
 */

/*! \internal */
QAxisAccumulatorPrivate::QAxisAccumulatorPrivate()
    : Qt3DCore::QComponentPrivate()
    , m_sourceAxis(nullptr)
    , m_sourceAxisType(QAxisAccumulator::Velocity)
    , m_scale(1.0f)
    , m_value(0.0f)
    , m_velocity(0.0f)
{
}

/*! \internal */
void QAxisAccumulatorPrivate::setValue(float value)
{
    if (value != m_value) {
        Q_Q(QAxisAccumulator);
        m_value = value;
        const bool wasBlocked = q->blockNotifications(true);
        emit q->valueChanged(m_value);
        q->blockNotifications(wasBlocked);
    }
}

/*! \internal */
void QAxisAccumulatorPrivate::setVelocity(float velocity)
{
    if (velocity != m_velocity) {
        Q_Q(QAxisAccumulator);
        m_velocity = velocity;
        const bool wasBlocked = q->blockNotifications(true);
        emit q->velocityChanged(m_velocity);
        q->blockNotifications(wasBlocked);
    }
}

/*!
    Constructs a new QAxisAccumulator instance with parent \a parent.
 */
QAxisAccumulator::QAxisAccumulator(Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(*new QAxisAccumulatorPrivate, parent)
{
}

/*! \internal */
QAxisAccumulator::~QAxisAccumulator()
{
}

/*!
    \qmlproperty Axis Qt3D.Input::AxisAccumulator::sourceAxis

    The Axis for which the accumulator should integrate axis values.
 */

/*!
   \return QAxis for which the accumulator should integrate axis values.
 */
QAxis *QAxisAccumulator::sourceAxis() const
{
    Q_D(const QAxisAccumulator);
    return d->m_sourceAxis;
}

/*!
    \qmlproperty SourceAxisType Qt3D.Input::AxisAccumulator::sourceAxisType

    The sourceAxisType property specifies how the accumulator treats the values
    from the source axis.
 */

/*!
   \return how the accumulator treats the value of the sourceAxis.
 */
QAxisAccumulator::SourceAxisType QAxisAccumulator::sourceAxisType() const
{
    Q_D(const QAxisAccumulator);
    return d->m_sourceAxisType;
}

/*!
    \qmlproperty real Qt3D.Input::AxisAccumulator::value

    The amount to scale the axis value by when accumulating. This can be
    thought of as the maximum velocity or acceleration the axis can
    control.
 */

/*!
   \property Qt3DInput::QAxisAccumulator::value
   Returns the accumulated (integrated) value.
 */
float QAxisAccumulator::value() const
{
    Q_D(const QAxisAccumulator);
    return d->m_value;
}

/*!
    \qmlproperty real Qt3D.Input::Axis::velocity
    \readonly

    Returns the velocity. If the sourceAxisType is set to Velocity this is
    simply the value of the source axis multiplied by the scale. If the
    sourceAxisType is set to Acceleration, the velocity is integrated using the
    source axis' value as an acceleration.
*/

/*!
   Returns the velocity. If the sourceAxisType is set to Velocity this is
   simply the value of the source axis multiplied by the scale. If the
   sourceAxisType is set to Acceleration, the velocity is integrated using
   the source axis' value as an acceleration.
 */
float QAxisAccumulator::velocity() const
{
    Q_D(const QAxisAccumulator);
    return d->m_velocity;
}

/*!
    \qmlproperty real Qt3D.Input::Axis::scale

    The amount to scale the axis value by when accumulating. This can be
    thought of as the maximum velocity or acceleration the axis can
    control.
*/

/*!
    The amount to scale the axis value by when accumulating. This can be
    thought of as the maximum velocity or acceleration the axis can
    control.

   \return the amount the input axis values are scaled by.
 */
float QAxisAccumulator::scale() const
{
    Q_D(const QAxisAccumulator);
    return d->m_scale;
}

/*!
    Sets the source axis from which the accumulator should receive values from to
    \a sourceAxis. How these values are treated is controlled by the sourceAxisType
    and scale properties.
 */
void QAxisAccumulator::setSourceAxis(QAxis *sourceAxis)
{
    Q_D(QAxisAccumulator);
    if (d->m_sourceAxis == sourceAxis)
        return;

    if (d->m_sourceAxis)
        d->unregisterDestructionHelper(d->m_sourceAxis);

    if (sourceAxis && !sourceAxis->parent())
        sourceAxis->setParent(this);
    d->m_sourceAxis = sourceAxis;

    // Ensures proper bookkeeping
    if (d->m_sourceAxis)
        d->registerDestructionHelper(d->m_sourceAxis, &QAxisAccumulator::setSourceAxis, d->m_sourceAxis);

    emit sourceAxisChanged(sourceAxis);
}

/*!
    Sets how the accumulator treats the values originating from the \a sourceAxisType.
 */
void QAxisAccumulator::setSourceAxisType(QAxisAccumulator::SourceAxisType sourceAxisType)
{
    Q_D(QAxisAccumulator);
    if (d->m_sourceAxisType == sourceAxisType)
        return;

    d->m_sourceAxisType = sourceAxisType;
    emit sourceAxisTypeChanged(sourceAxisType);
}

void QAxisAccumulator::setScale(float scale)
{
    Q_D(QAxisAccumulator);
    if (d->m_scale == scale)
        return;

    d->m_scale = scale;
    emit scaleChanged(scale);
}

// TODO Unused remove in Qt6
void QAxisAccumulator::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &)
{
}

/*! \internal */
Qt3DCore::QNodeCreatedChangeBasePtr QAxisAccumulator::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QAxisAccumulatorData>::create(this);
    auto &data = creationChange->data;
    Q_D(const QAxisAccumulator);
    data.sourceAxisId = qIdForNode(d->m_sourceAxis);
    data.sourceAxisType = d->m_sourceAxisType;
    data.scale = d->m_scale;
    return creationChange;
}

} // namespace Qt3DInput

QT_END_NAMESPACE