1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
55 class ImageMaterialData;
56 class QSGGeometryNode;
59 class QQuickStochasticEngine;
74 struct ColoredVertex {
88 struct DeformableVertex {
108 float autoRotate;//Assumed that GPUs prefer floats to bools
111 struct SpriteVertex {
131 float autoRotate;//Assumed that GPUs prefer floats to bools
141 template <typename Vertex>
149 class QQuickImageParticle : public QQuickParticlePainter
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)
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)
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)
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)
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)
187 Q_PROPERTY(EntryEffect entryEffect READ entryEffect WRITE setEntryEffect NOTIFY entryEffectChanged)
191 explicit QQuickImageParticle(QQuickItem *parent = 0);
192 virtual ~QQuickImageParticle();
194 enum Status { Null, Ready, Loading, Error };
196 QQmlListProperty<QQuickSprite> sprites();
197 QQuickStochasticEngine* spriteEngine() {return m_spriteEngine;}
205 enum PerformanceLevel{//TODO: Expose?
214 QUrl image() const { return m_image ? m_image->source : QUrl(); }
215 void setImage(const QUrl &image);
217 QUrl colortable() const { return m_colorTable ? m_colorTable->source : QUrl(); }
218 void setColortable(const QUrl &table);
220 QUrl sizetable() const { return m_sizeTable ? m_sizeTable->source : QUrl(); }
221 void setSizetable (const QUrl &table);
223 QUrl opacitytable() const { return m_opacityTable ? m_opacityTable->source : QUrl(); }
224 void setOpacitytable(const QUrl &table);
226 QColor color() const { return m_color; }
227 void setColor(const QColor &color);
229 qreal colorVariation() const { return m_color_variation; }
230 void setColorVariation(qreal var);
232 qreal alphaVariation() const { return m_alphaVariation; }
234 qreal alpha() const { return m_alpha; }
236 qreal redVariation() const { return m_redVariation; }
238 qreal greenVariation() const { return m_greenVariation; }
240 qreal blueVariation() const { return m_blueVariation; }
242 qreal rotation() const { return m_rotation; }
244 qreal rotationVariation() const { return m_rotationVariation; }
246 qreal rotationSpeed() const { return m_rotationSpeed; }
248 qreal rotationSpeedVariation() const { return m_rotationSpeedVariation; }
250 bool autoRotation() const { return m_autoRotation; }
252 QQuickDirection* xVector() const { return m_xVector; }
254 QQuickDirection* yVector() const { return m_yVector; }
256 bool spritesInterpolate() const { return m_spritesInterpolate; }
258 bool bypassOptimizations() const { return m_bypassOptimizations; }
260 EntryEffect entryEffect() const { return m_entryEffect; }
262 Status status() const { return m_status; }
265 void resetRotation();
266 void resetDeformation();
271 void colortableChanged();
272 void sizetableChanged();
273 void opacitytableChanged();
276 void colorVariationChanged();
278 void alphaVariationChanged(qreal arg);
280 void alphaChanged(qreal arg);
282 void redVariationChanged(qreal arg);
284 void greenVariationChanged(qreal arg);
286 void blueVariationChanged(qreal arg);
288 void rotationChanged(qreal arg);
290 void rotationVariationChanged(qreal arg);
292 void rotationSpeedChanged(qreal arg);
294 void rotationSpeedVariationChanged(qreal arg);
296 void autoRotationChanged(bool arg);
298 void xVectorChanged(QQuickDirection* arg);
300 void yVectorChanged(QQuickDirection* arg);
302 void spritesInterpolateChanged(bool arg);
304 void bypassOptimizationsChanged(bool arg);
306 void entryEffectChanged(EntryEffect arg);
308 void statusChanged(Status arg);
311 void reloadColor(const Color4ub &c, QQuickParticleData* d);
312 void setAlphaVariation(qreal arg);
314 void setAlpha(qreal arg);
316 void setRedVariation(qreal arg);
318 void setGreenVariation(qreal arg);
320 void setBlueVariation(qreal arg);
322 void setRotation(qreal arg);
324 void setRotationVariation(qreal arg);
326 void setRotationSpeed(qreal arg);
328 void setRotationSpeedVariation(qreal arg);
330 void setAutoRotation(bool arg);
332 void setXVector(QQuickDirection* arg);
334 void setYVector(QQuickDirection* arg);
336 void setSpritesInterpolate(bool arg);
338 void setBypassOptimizations(bool arg);
340 void setEntryEffect(EntryEffect arg);
344 virtual void initialize(int gIdx, int pIdx);
345 virtual void commit(int gIdx, int pIdx);
347 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
348 void prepareNextFrame();
349 void buildParticleNodes();
351 void sceneGraphInvalidated();
354 void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
356 void spriteAdvance(int spriteIndex);
357 void spritesUpdate(qreal time = 0 );
358 void finishBuildParticleNodes();
365 ImageData *m_colorTable;
366 ImageData *m_sizeTable;
367 ImageData *m_opacityTable;
368 bool loadingSomething();
372 qreal m_color_variation;
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
380 QSGMaterial *m_material;
384 qreal m_alphaVariation;
386 qreal m_redVariation;
387 qreal m_greenVariation;
388 qreal m_blueVariation;
390 qreal m_rotationVariation;
391 qreal m_rotationSpeed;
392 qreal m_rotationSpeedVariation;
394 QQuickDirection* m_xVector;
395 QQuickDirection* m_yVector;
397 QList<QQuickSprite*> m_sprites;
398 QQuickSpriteEngine* m_spriteEngine;
399 bool m_spritesInterpolate;
401 bool m_explicitColor;
402 bool m_explicitRotation;
403 bool m_explicitDeformation;
404 bool m_explicitAnimation;
405 QHash<int, QVector<QQuickParticleData*> > m_shadowData;
407 QQuickParticleData* getShadowDatum(QQuickParticleData* datum);
409 bool m_bypassOptimizations;
410 PerformanceLevel perfLevel;
412 PerformanceLevel m_lastLevel;
415 template<class Vertex>
416 void initTexCoords(Vertex* v, int count){
417 Vertex* end = v + count;
435 template<class MaterialData>
436 MaterialData* getState(QSGMaterial* m){
437 return static_cast<QSGSimpleMaterial<MaterialData> *>(m)->state();
439 EntryEffect m_entryEffect;
441 bool m_buildingNodes;
446 #endif // ULTRAPARTICLE_H