Rename Qt Quick-specific classes to QQuick*
[profile/ivi/qtdeclarative.git] / src / declarative / particles / qsgimageparticle_p.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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef ULTRAPARTICLE_H
43 #define ULTRAPARTICLE_H
44 #include "qsgparticlepainter_p.h"
45 #include "qsgdirection_p.h"
46 #include <QDeclarativeListProperty>
47 #include <qsgsimplematerial.h>
48 #include <QtGui/qcolor.h>
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 QT_MODULE(Declarative)
55
56 class ImageMaterialData;
57 class QSGGeometryNode;
58
59 class QQuickSprite;
60 class QQuickStochasticEngine;
61
62 struct SimpleVertex {
63     float x;
64     float y;
65     float t;
66     float lifeSpan;
67     float size;
68     float endSize;
69     float vx;
70     float vy;
71     float ax;
72     float ay;
73 };
74
75 struct ColoredVertex {
76     float x;
77     float y;
78     float t;
79     float lifeSpan;
80     float size;
81     float endSize;
82     float vx;
83     float vy;
84     float ax;
85     float ay;
86     Color4ub color;
87 };
88
89 struct DeformableVertex {
90     float x;
91     float y;
92     float tx;
93     float ty;
94     float t;
95     float lifeSpan;
96     float size;
97     float endSize;
98     float vx;
99     float vy;
100     float ax;
101     float ay;
102     Color4ub color;
103     float xx;
104     float xy;
105     float yx;
106     float yy;
107     float rotation;
108     float rotationSpeed;
109     float autoRotate;//Assumed that GPUs prefer floats to bools
110 };
111
112 struct SpriteVertex {
113     float x;
114     float y;
115     float tx;
116     float ty;
117     float t;
118     float lifeSpan;
119     float size;
120     float endSize;
121     float vx;
122     float vy;
123     float ax;
124     float ay;
125     Color4ub color;
126     float xx;
127     float xy;
128     float yx;
129     float yy;
130     float rotation;
131     float rotationSpeed;
132     float autoRotate;//Assumed that GPUs prefer floats to bools
133     float animInterpolate;
134     float frameDuration;
135     float frameCount;
136     float animT;
137     float animX;
138     float animY;
139     float animWidth;
140     float animHeight;
141 };
142
143 template <typename Vertex>
144 struct Vertices {
145     Vertex v1;
146     Vertex v2;
147     Vertex v3;
148     Vertex v4;
149 };
150
151 class QSGImageParticle : public QSGParticlePainter
152 {
153     Q_OBJECT
154     Q_PROPERTY(QUrl source READ image WRITE setImage NOTIFY imageChanged)
155     Q_PROPERTY(QUrl colorTable READ colortable WRITE setColortable NOTIFY colortableChanged)
156     Q_PROPERTY(QUrl sizeTable READ sizetable WRITE setSizetable NOTIFY sizetableChanged)
157     Q_PROPERTY(QUrl opacityTable READ opacitytable WRITE setOpacitytable NOTIFY opacitytableChanged)
158
159     //###Now just colorize - add a flag for 'solid' color particles(where the img is just a mask?)?
160     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged RESET resetColor)
161     //Stacks (added) with individual colorVariations
162     Q_PROPERTY(qreal colorVariation READ colorVariation WRITE setColorVariation NOTIFY colorVariationChanged RESET resetColor)
163     Q_PROPERTY(qreal redVariation READ redVariation WRITE setRedVariation NOTIFY redVariationChanged RESET resetColor)
164     Q_PROPERTY(qreal greenVariation READ greenVariation WRITE setGreenVariation NOTIFY greenVariationChanged RESET resetColor)
165     Q_PROPERTY(qreal blueVariation READ blueVariation WRITE setBlueVariation NOTIFY blueVariationChanged RESET resetColor)
166     //Stacks (multiplies) with the Alpha in the color, mostly here so you can use svg color names (which have full alpha)
167     Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha NOTIFY alphaChanged RESET resetColor)
168     Q_PROPERTY(qreal alphaVariation READ alphaVariation WRITE setAlphaVariation NOTIFY alphaVariationChanged RESET resetColor)
169
170     Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged RESET resetRotation)
171     Q_PROPERTY(qreal rotationVariation READ rotationVariation WRITE setRotationVariation NOTIFY rotationVariationChanged RESET resetRotation)
172     Q_PROPERTY(qreal rotationSpeed READ rotationSpeed WRITE setRotationSpeed NOTIFY rotationSpeedChanged RESET resetRotation)
173     Q_PROPERTY(qreal rotationSpeedVariation READ rotationSpeedVariation WRITE setRotationSpeedVariation NOTIFY rotationSpeedVariationChanged RESET resetRotation)
174     //If true, then will face the direction of motion. Stacks with rotation, e.g. setting rotation
175     //to 180 will lead to facing away from the direction of motion
176     Q_PROPERTY(bool autoRotation READ autoRotation WRITE setAutoRotation NOTIFY autoRotationChanged RESET resetRotation)
177
178     //###Call i/j? Makes more sense to those with vector calculus experience, and I could even add the cirumflex in QML?
179     //xVector is the vector from the top-left point to the top-right point, and is multiplied by current size
180     Q_PROPERTY(QSGDirection* xVector READ xVector WRITE setXVector NOTIFY xVectorChanged RESET resetDeformation)
181     //yVector is the same, but top-left to bottom-left. The particle is always a parallelogram.
182     Q_PROPERTY(QSGDirection* yVector READ yVector WRITE setYVector NOTIFY yVectorChanged RESET resetDeformation)
183     Q_PROPERTY(QDeclarativeListProperty<QQuickSprite> sprites READ sprites)
184     Q_PROPERTY(bool spritesInterpolate READ spritesInterpolate WRITE setSpritesInterpolate NOTIFY spritesInterpolateChanged)
185
186     Q_PROPERTY(EntryEffect entryEffect READ entryEffect WRITE setEntryEffect NOTIFY entryEffectChanged)
187     Q_PROPERTY(bool bloat READ bloat WRITE setBloat NOTIFY bloatChanged)//Just a debugging property to bypass optimizations
188     Q_ENUMS(EntryEffect)
189 public:
190     explicit QSGImageParticle(QQuickItem *parent = 0);
191     virtual ~QSGImageParticle();
192
193
194     QDeclarativeListProperty<QQuickSprite> sprites();
195     QQuickStochasticEngine* spriteEngine() {return m_spriteEngine;}
196
197     enum EntryEffect {
198         None = 0,
199         Fade = 1,
200         Scale = 2
201     };
202
203     enum PerformanceLevel{//TODO: Expose?
204         Unknown = 0,
205         Simple,
206         Colored,
207         Deformable,
208         Tabled,
209         Sprites
210     };
211
212     QUrl image() const { return m_image_name; }
213     void setImage(const QUrl &image);
214
215     QUrl colortable() const { return m_colortable_name; }
216     void setColortable(const QUrl &table);
217
218     QUrl sizetable() const { return m_sizetable_name; }
219     void setSizetable (const QUrl &table);
220
221     QUrl opacitytable() const { return m_opacitytable_name; }
222     void setOpacitytable(const QUrl &table);
223
224     QColor color() const { return m_color; }
225     void setColor(const QColor &color);
226
227     qreal colorVariation() const { return m_color_variation; }
228     void setColorVariation(qreal var);
229
230     qreal renderOpacity() const { return m_render_opacity; }
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     QSGDirection* xVector() const { return m_xVector; }
253
254     QSGDirection* yVector() const { return m_yVector; }
255
256     bool spritesInterpolate() const { return m_spritesInterpolate; }
257
258     bool bloat() const { return m_bloat; }
259
260     EntryEffect entryEffect() const { return m_entryEffect; }
261
262     void resetColor();
263     void resetRotation();
264     void resetDeformation();
265
266 signals:
267
268     void imageChanged();
269     void colortableChanged();
270     void sizetableChanged();
271     void opacitytableChanged();
272
273     void colorChanged();
274     void colorVariationChanged();
275
276     void particleDurationChanged();
277     void alphaVariationChanged(qreal arg);
278
279     void alphaChanged(qreal arg);
280
281     void redVariationChanged(qreal arg);
282
283     void greenVariationChanged(qreal arg);
284
285     void blueVariationChanged(qreal arg);
286
287     void rotationChanged(qreal arg);
288
289     void rotationVariationChanged(qreal arg);
290
291     void rotationSpeedChanged(qreal arg);
292
293     void rotationSpeedVariationChanged(qreal arg);
294
295     void autoRotationChanged(bool arg);
296
297     void xVectorChanged(QSGDirection* arg);
298
299     void yVectorChanged(QSGDirection* arg);
300
301     void spritesInterpolateChanged(bool arg);
302
303     void bloatChanged(bool arg);
304
305     void entryEffectChanged(EntryEffect arg);
306
307 public slots:
308     void reloadColor(const Color4ub &c, QSGParticleData* d);
309     void setAlphaVariation(qreal arg);
310
311     void setAlpha(qreal arg);
312
313     void setRedVariation(qreal arg);
314
315     void setGreenVariation(qreal arg);
316
317     void setBlueVariation(qreal arg);
318
319     void setRotation(qreal arg);
320
321     void setRotationVariation(qreal arg);
322
323     void setRotationSpeed(qreal arg);
324
325     void setRotationSpeedVariation(qreal arg);
326
327     void setAutoRotation(bool arg);
328
329     void setXVector(QSGDirection* arg);
330
331     void setYVector(QSGDirection* arg);
332
333     void setSpritesInterpolate(bool arg);
334
335     void setBloat(bool arg);
336
337     void setEntryEffect(EntryEffect arg);
338
339 protected:
340     void reset();
341     virtual void initialize(int gIdx, int pIdx);
342     virtual void commit(int gIdx, int pIdx);
343
344     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
345     void prepareNextFrame();
346     QSGGeometryNode* buildParticleNodes();
347
348 private slots:
349     void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
350
351 private:
352     QUrl m_image_name;
353     QUrl m_colortable_name;
354     QUrl m_sizetable_name;
355     QUrl m_opacitytable_name;
356
357
358     QColor m_color;
359     qreal m_color_variation;
360     qreal m_particleDuration;
361
362     QSGGeometryNode *m_rootNode;
363     QHash<int, QSGGeometryNode *> m_nodes;
364     QHash<int, int> m_idxStarts;//TODO: Proper resizing will lead to needing a spriteEngine per particle - do this after sprite engine gains transparent sharing?
365     int m_lastIdxStart;
366     QSGMaterial *m_material;
367
368     // derived values...
369
370     qreal m_render_opacity;
371     qreal m_alphaVariation;
372     qreal m_alpha;
373     qreal m_redVariation;
374     qreal m_greenVariation;
375     qreal m_blueVariation;
376     qreal m_rotation;
377     qreal m_rotationVariation;
378     qreal m_rotationSpeed;
379     qreal m_rotationSpeedVariation;
380     bool m_autoRotation;
381     QSGDirection* m_xVector;
382     QSGDirection* m_yVector;
383
384     QList<QQuickSprite*> m_sprites;
385     QQuickSpriteEngine* m_spriteEngine;
386     bool m_spritesInterpolate;
387
388     bool m_explicitColor;
389     bool m_explicitRotation;
390     bool m_explicitDeformation;
391     bool m_explicitAnimation;
392     QHash<int, QVector<QSGParticleData*> > m_shadowData;
393     bool m_shadowInit;
394     void clearShadows();
395     QSGParticleData* getShadowDatum(QSGParticleData* datum);
396
397     bool m_bloat;
398     PerformanceLevel perfLevel;
399
400     PerformanceLevel m_lastLevel;
401     bool m_debugMode;
402
403     template<class Vertex>
404     void initTexCoords(Vertex* v, int count){
405         Vertex* end = v + count;
406         while (v < end){
407             v[0].tx = 0;
408             v[0].ty = 0;
409
410             v[1].tx = 1;
411             v[1].ty = 0;
412
413             v[2].tx = 0;
414             v[2].ty = 1;
415
416             v[3].tx = 1;
417             v[3].ty = 1;
418
419             v += 4;
420         }
421     }
422
423     template<class MaterialData>
424     MaterialData* getState(QSGMaterial* m){
425         return static_cast<QSGSimpleMaterial<MaterialData> *>(m)->state();
426     }
427     EntryEffect m_entryEffect;
428 };
429
430 QT_END_NAMESPACE
431 QT_END_HEADER
432 #endif // ULTRAPARTICLE_H