diff options
Diffstat (limited to 'src/quick/items/qquickanimatedsprite.cpp')
-rw-r--r-- | src/quick/items/qquickanimatedsprite.cpp | 130 |
1 files changed, 80 insertions, 50 deletions
diff --git a/src/quick/items/qquickanimatedsprite.cpp b/src/quick/items/qquickanimatedsprite.cpp index d22e77c8ad..3e56b0a69e 100644 --- a/src/quick/items/qquickanimatedsprite.cpp +++ b/src/quick/items/qquickanimatedsprite.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qquickanimatedsprite_p.h" #include "qquickanimatedsprite_p_p.h" @@ -263,6 +227,19 @@ QT_BEGIN_NAMESPACE */ /*! + \qmlproperty enumeration QtQuick::AnimatedSprite::finishBehavior + + The behavior when the animation finishes on its own. + + \value FinishAtInitialFrame + When the animation finishes it returns to the initial frame. + This is the default behavior. + + \value FinishAtFinalFrame + When the animation finishes it stays on the final frame. +*/ + +/*! \qmlmethod int QtQuick::AnimatedSprite::restart() Stops, then starts the sprite animation. @@ -381,6 +358,12 @@ int QQuickAnimatedSprite::currentFrame() const return d->m_curFrame; } +QQuickAnimatedSprite::FinishBehavior QQuickAnimatedSprite::finishBehavior() const +{ + Q_D(const QQuickAnimatedSprite); + return d->m_finishBehavior; +} + bool QQuickAnimatedSprite::isCurrentFrameChangedConnected() { IS_SIGNAL_CONNECTED(this, QQuickAnimatedSprite, currentFrameChanged, (int)); @@ -395,20 +378,34 @@ void QQuickAnimatedSprite::reloadImage() void QQuickAnimatedSprite::componentComplete() { - Q_D(const QQuickAnimatedSprite); + Q_D(QQuickAnimatedSprite); createEngine(); QQuickItem::componentComplete(); - if (d->m_running) + if (d->m_running) { + d->m_running = false; start(); + } } +/*! + \qmlmethod QtQuick::AnimatedSprite::start() + \since 5.15 + + Starts the sprite animation. If the animation is already running, calling + this method has no effect. + + \sa stop() +*/ void QQuickAnimatedSprite::start() { Q_D(QQuickAnimatedSprite); + if (d->m_running) + return; d->m_running = true; if (!isComponentComplete()) return; d->m_curLoop = 0; + d->m_curFrame = 0; d->m_timestamp.start(); if (d->m_spriteEngine) { d->m_spriteEngine->stop(0); @@ -420,9 +417,20 @@ void QQuickAnimatedSprite::start() maybeUpdate(); } +/*! + \qmlmethod QtQuick::AnimatedSprite::stop() + \since 5.15 + + Stops the sprite animation. If the animation is not running, calling this + method has no effect. + + \sa start() +*/ void QQuickAnimatedSprite::stop() { Q_D(QQuickAnimatedSprite); + if (!d->m_running) + return; d->m_running = false; if (!isComponentComplete()) return; @@ -453,11 +461,19 @@ void QQuickAnimatedSprite::advance(int frames) void QQuickAnimatedSprite::maybeUpdate() { QQuickItemPrivate *priv = QQuickItemPrivate::get(this); - const QLazilyAllocated<QQuickItemPrivate::ExtraData> &extraData = priv->extra; + const auto &extraData = priv->extra; if ((extraData.isAllocated() && extraData->effectRefCount > 0) || priv->effectiveVisible) update(); } +void QQuickAnimatedSprite::itemChange(ItemChange change, const ItemChangeData &value) +{ + Q_D(QQuickAnimatedSprite); + if (change == ItemVisibleHasChanged && d->m_running && !d->m_paused) + maybeUpdate(); + QQuickItem::itemChange(change, value); +} + /*! \qmlmethod int QtQuick::AnimatedSprite::pause() @@ -679,6 +695,16 @@ void QQuickAnimatedSprite::setCurrentFrame(int arg) //TODO-C: Probably only work } } +void QQuickAnimatedSprite::setFinishBehavior(FinishBehavior arg) +{ + Q_D(QQuickAnimatedSprite); + + if (d->m_finishBehavior != arg) { + d->m_finishBehavior = arg; + Q_EMIT finishBehaviorChanged(arg); + } +} + void QQuickAnimatedSprite::createEngine() { Q_D(QQuickAnimatedSprite); @@ -725,7 +751,7 @@ QSGSpriteNode* QQuickAnimatedSprite::initNode() QSGSpriteNode *node = d->sceneGraphContext()->createSpriteNode(); - d->m_sheetSize = QSize(image.size() / image.devicePixelRatioF()); + d->m_sheetSize = QSize(image.size() / image.devicePixelRatio()); node->setTexture(window()->createTextureFromImage(image)); d->m_spriteEngine->start(0); node->setTime(0.0f); @@ -813,7 +839,11 @@ void QQuickAnimatedSprite::prepareNextFrame(QSGSpriteNode *node) progress = 0; } if (d->m_loops > 0 && d->m_curLoop >= d->m_loops) { - frameAt = 0; + if (d->m_finishBehavior == FinishAtInitialFrame) + frameAt = 0; + else + frameAt = frameCount() - 1; + d->m_curFrame = frameAt; d->m_running = false; emit runningChanged(false); emit finished(); @@ -828,7 +858,7 @@ void QQuickAnimatedSprite::prepareNextFrame(QSGSpriteNode *node) maybeUpdate(); } - qreal frameCount = d->m_spriteEngine->spriteFrames(); + int frameCount = d->m_spriteEngine->spriteFrames(); bool reverse = d->m_spriteEngine->sprite()->reverse(); if (reverse) frameAt = (frameCount - 1) - frameAt; @@ -865,15 +895,15 @@ void QQuickAnimatedSprite::prepareNextFrame(QSGSpriteNode *node) x2 = x1 - w; y2 = y1; } else { - x2 = 1.0 - w; + x2 = d->m_sheetSize.width() - w; y2 = y1 - h; - if (y2 < 0.0) { + if (y2 < 0) { //the last row may not fill the entire width int maxRowFrames = d->m_sheetSize.width() / d->m_spriteEngine->spriteWidth(); if (d->m_spriteEngine->maxFrames() % maxRowFrames) x2 = ((d->m_spriteEngine->maxFrames() % maxRowFrames) - 1) * w; - y2 = 1.0 - h; + y2 = d->m_sheetSize.height() - h; } } } else { @@ -881,10 +911,10 @@ void QQuickAnimatedSprite::prepareNextFrame(QSGSpriteNode *node) x2 = x1 + w; y2 = y1; } else { - x2 = 0.0; + x2 = 0; y2 = y1 + h; - if (y2 >= 1.0) - y2 = 0.0; + if (y2 >= d->m_sheetSize.height()) + y2 = 0; } } |