Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquickspritesequence.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 | #include "qquickspritesequence_p.h" | - | ||||||
41 | #include "qquickspritesequence_p_p.h" | - | ||||||
42 | #include "qquicksprite_p.h" | - | ||||||
43 | #include "qquickspriteengine_p.h" | - | ||||||
44 | #include <QtQuick/private/qsgcontext_p.h> | - | ||||||
45 | #include <private/qsgadaptationlayer_p.h> | - | ||||||
46 | #include <QtQuick/qsgnode.h> | - | ||||||
47 | #include <QtQuick/qsgtexturematerial.h> | - | ||||||
48 | #include <QtQuick/qsgtexture.h> | - | ||||||
49 | #include <QtQuick/qquickwindow.h> | - | ||||||
50 | #include <QtQml/qqmlinfo.h> | - | ||||||
51 | #include <QFile> | - | ||||||
52 | #include <cmath> | - | ||||||
53 | #include <qmath.h> | - | ||||||
54 | #include <QDebug> | - | ||||||
55 | - | |||||||
56 | QT_BEGIN_NAMESPACE | - | ||||||
57 | - | |||||||
58 | /*! | - | ||||||
59 | \qmltype SpriteSequence | - | ||||||
60 | \instantiates QQuickSpriteSequence | - | ||||||
61 | \inqmlmodule QtQuick | - | ||||||
62 | \ingroup qtquick-visual-utility | - | ||||||
63 | \inherits Item | - | ||||||
64 | \brief Draws a sprite animation. | - | ||||||
65 | - | |||||||
66 | SpriteSequence renders and controls a list of animations defined | - | ||||||
67 | by \l Sprite types. | - | ||||||
68 | - | |||||||
69 | For full details, see the \l{Sprite Animations} overview. | - | ||||||
70 | */ | - | ||||||
71 | /*! | - | ||||||
72 | \qmlproperty bool QtQuick::SpriteSequence::running | - | ||||||
73 | - | |||||||
74 | Whether the sprite is animating or not. | - | ||||||
75 | - | |||||||
76 | Default is \c true. | - | ||||||
77 | */ | - | ||||||
78 | /*! | - | ||||||
79 | \qmlproperty bool QtQuick::SpriteSequence::interpolate | - | ||||||
80 | - | |||||||
81 | If \c true, interpolation will occur between sprite frames to make the | - | ||||||
82 | animation appear smoother. | - | ||||||
83 | - | |||||||
84 | Default is \c true. | - | ||||||
85 | */ | - | ||||||
86 | /*! | - | ||||||
87 | \qmlproperty string QtQuick::SpriteSequence::currentSprite | - | ||||||
88 | - | |||||||
89 | The name of the \l Sprite that is currently animating. | - | ||||||
90 | */ | - | ||||||
91 | /*! | - | ||||||
92 | \qmlproperty string QtQuick::SpriteSequence::goalSprite | - | ||||||
93 | - | |||||||
94 | The name of the \l Sprite that the animation should move to. | - | ||||||
95 | - | |||||||
96 | Sprite states have defined durations and transitions between them; setting \c goalSprite | - | ||||||
97 | will cause it to disregard any path weightings (including \c 0) and head down the path | - | ||||||
98 | that will reach the \c goalSprite quickest (fewest animations). It will pass through | - | ||||||
99 | intermediate states on that path, and animate them for their duration. | - | ||||||
100 | - | |||||||
101 | If it is possible to return to the \c goalSprite from the starting point of the \c goalSprite, | - | ||||||
102 | it will continue to do so until \c goalSprite is set to \c "" or an unreachable state. | - | ||||||
103 | */ | - | ||||||
104 | /*! \qmlmethod QtQuick::SpriteSequence::jumpTo(string sprite) | - | ||||||
105 | - | |||||||
106 | This function causes the SpriteSequence to jump to the specified \a sprite immediately; | - | ||||||
107 | intermediate sprites are not played. | - | ||||||
108 | */ | - | ||||||
109 | /*! | - | ||||||
110 | \qmlproperty list<Sprite> QtQuick::SpriteSequence::sprites | - | ||||||
111 | - | |||||||
112 | The sprite or sprites to draw. Sprites will be scaled to the size of this item. | - | ||||||
113 | */ | - | ||||||
114 | - | |||||||
115 | //TODO: Implicitly size element to size of first sprite? | - | ||||||
116 | QQuickSpriteSequence::QQuickSpriteSequence(QQuickItem *parent) : | - | ||||||
117 | QQuickItem(*(new QQuickSpriteSequencePrivate), parent) | - | ||||||
118 | { | - | ||||||
119 | setFlag(ItemHasContents); | - | ||||||
120 | connect(this, SIGNAL(runningChanged(bool)), | - | ||||||
121 | this, SLOT(update())); | - | ||||||
122 | } executed 14 times by 2 tests: end of block Executed by:
| 14 | ||||||
123 | - | |||||||
124 | void QQuickSpriteSequence::jumpTo(const QString &sprite) | - | ||||||
125 | { | - | ||||||
126 | Q_D(QQuickSpriteSequence); | - | ||||||
127 | if (!d->m_spriteEngine)
| 0-2 | ||||||
128 | return; never executed: return; | 0 | ||||||
129 | d->m_spriteEngine->setGoal(d->m_spriteEngine->stateIndex(sprite), 0, true); | - | ||||||
130 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
131 | - | |||||||
132 | void QQuickSpriteSequence::setGoalSprite(const QString &sprite) | - | ||||||
133 | { | - | ||||||
134 | Q_D(QQuickSpriteSequence); | - | ||||||
135 | if (d->m_goalState != sprite){
| 0-4 | ||||||
136 | d->m_goalState = sprite; | - | ||||||
137 | emit goalSpriteChanged(sprite); | - | ||||||
138 | if (d->m_spriteEngine)
| 2 | ||||||
139 | d->m_spriteEngine->setGoal(d->m_spriteEngine->stateIndex(sprite)); executed 2 times by 1 test: d->m_spriteEngine->setGoal(d->m_spriteEngine->stateIndex(sprite)); Executed by:
| 2 | ||||||
140 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||
141 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||
142 | - | |||||||
143 | void QQuickSpriteSequence::setRunning(bool arg) | - | ||||||
144 | { | - | ||||||
145 | Q_D(QQuickSpriteSequence); | - | ||||||
146 | if (d->m_running != arg) {
| 0-2 | ||||||
147 | d->m_running = arg; | - | ||||||
148 | Q_EMIT runningChanged(arg); | - | ||||||
149 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
150 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
151 | - | |||||||
152 | void QQuickSpriteSequence::setInterpolate(bool arg) | - | ||||||
153 | { | - | ||||||
154 | Q_D(QQuickSpriteSequence); | - | ||||||
155 | if (d->m_interpolate != arg) {
| 0-2 | ||||||
156 | d->m_interpolate = arg; | - | ||||||
157 | Q_EMIT interpolateChanged(arg); | - | ||||||
158 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
159 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
160 | - | |||||||
161 | QQmlListProperty<QQuickSprite> QQuickSpriteSequence::sprites() | - | ||||||
162 | { | - | ||||||
163 | Q_D(QQuickSpriteSequence); | - | ||||||
164 | return QQmlListProperty<QQuickSprite>(this, &d->m_sprites, spriteAppend, spriteCount, spriteAt, spriteClear); executed 16 times by 2 tests: return QQmlListProperty<QQuickSprite>(this, &d->m_sprites, spriteAppend, spriteCount, spriteAt, spriteClear); Executed by:
| 16 | ||||||
165 | } | - | ||||||
166 | - | |||||||
167 | bool QQuickSpriteSequence::running() const | - | ||||||
168 | { | - | ||||||
169 | Q_D(const QQuickSpriteSequence); | - | ||||||
170 | return d->m_running; executed 4 times by 1 test: return d->m_running; Executed by:
| 4 | ||||||
171 | } | - | ||||||
172 | - | |||||||
173 | bool QQuickSpriteSequence::interpolate() const | - | ||||||
174 | { | - | ||||||
175 | Q_D(const QQuickSpriteSequence); | - | ||||||
176 | return d->m_interpolate; executed 4 times by 1 test: return d->m_interpolate; Executed by:
| 4 | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | QString QQuickSpriteSequence::goalSprite() const | - | ||||||
180 | { | - | ||||||
181 | Q_D(const QQuickSpriteSequence); | - | ||||||
182 | return d->m_goalState; never executed: return d->m_goalState; | 0 | ||||||
183 | } | - | ||||||
184 | - | |||||||
185 | QString QQuickSpriteSequence::currentSprite() const | - | ||||||
186 | { | - | ||||||
187 | Q_D(const QQuickSpriteSequence); | - | ||||||
188 | return d->m_curState; executed 9 times by 1 test: return d->m_curState; Executed by:
| 9 | ||||||
189 | } | - | ||||||
190 | - | |||||||
191 | void QQuickSpriteSequence::createEngine() | - | ||||||
192 | { | - | ||||||
193 | Q_D(QQuickSpriteSequence); | - | ||||||
194 | //TODO: delay until component complete | - | ||||||
195 | if (d->m_spriteEngine)
| 4-16 | ||||||
196 | delete d->m_spriteEngine; executed 4 times by 1 test: delete d->m_spriteEngine; Executed by:
| 4 | ||||||
197 | if (d->m_sprites.count()) {
| 2-18 | ||||||
198 | d->m_spriteEngine = new QQuickSpriteEngine(d->m_sprites, this); | - | ||||||
199 | if (!d->m_goalState.isEmpty())
| 2-16 | ||||||
200 | d->m_spriteEngine->setGoal(d->m_spriteEngine->stateIndex(d->m_goalState)); executed 2 times by 1 test: d->m_spriteEngine->setGoal(d->m_spriteEngine->stateIndex(d->m_goalState)); Executed by:
| 2 | ||||||
201 | } else { executed 18 times by 2 tests: end of block Executed by:
| 18 | ||||||
202 | d->m_spriteEngine = nullptr; | - | ||||||
203 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
204 | reset(); | - | ||||||
205 | } executed 20 times by 2 tests: end of block Executed by:
| 20 | ||||||
206 | - | |||||||
207 | QSGSpriteNode *QQuickSpriteSequence::initNode() | - | ||||||
208 | { | - | ||||||
209 | Q_D(QQuickSpriteSequence); | - | ||||||
210 | - | |||||||
211 | if (!d->m_spriteEngine) {
| 0-26 | ||||||
212 | qmlWarning(this) << "No sprite engine..."; | - | ||||||
213 | return nullptr; never executed: return nullptr; | 0 | ||||||
214 | } else if (d->m_spriteEngine->status() == QQuickPixmap::Null) {
| 12-14 | ||||||
215 | d->m_spriteEngine->startAssemblingImage(); | - | ||||||
216 | update();//Schedule another update, where we will check again | - | ||||||
217 | return nullptr; executed 14 times by 2 tests: return nullptr; Executed by:
| 14 | ||||||
218 | } else if (d->m_spriteEngine->status() == QQuickPixmap::Loading) {
| 0-12 | ||||||
219 | update();//Schedule another update, where we will check again | - | ||||||
220 | return nullptr; never executed: return nullptr; | 0 | ||||||
221 | } | - | ||||||
222 | - | |||||||
223 | QImage image = d->m_spriteEngine->assembledImage(d->sceneGraphRenderContext()->maxTextureSize()); | - | ||||||
224 | if (image.isNull())
| 0-12 | ||||||
225 | return nullptr; never executed: return nullptr; | 0 | ||||||
226 | - | |||||||
227 | QSGSpriteNode *node = d->sceneGraphContext()->createSpriteNode(); | - | ||||||
228 | - | |||||||
229 | d->m_sheetSize = QSize(image.size() / image.devicePixelRatioF()); | - | ||||||
230 | node->setTexture(window()->createTextureFromImage(image)); | - | ||||||
231 | d->m_spriteEngine->start(0); | - | ||||||
232 | node->setTime(0.0f); | - | ||||||
233 | node->setSourceA(QPoint(d->m_spriteEngine->spriteX(), d->m_spriteEngine->spriteY())); | - | ||||||
234 | node->setSourceB(QPoint(d->m_spriteEngine->spriteX(), d->m_spriteEngine->spriteY())); | - | ||||||
235 | node->setSpriteSize(QSize(d->m_spriteEngine->spriteWidth(), d->m_spriteEngine->spriteHeight())); | - | ||||||
236 | node->setSheetSize(d->m_sheetSize); | - | ||||||
237 | node->setSize(QSizeF(width(), height())); | - | ||||||
238 | - | |||||||
239 | d->m_curState = d->m_spriteEngine->state(d->m_spriteEngine->curState())->name(); | - | ||||||
240 | emit currentSpriteChanged(d->m_curState); | - | ||||||
241 | d->m_timestamp.start(); | - | ||||||
242 | return node; executed 12 times by 1 test: return node; Executed by:
| 12 | ||||||
243 | } | - | ||||||
244 | - | |||||||
245 | void QQuickSpriteSequence::reset() | - | ||||||
246 | { | - | ||||||
247 | Q_D(QQuickSpriteSequence); | - | ||||||
248 | d->m_pleaseReset = true; | - | ||||||
249 | } executed 20 times by 2 tests: end of block Executed by:
| 20 | ||||||
250 | - | |||||||
251 | QSGNode *QQuickSpriteSequence::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) | - | ||||||
252 | { | - | ||||||
253 | Q_D(QQuickSpriteSequence); | - | ||||||
254 | - | |||||||
255 | if (d->m_pleaseReset) {
| 14-1072 | ||||||
256 | delete oldNode; | - | ||||||
257 | - | |||||||
258 | oldNode = nullptr; | - | ||||||
259 | d->m_pleaseReset = false; | - | ||||||
260 | } executed 14 times by 2 tests: end of block Executed by:
| 14 | ||||||
261 | - | |||||||
262 | QSGSpriteNode *node = static_cast<QSGSpriteNode *>(oldNode); | - | ||||||
263 | if (!node)
| 26-1060 | ||||||
264 | node = initNode(); executed 26 times by 2 tests: node = initNode(); Executed by:
| 26 | ||||||
265 | - | |||||||
266 | if (node)
| 14-1072 | ||||||
267 | prepareNextFrame(node); executed 1072 times by 1 test: prepareNextFrame(node); Executed by:
| 1072 | ||||||
268 | - | |||||||
269 | if (d->m_running) {
| 0-1086 | ||||||
270 | update(); | - | ||||||
271 | } executed 1086 times by 2 tests: end of block Executed by:
| 1086 | ||||||
272 | - | |||||||
273 | return node; executed 1086 times by 2 tests: return node; Executed by:
| 1086 | ||||||
274 | } | - | ||||||
275 | - | |||||||
276 | void QQuickSpriteSequence::prepareNextFrame(QSGSpriteNode *node) | - | ||||||
277 | { | - | ||||||
278 | Q_D(QQuickSpriteSequence); | - | ||||||
279 | - | |||||||
280 | uint timeInt = d->m_timestamp.elapsed(); | - | ||||||
281 | qreal time = timeInt / 1000.; | - | ||||||
282 | - | |||||||
283 | //Advance State | - | ||||||
284 | d->m_spriteEngine->updateSprites(timeInt); | - | ||||||
285 | if (d->m_curStateIdx != d->m_spriteEngine->curState()) {
| 2-1070 | ||||||
286 | d->m_curStateIdx = d->m_spriteEngine->curState(); | - | ||||||
287 | d->m_curState = d->m_spriteEngine->state(d->m_spriteEngine->curState())->name(); | - | ||||||
288 | emit currentSpriteChanged(d->m_curState); | - | ||||||
289 | d->m_curFrame= -1; | - | ||||||
290 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
291 | - | |||||||
292 | //Advance Sprite | - | ||||||
293 | qreal animT = d->m_spriteEngine->spriteStart()/1000.0; | - | ||||||
294 | qreal frameCount = d->m_spriteEngine->spriteFrames(); | - | ||||||
295 | qreal frameDuration = d->m_spriteEngine->spriteDuration()/frameCount; | - | ||||||
296 | double frameAt; | - | ||||||
297 | qreal progress; | - | ||||||
298 | if (frameDuration > 0) {
| 14-1058 | ||||||
299 | qreal frame = (time - animT)/(frameDuration / 1000.0); | - | ||||||
300 | frame = qBound(qreal(0.0), frame, frameCount - qreal(1.0));//Stop at count-1 frames until we have between anim interpolation | - | ||||||
301 | progress = std::modf(frame,&frameAt); | - | ||||||
302 | } else { executed 1058 times by 1 test: end of block Executed by:
| 1058 | ||||||
303 | d->m_curFrame++; | - | ||||||
304 | if (d->m_curFrame >= frameCount){
| 2-12 | ||||||
305 | d->m_curFrame = 0; | - | ||||||
306 | d->m_spriteEngine->advance(); | - | ||||||
307 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
308 | frameAt = d->m_curFrame; | - | ||||||
309 | progress = 0; | - | ||||||
310 | } executed 14 times by 1 test: end of block Executed by:
| 14 | ||||||
311 | if (d->m_spriteEngine->sprite()->reverse())
| 0-1072 | ||||||
312 | frameAt = (d->m_spriteEngine->spriteFrames() - 1) - frameAt; never executed: frameAt = (d->m_spriteEngine->spriteFrames() - 1) - frameAt; | 0 | ||||||
313 | int y = d->m_spriteEngine->spriteY(); | - | ||||||
314 | int w = d->m_spriteEngine->spriteWidth(); | - | ||||||
315 | int h = d->m_spriteEngine->spriteHeight(); | - | ||||||
316 | int x1 = d->m_spriteEngine->spriteX(); | - | ||||||
317 | x1 += frameAt * w; | - | ||||||
318 | int x2 = x1; | - | ||||||
319 | if (frameAt < (frameCount-1))
| 101-971 | ||||||
320 | x2 += w; executed 971 times by 1 test: x2 += w; Executed by:
| 971 | ||||||
321 | - | |||||||
322 | node->setSourceA(QPoint(x1, y)); | - | ||||||
323 | node->setSourceB(QPoint(x2, y)); | - | ||||||
324 | node->setSpriteSize(QSize(w, h)); | - | ||||||
325 | node->setTime(d->m_interpolate ? progress : 0.0); | - | ||||||
326 | node->setSize(QSizeF(width(), height())); | - | ||||||
327 | node->setFiltering(smooth() ? QSGTexture::Linear : QSGTexture::Nearest); | - | ||||||
328 | node->update(); | - | ||||||
329 | } executed 1072 times by 1 test: end of block Executed by:
| 1072 | ||||||
330 | - | |||||||
331 | QT_END_NAMESPACE | - | ||||||
332 | - | |||||||
333 | #include "moc_qquickspritesequence_p.cpp" | - | ||||||
Source code | Switch to Preprocessed file |