1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQUICKANIMATEDSPRITE_P_H |
41 | #define QQUICKANIMATEDSPRITE_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <private/qtquickglobal_p.h> |
55 | |
56 | QT_REQUIRE_CONFIG(quick_sprite); |
57 | |
58 | #include <QtQuick/QQuickItem> |
59 | #include <private/qquicksprite_p.h> |
60 | #include <QtCore/qelapsedtimer.h> |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | class QSGContext; |
65 | class QQuickSprite; |
66 | class QQuickSpriteEngine; |
67 | class QSGGeometryNode; |
68 | class QQuickAnimatedSpriteMaterial; |
69 | class QQuickAnimatedSpritePrivate; |
70 | class QSGSpriteNode; |
71 | class Q_AUTOTEST_EXPORT QQuickAnimatedSprite : public QQuickItem |
72 | { |
73 | Q_OBJECT |
74 | Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged) |
75 | Q_PROPERTY(bool interpolate READ interpolate WRITE setInterpolate NOTIFY interpolateChanged) |
76 | //###try to share similar spriteEngines for less overhead? |
77 | //These properties come out of QQuickSprite, since an AnimatedSprite is a renderer for a single sprite |
78 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
79 | Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged) |
80 | Q_PROPERTY(bool frameSync READ frameSync WRITE setFrameSync NOTIFY frameSyncChanged) |
81 | Q_PROPERTY(int frameCount READ frameCount WRITE setFrameCount NOTIFY frameCountChanged) |
82 | //If frame height or width is not specified, it is assumed to be a single long row of square frames. |
83 | //Otherwise, it can be multiple contiguous rows, when one row runs out the next will be used. |
84 | Q_PROPERTY(int frameHeight READ frameHeight WRITE setFrameHeight NOTIFY frameHeightChanged) |
85 | Q_PROPERTY(int frameWidth READ frameWidth WRITE setFrameWidth NOTIFY frameWidthChanged) |
86 | Q_PROPERTY(int frameX READ frameX WRITE setFrameX NOTIFY frameXChanged) |
87 | Q_PROPERTY(int frameY READ frameY WRITE setFrameY NOTIFY frameYChanged) |
88 | //Precedence order: frameRate, frameDuration |
89 | Q_PROPERTY(qreal frameRate READ frameRate WRITE setFrameRate NOTIFY frameRateChanged RESET resetFrameRate) |
90 | Q_PROPERTY(int frameDuration READ frameDuration WRITE setFrameDuration NOTIFY frameDurationChanged RESET resetFrameDuration) |
91 | //Extra Simple Sprite Stuff |
92 | Q_PROPERTY(int loops READ loops WRITE setLoops NOTIFY loopsChanged) |
93 | Q_PROPERTY(bool paused READ paused WRITE setPaused NOTIFY pausedChanged) |
94 | Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame NOTIFY currentFrameChanged) |
95 | Q_PROPERTY(FinishBehavior finishBehavior READ finishBehavior WRITE setFinishBehavior NOTIFY finishBehaviorChanged REVISION 15) |
96 | QML_NAMED_ELEMENT(AnimatedSprite) |
97 | |
98 | public: |
99 | explicit QQuickAnimatedSprite(QQuickItem *parent = nullptr); |
100 | enum LoopParameters { |
101 | Infinite = -1 |
102 | }; |
103 | Q_ENUM(LoopParameters) |
104 | |
105 | enum FinishBehavior { |
106 | FinishAtInitialFrame, |
107 | FinishAtFinalFrame |
108 | }; |
109 | Q_ENUM(FinishBehavior) |
110 | |
111 | bool running() const; |
112 | bool interpolate() const; |
113 | QUrl source() const; |
114 | bool reverse() const; |
115 | bool frameSync() const; |
116 | int frameCount() const; |
117 | int frameHeight() const; |
118 | int frameWidth() const; |
119 | int frameX() const; |
120 | int frameY() const; |
121 | qreal frameRate() const; |
122 | int frameDuration() const; |
123 | int loops() const; |
124 | bool paused() const; |
125 | int currentFrame() const; |
126 | FinishBehavior finishBehavior() const; |
127 | void setFinishBehavior(FinishBehavior arg); |
128 | |
129 | Q_SIGNALS: |
130 | |
131 | void pausedChanged(bool arg); |
132 | void runningChanged(bool arg); |
133 | void interpolateChanged(bool arg); |
134 | |
135 | void sourceChanged(const QUrl &arg); |
136 | void reverseChanged(bool arg); |
137 | void frameSyncChanged(bool arg); |
138 | void frameCountChanged(int arg); |
139 | void frameHeightChanged(int arg); |
140 | void frameWidthChanged(int arg); |
141 | void frameXChanged(int arg); |
142 | void frameYChanged(int arg); |
143 | void frameRateChanged(qreal arg); |
144 | void frameDurationChanged(int arg); |
145 | void loopsChanged(int arg); |
146 | void currentFrameChanged(int arg); |
147 | Q_REVISION(15) void finishBehaviorChanged(FinishBehavior arg); |
148 | |
149 | Q_REVISION(12) void finished(); |
150 | |
151 | public Q_SLOTS: |
152 | void start(); |
153 | void stop(); |
154 | void restart() {stop(); start();} |
155 | void advance(int frames=1); |
156 | void pause(); |
157 | void resume(); |
158 | |
159 | void setRunning(bool arg); |
160 | void setPaused(bool arg); |
161 | void setInterpolate(bool arg); |
162 | void setSource(const QUrl &arg); |
163 | void setReverse(bool arg); |
164 | void setFrameSync(bool arg); |
165 | void setFrameCount(int arg); |
166 | void setFrameHeight(int arg); |
167 | void setFrameWidth(int arg); |
168 | void setFrameX(int arg); |
169 | void setFrameY(int arg); |
170 | void setFrameRate(qreal arg); |
171 | void setFrameDuration(int arg); |
172 | void resetFrameRate(); |
173 | void resetFrameDuration(); |
174 | void setLoops(int arg); |
175 | void setCurrentFrame(int arg); |
176 | |
177 | private Q_SLOTS: |
178 | void createEngine(); |
179 | |
180 | protected Q_SLOTS: |
181 | void reset(); |
182 | |
183 | protected: |
184 | void componentComplete() override; |
185 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
186 | void itemChange(ItemChange, const ItemChangeData &) override; |
187 | |
188 | private: |
189 | void maybeUpdate(); |
190 | bool isCurrentFrameChangedConnected(); |
191 | void prepareNextFrame(QSGSpriteNode *node); |
192 | void reloadImage(); |
193 | QSGSpriteNode* initNode(); |
194 | |
195 | private: |
196 | Q_DISABLE_COPY(QQuickAnimatedSprite) |
197 | Q_DECLARE_PRIVATE(QQuickAnimatedSprite) |
198 | }; |
199 | |
200 | QT_END_NAMESPACE |
201 | |
202 | #endif // QQUICKANIMATEDSPRITE_P_H |
203 | |