Flesh out the sprite documentation
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickspritesequence.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickspritesequence_p.h"
43 #include "qquicksprite_p.h"
44 #include "qquickspriteengine_p.h"
45 #include <QtQuick/private/qsgcontext_p.h>
46 #include <private/qsgadaptationlayer_p.h>
47 #include <QtQuick/qsgnode.h>
48 #include <QtQuick/qsgtexturematerial.h>
49 #include <QtQuick/qsgtexture.h>
50 #include <QtQuick/qquickcanvas.h>
51 #include <QtQml/qqmlinfo.h>
52 #include <QFile>
53 #include <cmath>
54 #include <qmath.h>
55 #include <QDebug>
56
57 QT_BEGIN_NAMESPACE
58
59 static const char vertexShaderCode[] =
60     "attribute highp vec2 vTex;\n"
61     "uniform highp vec3 animData;// w,h(premultiplied of anim), interpolation progress\n"
62     "uniform highp vec4 animPos;//x,y, x,y (two frames for interpolation)\n"
63     "uniform highp vec2 size;//w,h of element\n"
64     "\n"
65     "uniform highp mat4 qt_Matrix;\n"
66     "\n"
67     "varying highp vec4 fTexS;\n"
68     "varying lowp float progress;\n"
69     "\n"
70     "\n"
71     "void main() {\n"
72     "    progress = animData.z;\n"
73     "    //Calculate frame location in texture\n"
74     "    fTexS.xy = animPos.xy + vTex.xy * animData.xy;\n"
75     "    //Next frame is also passed, for interpolation\n"
76     "    fTexS.zw = animPos.zw + vTex.xy * animData.xy;\n"
77     "\n"
78     "    gl_Position = qt_Matrix * vec4(size.x * vTex.x, size.y * vTex.y, 0, 1);\n"
79     "}\n";
80
81 static const char fragmentShaderCode[] =
82     "uniform sampler2D texture;\n"
83     "uniform lowp float qt_Opacity;\n"
84     "\n"
85     "varying highp vec4 fTexS;\n"
86     "varying lowp float progress;\n"
87     "\n"
88     "void main() {\n"
89     "    gl_FragColor = mix(texture2D(texture, fTexS.xy), texture2D(texture, fTexS.zw), progress) * qt_Opacity;\n"
90     "}\n";
91
92 class QQuickSpriteSequenceMaterial : public QSGMaterial
93 {
94 public:
95     QQuickSpriteSequenceMaterial();
96     ~QQuickSpriteSequenceMaterial();
97     virtual QSGMaterialType *type() const { static QSGMaterialType type; return &type; }
98     virtual QSGMaterialShader *createShader() const;
99     virtual int compare(const QSGMaterial *other) const
100     {
101         return this - static_cast<const QQuickSpriteSequenceMaterial *>(other);
102     }
103
104     QSGTexture *texture;
105
106     float animT;
107     float animX1;
108     float animY1;
109     float animX2;
110     float animY2;
111     float animW;
112     float animH;
113     float elementWidth;
114     float elementHeight;
115 };
116
117 QQuickSpriteSequenceMaterial::QQuickSpriteSequenceMaterial()
118     : animT(0.0f)
119     , animX1(0.0f)
120     , animY1(0.0f)
121     , animX2(0.0f)
122     , animY2(0.0f)
123     , animW(1.0f)
124     , animH(1.0f)
125     , elementWidth(1.0f)
126     , elementHeight(1.0f)
127 {
128     setFlag(Blending, true);
129 }
130
131 QQuickSpriteSequenceMaterial::~QQuickSpriteSequenceMaterial()
132 {
133     delete texture;
134 }
135
136 class SpriteSequenceMaterialData : public QSGMaterialShader
137 {
138 public:
139     SpriteSequenceMaterialData(const char * /* vertexFile */ = 0, const char * /* fragmentFile */ = 0)
140     {
141     }
142
143     void deactivate() {
144         QSGMaterialShader::deactivate();
145
146         for (int i=0; i<8; ++i) {
147             program()->setAttributeArray(i, GL_FLOAT, chunkOfBytes, 1, 0);
148         }
149     }
150
151     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *)
152     {
153         QQuickSpriteSequenceMaterial *m = static_cast<QQuickSpriteSequenceMaterial *>(newEffect);
154         m->texture->bind();
155
156         program()->setUniformValue(m_opacity_id, state.opacity());
157         program()->setUniformValue(m_animData_id, m->animW, m->animH, m->animT);
158         program()->setUniformValue(m_animPos_id, m->animX1, m->animY1, m->animX2, m->animY2);
159         program()->setUniformValue(m_size_id, m->elementWidth, m->elementHeight);
160
161         if (state.isMatrixDirty())
162             program()->setUniformValue(m_matrix_id, state.combinedMatrix());
163     }
164
165     virtual void initialize() {
166         m_matrix_id = program()->uniformLocation("qt_Matrix");
167         m_opacity_id = program()->uniformLocation("qt_Opacity");
168         m_animData_id = program()->uniformLocation("animData");
169         m_animPos_id = program()->uniformLocation("animPos");
170         m_size_id = program()->uniformLocation("size");
171     }
172
173     virtual const char *vertexShader() const { return vertexShaderCode; }
174     virtual const char *fragmentShader() const { return fragmentShaderCode; }
175
176     virtual char const *const *attributeNames() const {
177         static const char *attr[] = {
178            "vTex",
179             0
180         };
181         return attr;
182     }
183
184     int m_matrix_id;
185     int m_opacity_id;
186     int m_animData_id;
187     int m_animPos_id;
188     int m_size_id;
189
190     static float chunkOfBytes[1024];
191 };
192
193 float SpriteSequenceMaterialData::chunkOfBytes[1024];
194
195 QSGMaterialShader *QQuickSpriteSequenceMaterial::createShader() const
196 {
197     return new SpriteSequenceMaterialData;
198 }
199
200 struct SpriteVertex {
201     float tx;
202     float ty;
203 };
204
205 struct SpriteVertices {
206     SpriteVertex v1;
207     SpriteVertex v2;
208     SpriteVertex v3;
209     SpriteVertex v4;
210 };
211
212 /*!
213     \qmlclass SpriteSequence QQuickSpriteSequence
214     \inqmlmodule QtQuick 2
215     \inherits Item
216     \brief The SpriteSequence element draws a sprite animation
217
218     SpriteSequence renders and controls a list of animations defined
219     by \l Sprite elements.
220
221     For full details, see the \l{Sprite Animation} overview.
222 */
223 /*!
224     \qmlproperty bool QtQuick2::SpriteSequence::running
225
226     Whether the sprite is animating or not.
227
228     Default is true
229 */
230 /*!
231     \qmlproperty bool QtQuick2::SpriteSequence::interpolate
232
233     If true, interpolation will occur between sprite frames to make the
234     animation appear smoother.
235
236     Default is true.
237 */
238 /*!
239     \qmlproperty string QtQuick2::SpriteSequence::goalSprite
240
241     The name of the Sprite which is currently animating.
242 */
243 /*!
244     \qmlproperty string QtQuick2::SpriteSequence::goalSprite
245
246     The name of the Sprite which the animation should move to.
247
248     Sprite states have defined durations and transitions between them, setting goalState
249     will cause it to disregard any path weightings (including 0) and head down the path
250     which will reach the goalState quickest (fewest animations). It will pass through
251     intermediate states on that path, and animate them for their duration.
252
253     If it is possible to return to the goalState from the starting point of the goalState
254     it will continue to do so until goalState is set to "" or an unreachable state.
255 */
256 /*! \qmlmethod void QtQuick2::SpriteSequence::jumpTo(string sprite)
257
258     This function causes the sprite to jump to the specified state immediately, intermediate
259     states are not played.
260 */
261 /*!
262     \qmlproperty list<Sprite> QtQuick2::SpriteSequence::sprites
263
264     The sprite or sprites to draw. Sprites will be scaled to the size of this element.
265 */
266
267 //TODO: Implicitly size element to size of first sprite?
268 QQuickSpriteSequence::QQuickSpriteSequence(QQuickItem *parent) :
269     QQuickItem(parent)
270     , m_node(0)
271     , m_material(0)
272     , m_spriteEngine(0)
273     , m_curFrame(0)
274     , m_pleaseReset(false)
275     , m_running(true)
276     , m_interpolate(true)
277     , m_curStateIdx(0)
278 {
279     setFlag(ItemHasContents);
280     connect(this, SIGNAL(runningChanged(bool)),
281             this, SLOT(update()));
282 }
283
284 void QQuickSpriteSequence::jumpTo(const QString &sprite)
285 {
286     if (!m_spriteEngine)
287         return;
288     m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite), 0, true);
289 }
290
291 void QQuickSpriteSequence::setGoalSprite(const QString &sprite)
292 {
293     if (m_goalState != sprite){
294         m_goalState = sprite;
295         emit goalSpriteChanged(sprite);
296         m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite));
297     }
298 }
299
300 QQmlListProperty<QQuickSprite> QQuickSpriteSequence::sprites()
301 {
302     return QQmlListProperty<QQuickSprite>(this, &m_sprites, spriteAppend, spriteCount, spriteAt, spriteClear);
303 }
304
305 void QQuickSpriteSequence::createEngine()
306 {
307     //TODO: delay until component complete
308     if (m_spriteEngine)
309         delete m_spriteEngine;
310     if (m_sprites.count())
311         m_spriteEngine = new QQuickSpriteEngine(m_sprites, this);
312     else
313         m_spriteEngine = 0;
314     reset();
315 }
316
317 static QSGGeometry::Attribute SpriteSequence_Attributes[] = {
318     QSGGeometry::Attribute::create(0, 2, GL_FLOAT),         // tex
319 };
320
321 static QSGGeometry::AttributeSet SpriteSequence_AttributeSet =
322 {
323     1, // Attribute Count
324     2 * sizeof(float),
325     SpriteSequence_Attributes
326 };
327
328 QSGGeometryNode* QQuickSpriteSequence::buildNode()
329 {
330     if (!m_spriteEngine) {
331         qmlInfo(this) << "No sprite engine...";
332         return 0;
333     } else if (m_spriteEngine->status() == QQuickPixmap::Null) {
334         m_spriteEngine->startAssemblingImage();
335         update();//Schedule another update, where we will check again
336         return 0;
337     } else if (m_spriteEngine->status() == QQuickPixmap::Loading) {
338         update();//Schedule another update, where we will check again
339         return 0;
340     }
341
342     m_material = new QQuickSpriteSequenceMaterial();
343
344     QImage image = m_spriteEngine->assembledImage();
345     if (image.isNull())
346         return 0;
347     m_sheetSize = QSizeF(image.size());
348     m_material->texture = canvas()->createTextureFromImage(image);
349     m_material->texture->setFiltering(QSGTexture::Linear);
350     m_spriteEngine->start(0);
351     m_material->animT = 0;
352     m_material->animX1 = m_spriteEngine->spriteX() / m_sheetSize.width();
353     m_material->animY1 = m_spriteEngine->spriteY() / m_sheetSize.height();
354     m_material->animX2 = m_material->animX1;
355     m_material->animY2 = m_material->animY1;
356     m_material->animW = m_spriteEngine->spriteWidth() / m_sheetSize.width();
357     m_material->animH = m_spriteEngine->spriteHeight() / m_sheetSize.height();
358     m_material->elementWidth = width();
359     m_material->elementHeight = height();
360     m_curState = m_spriteEngine->state(m_spriteEngine->curState())->name();
361     emit currentSpriteChanged(m_curState);
362
363     int vCount = 4;
364     int iCount = 6;
365     QSGGeometry *g = new QSGGeometry(SpriteSequence_AttributeSet, vCount, iCount);
366     g->setDrawingMode(GL_TRIANGLES);
367
368     SpriteVertices *p = (SpriteVertices *) g->vertexData();
369
370     p->v1.tx = 0;
371     p->v1.ty = 0;
372
373     p->v2.tx = 1.0;
374     p->v2.ty = 0;
375
376     p->v3.tx = 0;
377     p->v3.ty = 1.0;
378
379     p->v4.tx = 1.0;
380     p->v4.ty = 1.0;
381
382     quint16 *indices = g->indexDataAsUShort();
383     indices[0] = 0;
384     indices[1] = 1;
385     indices[2] = 2;
386     indices[3] = 1;
387     indices[4] = 3;
388     indices[5] = 2;
389
390
391     m_timestamp.start();
392     m_node = new QSGGeometryNode();
393     m_node->setGeometry(g);
394     m_node->setMaterial(m_material);
395     m_node->setFlag(QSGGeometryNode::OwnsMaterial);
396     return m_node;
397 }
398
399 void QQuickSpriteSequence::reset()
400 {
401     m_pleaseReset = true;
402 }
403
404 QSGNode *QQuickSpriteSequence::updatePaintNode(QSGNode *, UpdatePaintNodeData *)
405 {
406     if (m_pleaseReset) {
407         delete m_node;
408
409         m_node = 0;
410         m_material = 0;
411         m_pleaseReset = false;
412     }
413
414     prepareNextFrame();
415
416     if (m_running) {
417         update();
418         if (m_node)
419             m_node->markDirty(QSGNode::DirtyMaterial);
420     }
421
422     return m_node;
423 }
424
425 void QQuickSpriteSequence::prepareNextFrame()
426 {
427     if (m_node == 0)
428         m_node = buildNode();
429     if (m_node == 0) //error creating node
430         return;
431
432     uint timeInt = m_timestamp.elapsed();
433     qreal time =  timeInt / 1000.;
434     m_material->elementHeight = height();
435     m_material->elementWidth = width();
436
437     //Advance State
438     m_spriteEngine->updateSprites(timeInt);
439     if (m_curStateIdx != m_spriteEngine->curState()) {
440         m_curStateIdx = m_spriteEngine->curState();
441         m_curState = m_spriteEngine->state(m_spriteEngine->curState())->name();
442         emit currentSpriteChanged(m_curState);
443         m_curFrame= -1;
444     }
445
446     //Advance Sprite
447     qreal animT = m_spriteEngine->spriteStart()/1000.0;
448     qreal frameCount = m_spriteEngine->spriteFrames();
449     qreal frameDuration = m_spriteEngine->spriteDuration()/frameCount;
450     double frameAt;
451     qreal progress;
452     if (frameDuration > 0) {
453         qreal frame = (time - animT)/(frameDuration / 1000.0);
454         frame = qBound(qreal(0.0), frame, frameCount - qreal(1.0));//Stop at count-1 frames until we have between anim interpolation
455         progress = modf(frame,&frameAt);
456     } else {
457         m_curFrame++;
458         if (m_curFrame >= frameCount){
459             m_curFrame = 0;
460             m_spriteEngine->advance();
461         }
462         frameAt = m_curFrame;
463         progress = 0;
464     }
465     if (m_spriteEngine->sprite()->reverse())
466         frameAt = (m_spriteEngine->spriteFrames() - 1) - frameAt;
467     qreal y = m_spriteEngine->spriteY() / m_sheetSize.height();
468     qreal w = m_spriteEngine->spriteWidth() / m_sheetSize.width();
469     qreal h = m_spriteEngine->spriteHeight() / m_sheetSize.height();
470     qreal x1 = m_spriteEngine->spriteX() / m_sheetSize.width();
471     x1 += frameAt * w;
472     qreal x2 = x1;
473     if (frameAt < (frameCount-1))
474         x2 += w;
475
476     m_material->animX1 = x1;
477     m_material->animY1 = y;
478     m_material->animX2 = x2;
479     m_material->animY2 = y;
480     m_material->animW = w;
481     m_material->animH = h;
482     m_material->animT = m_interpolate ? progress : 0.0;
483 }
484
485 QT_END_NAMESPACE