summaryrefslogtreecommitdiffstats
path: root/src/animation/frontend/qabstractclipanimator.cpp
blob: 01b0231921299bb69b80d410685f6b38717e642c (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/****************************************************************************
**
** 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:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qabstractclipanimator.h"
#include "qabstractclipanimator_p.h"
#include <Qt3DAnimation/qchannelmapper.h>
#include <Qt3DAnimation/qclock.h>

QT_BEGIN_NAMESPACE

namespace Qt3DAnimation {

QAbstractClipAnimatorPrivate::QAbstractClipAnimatorPrivate()
    : Qt3DCore::QComponentPrivate()
    , m_mapper(nullptr)
    , m_clock(nullptr)
    , m_running(false)
    , m_loops(1)
    , m_normalizedTime(0.0f)
{
}

bool QAbstractClipAnimatorPrivate::canPlay() const
{
    return true;
}

/*!
    \qmltype AbstractClipAnimator
    \instantiates Qt3DAnimation::QAbstractClipAnimator
    \inqmlmodule Qt3D.Animation
    \since 5.9

    \brief AbstractClipAnimator is the base class for types providing animation playback
    capabilities.

    Subclasses of AbstractClipAnimator can be aggregated by an Entity to
    provide animation capabilities. The animator components provide an
    interface for controlling the animation (e.g. start, stop). Each animator
    type requires some form of animation data such as an AbstractAnimationClip
    as well as a ChannelMapper which describes how the channels in the
    animation clip should be mapped onto the properties of the objects you wish
    to animate.

    The following subclasses are available:

    \list
    \li Qt3D.Animation.ClipAnimator
    \li Qt3D.Animation.BlendedClipAnimator
    \endlist
*/

/*!
    \class Qt3DAnimation::QAbstractClipAnimator
    \inherits Qt3DCore::QComponent

    \inmodule Qt3DAnimation
    \since 5.9

    \brief QAbstractClipAnimator is the base class for types providing animation playback
    capabilities.

    Subclasses of QAbstractClipAnimator can be aggregated by a QEntity to
    provide animation capabilities. The animator components provide an
    interface for controlling the animation (e.g. start, stop). Each animator
    type requires some form of animation data such as a QAbstractAnimationClip
    as well as a QChannelMapper which describes how the channels in the
    animation clip should be mapped onto the properties of the objects you wish
    to animate.

    The following subclasses are available:

    \list
    \li Qt3DAnimation::QClipAnimator
    \li Qt3DAnimation::QBlendedClipAnimator
    \endlist
*/

QAbstractClipAnimator::QAbstractClipAnimator(Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(*new QAbstractClipAnimatorPrivate, parent)
{
}

QAbstractClipAnimator::QAbstractClipAnimator(QAbstractClipAnimatorPrivate &dd, Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(dd, parent)
{
}

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

QAbstractClipAnimator::~QAbstractClipAnimator()
{
}
/*!
    \qmlproperty bool Qt3DAnimation::AbstractClipAnimator::running

    This property holds a boolean indicating whether the animation is currently running.
*/

/*!
    \property Qt3DAnimation::QAbstractClipAnimator::running

    This property holds a boolean indicating whether the animation is currently running.
*/

/*!
    Returns a boolean indicating whether the animation is currently running.
*/
bool QAbstractClipAnimator::isRunning() const
{
    Q_D(const QAbstractClipAnimator);
    return d->m_running;
}

/*!
    \qmlproperty ChannelMapper Qt3DAnimation::AbstractClipAnimator::channelMapper

    This property holds the ChannelMapper that controls how the channels in
    the animation clip map onto the properties of the target objects.
*/

/*!
    \property Qt3DAnimation::QAbstractClipAnimator::channelMapper

    This property holds the ChannelMapper that controls how the channels in
    the animation clip map onto the properties of the target objects.
*/

QChannelMapper *QAbstractClipAnimator::channelMapper() const
{
    Q_D(const QAbstractClipAnimator);
    return d->m_mapper;
}

/*!
    \qmlproperty int Qt3DAnimation::AbstractClipAnimator::loops

    This property holds the number of times the animation should play.

    By default, loops is 1: the animation will play through once and then stop.

    If set to QAbstractClipAnimator::Infinite, the animation will continuously
    repeat until it is explicitly stopped.
*/
/*!
    \enum QAbstractClipAnimator::Loops

    Holds the number of times the animation should play.

    \value Infinite
         This will repeat the loop continuously until it is explicitly
         stopped.

*/
/*!
    \property Qt3DAnimation::QAbstractClipAnimator::loops

    Holds the number of times the animation should play.

    The value is 1 by default: the animation will be played once and then stop.

    If set to QAbstractClipAnimator::Infinite, the animation will continuously
    repeat until it is explicitly stopped.
*/

/*!
    Returns the number of times the animation should play.

    The value is 1 by default: the animation will play through once and then stop.

    If set to QAbstractClipAnimator::Infinite, the animation will continuously
    repeat until it is explicitly stopped.
*/
int QAbstractClipAnimator::loopCount() const
{
    Q_D(const QAbstractClipAnimator);
    return d->m_loops;
}
/*!
    \qmlproperty Clock Qt3DAnimation::AbstractClipAnimator::clock

    The clock controls the speed with which an animation is played.
*/

/*!
    \property Qt3DAnimation::QAbstractClipAnimator::clock

    The clock controls the speed with which an animation is played.
*/
QClock *QAbstractClipAnimator::clock() const
{
    Q_D(const QAbstractClipAnimator);
    return d->m_clock;
}
/*!
    \qmlproperty real Qt3DAnimation::AbstractClipAnimator::normalizedTime

    This property holds the clips normalized time.
*/
/*!
    \property Qt3DAnimation::QAbstractClipAnimator::normalizedTime

    This property holds the clips normalized time.
*/
float QAbstractClipAnimator::normalizedTime() const
{
    Q_D(const QAbstractClipAnimator);
    return d->m_normalizedTime;
}

void QAbstractClipAnimator::setRunning(bool running)
{
    Q_D(QAbstractClipAnimator);
    if (d->m_running == running)
        return;

    if (running && !d->canPlay())
        return;

    d->m_running = running;
    emit runningChanged(running);
}

void QAbstractClipAnimator::setChannelMapper(QChannelMapper *mapping)
{
    Q_D(QAbstractClipAnimator);
    if (d->m_mapper == mapping)
        return;

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

    if (mapping && !mapping->parent())
        mapping->setParent(this);
    d->m_mapper = mapping;

    // Ensures proper bookkeeping
    if (d->m_mapper)
        d->registerDestructionHelper(d->m_mapper, &QAbstractClipAnimator::setChannelMapper, d->m_mapper);
    emit channelMapperChanged(mapping);
}

void QAbstractClipAnimator::setLoopCount(int loops)
{
    Q_D(QAbstractClipAnimator);
    if (d->m_loops == loops)
        return;

    d->m_loops = loops;
    emit loopCountChanged(loops);
}

void QAbstractClipAnimator::setClock(QClock *clock)
{
    Q_D(QAbstractClipAnimator);
    if (d->m_clock == clock)
        return;

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

    if (clock && !clock->parent())
        clock->setParent(this);
    d->m_clock = clock;

    if (d->m_clock)
        d->registerDestructionHelper(d->m_clock, &QAbstractClipAnimator::setClock, d->m_clock);
    emit clockChanged(clock);
}

void QAbstractClipAnimator::setNormalizedTime(float timeFraction)
{
    Q_D(QAbstractClipAnimator);
    const bool validTime = !(timeFraction < 0.0f) && !(timeFraction > 1.0f);
    if (!validTime) {
        qWarning("Time value %f is not valid, needs to be in the range 0.0 to 1.0", timeFraction);
        return;
    }

    if (qFuzzyCompare(d->m_normalizedTime, timeFraction))
        return;

    d->m_normalizedTime = timeFraction;
    emit normalizedTimeChanged(timeFraction);
}

/*!
    Starts the animation.
*/
void QAbstractClipAnimator::start()
{
    setRunning(true);
}

/*!
    Stops the animation.
*/
void QAbstractClipAnimator::stop()
{
    setRunning(false);
}

} // namespace Qt3DAnimation

QT_END_NAMESPACE