1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
|
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "qquickanimatedsprite_p.h"
#include "qquickanimatedsprite_p_p.h"
#include "qquicksprite_p.h"
#include "qquickspriteengine_p.h"
#include <QtQuick/private/qsgcontext_p.h>
#include <QtQuick/private/qquickitem_p.h>
#include <private/qsgadaptationlayer_p.h>
#include <private/qqmlglobal_p.h>
#include <QtQuick/qsgnode.h>
#include <QtQuick/qsgtexturematerial.h>
#include <QtQuick/qsgtexture.h>
#include <QtQuick/qquickwindow.h>
#include <QtQml/qqmlinfo.h>
#include <QFile>
#include <cmath>
#include <qmath.h>
#include <QDebug>
QT_BEGIN_NAMESPACE
/*!
\qmltype AnimatedSprite
\instantiates QQuickAnimatedSprite
\inqmlmodule QtQuick
\inherits Item
\ingroup qtquick-visual
\brief Draws a sprite animation.
AnimatedSprite provides rendering and control over animations which are provided
as multiple frames in the same image file. You can play it at a fixed speed, at the
frame rate of your display, or manually advance and control the progress.
Consider the following sprite sheet:
\image animatedsprite-loading.png
It can be divided up into four frames:
\image animatedsprite-loading-frames.png
To play each of these frames at a speed of 500 milliseconds per frame, the
following code can be used:
\table
\header
\li Code
\li Result
\row
\li
\code
AnimatedSprite {
source: "loading.png"
frameWidth: 64
frameHeight: 64
frameCount: 4
frameDuration: 500
}
\endcode
\li
\image animatedsprite-loading-interpolated.gif
\endtable
By default, the frames are interpolated (blended together) to make the
animation appear smoother. To disable this, set \l interpolate to \c false:
\table
\header
\li Code
\li Result
\row
\li
\code
AnimatedSprite {
source: "loading.png"
frameWidth: 64
frameHeight: 64
frameCount: 4
frameDuration: 500
interpolate: false
}
\endcode
\li
\image animatedsprite-loading.gif
\endtable
To control how AnimatedSprite responds to being scaled, use the
\l {Item::}{smooth} property.
Note that unlike \l SpriteSequence, the AnimatedSprite type does not use
\l Sprite to define multiple animations, but instead encapsulates a
single animation itself.
\sa {Sprite Animations}
*/
/*!
\qmlproperty bool QtQuick::AnimatedSprite::running
Whether the sprite is animating or not.
Default is true
*/
/*!
\qmlproperty bool QtQuick::AnimatedSprite::interpolate
If true, interpolation will occur between sprite frames to make the
animation appear smoother.
Default is true.
*/
/*!
\qmlproperty qreal QtQuick::AnimatedSprite::frameRate
Frames per second to show in the animation. Values less than or equal to \c 0 are invalid.
If \c frameRate is valid, it will be used to calculate the duration of the frames.
If not, and \l frameDuration is valid, \c frameDuration will be used.
Changing this parameter will restart the animation.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameDuration
Duration of each frame of the animation in milliseconds. Values less than or equal to \c 0 are invalid.
If frameRate is valid, it will be used to calculate the duration of the frames.
If not, and \l frameDuration is valid, \c frameDuration will be used.
Changing this parameter will restart the animation.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameCount
Number of frames in this AnimatedSprite.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameHeight
Height of a single frame in this AnimatedSprite.
May be omitted if it is the only sprite in the file.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameWidth
Width of a single frame in this AnimatedSprite.
May be omitted if it is the only sprite in the file.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameX
The X coordinate in the image file of the first frame of the AnimatedSprite.
May be omitted if the first frame starts in the upper left corner of the file.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::frameY
The Y coordinate in the image file of the first frame of the AnimatedSprite.
May be omitted if the first frame starts in the upper left corner of the file.
*/
/*!
\qmlproperty url QtQuick::AnimatedSprite::source
The image source for the animation.
If frameHeight and frameWidth are not specified, it is assumed to be a single long row of square frames.
Otherwise, it can be multiple contiguous rows or rectangluar frames, when one row runs out the next will be used.
If frameX and frameY are specified, the row of frames will be taken with that x/y coordinate as the upper left corner.
*/
/*!
\qmlproperty bool QtQuick::AnimatedSprite::reverse
If \c true, the animation will be played in reverse.
Default is \c false.
*/
/*!
\qmlproperty bool QtQuick::AnimatedSprite::frameSync
If \c true, the animation will have no duration. Instead, the animation will advance
one frame each time a frame is rendered to the screen. This synchronizes it with the painting
rate as opposed to elapsed time.
If frameSync is set to true, it overrides both frameRate and frameDuration.
Default is \c false.
Changing this parameter will restart the animation.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::loops
After playing the animation this many times, the animation will automatically stop. Negative values are invalid.
If this is set to \c AnimatedSprite.Infinite the animation will not stop playing on its own.
Default is \c AnimatedSprite.Infinite
*/
/*!
\qmlproperty bool QtQuick::AnimatedSprite::paused
When paused, the current frame can be advanced manually.
Default is \c false.
*/
/*!
\qmlproperty int QtQuick::AnimatedSprite::currentFrame
When paused, the current frame can be advanced manually by setting this property or calling \l advance().
*/
/*!
\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.
*/
/*!
\qmlsignal QtQuick::AnimatedSprite::finished()
\since 5.12
This signal is emitted when the sprite has finished animating.
It is not emitted when running is set to \c false, nor for sprites whose
\l loops property is set to \c AnimatedSprite.Infinite.
*/
QQuickAnimatedSprite::QQuickAnimatedSprite(QQuickItem *parent) :
QQuickItem(*(new QQuickAnimatedSpritePrivate), parent)
{
Q_D(QQuickAnimatedSprite);
d->m_sprite = new QQuickSprite(this);
setFlag(ItemHasContents);
connect(this, SIGNAL(widthChanged()),
this, SLOT(reset()));
connect(this, SIGNAL(heightChanged()),
this, SLOT(reset()));
}
bool QQuickAnimatedSprite::running() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_running;
}
bool QQuickAnimatedSprite::interpolate() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_interpolate;
}
QUrl QQuickAnimatedSprite::source() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->source();
}
bool QQuickAnimatedSprite::reverse() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->reverse();
}
bool QQuickAnimatedSprite::frameSync() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameSync();
}
int QQuickAnimatedSprite::frameCount() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frames();
}
int QQuickAnimatedSprite::frameHeight() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameHeight();
}
int QQuickAnimatedSprite::frameWidth() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameWidth();
}
int QQuickAnimatedSprite::frameX() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameX();
}
int QQuickAnimatedSprite::frameY() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameY();
}
qreal QQuickAnimatedSprite::frameRate() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameRate();
}
int QQuickAnimatedSprite::frameDuration() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_sprite->frameDuration();
}
int QQuickAnimatedSprite::loops() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_loops;
}
bool QQuickAnimatedSprite::paused() const
{
Q_D(const QQuickAnimatedSprite);
return d->m_paused;
}
int QQuickAnimatedSprite::currentFrame() const
{
Q_D(const QQuickAnimatedSprite);
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));
}
void QQuickAnimatedSprite::reloadImage()
{
if (!isComponentComplete())
return;
createEngine();//### It's not as inefficient as it sounds, but it still sucks having to recreate the engine
}
void QQuickAnimatedSprite::componentComplete()
{
Q_D(QQuickAnimatedSprite);
createEngine();
QQuickItem::componentComplete();
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);
d->m_spriteEngine->updateSprites(0);
d->m_spriteEngine->start(0);
}
emit currentFrameChanged(0);
emit runningChanged(true);
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;
d->m_pauseOffset = 0;
emit runningChanged(false);
maybeUpdate();
}
/*!
\qmlmethod int QtQuick::AnimatedSprite::advance()
Advances the sprite animation by one frame.
*/
void QQuickAnimatedSprite::advance(int frames)
{
Q_D(QQuickAnimatedSprite);
if (!frames)
return;
//TODO-C: May not work when running - only when paused
d->m_curFrame += frames;
while (d->m_curFrame < 0)
d->m_curFrame += d->m_spriteEngine->maxFrames();
d->m_curFrame = d->m_curFrame % d->m_spriteEngine->maxFrames();
emit currentFrameChanged(d->m_curFrame);
maybeUpdate();
}
void QQuickAnimatedSprite::maybeUpdate()
{
QQuickItemPrivate *priv = QQuickItemPrivate::get(this);
const QLazilyAllocated<QQuickItemPrivate::ExtraData> &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()
Pauses the sprite animation. This does nothing if
\l paused is \c true.
\sa resume()
*/
void QQuickAnimatedSprite::pause()
{
Q_D(QQuickAnimatedSprite);
if (d->m_paused)
return;
d->m_pauseOffset = d->m_timestamp.elapsed();
d->m_paused = true;
emit pausedChanged(true);
maybeUpdate();
}
/*!
\qmlmethod int QtQuick::AnimatedSprite::resume()
Resumes the sprite animation if \l paused is \c true;
otherwise, this does nothing.
\sa pause()
*/
void QQuickAnimatedSprite::resume()
{
Q_D(QQuickAnimatedSprite);
if (!d->m_paused)
return;
d->m_pauseOffset = d->m_pauseOffset - d->m_timestamp.elapsed();
d->m_paused = false;
emit pausedChanged(false);
maybeUpdate();
}
void QQuickAnimatedSprite::setRunning(bool arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_running != arg) {
if (d->m_running)
stop();
else
start();
}
}
void QQuickAnimatedSprite::setPaused(bool arg)
{
Q_D(const QQuickAnimatedSprite);
if (d->m_paused != arg) {
if (d->m_paused)
resume();
else
pause();
}
}
void QQuickAnimatedSprite::setInterpolate(bool arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_interpolate != arg) {
d->m_interpolate = arg;
Q_EMIT interpolateChanged(arg);
}
}
void QQuickAnimatedSprite::setSource(const QUrl &arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_source != arg) {
const qreal targetDevicePixelRatio = (window() ? window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio());
d->m_sprite->setDevicePixelRatio(targetDevicePixelRatio);
d->m_sprite->setSource(arg);
Q_EMIT sourceChanged(arg);
reloadImage();
}
}
void QQuickAnimatedSprite::setReverse(bool arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_reverse != arg) {
d->m_sprite->setReverse(arg);
Q_EMIT reverseChanged(arg);
}
}
void QQuickAnimatedSprite::setFrameSync(bool arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameSync != arg) {
d->m_sprite->setFrameSync(arg);
Q_EMIT frameSyncChanged(arg);
if (d->m_running)
restart();
}
}
void QQuickAnimatedSprite::setFrameCount(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frames != arg) {
d->m_sprite->setFrameCount(arg);
Q_EMIT frameCountChanged(arg);
reloadImage();
}
}
void QQuickAnimatedSprite::setFrameHeight(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameHeight != arg) {
d->m_sprite->setFrameHeight(arg);
Q_EMIT frameHeightChanged(arg);
setImplicitHeight(frameHeight());
reloadImage();
}
}
void QQuickAnimatedSprite::setFrameWidth(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameWidth != arg) {
d->m_sprite->setFrameWidth(arg);
Q_EMIT frameWidthChanged(arg);
setImplicitWidth(frameWidth());
reloadImage();
}
}
void QQuickAnimatedSprite::setFrameX(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameX != arg) {
d->m_sprite->setFrameX(arg);
Q_EMIT frameXChanged(arg);
reloadImage();
}
}
void QQuickAnimatedSprite::setFrameY(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameY != arg) {
d->m_sprite->setFrameY(arg);
Q_EMIT frameYChanged(arg);
reloadImage();
}
}
void QQuickAnimatedSprite::setFrameRate(qreal arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameRate != arg) {
d->m_sprite->setFrameRate(arg);
Q_EMIT frameRateChanged(arg);
if (d->m_running)
restart();
}
}
void QQuickAnimatedSprite::setFrameDuration(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_sprite->m_frameDuration != arg) {
d->m_sprite->setFrameDuration(arg);
Q_EMIT frameDurationChanged(arg);
if (d->m_running)
restart();
}
}
void QQuickAnimatedSprite::resetFrameRate()
{
setFrameRate(-1.0);
}
void QQuickAnimatedSprite::resetFrameDuration()
{
setFrameDuration(-1);
}
void QQuickAnimatedSprite::setLoops(int arg)
{
Q_D(QQuickAnimatedSprite);
if (d->m_loops != arg) {
d->m_loops = arg;
Q_EMIT loopsChanged(arg);
}
}
void QQuickAnimatedSprite::setCurrentFrame(int arg) //TODO-C: Probably only works when paused
{
Q_D(QQuickAnimatedSprite);
if (d->m_curFrame != arg) {
d->m_curFrame = arg;
Q_EMIT currentFrameChanged(arg); //TODO-C Only emitted on manual advance!
update();
}
}
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);
if (d->m_spriteEngine)
delete d->m_spriteEngine;
QList<QQuickSprite*> spriteList;
spriteList << d->m_sprite;
d->m_spriteEngine = new QQuickSpriteEngine(QList<QQuickSprite*>(spriteList), this);
d->m_spriteEngine->startAssemblingImage();
reset();
}
QSGSpriteNode* QQuickAnimatedSprite::initNode()
{
Q_D(QQuickAnimatedSprite);
if (!d->m_spriteEngine) {
qmlWarning(this) << "No sprite engine...";
return nullptr;
} else if (d->m_spriteEngine->status() == QQuickPixmap::Null) {
d->m_spriteEngine->startAssemblingImage();
maybeUpdate();//Schedule another update, where we will check again
return nullptr;
} else if (d->m_spriteEngine->status() == QQuickPixmap::Loading) {
maybeUpdate();//Schedule another update, where we will check again
return nullptr;
}
QImage image = d->m_spriteEngine->assembledImage(d->sceneGraphRenderContext()->maxTextureSize()); //Engine prints errors if there are any
if (image.isNull())
return nullptr;
// If frameWidth or frameHeight are not explicitly set, frameWidth
// will be set to the width of the image divided by the number of frames,
// and frameHeight will be set to the height of the image.
// In this case, QQuickAnimatedSprite currently won't emit frameWidth/HeightChanged
// at all, so we have to do this here, as it's the only place where assembledImage()
// is called (which calculates the "implicit" frameWidth/Height.
// In addition, currently the "implicit" frameWidth/Height are only calculated once,
// even after changing to a different source.
setImplicitWidth(frameWidth());
setImplicitHeight(frameHeight());
QSGSpriteNode *node = d->sceneGraphContext()->createSpriteNode();
d->m_sheetSize = QSize(image.size() / image.devicePixelRatioF());
node->setTexture(window()->createTextureFromImage(image));
d->m_spriteEngine->start(0);
node->setTime(0.0f);
node->setSourceA(QPoint(d->m_spriteEngine->spriteX(), d->m_spriteEngine->spriteY()));
node->setSourceB(QPoint(d->m_spriteEngine->spriteX(), d->m_spriteEngine->spriteY()));
node->setSpriteSize(QSize(d->m_spriteEngine->spriteWidth(), d->m_spriteEngine->spriteHeight()));
node->setSheetSize(d->m_sheetSize);
node->setSize(QSizeF(width(), height()));
return node;
}
void QQuickAnimatedSprite::reset()
{
Q_D(QQuickAnimatedSprite);
d->m_pleaseReset = true;
maybeUpdate();
}
QSGNode *QQuickAnimatedSprite::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
Q_D(QQuickAnimatedSprite);
if (d->m_pleaseReset) {
delete oldNode;
oldNode = nullptr;
d->m_pleaseReset = false;
}
QSGSpriteNode *node = static_cast<QSGSpriteNode *>(oldNode);
if (!node)
node = initNode();
if (node)
prepareNextFrame(node);
if (d->m_running && !d->m_paused)
maybeUpdate();
return node;
}
void QQuickAnimatedSprite::prepareNextFrame(QSGSpriteNode *node)
{
Q_D(QQuickAnimatedSprite);
int timeInt = d->m_timestamp.elapsed() + d->m_pauseOffset;
qreal time = timeInt / 1000.;
int frameAt;
qreal progress = 0.0;
int lastFrame = d->m_curFrame;
if (d->m_running && !d->m_paused) {
const int nColumns = d->m_sheetSize.width() / d->m_spriteEngine->spriteWidth();
//Advance State (keeps time for psuedostates)
d->m_spriteEngine->updateSprites(timeInt);
//Advance AnimatedSprite
qreal animT = d->m_spriteEngine->spriteStart()/1000.0;
const int frameCountInRow = d->m_spriteEngine->spriteFrames();
const qreal frameDuration = d->m_spriteEngine->spriteDuration() / frameCountInRow;
if (frameDuration > 0) {
qreal frame = (time - animT)/(frameDuration / 1000.0);
bool lastLoop = d->m_loops > 0 && d->m_curLoop == d->m_loops-1;
//don't visually interpolate for the last frame of the last loop
const int max = lastLoop ? frameCountInRow - 1 : frameCountInRow;
frame = qBound(qreal(0.0), frame, qreal(max));
double intpart;
progress = std::modf(frame,&intpart);
frameAt = (int)intpart;
const int rowIndex = d->m_spriteEngine->spriteY()/frameHeight();
const int newFrame = rowIndex * nColumns + frameAt;
if (d->m_curFrame > newFrame) //went around
d->m_curLoop++;
d->m_curFrame = newFrame;
} else {
d->m_curFrame++;
if (d->m_curFrame >= d->m_spriteEngine->maxFrames()) { // maxFrames: total number of frames including all rows
d->m_curFrame = 0;
d->m_curLoop++;
}
frameAt = d->m_curFrame % nColumns;
if (frameAt == 0)
d->m_spriteEngine->advance();
progress = 0;
}
if (d->m_loops > 0 && d->m_curLoop >= d->m_loops) {
if (d->m_finishBehavior == FinishAtInitialFrame)
frameAt = 0;
else
frameAt = frameCount() - 1;
d->m_curFrame = frameAt;
d->m_running = false;
emit runningChanged(false);
emit finished();
maybeUpdate();
}
} else {
frameAt = d->m_curFrame;
}
if (d->m_curFrame != lastFrame) {
if (isCurrentFrameChangedConnected())
emit currentFrameChanged(d->m_curFrame);
maybeUpdate();
}
qreal frameCount = d->m_spriteEngine->spriteFrames();
bool reverse = d->m_spriteEngine->sprite()->reverse();
if (reverse)
frameAt = (frameCount - 1) - frameAt;
int w = d->m_spriteEngine->spriteWidth();
int h = d->m_spriteEngine->spriteHeight();
int x1;
int y1;
if (d->m_paused) {
int spriteY = d->m_spriteEngine->spriteY();
if (reverse) {
int rows = d->m_spriteEngine->maxFrames() * d->m_spriteEngine->spriteWidth() / d->m_sheetSize.width();
spriteY -= rows * d->m_spriteEngine->spriteHeight();
frameAt = (frameCount - 1) - frameAt;
}
int position = frameAt * d->m_spriteEngine->spriteWidth() + d->m_spriteEngine->spriteX();
int row = position / d->m_sheetSize.width();
x1 = (position - (row * d->m_sheetSize.width()));
y1 = (row * d->m_spriteEngine->spriteHeight() + spriteY);
} else {
x1 = d->m_spriteEngine->spriteX() + frameAt * w;
y1 = d->m_spriteEngine->spriteY();
}
//### hard-coded 0/1 work because we are the only
// images in the sprite sheet (without this we cannot assume
// where in the sheet we begin/end).
int x2;
int y2;
if (reverse) {
if (frameAt > 0) {
x2 = x1 - w;
y2 = y1;
} else {
x2 = 1.0 - w;
y2 = y1 - h;
if (y2 < 0.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;
}
}
} else {
if (frameAt < (frameCount-1)) {
x2 = x1 + w;
y2 = y1;
} else {
x2 = 0.0;
y2 = y1 + h;
if (y2 >= 1.0)
y2 = 0.0;
}
}
node->setSourceA(QPoint(x1, y1));
node->setSourceB(QPoint(x2, y2));
node->setSpriteSize(QSize(w, h));
node->setTime(d->m_interpolate ? progress : 0.0);
node->setSize(QSizeF(width(), height()));
node->setFiltering(smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
node->update();
}
QT_END_NAMESPACE
#include "moc_qquickanimatedsprite_p.cpp"
|