| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 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 "qanimationgroup.h" |
| 5 | #include "Qt3DAnimation/private/qanimationgroup_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | namespace Qt3DAnimation { |
| 10 | |
| 11 | /*! |
| 12 | \class Qt3DAnimation::QAnimationGroup |
| 13 | \brief A class grouping animations together. |
| 14 | \inmodule Qt3DAnimation |
| 15 | \since 5.9 |
| 16 | \inherits QObject |
| 17 | |
| 18 | Qt3DAnimation::QAnimationGroup class is used to group multiple animations so that |
| 19 | they can act as one animation. The position set to the group is also set to |
| 20 | all animations in a group. The duration is the maximum of the individual animations. |
| 21 | The animations can be any supported animation type and do not have to have the same name. |
| 22 | */ |
| 23 | /*! |
| 24 | \qmltype AnimationGroup |
| 25 | \brief A type grouping animations together. |
| 26 | \inqmlmodule Qt3D.Animation |
| 27 | \since 5.9 |
| 28 | |
| 29 | AnimationGroup type is used to group multiple animations so that |
| 30 | they can act as one animation. The position set to the group is also set to |
| 31 | all animations in a group. The duration is the maximum of the individual animations. |
| 32 | The animations can be any supported animation type and do not have to have the same name. |
| 33 | */ |
| 34 | |
| 35 | /*! |
| 36 | \property Qt3DAnimation::QAnimationGroup::name |
| 37 | Holds the name of the animation group. |
| 38 | */ |
| 39 | /*! |
| 40 | \property Qt3DAnimation::QAnimationGroup::position |
| 41 | Holds the animation position. |
| 42 | */ |
| 43 | /*! |
| 44 | \property Qt3DAnimation::QAnimationGroup::duration |
| 45 | Holds the maximum duration of the animations in the group. |
| 46 | \readonly |
| 47 | */ |
| 48 | |
| 49 | /*! |
| 50 | \qmlproperty string AnimationGroup::name |
| 51 | Holds the name of the animation group. |
| 52 | */ |
| 53 | /*! |
| 54 | \qmlproperty real AnimationGroup::position |
| 55 | Holds the animation position. |
| 56 | */ |
| 57 | /*! |
| 58 | \qmlproperty real AnimationGroup::duration |
| 59 | Holds the maximum duration of the animations in the group. |
| 60 | \readonly |
| 61 | */ |
| 62 | /*! |
| 63 | \qmlproperty list<AbstractAnimation> AnimationGroup::animations |
| 64 | Holds the list of animations in the animation group. |
| 65 | */ |
| 66 | |
| 67 | QAnimationGroupPrivate::QAnimationGroupPrivate() |
| 68 | : QObjectPrivate() |
| 69 | , m_position(0.0f) |
| 70 | , m_duration(0.0f) |
| 71 | { |
| 72 | |
| 73 | } |
| 74 | |
| 75 | void QAnimationGroupPrivate::updatePosition(float position) |
| 76 | { |
| 77 | m_position = position; |
| 78 | for (QAbstractAnimation *aa : std::as_const(t&: m_animations)) |
| 79 | aa->setPosition(position); |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | Constructs an QAnimationGroup with \a parent. |
| 84 | */ |
| 85 | QAnimationGroup::QAnimationGroup(QObject *parent) |
| 86 | : QObject(*new QAnimationGroupPrivate, parent) |
| 87 | { |
| 88 | |
| 89 | } |
| 90 | |
| 91 | QString QAnimationGroup::name() const |
| 92 | { |
| 93 | Q_D(const QAnimationGroup); |
| 94 | return d->m_name; |
| 95 | } |
| 96 | |
| 97 | /*! |
| 98 | Returns the list of animations in the group. |
| 99 | */ |
| 100 | QList<Qt3DAnimation::QAbstractAnimation *> QAnimationGroup::animationList() |
| 101 | { |
| 102 | Q_D(QAnimationGroup); |
| 103 | return d->m_animations; |
| 104 | } |
| 105 | |
| 106 | float QAnimationGroup::position() const |
| 107 | { |
| 108 | Q_D(const QAnimationGroup); |
| 109 | return d->m_position; |
| 110 | } |
| 111 | |
| 112 | float QAnimationGroup::duration() const |
| 113 | { |
| 114 | Q_D(const QAnimationGroup); |
| 115 | return d->m_duration; |
| 116 | } |
| 117 | |
| 118 | void QAnimationGroup::setName(const QString &name) |
| 119 | { |
| 120 | Q_D(QAnimationGroup); |
| 121 | if (d->m_name != name) { |
| 122 | d->m_name = name; |
| 123 | emit nameChanged(name); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /*! |
| 128 | Sets the \a animations to the group. Old animations are removed. |
| 129 | */ |
| 130 | void QAnimationGroup::setAnimations(const QList<Qt3DAnimation::QAbstractAnimation *> &animations) |
| 131 | { |
| 132 | Q_D(QAnimationGroup); |
| 133 | d->m_animations = animations; |
| 134 | d->m_duration = 0.0f; |
| 135 | for (const Qt3DAnimation::QAbstractAnimation *a : animations) |
| 136 | d->m_duration = qMax(a: d->m_duration, b: a->duration()); |
| 137 | } |
| 138 | |
| 139 | /*! |
| 140 | Adds new \a animation to the group. |
| 141 | */ |
| 142 | void QAnimationGroup::addAnimation(Qt3DAnimation::QAbstractAnimation *animation) |
| 143 | { |
| 144 | Q_D(QAnimationGroup); |
| 145 | if (!d->m_animations.contains(t: animation)) { |
| 146 | d->m_animations.push_back(t: animation); |
| 147 | d->m_duration = qMax(a: d->m_duration, b: animation->duration()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /*! |
| 152 | Removes \a animation from the group. |
| 153 | */ |
| 154 | void QAnimationGroup::removeAnimation(Qt3DAnimation::QAbstractAnimation *animation) |
| 155 | { |
| 156 | Q_D(QAnimationGroup); |
| 157 | if (!d->m_animations.contains(t: animation)) { |
| 158 | d->m_animations.removeAll(t: animation); |
| 159 | if (qFuzzyCompare(p1: d->m_duration, p2: animation->duration())) { |
| 160 | d->m_duration = 0.0f; |
| 161 | for (const Qt3DAnimation::QAbstractAnimation *a : std::as_const(t&: d->m_animations)) |
| 162 | d->m_duration = qMax(a: d->m_duration, b: a->duration()); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void QAnimationGroup::setPosition(float position) |
| 168 | { |
| 169 | Q_D(QAnimationGroup); |
| 170 | if (!qFuzzyCompare(p1: d->m_position, p2: position)) { |
| 171 | d->updatePosition(position); |
| 172 | emit positionChanged(position); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } // Qt3DAnimation |
| 177 | |
| 178 | QT_END_NAMESPACE |
| 179 | |
| 180 | #include "moc_qanimationgroup.cpp" |
| 181 |
