| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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 | |
| 40 | #include "qabstractclipanimator.h" |
| 41 | #include "qabstractclipanimator_p.h" |
| 42 | #include <Qt3DAnimation/qchannelmapper.h> |
| 43 | #include <Qt3DAnimation/qclock.h> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | namespace Qt3DAnimation { |
| 48 | |
| 49 | QAbstractClipAnimatorPrivate::QAbstractClipAnimatorPrivate() |
| 50 | : Qt3DCore::QComponentPrivate() |
| 51 | , m_mapper(nullptr) |
| 52 | , m_clock(nullptr) |
| 53 | , m_running(false) |
| 54 | , m_loops(1) |
| 55 | , m_normalizedTime(0.0f) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | bool QAbstractClipAnimatorPrivate::canPlay() const |
| 60 | { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /*! |
| 65 | \qmltype AbstractClipAnimator |
| 66 | \instantiates Qt3DAnimation::QAbstractClipAnimator |
| 67 | \inqmlmodule Qt3D.Animation |
| 68 | \since 5.9 |
| 69 | |
| 70 | \brief AbstractClipAnimator is the base class for types providing animation playback |
| 71 | capabilities. |
| 72 | |
| 73 | Subclasses of AbstractClipAnimator can be aggregated by an Entity to |
| 74 | provide animation capabilities. The animator components provide an |
| 75 | interface for controlling the animation (e.g. start, stop). Each animator |
| 76 | type requires some form of animation data such as an AbstractAnimationClip |
| 77 | as well as a ChannelMapper which describes how the channels in the |
| 78 | animation clip should be mapped onto the properties of the objects you wish |
| 79 | to animate. |
| 80 | |
| 81 | The following subclasses are available: |
| 82 | |
| 83 | \list |
| 84 | \li Qt3D.Animation.ClipAnimator |
| 85 | \li Qt3D.Animation.BlendedClipAnimator |
| 86 | \endlist |
| 87 | */ |
| 88 | |
| 89 | /*! |
| 90 | \class Qt3DAnimation::QAbstractClipAnimator |
| 91 | \inherits Qt3DCore::QComponent |
| 92 | |
| 93 | \inmodule Qt3DAnimation |
| 94 | \since 5.9 |
| 95 | |
| 96 | \brief QAbstractClipAnimator is the base class for types providing animation playback |
| 97 | capabilities. |
| 98 | |
| 99 | Subclasses of QAbstractClipAnimator can be aggregated by a QEntity to |
| 100 | provide animation capabilities. The animator components provide an |
| 101 | interface for controlling the animation (e.g. start, stop). Each animator |
| 102 | type requires some form of animation data such as a QAbstractAnimationClip |
| 103 | as well as a QChannelMapper which describes how the channels in the |
| 104 | animation clip should be mapped onto the properties of the objects you wish |
| 105 | to animate. |
| 106 | |
| 107 | The following subclasses are available: |
| 108 | |
| 109 | \list |
| 110 | \li Qt3DAnimation::QClipAnimator |
| 111 | \li Qt3DAnimation::QBlendedClipAnimator |
| 112 | \endlist |
| 113 | */ |
| 114 | |
| 115 | QAbstractClipAnimator::QAbstractClipAnimator(Qt3DCore::QNode *parent) |
| 116 | : Qt3DCore::QComponent(*new QAbstractClipAnimatorPrivate, parent) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | QAbstractClipAnimator::QAbstractClipAnimator(QAbstractClipAnimatorPrivate &dd, Qt3DCore::QNode *parent) |
| 121 | : Qt3DCore::QComponent(dd, parent) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | // TODO Unused remove in Qt6 |
| 126 | void QAbstractClipAnimator::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | QAbstractClipAnimator::~QAbstractClipAnimator() |
| 131 | { |
| 132 | } |
| 133 | /*! |
| 134 | \qmlproperty bool Qt3DAnimation::AbstractClipAnimator::running |
| 135 | |
| 136 | This property holds a boolean indicating whether the animation is currently running. |
| 137 | */ |
| 138 | |
| 139 | /*! |
| 140 | \property Qt3DAnimation::QAbstractClipAnimator::running |
| 141 | |
| 142 | This property holds a boolean indicating whether the animation is currently running. |
| 143 | */ |
| 144 | |
| 145 | /*! |
| 146 | Returns a boolean indicating whether the animation is currently running. |
| 147 | */ |
| 148 | bool QAbstractClipAnimator::isRunning() const |
| 149 | { |
| 150 | Q_D(const QAbstractClipAnimator); |
| 151 | return d->m_running; |
| 152 | } |
| 153 | |
| 154 | /*! |
| 155 | \qmlproperty ChannelMapper Qt3DAnimation::AbstractClipAnimator::channelMapper |
| 156 | |
| 157 | This property holds the ChannelMapper that controls how the channels in |
| 158 | the animation clip map onto the properties of the target objects. |
| 159 | */ |
| 160 | |
| 161 | /*! |
| 162 | \property Qt3DAnimation::QAbstractClipAnimator::channelMapper |
| 163 | |
| 164 | This property holds the ChannelMapper that controls how the channels in |
| 165 | the animation clip map onto the properties of the target objects. |
| 166 | */ |
| 167 | |
| 168 | QChannelMapper *QAbstractClipAnimator::channelMapper() const |
| 169 | { |
| 170 | Q_D(const QAbstractClipAnimator); |
| 171 | return d->m_mapper; |
| 172 | } |
| 173 | |
| 174 | /*! |
| 175 | \qmlproperty int Qt3DAnimation::AbstractClipAnimator::loops |
| 176 | |
| 177 | This property holds the number of times the animation should play. |
| 178 | |
| 179 | By default, loops is 1: the animation will play through once and then stop. |
| 180 | |
| 181 | If set to QAbstractClipAnimator::Infinite, the animation will continuously |
| 182 | repeat until it is explicitly stopped. |
| 183 | */ |
| 184 | /*! |
| 185 | \enum QAbstractClipAnimator::Loops |
| 186 | |
| 187 | Holds the number of times the animation should play. |
| 188 | |
| 189 | \value Infinite |
| 190 | This will repeat the loop continuously until it is explicitly |
| 191 | stopped. |
| 192 | |
| 193 | */ |
| 194 | /*! |
| 195 | \property Qt3DAnimation::QAbstractClipAnimator::loops |
| 196 | |
| 197 | Holds the number of times the animation should play. |
| 198 | |
| 199 | The value is 1 by default: the animation will be played once and then stop. |
| 200 | |
| 201 | If set to QAbstractClipAnimator::Infinite, the animation will continuously |
| 202 | repeat until it is explicitly stopped. |
| 203 | */ |
| 204 | |
| 205 | /*! |
| 206 | Returns the number of times the animation should play. |
| 207 | |
| 208 | The value is 1 by default: the animation will play through once and then stop. |
| 209 | |
| 210 | If set to QAbstractClipAnimator::Infinite, the animation will continuously |
| 211 | repeat until it is explicitly stopped. |
| 212 | */ |
| 213 | int QAbstractClipAnimator::loopCount() const |
| 214 | { |
| 215 | Q_D(const QAbstractClipAnimator); |
| 216 | return d->m_loops; |
| 217 | } |
| 218 | /*! |
| 219 | \qmlproperty Clock Qt3DAnimation::AbstractClipAnimator::clock |
| 220 | |
| 221 | The clock controls the speed with which an animation is played. |
| 222 | */ |
| 223 | |
| 224 | /*! |
| 225 | \property Qt3DAnimation::QAbstractClipAnimator::clock |
| 226 | |
| 227 | The clock controls the speed with which an animation is played. |
| 228 | */ |
| 229 | QClock *QAbstractClipAnimator::clock() const |
| 230 | { |
| 231 | Q_D(const QAbstractClipAnimator); |
| 232 | return d->m_clock; |
| 233 | } |
| 234 | /*! |
| 235 | \qmlproperty real Qt3DAnimation::AbstractClipAnimator::normalizedTime |
| 236 | |
| 237 | This property holds the clips normalized time. |
| 238 | */ |
| 239 | /*! |
| 240 | \property Qt3DAnimation::QAbstractClipAnimator::normalizedTime |
| 241 | |
| 242 | This property holds the clips normalized time. |
| 243 | */ |
| 244 | float QAbstractClipAnimator::normalizedTime() const |
| 245 | { |
| 246 | Q_D(const QAbstractClipAnimator); |
| 247 | return d->m_normalizedTime; |
| 248 | } |
| 249 | |
| 250 | void QAbstractClipAnimator::setRunning(bool running) |
| 251 | { |
| 252 | Q_D(QAbstractClipAnimator); |
| 253 | if (d->m_running == running) |
| 254 | return; |
| 255 | |
| 256 | if (running && !d->canPlay()) |
| 257 | return; |
| 258 | |
| 259 | d->m_running = running; |
| 260 | emit runningChanged(running); |
| 261 | } |
| 262 | |
| 263 | void QAbstractClipAnimator::setChannelMapper(QChannelMapper *mapping) |
| 264 | { |
| 265 | Q_D(QAbstractClipAnimator); |
| 266 | if (d->m_mapper == mapping) |
| 267 | return; |
| 268 | |
| 269 | if (d->m_mapper) |
| 270 | d->unregisterDestructionHelper(node: d->m_mapper); |
| 271 | |
| 272 | if (mapping && !mapping->parent()) |
| 273 | mapping->setParent(this); |
| 274 | d->m_mapper = mapping; |
| 275 | |
| 276 | // Ensures proper bookkeeping |
| 277 | if (d->m_mapper) |
| 278 | d->registerDestructionHelper(node: d->m_mapper, func: &QAbstractClipAnimator::setChannelMapper, d->m_mapper); |
| 279 | emit channelMapperChanged(channelMapper: mapping); |
| 280 | } |
| 281 | |
| 282 | void QAbstractClipAnimator::setLoopCount(int loops) |
| 283 | { |
| 284 | Q_D(QAbstractClipAnimator); |
| 285 | if (d->m_loops == loops) |
| 286 | return; |
| 287 | |
| 288 | d->m_loops = loops; |
| 289 | emit loopCountChanged(loops); |
| 290 | } |
| 291 | |
| 292 | void QAbstractClipAnimator::setClock(QClock *clock) |
| 293 | { |
| 294 | Q_D(QAbstractClipAnimator); |
| 295 | if (d->m_clock == clock) |
| 296 | return; |
| 297 | |
| 298 | if (d->m_clock) |
| 299 | d->unregisterDestructionHelper(node: d->m_clock); |
| 300 | |
| 301 | if (clock && !clock->parent()) |
| 302 | clock->setParent(this); |
| 303 | d->m_clock = clock; |
| 304 | |
| 305 | if (d->m_clock) |
| 306 | d->registerDestructionHelper(node: d->m_clock, func: &QAbstractClipAnimator::setClock, d->m_clock); |
| 307 | emit clockChanged(clock); |
| 308 | } |
| 309 | |
| 310 | void QAbstractClipAnimator::setNormalizedTime(float timeFraction) |
| 311 | { |
| 312 | Q_D(QAbstractClipAnimator); |
| 313 | const bool validTime = !(timeFraction < 0.0f) && !(timeFraction > 1.0f); |
| 314 | if (!validTime) { |
| 315 | qWarning(msg: "Time value %f is not valid, needs to be in the range 0.0 to 1.0" , timeFraction); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | if (qFuzzyCompare(p1: d->m_normalizedTime, p2: timeFraction)) |
| 320 | return; |
| 321 | |
| 322 | d->m_normalizedTime = timeFraction; |
| 323 | emit normalizedTimeChanged(index: timeFraction); |
| 324 | } |
| 325 | |
| 326 | /*! |
| 327 | Starts the animation. |
| 328 | */ |
| 329 | void QAbstractClipAnimator::start() |
| 330 | { |
| 331 | setRunning(true); |
| 332 | } |
| 333 | |
| 334 | /*! |
| 335 | Stops the animation. |
| 336 | */ |
| 337 | void QAbstractClipAnimator::stop() |
| 338 | { |
| 339 | setRunning(false); |
| 340 | } |
| 341 | |
| 342 | } // namespace Qt3DAnimation |
| 343 | |
| 344 | QT_END_NAMESPACE |
| 345 | |