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