4db2c9801ab3ad2d333611a2b3edf8142a347e58
[profile/ivi/qtdeclarative.git] / src / particles / qquickimageparticle_p.h
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 #ifndef ULTRAPARTICLE_H
43 #define ULTRAPARTICLE_H
44 #include "qquickparticlepainter_p.h"
45 #include "qquickdirection_p.h"
46 #include <private/qquickpixmapcache_p.h>
47 #include <QQmlListProperty>
48 #include <QtQuick/qsgsimplematerial.h>
49 #include <QtGui/qcolor.h>
50
51 QT_BEGIN_HEADER
52
53 QT_BEGIN_NAMESPACE
54
55 class ImageMaterialData;
56 class QSGGeometryNode;
57
58 class QQuickSprite;
59 class QQuickStochasticEngine;
60
61 struct SimpleVertex {
62     float x;
63     float y;
64     float t;
65     float lifeSpan;
66     float size;
67     float endSize;
68     float vx;
69     float vy;
70     float ax;
71     float ay;
72 };
73
74 struct ColoredVertex {
75     float x;
76     float y;
77     float t;
78     float lifeSpan;
79     float size;
80     float endSize;
81     float vx;
82     float vy;
83     float ax;
84     float ay;
85     Color4ub color;
86 };
87
88 struct DeformableVertex {
89     float x;
90     float y;
91     float tx;
92     float ty;
93     float t;
94     float lifeSpan;
95     float size;
96     float endSize;
97     float vx;
98     float vy;
99     float ax;
100     float ay;
101     Color4ub color;
102     float xx;
103     float xy;
104     float yx;
105     float yy;
106     float rotation;
107     float rotationSpeed;
108     float autoRotate;//Assumed that GPUs prefer floats to bools
109 };
110
111 struct SpriteVertex {
112     float x;
113     float y;
114     float tx;
115     float ty;
116     float t;
117     float lifeSpan;
118     float size;
119     float endSize;
120     float vx;
121     float vy;
122     float ax;
123     float ay;
124     Color4ub color;
125     float xx;
126     float xy;
127     float yx;
128     float yy;
129     float rotation;
130     float rotationSpeed;
131     float autoRotate;//Assumed that GPUs prefer floats to bools
132     float animW;
133     float animH;
134     float animProgress;
135     float animX1;
136     float animY1;
137     float animX2;
138     float animY2;
139 };
140
141 template <typename Vertex>
142 struct Vertices {
143     Vertex v1;
144     Vertex v2;
145     Vertex v3;
146     Vertex v4;
147 };
148
149 class QQuickImageParticle : public QQuickParticlePainter
150 {
151     Q_OBJECT
152     Q_PROPERTY(QUrl source READ image WRITE setImage NOTIFY imageChanged)
153     Q_PROPERTY(QQmlListProperty<QQuickSprite> sprites READ sprites)
154     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
155     //### Is it worth having progress like Image has?
156     //Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
157
158     Q_PROPERTY(QUrl colorTable READ colortable WRITE setColortable NOTIFY colortableChanged)
159     Q_PROPERTY(QUrl sizeTable READ sizetable WRITE setSizetable NOTIFY sizetableChanged)
160     Q_PROPERTY(QUrl opacityTable READ opacitytable WRITE setOpacitytable NOTIFY opacitytableChanged)
161
162     //###Now just colorize - add a flag for 'solid' color particles(where the img is just a mask?)?
163     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged RESET resetColor)
164     //Stacks (added) with individual colorVariations
165     Q_PROPERTY(qreal colorVariation READ colorVariation WRITE setColorVariation NOTIFY colorVariationChanged RESET resetColor)
166     Q_PROPERTY(qreal redVariation READ redVariation WRITE setRedVariation NOTIFY redVariationChanged RESET resetColor)
167     Q_PROPERTY(qreal greenVariation READ greenVariation WRITE setGreenVariation NOTIFY greenVariationChanged RESET resetColor)
168     Q_PROPERTY(qreal blueVariation READ blueVariation WRITE setBlueVariation NOTIFY blueVariationChanged RESET resetColor)
169     //Stacks (multiplies) with the Alpha in the color, mostly here so you can use svg color names (which have full alpha)
170     Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha NOTIFY alphaChanged RESET resetColor)
171     Q_PROPERTY(qreal alphaVariation READ alphaVariation WRITE setAlphaVariation NOTIFY alphaVariationChanged RESET resetColor)
172
173     Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged RESET resetRotation)
174     Q_PROPERTY(qreal rotationVariation READ rotationVariation WRITE setRotationVariation NOTIFY rotationVariationChanged RESET resetRotation)
175     Q_PROPERTY(qreal rotationSpeed READ rotationSpeed WRITE setRotationSpeed NOTIFY rotationSpeedChanged RESET resetRotation)
176     Q_PROPERTY(qreal rotationSpeedVariation READ rotationSpeedVariation WRITE setRotationSpeedVariation NOTIFY rotationSpeedVariationChanged RESET resetRotation)
177     //If true, then will face the direction of motion. Stacks with rotation, e.g. setting rotation
178     //to 180 will lead to facing away from the direction of motion
179     Q_PROPERTY(bool autoRotation READ autoRotation WRITE setAutoRotation NOTIFY autoRotationChanged RESET resetRotation)
180
181     //xVector is the vector from the top-left point to the top-right point, and is multiplied by current size
182     Q_PROPERTY(QQuickDirection* xVector READ xVector WRITE setXVector NOTIFY xVectorChanged RESET resetDeformation)
183     //yVector is the same, but top-left to bottom-left. The particle is always a parallelogram.
184     Q_PROPERTY(QQuickDirection* yVector READ yVector WRITE setYVector NOTIFY yVectorChanged RESET resetDeformation)
185     Q_PROPERTY(bool spritesInterpolate READ spritesInterpolate WRITE setSpritesInterpolate NOTIFY spritesInterpolateChanged)
186
187     Q_PROPERTY(EntryEffect entryEffect READ entryEffect WRITE setEntryEffect NOTIFY entryEffectChanged)
188     Q_ENUMS(EntryEffect)
189     Q_ENUMS(Status)
190 public:
191     explicit QQuickImageParticle(QQuickItem *parent = 0);
192     virtual ~QQuickImageParticle();
193
194     enum Status { Null, Ready, Loading, Error };
195
196     QQmlListProperty<QQuickSprite> sprites();
197     QQuickStochasticEngine* spriteEngine() {return m_spriteEngine;}
198
199     enum EntryEffect {
200         None = 0,
201         Fade = 1,
202         Scale = 2
203     };
204
205     enum PerformanceLevel{//TODO: Expose?
206         Unknown = 0,
207         Simple,
208         Colored,
209         Deformable,
210         Tabled,
211         Sprites
212     };
213
214     QUrl image() const { return m_image ? m_image->source : QUrl(); }
215     void setImage(const QUrl &image);
216
217     QUrl colortable() const { return m_colorTable ? m_colorTable->source : QUrl(); }
218     void setColortable(const QUrl &table);
219
220     QUrl sizetable() const { return m_sizeTable ? m_sizeTable->source : QUrl(); }
221     void setSizetable (const QUrl &table);
222
223     QUrl opacitytable() const { return m_opacityTable ? m_opacityTable->source : QUrl(); }
224     void setOpacitytable(const QUrl &table);
225
226     QColor color() const { return m_color; }
227     void setColor(const QColor &color);
228
229     qreal colorVariation() const { return m_color_variation; }
230     void setColorVariation(qreal var);
231
232     qreal alphaVariation() const { return m_alphaVariation; }
233
234     qreal alpha() const { return m_alpha; }
235
236     qreal redVariation() const { return m_redVariation; }
237
238     qreal greenVariation() const { return m_greenVariation; }
239
240     qreal blueVariation() const { return m_blueVariation; }
241
242     qreal rotation() const { return m_rotation; }
243
244     qreal rotationVariation() const { return m_rotationVariation; }
245
246     qreal rotationSpeed() const { return m_rotationSpeed; }
247
248     qreal rotationSpeedVariation() const { return m_rotationSpeedVariation; }
249
250     bool autoRotation() const { return m_autoRotation; }
251
252     QQuickDirection* xVector() const { return m_xVector; }
253
254     QQuickDirection* yVector() const { return m_yVector; }
255
256     bool spritesInterpolate() const { return m_spritesInterpolate; }
257
258     bool bypassOptimizations() const { return m_bypassOptimizations; }
259
260     EntryEffect entryEffect() const { return m_entryEffect; }
261
262     Status status() const { return m_status; }
263
264     void resetColor();
265     void resetRotation();
266     void resetDeformation();
267
268 signals:
269
270     void imageChanged();
271     void colortableChanged();
272     void sizetableChanged();
273     void opacitytableChanged();
274
275     void colorChanged();
276     void colorVariationChanged();
277
278     void alphaVariationChanged(qreal arg);
279
280     void alphaChanged(qreal arg);
281
282     void redVariationChanged(qreal arg);
283
284     void greenVariationChanged(qreal arg);
285
286     void blueVariationChanged(qreal arg);
287
288     void rotationChanged(qreal arg);
289
290     void rotationVariationChanged(qreal arg);
291
292     void rotationSpeedChanged(qreal arg);
293
294     void rotationSpeedVariationChanged(qreal arg);
295
296     void autoRotationChanged(bool arg);
297
298     void xVectorChanged(QQuickDirection* arg);
299
300     void yVectorChanged(QQuickDirection* arg);
301
302     void spritesInterpolateChanged(bool arg);
303
304     void bypassOptimizationsChanged(bool arg);
305
306     void entryEffectChanged(EntryEffect arg);
307
308     void statusChanged(Status arg);
309
310 public slots:
311     void reloadColor(const Color4ub &c, QQuickParticleData* d);
312     void setAlphaVariation(qreal arg);
313
314     void setAlpha(qreal arg);
315
316     void setRedVariation(qreal arg);
317
318     void setGreenVariation(qreal arg);
319
320     void setBlueVariation(qreal arg);
321
322     void setRotation(qreal arg);
323
324     void setRotationVariation(qreal arg);
325
326     void setRotationSpeed(qreal arg);
327
328     void setRotationSpeedVariation(qreal arg);
329
330     void setAutoRotation(bool arg);
331
332     void setXVector(QQuickDirection* arg);
333
334     void setYVector(QQuickDirection* arg);
335
336     void setSpritesInterpolate(bool arg);
337
338     void setBypassOptimizations(bool arg);
339
340     void setEntryEffect(EntryEffect arg);
341
342 protected:
343     void reset();
344     virtual void initialize(int gIdx, int pIdx);
345     virtual void commit(int gIdx, int pIdx);
346
347     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
348     void prepareNextFrame();
349     void buildParticleNodes();
350
351     void sceneGraphInvalidated();
352
353 private slots:
354     void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
355
356     void spriteAdvance(int spriteIndex);
357     void spritesUpdate(qreal time = 0 );
358     void finishBuildParticleNodes();
359 private:
360     struct ImageData {
361         QUrl source;
362         QQuickPixmap pix;
363     };
364     ImageData *m_image;
365     ImageData *m_colorTable;
366     ImageData *m_sizeTable;
367     ImageData *m_opacityTable;
368     bool loadingSomething();
369
370
371     QColor m_color;
372     qreal m_color_variation;
373
374     QSGGeometryNode *m_rootNode;
375     QHash<int, QSGGeometryNode *> m_nodes;
376     QHash<int, int> m_idxStarts;//TODO: Proper resizing will lead to needing a spriteEngine per particle - do this after sprite engine gains transparent sharing?
377     QList<QPair<int, int> > m_startsIdx;//Same data, optimized for alternate retrieval
378
379     int m_lastIdxStart;
380     QSGMaterial *m_material;
381
382     // derived values...
383
384     qreal m_alphaVariation;
385     qreal m_alpha;
386     qreal m_redVariation;
387     qreal m_greenVariation;
388     qreal m_blueVariation;
389     qreal m_rotation;
390     qreal m_rotationVariation;
391     qreal m_rotationSpeed;
392     qreal m_rotationSpeedVariation;
393     bool m_autoRotation;
394     QQuickDirection* m_xVector;
395     QQuickDirection* m_yVector;
396
397     QList<QQuickSprite*> m_sprites;
398     QQuickSpriteEngine* m_spriteEngine;
399     bool m_spritesInterpolate;
400
401     bool m_explicitColor;
402     bool m_explicitRotation;
403     bool m_explicitDeformation;
404     bool m_explicitAnimation;
405     QHash<int, QVector<QQuickParticleData*> > m_shadowData;
406     void clearShadows();
407     QQuickParticleData* getShadowDatum(QQuickParticleData* datum);
408
409     bool m_bypassOptimizations;
410     PerformanceLevel perfLevel;
411
412     PerformanceLevel m_lastLevel;
413     bool m_debugMode;
414
415     template<class Vertex>
416     void initTexCoords(Vertex* v, int count){
417         Vertex* end = v + count;
418         while (v < end){
419             v[0].tx = 0;
420             v[0].ty = 0;
421
422             v[1].tx = 1;
423             v[1].ty = 0;
424
425             v[2].tx = 0;
426             v[2].ty = 1;
427
428             v[3].tx = 1;
429             v[3].ty = 1;
430
431             v += 4;
432         }
433     }
434
435     template<class MaterialData>
436     MaterialData* getState(QSGMaterial* m){
437         return static_cast<QSGSimpleMaterial<MaterialData> *>(m)->state();
438     }
439     EntryEffect m_entryEffect;
440     Status m_status;
441     bool m_buildingNodes;
442 };
443
444 QT_END_NAMESPACE
445 QT_END_HEADER
446 #endif // ULTRAPARTICLE_H