1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
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 http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qabstractanimationclip.h" |
38 | #include "qabstractanimationclip_p.h" |
39 | #include <Qt3DCore/qpropertyupdatedchange.h> |
40 | |
41 | QT_BEGIN_NAMESPACE |
42 | |
43 | namespace Qt3DAnimation { |
44 | |
45 | QAbstractAnimationClipPrivate::QAbstractAnimationClipPrivate() |
46 | : Qt3DCore::QNodePrivate() |
47 | , m_duration(0.0f) |
48 | { |
49 | } |
50 | |
51 | void QAbstractAnimationClipPrivate::setDuration(float duration) |
52 | { |
53 | if (qFuzzyCompare(duration, m_duration)) |
54 | return; |
55 | |
56 | Q_Q(QAbstractAnimationClip); |
57 | bool wasBlocked = q->blockNotifications(true); |
58 | m_duration = duration; |
59 | emit q->durationChanged(duration); |
60 | q->blockNotifications(wasBlocked); |
61 | } |
62 | |
63 | /*! |
64 | \class Qt3DAnimation::QAbstractAnimationClip |
65 | \inherits Qt3DCore::QNode |
66 | |
67 | \inmodule Qt3DAnimation |
68 | \since 5.9 |
69 | |
70 | \brief QAbstractAnimationClip is the base class for types providing key frame animation data. |
71 | |
72 | To utilise the key frame animation framework in the Qt 3D Animation module |
73 | the animator component in use needs to be provided with the key frame animation data. The |
74 | animation data is provided by one of the concrete subclasses of QAbstractAnimationClip: |
75 | |
76 | \list |
77 | \li Qt3DAnimation::QAnimationClip |
78 | \li Qt3DAnimation::QAnimationClipLoader |
79 | \endlist |
80 | |
81 | QAnimationClip should be used when you want to create the animation data |
82 | programmatically within your application. The actual data is set with a |
83 | QAnimationClipData value type. |
84 | |
85 | If you are loading baked animation data from a file, e.g. as created by an |
86 | artist, then use the QAnimationClipLoader class and set its \c source property. |
87 | |
88 | Once the animation clip has been populated with data using the above |
89 | methods, the read-only duration property will be updated by the Qt 3D Animation |
90 | backend. |
91 | |
92 | The typical usage of animation clips is: |
93 | |
94 | \code |
95 | auto animator = new QClipAnimator(); |
96 | auto clip = new QAnimationClipLoader(); |
97 | clip->setSource(QUrl::fromLocalFile("bounce.json")); |
98 | animator->setClip(clip); |
99 | animator->setChannelMapper(...); |
100 | animator->setRunning(true); |
101 | \endcode |
102 | |
103 | Animation clips are also used as the leaf node values in animation blend trees: |
104 | |
105 | \code |
106 | // Create leaf nodes of blend tree |
107 | auto slideClipValue = new QClipBlendValue( |
108 | new QAnimationClipLoader(QUrl::fromLocalFile("slide.json"))); |
109 | auto bounceClipValue = new QClipBlendValue( |
110 | new QAnimationClipLoader(QUrl::fromLocalFile("bounce.json"))); |
111 | |
112 | // Create blend tree inner node |
113 | auto additiveNode = new QAdditiveClipBlend(); |
114 | additiveNode->setBaseClip(slideClipValue); |
115 | additiveNode->setAdditiveClip(bounceClipValue); |
116 | additiveNode->setAdditiveFactor(0.5f); |
117 | |
118 | // Run the animator |
119 | auto animator = new QBlendedClipAnimator(); |
120 | animator->setBlendTree(additiveNode); |
121 | animator->setChannelMapper(...); |
122 | animator->setRunning(true); |
123 | \endcode |
124 | |
125 | \sa QAnimationClip, QAnimationClipLoader |
126 | */ |
127 | |
128 | /*! |
129 | \internal |
130 | */ |
131 | QAbstractAnimationClip::QAbstractAnimationClip(QAbstractAnimationClipPrivate &dd, |
132 | Qt3DCore::QNode *parent) |
133 | : Qt3DCore::QNode(dd, parent) |
134 | { |
135 | } |
136 | |
137 | /*! |
138 | Destroys this animation clip. |
139 | */ |
140 | QAbstractAnimationClip::~QAbstractAnimationClip() |
141 | { |
142 | } |
143 | |
144 | /*! |
145 | \property QAbstractAnimationClip::duration |
146 | |
147 | Holds the duration of the animation clip in seconds. Gets updated once the |
148 | animation data is provided to Qt 3D using one of the concrete subclasses. |
149 | */ |
150 | float QAbstractAnimationClip::duration() const |
151 | { |
152 | Q_D(const QAbstractAnimationClip); |
153 | return d->m_duration; |
154 | } |
155 | |
156 | /*! |
157 | \internal |
158 | */ |
159 | void QAbstractAnimationClip::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) |
160 | { |
161 | Q_D(QAbstractAnimationClip); |
162 | if (change->type() == Qt3DCore::PropertyUpdated) { |
163 | Qt3DCore::QPropertyUpdatedChangePtr e = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(change); |
164 | if (e->propertyName() == QByteArrayLiteral("duration" )) |
165 | d->setDuration(e->value().toFloat()); |
166 | } |
167 | } |
168 | |
169 | } // namespace Qt3DAnimation |
170 | |
171 | QT_END_NAMESPACE |
172 | |