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