| 1 | // Copyright (C) 2024 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 "qsvgabstractanimation_p.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | QSvgAbstractAnimation::QSvgAbstractAnimation() |
| 9 | : m_start(0) |
| 10 | , m_duration(0) |
| 11 | , m_finished(false) |
| 12 | , m_iterationCount(0) |
| 13 | { |
| 14 | |
| 15 | } |
| 16 | |
| 17 | QSvgAbstractAnimation::~QSvgAbstractAnimation() |
| 18 | { |
| 19 | for (auto prop : m_properties) |
| 20 | delete prop; |
| 21 | } |
| 22 | |
| 23 | void QSvgAbstractAnimation::appendProperty(QSvgAbstractAnimatedProperty *property) |
| 24 | { |
| 25 | m_properties.append(t: property); |
| 26 | } |
| 27 | |
| 28 | QList<QSvgAbstractAnimatedProperty *> QSvgAbstractAnimation::properties() const |
| 29 | { |
| 30 | return m_properties; |
| 31 | } |
| 32 | |
| 33 | bool QSvgAbstractAnimation::finished() const |
| 34 | { |
| 35 | return m_finished; |
| 36 | } |
| 37 | |
| 38 | bool QSvgAbstractAnimation::isActive() const |
| 39 | { |
| 40 | return !finished(); |
| 41 | } |
| 42 | |
| 43 | void QSvgAbstractAnimation::evaluateAnimation(qreal elapsedTime) |
| 44 | { |
| 45 | if (m_duration == 0 || m_start > elapsedTime) |
| 46 | return; |
| 47 | |
| 48 | qreal fractionOfTotalTime = (elapsedTime - m_start) / m_duration; |
| 49 | |
| 50 | if (m_iterationCount >= 0 && m_iterationCount < fractionOfTotalTime) |
| 51 | m_finished = true; |
| 52 | else |
| 53 | m_finished = false; |
| 54 | |
| 55 | if (m_finished) |
| 56 | return; |
| 57 | |
| 58 | qreal fractionOfCurrentIterationTime = fractionOfTotalTime - std::trunc(x: fractionOfTotalTime); |
| 59 | |
| 60 | for (QSvgAbstractAnimatedProperty *animProperty : m_properties) { |
| 61 | const QList<qreal> keyFrames = animProperty->keyFrames(); |
| 62 | for (int i = 1; i < keyFrames.size(); i++) { |
| 63 | qreal from = keyFrames.at(i: i - 1); |
| 64 | qreal to = keyFrames.at(i); |
| 65 | if (fractionOfCurrentIterationTime >= from && fractionOfCurrentIterationTime < to) { |
| 66 | qreal currFraction = (fractionOfCurrentIterationTime - from) / (to - from); |
| 67 | animProperty->interpolate(index: i, t: currFraction); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void QSvgAbstractAnimation::setRunningTime(int startMs, int durationMs) |
| 74 | { |
| 75 | m_start = (startMs > 0) ? startMs : 0; |
| 76 | m_duration = (durationMs > 0) ? durationMs : 0; |
| 77 | } |
| 78 | |
| 79 | int QSvgAbstractAnimation::start() const |
| 80 | { |
| 81 | return m_start; |
| 82 | } |
| 83 | |
| 84 | int QSvgAbstractAnimation::duration() const |
| 85 | { |
| 86 | return m_duration; |
| 87 | } |
| 88 | |
| 89 | void QSvgAbstractAnimation::setIterationCount(int count) |
| 90 | { |
| 91 | m_iterationCount = count; |
| 92 | } |
| 93 | |
| 94 | int QSvgAbstractAnimation::iterationCount() const |
| 95 | { |
| 96 | return m_iterationCount; |
| 97 | } |
| 98 | |
| 99 | QT_END_NAMESPACE |
| 100 |
