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#ifndef QQUICKANIMATEDSPRITE_P_H
5#define QQUICKANIMATEDSPRITE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qtquickglobal_p.h>
19
20QT_REQUIRE_CONFIG(quick_sprite);
21
22#include <QtQuick/QQuickItem>
23#include <private/qquicksprite_p.h>
24#include <QtCore/qelapsedtimer.h>
25
26QT_BEGIN_NAMESPACE
27
28class QSGContext;
29class QQuickSprite;
30class QQuickSpriteEngine;
31class QSGGeometryNode;
32class QQuickAnimatedSpritePrivate;
33class QSGSpriteNode;
34class Q_QUICK_PRIVATE_EXPORT QQuickAnimatedSprite : public QQuickItem
35{
36 Q_OBJECT
37 Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged FINAL)
38 Q_PROPERTY(bool interpolate READ interpolate WRITE setInterpolate NOTIFY interpolateChanged FINAL)
39 //###try to share similar spriteEngines for less overhead?
40 //These properties come out of QQuickSprite, since an AnimatedSprite is a renderer for a single sprite
41 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged FINAL)
42 Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged FINAL)
43 Q_PROPERTY(bool frameSync READ frameSync WRITE setFrameSync NOTIFY frameSyncChanged FINAL)
44 Q_PROPERTY(int frameCount READ frameCount WRITE setFrameCount NOTIFY frameCountChanged FINAL)
45 //If frame height or width is not specified, it is assumed to be a single long row of square frames.
46 //Otherwise, it can be multiple contiguous rows, when one row runs out the next will be used.
47 Q_PROPERTY(int frameHeight READ frameHeight WRITE setFrameHeight NOTIFY frameHeightChanged FINAL)
48 Q_PROPERTY(int frameWidth READ frameWidth WRITE setFrameWidth NOTIFY frameWidthChanged FINAL)
49 Q_PROPERTY(int frameX READ frameX WRITE setFrameX NOTIFY frameXChanged FINAL)
50 Q_PROPERTY(int frameY READ frameY WRITE setFrameY NOTIFY frameYChanged FINAL)
51 //Precedence order: frameRate, frameDuration
52 Q_PROPERTY(qreal frameRate READ frameRate WRITE setFrameRate NOTIFY frameRateChanged RESET resetFrameRate FINAL)
53 Q_PROPERTY(int frameDuration READ frameDuration WRITE setFrameDuration NOTIFY frameDurationChanged RESET resetFrameDuration FINAL)
54 //Extra Simple Sprite Stuff
55 Q_PROPERTY(int loops READ loops WRITE setLoops NOTIFY loopsChanged FINAL)
56 Q_PROPERTY(bool paused READ paused WRITE setPaused NOTIFY pausedChanged FINAL)
57 Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame NOTIFY currentFrameChanged FINAL)
58 Q_PROPERTY(FinishBehavior finishBehavior READ finishBehavior WRITE setFinishBehavior NOTIFY finishBehaviorChanged REVISION(2, 15) FINAL)
59 QML_NAMED_ELEMENT(AnimatedSprite)
60 QML_ADDED_IN_VERSION(2, 0)
61
62public:
63 explicit QQuickAnimatedSprite(QQuickItem *parent = nullptr);
64 enum LoopParameters {
65 Infinite = -1
66 };
67 Q_ENUM(LoopParameters)
68
69 enum FinishBehavior {
70 FinishAtInitialFrame,
71 FinishAtFinalFrame
72 };
73 Q_ENUM(FinishBehavior)
74
75 bool running() const;
76 bool interpolate() const;
77 QUrl source() const;
78 bool reverse() const;
79 bool frameSync() const;
80 int frameCount() const;
81 int frameHeight() const;
82 int frameWidth() const;
83 int frameX() const;
84 int frameY() const;
85 qreal frameRate() const;
86 int frameDuration() const;
87 int loops() const;
88 bool paused() const;
89 int currentFrame() const;
90 FinishBehavior finishBehavior() const;
91 void setFinishBehavior(FinishBehavior arg);
92
93Q_SIGNALS:
94
95 void pausedChanged(bool arg);
96 void runningChanged(bool arg);
97 void interpolateChanged(bool arg);
98
99 void sourceChanged(const QUrl &arg);
100 void reverseChanged(bool arg);
101 void frameSyncChanged(bool arg);
102 void frameCountChanged(int arg);
103 void frameHeightChanged(int arg);
104 void frameWidthChanged(int arg);
105 void frameXChanged(int arg);
106 void frameYChanged(int arg);
107 void frameRateChanged(qreal arg);
108 void frameDurationChanged(int arg);
109 void loopsChanged(int arg);
110 void currentFrameChanged(int arg);
111 Q_REVISION(2, 15) void finishBehaviorChanged(FinishBehavior arg);
112
113 Q_REVISION(2, 12) void finished();
114
115public Q_SLOTS:
116 void start();
117 void stop();
118 void restart() {stop(); start();}
119 void advance(int frames=1);
120 void pause();
121 void resume();
122
123 void setRunning(bool arg);
124 void setPaused(bool arg);
125 void setInterpolate(bool arg);
126 void setSource(const QUrl &arg);
127 void setReverse(bool arg);
128 void setFrameSync(bool arg);
129 void setFrameCount(int arg);
130 void setFrameHeight(int arg);
131 void setFrameWidth(int arg);
132 void setFrameX(int arg);
133 void setFrameY(int arg);
134 void setFrameRate(qreal arg);
135 void setFrameDuration(int arg);
136 void resetFrameRate();
137 void resetFrameDuration();
138 void setLoops(int arg);
139 void setCurrentFrame(int arg);
140
141private Q_SLOTS:
142 void createEngine();
143
144protected Q_SLOTS:
145 void reset();
146
147protected:
148 void componentComplete() override;
149 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
150 void itemChange(ItemChange, const ItemChangeData &) override;
151
152private:
153 void maybeUpdate();
154 bool isCurrentFrameChangedConnected();
155 void prepareNextFrame(QSGSpriteNode *node);
156 void reloadImage();
157 QSGSpriteNode* initNode();
158
159private:
160 Q_DECLARE_PRIVATE(QQuickAnimatedSprite)
161};
162
163QT_END_NAMESPACE
164
165#endif // QQUICKANIMATEDSPRITE_P_H
166

source code of qtdeclarative/src/quick/items/qquickanimatedsprite_p.h