1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the Declarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
54 class ImageMaterialData;
55 class QSGGeometryNode;
58 class QQuickStochasticEngine;
73 struct ColoredVertex {
87 struct DeformableVertex {
107 float autoRotate;//Assumed that GPUs prefer floats to bools
110 struct SpriteVertex {
130 float autoRotate;//Assumed that GPUs prefer floats to bools
131 float animInterpolate;
141 template <typename Vertex>
149 class QQuickImageParticle : public QQuickParticlePainter
152 Q_PROPERTY(QUrl source READ image WRITE setImage NOTIFY imageChanged)
153 Q_PROPERTY(QUrl colorTable READ colortable WRITE setColortable NOTIFY colortableChanged)
154 Q_PROPERTY(QUrl sizeTable READ sizetable WRITE setSizetable NOTIFY sizetableChanged)
155 Q_PROPERTY(QUrl opacityTable READ opacitytable WRITE setOpacitytable NOTIFY opacitytableChanged)
157 //###Now just colorize - add a flag for 'solid' color particles(where the img is just a mask?)?
158 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged RESET resetColor)
159 //Stacks (added) with individual colorVariations
160 Q_PROPERTY(qreal colorVariation READ colorVariation WRITE setColorVariation NOTIFY colorVariationChanged RESET resetColor)
161 Q_PROPERTY(qreal redVariation READ redVariation WRITE setRedVariation NOTIFY redVariationChanged RESET resetColor)
162 Q_PROPERTY(qreal greenVariation READ greenVariation WRITE setGreenVariation NOTIFY greenVariationChanged RESET resetColor)
163 Q_PROPERTY(qreal blueVariation READ blueVariation WRITE setBlueVariation NOTIFY blueVariationChanged RESET resetColor)
164 //Stacks (multiplies) with the Alpha in the color, mostly here so you can use svg color names (which have full alpha)
165 Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha NOTIFY alphaChanged RESET resetColor)
166 Q_PROPERTY(qreal alphaVariation READ alphaVariation WRITE setAlphaVariation NOTIFY alphaVariationChanged RESET resetColor)
168 Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged RESET resetRotation)
169 Q_PROPERTY(qreal rotationVariation READ rotationVariation WRITE setRotationVariation NOTIFY rotationVariationChanged RESET resetRotation)
170 Q_PROPERTY(qreal rotationSpeed READ rotationSpeed WRITE setRotationSpeed NOTIFY rotationSpeedChanged RESET resetRotation)
171 Q_PROPERTY(qreal rotationSpeedVariation READ rotationSpeedVariation WRITE setRotationSpeedVariation NOTIFY rotationSpeedVariationChanged RESET resetRotation)
172 //If true, then will face the direction of motion. Stacks with rotation, e.g. setting rotation
173 //to 180 will lead to facing away from the direction of motion
174 Q_PROPERTY(bool autoRotation READ autoRotation WRITE setAutoRotation NOTIFY autoRotationChanged RESET resetRotation)
176 //###Call i/j? Makes more sense to those with vector calculus experience, and I could even add the cirumflex in QML?
177 //xVector is the vector from the top-left point to the top-right point, and is multiplied by current size
178 Q_PROPERTY(QQuickDirection* xVector READ xVector WRITE setXVector NOTIFY xVectorChanged RESET resetDeformation)
179 //yVector is the same, but top-left to bottom-left. The particle is always a parallelogram.
180 Q_PROPERTY(QQuickDirection* yVector READ yVector WRITE setYVector NOTIFY yVectorChanged RESET resetDeformation)
181 Q_PROPERTY(QDeclarativeListProperty<QQuickSprite> sprites READ sprites)
182 Q_PROPERTY(bool spritesInterpolate READ spritesInterpolate WRITE setSpritesInterpolate NOTIFY spritesInterpolateChanged)
184 Q_PROPERTY(EntryEffect entryEffect READ entryEffect WRITE setEntryEffect NOTIFY entryEffectChanged)
187 explicit QQuickImageParticle(QQuickItem *parent = 0);
188 virtual ~QQuickImageParticle();
191 QDeclarativeListProperty<QQuickSprite> sprites();
192 QQuickStochasticEngine* spriteEngine() {return m_spriteEngine;}
200 enum PerformanceLevel{//TODO: Expose?
209 QUrl image() const { return m_image_name; }
210 void setImage(const QUrl &image);
212 QUrl colortable() const { return m_colortable_name; }
213 void setColortable(const QUrl &table);
215 QUrl sizetable() const { return m_sizetable_name; }
216 void setSizetable (const QUrl &table);
218 QUrl opacitytable() const { return m_opacitytable_name; }
219 void setOpacitytable(const QUrl &table);
221 QColor color() const { return m_color; }
222 void setColor(const QColor &color);
224 qreal colorVariation() const { return m_color_variation; }
225 void setColorVariation(qreal var);
227 qreal alphaVariation() const { return m_alphaVariation; }
229 qreal alpha() const { return m_alpha; }
231 qreal redVariation() const { return m_redVariation; }
233 qreal greenVariation() const { return m_greenVariation; }
235 qreal blueVariation() const { return m_blueVariation; }
237 qreal rotation() const { return m_rotation; }
239 qreal rotationVariation() const { return m_rotationVariation; }
241 qreal rotationSpeed() const { return m_rotationSpeed; }
243 qreal rotationSpeedVariation() const { return m_rotationSpeedVariation; }
245 bool autoRotation() const { return m_autoRotation; }
247 QQuickDirection* xVector() const { return m_xVector; }
249 QQuickDirection* yVector() const { return m_yVector; }
251 bool spritesInterpolate() const { return m_spritesInterpolate; }
253 bool bypassOptimizations() const { return m_bypassOptimizations; }
255 EntryEffect entryEffect() const { return m_entryEffect; }
258 void resetRotation();
259 void resetDeformation();
264 void colortableChanged();
265 void sizetableChanged();
266 void opacitytableChanged();
269 void colorVariationChanged();
271 void alphaVariationChanged(qreal arg);
273 void alphaChanged(qreal arg);
275 void redVariationChanged(qreal arg);
277 void greenVariationChanged(qreal arg);
279 void blueVariationChanged(qreal arg);
281 void rotationChanged(qreal arg);
283 void rotationVariationChanged(qreal arg);
285 void rotationSpeedChanged(qreal arg);
287 void rotationSpeedVariationChanged(qreal arg);
289 void autoRotationChanged(bool arg);
291 void xVectorChanged(QQuickDirection* arg);
293 void yVectorChanged(QQuickDirection* arg);
295 void spritesInterpolateChanged(bool arg);
297 void bypassOptimizationsChanged(bool arg);
299 void entryEffectChanged(EntryEffect arg);
302 void reloadColor(const Color4ub &c, QQuickParticleData* d);
303 void setAlphaVariation(qreal arg);
305 void setAlpha(qreal arg);
307 void setRedVariation(qreal arg);
309 void setGreenVariation(qreal arg);
311 void setBlueVariation(qreal arg);
313 void setRotation(qreal arg);
315 void setRotationVariation(qreal arg);
317 void setRotationSpeed(qreal arg);
319 void setRotationSpeedVariation(qreal arg);
321 void setAutoRotation(bool arg);
323 void setXVector(QQuickDirection* arg);
325 void setYVector(QQuickDirection* arg);
327 void setSpritesInterpolate(bool arg);
329 void setBypassOptimizations(bool arg);
331 void setEntryEffect(EntryEffect arg);
335 virtual void initialize(int gIdx, int pIdx);
336 virtual void commit(int gIdx, int pIdx);
338 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
339 void prepareNextFrame();
340 QSGGeometryNode* buildParticleNodes();
343 void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
345 void spriteAdvance(int spriteIndex);
348 QUrl m_colortable_name;
349 QUrl m_sizetable_name;
350 QUrl m_opacitytable_name;
354 qreal m_color_variation;
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
362 QSGMaterial *m_material;
366 qreal m_alphaVariation;
368 qreal m_redVariation;
369 qreal m_greenVariation;
370 qreal m_blueVariation;
372 qreal m_rotationVariation;
373 qreal m_rotationSpeed;
374 qreal m_rotationSpeedVariation;
376 QQuickDirection* m_xVector;
377 QQuickDirection* m_yVector;
379 QList<QQuickSprite*> m_sprites;
380 QQuickSpriteEngine* m_spriteEngine;
381 bool m_spritesInterpolate;
383 bool m_explicitColor;
384 bool m_explicitRotation;
385 bool m_explicitDeformation;
386 bool m_explicitAnimation;
387 QHash<int, QVector<QQuickParticleData*> > m_shadowData;
389 QQuickParticleData* getShadowDatum(QQuickParticleData* datum);
391 bool m_bypassOptimizations;
392 PerformanceLevel perfLevel;
394 PerformanceLevel m_lastLevel;
397 template<class Vertex>
398 void initTexCoords(Vertex* v, int count){
399 Vertex* end = v + count;
417 template<class MaterialData>
418 MaterialData* getState(QSGMaterial* m){
419 return static_cast<QSGSimpleMaterial<MaterialData> *>(m)->state();
421 EntryEffect m_entryEffect;
426 #endif // ULTRAPARTICLE_H