Add BurstEmitter, and a simple render path for UltraParticles
[profile/ivi/qtdeclarative.git] / src / imports / particles / ultraparticle.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef ULTRAPARTICLE_H
43 #define ULTRAPARTICLE_H
44 #include "particle.h"
45 #include "varyingvector.h"
46 #include <QDeclarativeListProperty>
47
48 #include "coloredparticle.h"
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 QT_MODULE(Declarative)
55
56 class UltraMaterial;
57 class QSGGeometryNode;
58
59 class SpriteState;
60 class SpriteEngine;
61
62 /*struct Color4ub {//in coloredparticle
63     uchar r;
64     uchar g;
65     uchar b;
66     uchar a;
67 };*/
68
69 struct SimpleVertex {
70     float x;
71     float y;
72     float tx;
73     float ty;
74     float t;
75     float lifeSpan;
76     float size;
77     float endSize;
78     float sx;
79     float sy;
80     float ax;
81     float ay;
82 };
83
84 struct SimpleVertices {
85     SimpleVertex v1;
86     SimpleVertex v2;
87     SimpleVertex v3;
88     SimpleVertex v4;
89 };
90
91 struct UltraVertex {
92     float x;
93     float y;
94     float tx;
95     float ty;
96     float t;
97     float lifeSpan;
98     float size;
99     float endSize;
100     float sx;
101     float sy;
102     float ax;
103     float ay;
104     Color4ub color;
105     float xx;
106     float xy;
107     float yx;
108     float yy;
109     float rotation;
110     float rotationSpeed;
111     float autoRotate;//Assume that GPUs prefer floats to bools
112     float animIdx;
113     float frameDuration;
114     float frameCount;
115     float animT;
116 };
117
118 struct UltraVertices {
119     UltraVertex v1;
120     UltraVertex v2;
121     UltraVertex v3;
122     UltraVertex v4;
123 };
124
125 struct IntermediateVertices {
126     UltraVertex* v1;
127     UltraVertex* v2;
128     UltraVertex* v3;
129     UltraVertex* v4;
130 };
131
132 class UltraParticle : public ParticleType
133 {
134     Q_OBJECT
135     Q_PROPERTY(QUrl image READ image WRITE setImage NOTIFY imageChanged)
136     Q_PROPERTY(QUrl colorTable READ colortable WRITE setColortable NOTIFY colortableChanged)
137     Q_PROPERTY(QUrl sizeTable READ sizetable WRITE setSizetable NOTIFY sizetableChanged)
138     Q_PROPERTY(QUrl opacityTable READ opacitytable WRITE setOpacitytable NOTIFY opacitytableChanged)
139
140     //###Now just colorize - add a flag for 'solid' color particles(where the img is just a mask?)?
141     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
142     //Stacks (added) with individual colorVariations
143     Q_PROPERTY(qreal colorVariation READ colorVariation WRITE setColorVariation NOTIFY colorVariationChanged)
144     Q_PROPERTY(qreal redVariation READ redVariation WRITE setRedVariation NOTIFY redVariationChanged)
145     Q_PROPERTY(qreal greenVariation READ greenVariation WRITE setGreenVariation NOTIFY greenVariationChanged)
146     Q_PROPERTY(qreal blueVariation READ blueVariation WRITE setBlueVariation NOTIFY blueVariationChanged)
147     //Stacks (multiplies) with the Alpha in the color, mostly here so you can use svg color names (which have full alpha)
148     Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha NOTIFY alphaChanged)
149     Q_PROPERTY(qreal alphaVariation READ alphaVariation WRITE setAlphaVariation NOTIFY alphaVariationChanged)
150
151     Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged)
152     Q_PROPERTY(qreal rotationVariation READ rotationVariation WRITE setRotationVariation NOTIFY rotationVariationChanged)
153     Q_PROPERTY(qreal rotationSpeed READ rotationSpeed WRITE setRotationSpeed NOTIFY rotationSpeedChanged)
154     Q_PROPERTY(qreal rotationSpeedVariation READ rotationSpeedVariation WRITE setRotationSpeedVariation NOTIFY rotationSpeedVariationChanged)
155     //If true, then will face the direction of motion. Stacks with rotation, e.g. setting rotation
156     //to 180 will lead to facing away from the direction of motion
157     Q_PROPERTY(bool autoRotation READ autoRotation WRITE autoRotation NOTIFY autoRotationChanged)
158
159     //###Call i/j? Makes more sense to those with vector calculus experience, and I could even add the cirumflex in QML?
160     //xVector is the vector from the top-left point to the top-right point, and is multiplied by current size
161     Q_PROPERTY(VaryingVector* xVector READ xVector WRITE setXVector NOTIFY xVectorChanged)
162     //yVector is the same, but top-left to bottom-left. The particle is always a parallelogram.
163     Q_PROPERTY(VaryingVector* yVector READ yVector WRITE setYVector NOTIFY yVectorChanged)
164     Q_PROPERTY(QDeclarativeListProperty<SpriteState> sprites READ sprites)
165     Q_PROPERTY(bool bloat READ bloat WRITE setBloat NOTIFY bloatChanged)//Just a debugging property to bypass optimizations
166 public:
167     explicit UltraParticle(QSGItem *parent = 0);
168     virtual ~UltraParticle(){}
169
170     virtual void load(ParticleData*);
171     virtual void reload(ParticleData*);
172     virtual void setCount(int c);
173
174     QDeclarativeListProperty<SpriteState> sprites();
175     SpriteEngine* spriteEngine() {return m_spriteEngine;}
176
177     enum PerformanceLevel{//TODO: Expose?
178         Unknown = 0,
179         Simple,
180         Coloured,
181         Deformable,
182         Tabled,
183         Sprites
184     };
185
186     QUrl image() const { return m_image_name; }
187     void setImage(const QUrl &image);
188
189     QUrl colortable() const { return m_colortable_name; }
190     void setColortable(const QUrl &table);
191
192     QUrl sizetable() const { return m_sizetable_name; }
193     void setSizetable (const QUrl &table);
194
195     QUrl opacitytable() const { return m_opacitytable_name; }
196     void setOpacitytable(const QUrl &table);
197
198     QColor color() const { return m_color; }
199     void setColor(const QColor &color);
200
201     qreal colorVariation() const { return m_color_variation; }
202     void setColorVariation(qreal var);
203
204     qreal renderOpacity() const { return m_render_opacity; }
205
206     qreal alphaVariation() const
207     {
208         return m_alphaVariation;
209     }
210
211     qreal alpha() const
212     {
213         return m_alpha;
214     }
215
216     qreal redVariation() const
217     {
218         return m_redVariation;
219     }
220
221     qreal greenVariation() const
222     {
223         return m_greenVariation;
224     }
225
226     qreal blueVariation() const
227     {
228         return m_blueVariation;
229     }
230
231     qreal rotation() const
232     {
233         return m_rotation;
234     }
235
236     qreal rotationVariation() const
237     {
238         return m_rotationVariation;
239     }
240
241     qreal rotationSpeed() const
242     {
243         return m_rotationSpeed;
244     }
245
246     qreal rotationSpeedVariation() const
247     {
248         return m_rotationSpeedVariation;
249     }
250
251     bool autoRotation() const
252     {
253         return m_autoRotation;
254     }
255
256     VaryingVector* xVector() const
257     {
258         return m_xVector;
259     }
260
261     VaryingVector* yVector() const
262     {
263         return m_yVector;
264     }
265
266     bool bloat() const
267     {
268         return m_bloat;
269     }
270
271 signals:
272
273     void imageChanged();
274     void colortableChanged();
275     void sizetableChanged();
276     void opacitytableChanged();
277
278     void colorChanged();
279     void colorVariationChanged();
280
281     void particleDurationChanged();
282     void alphaVariationChanged(qreal arg);
283
284     void alphaChanged(qreal arg);
285
286     void redVariationChanged(qreal arg);
287
288     void greenVariationChanged(qreal arg);
289
290     void blueVariationChanged(qreal arg);
291
292     void rotationChanged(qreal arg);
293
294     void rotationVariationChanged(qreal arg);
295
296     void rotationSpeedChanged(qreal arg);
297
298     void rotationSpeedVariationChanged(qreal arg);
299
300     void autoRotationChanged(bool arg);
301
302     void xVectorChanged(VaryingVector* arg);
303
304     void yVectorChanged(VaryingVector* arg);
305
306     void bloatChanged(bool arg);
307
308 public slots:
309     void setAlphaVariation(qreal arg)
310     {
311         if (m_alphaVariation != arg) {
312             m_alphaVariation = arg;
313             emit alphaVariationChanged(arg);
314         }
315     }
316
317     void setAlpha(qreal arg)
318     {
319         if (m_alpha != arg) {
320             m_alpha = arg;
321             emit alphaChanged(arg);
322         }
323     }
324
325     void setRedVariation(qreal arg)
326     {
327         if (m_redVariation != arg) {
328             m_redVariation = arg;
329             emit redVariationChanged(arg);
330         }
331     }
332
333     void setGreenVariation(qreal arg)
334     {
335         if (m_greenVariation != arg) {
336             m_greenVariation = arg;
337             emit greenVariationChanged(arg);
338         }
339     }
340
341     void setBlueVariation(qreal arg)
342     {
343         if (m_blueVariation != arg) {
344             m_blueVariation = arg;
345             emit blueVariationChanged(arg);
346         }
347     }
348
349     void reloadColor(const Color4ub &c, ParticleData* d);
350     void setRotation(qreal arg)
351     {
352         if (m_rotation != arg) {
353             m_rotation = arg;
354             emit rotationChanged(arg);
355         }
356     }
357
358     void setRotationVariation(qreal arg)
359     {
360         if (m_rotationVariation != arg) {
361             m_rotationVariation = arg;
362             emit rotationVariationChanged(arg);
363         }
364     }
365
366     void setRotationSpeed(qreal arg)
367     {
368         if (m_rotationSpeed != arg) {
369             m_rotationSpeed = arg;
370             emit rotationSpeedChanged(arg);
371         }
372     }
373
374     void setRotationSpeedVariation(qreal arg)
375     {
376         if (m_rotationSpeedVariation != arg) {
377             m_rotationSpeedVariation = arg;
378             emit rotationSpeedVariationChanged(arg);
379         }
380     }
381
382     void autoRotation(bool arg)
383     {
384         if (m_autoRotation != arg) {
385             m_autoRotation = arg;
386             emit autoRotationChanged(arg);
387         }
388     }
389
390     void setXVector(VaryingVector* arg)
391     {
392         if (m_xVector != arg) {
393             m_xVector = arg;
394             emit xVectorChanged(arg);
395         }
396     }
397
398     void setYVector(VaryingVector* arg)
399     {
400         if (m_yVector != arg) {
401             m_yVector = arg;
402             emit yVectorChanged(arg);
403         }
404     }
405
406     void setBloat(bool arg)
407     {
408         if (m_bloat != arg) {
409             m_bloat = arg;
410             emit bloatChanged(arg);
411         }
412     }
413
414 protected:
415     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
416     void reset();
417     void prepareNextFrame();
418     QSGGeometryNode* buildParticleNode();
419     QSGGeometryNode* buildSimpleParticleNode();
420
421 private slots:
422     void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
423
424 private:
425     //void vertexCopy(UltraVertex &b,const ParticleVertex& a);
426     IntermediateVertices* fetchIntermediateVertices(int pos);
427     bool m_do_reset;
428
429     QUrl m_image_name;
430     QUrl m_colortable_name;
431     QUrl m_sizetable_name;
432     QUrl m_opacitytable_name;
433
434
435     QColor m_color;
436     qreal m_color_variation;
437     qreal m_particleDuration;
438
439     QSGGeometryNode *m_node;
440     UltraMaterial *m_material;
441
442     // derived values...
443     int m_last_particle;
444
445     qreal m_render_opacity;
446     qreal m_alphaVariation;
447     qreal m_alpha;
448     qreal m_redVariation;
449     qreal m_greenVariation;
450     qreal m_blueVariation;
451     qreal m_rotation;
452     qreal m_rotationVariation;
453     qreal m_rotationSpeed;
454     qreal m_rotationSpeedVariation;
455     bool m_autoRotation;
456     VaryingVector* m_xVector;
457     VaryingVector* m_yVector;
458
459     QList<SpriteState*> m_sprites;
460     SpriteEngine* m_spriteEngine;
461
462     bool m_bloat;
463     PerformanceLevel perfLevel;
464 };
465
466 QT_END_NAMESPACE
467 QT_END_HEADER
468 #endif // ULTRAPARTICLE_H