| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 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 "qanimationgroup.h" |
| 38 | #include "Qt3DAnimation/private/qanimationgroup_p.h" |
| 39 | |
| 40 | QT_BEGIN_NAMESPACE |
| 41 | |
| 42 | namespace Qt3DAnimation { |
| 43 | |
| 44 | /*! |
| 45 | \class Qt3DAnimation::QAnimationGroup |
| 46 | \brief A class grouping animations together. |
| 47 | \inmodule Qt3DAnimation |
| 48 | \since 5.9 |
| 49 | \inherits QObject |
| 50 | |
| 51 | Qt3DAnimation::QAnimationGroup class is used to group multiple animations so that |
| 52 | they can act as one animation. The position set to the group is also set to |
| 53 | all animations in a group. The duration is the maximum of the individual animations. |
| 54 | The animations can be any supported animation type and do not have to have the same name. |
| 55 | */ |
| 56 | /*! |
| 57 | \qmltype AnimationGroup |
| 58 | \brief A type grouping animations together. |
| 59 | \inqmlmodule Qt3D.Animation |
| 60 | \since 5.9 |
| 61 | \inherits QObject |
| 62 | |
| 63 | AnimationGroup type is used to group multiple animations so that |
| 64 | they can act as one animation. The position set to the group is also set to |
| 65 | all animations in a group. The duration is the maximum of the individual animations. |
| 66 | The animations can be any supported animation type and do not have to have the same name. |
| 67 | */ |
| 68 | |
| 69 | /*! |
| 70 | \property Qt3DAnimation::QAnimationGroup::name |
| 71 | Holds the name of the animation group. |
| 72 | */ |
| 73 | /*! |
| 74 | \property Qt3DAnimation::QAnimationGroup::position |
| 75 | Holds the animation position. |
| 76 | */ |
| 77 | /*! |
| 78 | \property Qt3DAnimation::QAnimationGroup::duration |
| 79 | Holds the maximum duration of the animations in the group. |
| 80 | \readonly |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | \qmlproperty string AnimationGroup::name |
| 85 | Holds the name of the animation group. |
| 86 | */ |
| 87 | /*! |
| 88 | \qmlproperty real AnimationGroup::position |
| 89 | Holds the animation position. |
| 90 | */ |
| 91 | /*! |
| 92 | \qmlproperty real AnimationGroup::duration |
| 93 | Holds the maximum duration of the animations in the group. |
| 94 | \readonly |
| 95 | */ |
| 96 | /*! |
| 97 | \qmlproperty list<AbstractAnimation> AnimationGroup::animations |
| 98 | Holds the list of animations in the animation group. |
| 99 | */ |
| 100 | |
| 101 | QAnimationGroupPrivate::QAnimationGroupPrivate() |
| 102 | : QObjectPrivate() |
| 103 | , m_position(0.0f) |
| 104 | , m_duration(0.0f) |
| 105 | { |
| 106 | |
| 107 | } |
| 108 | |
| 109 | void QAnimationGroupPrivate::updatePosition(float position) |
| 110 | { |
| 111 | m_position = position; |
| 112 | for (QAbstractAnimation *aa : qAsConst(t&: m_animations)) |
| 113 | aa->setPosition(position); |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | Constructs an QAnimationGroup with \a parent. |
| 118 | */ |
| 119 | QAnimationGroup::QAnimationGroup(QObject *parent) |
| 120 | : QObject(*new QAnimationGroupPrivate, parent) |
| 121 | { |
| 122 | |
| 123 | } |
| 124 | |
| 125 | QString QAnimationGroup::name() const |
| 126 | { |
| 127 | Q_D(const QAnimationGroup); |
| 128 | return d->m_name; |
| 129 | } |
| 130 | |
| 131 | /*! |
| 132 | Returns the list of animations in the group. |
| 133 | */ |
| 134 | QVector<Qt3DAnimation::QAbstractAnimation *> QAnimationGroup::animationList() |
| 135 | { |
| 136 | Q_D(QAnimationGroup); |
| 137 | return d->m_animations; |
| 138 | } |
| 139 | |
| 140 | float QAnimationGroup::position() const |
| 141 | { |
| 142 | Q_D(const QAnimationGroup); |
| 143 | return d->m_position; |
| 144 | } |
| 145 | |
| 146 | float QAnimationGroup::duration() const |
| 147 | { |
| 148 | Q_D(const QAnimationGroup); |
| 149 | return d->m_duration; |
| 150 | } |
| 151 | |
| 152 | void QAnimationGroup::setName(const QString &name) |
| 153 | { |
| 154 | Q_D(QAnimationGroup); |
| 155 | if (d->m_name != name) { |
| 156 | d->m_name = name; |
| 157 | emit nameChanged(name); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | Sets the \a animations to the group. Old animations are removed. |
| 163 | */ |
| 164 | void QAnimationGroup::setAnimations(const QVector<Qt3DAnimation::QAbstractAnimation *> &animations) |
| 165 | { |
| 166 | Q_D(QAnimationGroup); |
| 167 | d->m_animations = animations; |
| 168 | d->m_duration = 0.0f; |
| 169 | for (const Qt3DAnimation::QAbstractAnimation *a : animations) |
| 170 | d->m_duration = qMax(a: d->m_duration, b: a->duration()); |
| 171 | } |
| 172 | |
| 173 | /*! |
| 174 | Adds new \a animation to the group. |
| 175 | */ |
| 176 | void QAnimationGroup::addAnimation(Qt3DAnimation::QAbstractAnimation *animation) |
| 177 | { |
| 178 | Q_D(QAnimationGroup); |
| 179 | if (!d->m_animations.contains(t: animation)) { |
| 180 | d->m_animations.push_back(t: animation); |
| 181 | d->m_duration = qMax(a: d->m_duration, b: animation->duration()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /*! |
| 186 | Removes \a animation from the group. |
| 187 | */ |
| 188 | void QAnimationGroup::removeAnimation(Qt3DAnimation::QAbstractAnimation *animation) |
| 189 | { |
| 190 | Q_D(QAnimationGroup); |
| 191 | if (!d->m_animations.contains(t: animation)) { |
| 192 | d->m_animations.removeAll(t: animation); |
| 193 | if (qFuzzyCompare(p1: d->m_duration, p2: animation->duration())) { |
| 194 | d->m_duration = 0.0f; |
| 195 | for (const Qt3DAnimation::QAbstractAnimation *a : qAsConst(t&: d->m_animations)) |
| 196 | d->m_duration = qMax(a: d->m_duration, b: a->duration()); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void QAnimationGroup::setPosition(float position) |
| 202 | { |
| 203 | Q_D(QAnimationGroup); |
| 204 | if (!qFuzzyCompare(p1: d->m_position, p2: position)) { |
| 205 | d->updatePosition(position); |
| 206 | emit positionChanged(position); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | } // Qt3DAnimation |
| 211 | |
| 212 | QT_END_NAMESPACE |
| 213 | |