summaryrefslogtreecommitdiffstats
path: root/src/animation/frontend/qclipanimator.cpp
blob: 698d8a82f2cf49f561ce28631e03cf2f23f9817e (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
/****************************************************************************
**
** 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 "qclipanimator.h"
#include "qclipanimator_p.h"
#include <Qt3DAnimation/qabstractanimationclip.h>
#include <Qt3DAnimation/qchannelmapper.h>
#include <Qt3DAnimation/qclock.h>

QT_BEGIN_NAMESPACE

namespace Qt3DAnimation {

QClipAnimatorPrivate::QClipAnimatorPrivate()
    : Qt3DAnimation::QAbstractClipAnimatorPrivate()
    , m_clip(nullptr)
{
}

bool QClipAnimatorPrivate::canPlay() const
{
    if (m_clip && m_mapper)
        return true;

    qWarning("ClipAnimators need a clip and a mapper to be played");
    return false;
}

/*!
    \qmltype ClipAnimator
    \instantiates Qt3DAnimation::QClipAnimator
    \inqmlmodule Qt3D.Animation
    \inherits AbstractClipAnimator
    \since 5.9

    \brief ClipAnimator is a component providing simple animation playback capabilities.

    An instance of ClipAnimator can be aggregated by an Entity to add the ability to play back
    animation clips and to apply the calculated animation values to properties of QObjects.

    The animation key frame data is provided via the clip property. This can be created
    programmatically with AnimationClip or loaded from file with AnimationClipLoader.

    In order to apply the values played back from the channels of data in the animation clip, the
    clip animator needs to have a ChannelMapper object assigned to the channelMapper property.

    The properties for controlling the animator are provided by the AbstractClipAnimator base
    class.

    \sa {Qt3DAnimation::QAbstractClipAnimator}{AbstractClipAnimator}, {Qt3DAnimation::QAbstractAnimationClip}{AbstractAnimationClip}, {Qt3DAnimation::QChannelMapper}{ChannelMapper}, {Qt3DAnimation::QBlendedClipAnimator}{BlendedClipAnimator}
*/

/*!
    \class Qt3DAnimation::QClipAnimator
    \inherits Qt3DAnimation::QAbstractClipAnimator

    \inmodule Qt3DAnimation
    \since 5.9

    \brief QClipAnimator is a component providing simple animation playback capabilities.

    An instance of QClipAnimator can be aggregated by a QEntity to add the ability to play back
    animation clips and to apply the calculated animation values to properties of QObjects.

    The animation key frame data is provided via the clip property. This can be created
    programmatically with QAnimationClip or loaded from file with QAnimationClipLoader.

    In order to apply the values played back from the channels of data in the animation clip, the
    clip animator needs to have a QChannelMapper object assigned to the channelMapper property.

    The properties for controlling the animator are provided by the QAbstractClipAnimator base
    class.

    \sa Qt3DAnimation::QAbstractClipAnimator, Qt3DAnimation::QAbstractAnimationClip,
        Qt3DAnimation::QChannelMapper, Qt3DAnimation::QBlendedClipAnimator
*/

QClipAnimator::QClipAnimator(Qt3DCore::QNode *parent)
    : Qt3DAnimation::QAbstractClipAnimator(*new QClipAnimatorPrivate, parent)
{
}

/*! \internal */
QClipAnimator::QClipAnimator(QClipAnimatorPrivate &dd, Qt3DCore::QNode *parent)
    : Qt3DAnimation::QAbstractClipAnimator(dd, parent)
{
}

QClipAnimator::~QClipAnimator()
{
}

/*!
    \qmlproperty var Qt3D.Animation::ClipAnimator::clip

    This property holds the animation clip which contains the key frame data to be played back.
    The key frame data can be specified in either an AnimationClip or AnimationClipLoader.
*/
/*!
    \property QClipAnimator::clip

    This property holds the animation clip which contains the key frame data to be played back.
    The key frame data can be specified in either a QAnimationClip or QAnimationClipLoader.
*/
QAbstractAnimationClip *QClipAnimator::clip() const
{
    Q_D(const QClipAnimator);
    return d->m_clip;
}

void QClipAnimator::setClip(QAbstractAnimationClip *clip)
{
    Q_D(QClipAnimator);
    if (d->m_clip == clip)
        return;

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

    if (clip && !clip->parent())
        clip->setParent(this);
    d->m_clip = clip;

    // Ensures proper bookkeeping
    if (d->m_clip)
        d->registerDestructionHelper(d->m_clip, &QClipAnimator::setClip, d->m_clip);
    emit clipChanged(clip);
}

/*! \internal */
Qt3DCore::QNodeCreatedChangeBasePtr QClipAnimator::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QClipAnimatorData>::create(this);
    auto &data = creationChange->data;
    Q_D(const QClipAnimator);
    data.clipId = Qt3DCore::qIdForNode(d->m_clip);
    data.mapperId = Qt3DCore::qIdForNode(d->m_mapper);
    data.clockId = Qt3DCore::qIdForNode(d->m_clock);
    data.running = d->m_running;
    data.loops = d->m_loops;
    data.normalizedTime = d->m_normalizedTime;
    return creationChange;
}

} // namespace Qt3DAnimation

QT_END_NAMESPACE