Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickparticlesystem_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 PARTICLESYSTEM_H
43 #define PARTICLESYSTEM_H
44
45 #include <QtQuick/QQuickItem>
46 #include <QElapsedTimer>
47 #include <QVector>
48 #include <QHash>
49 #include <QPointer>
50 #include <QSignalMapper>
51 #include <private/qquicksprite_p.h>
52 #include <QAbstractAnimation>
53 #include <QtDeclarative/qdeclarative.h>
54 #include <private/qv8engine_p.h> //For QDeclarativeV8Handle
55
56 QT_BEGIN_HEADER
57
58 QT_BEGIN_NAMESPACE
59
60 class QQuickParticleSystem;
61 class QQuickParticleAffector;
62 class QQuickParticleEmitter;
63 class QQuickParticlePainter;
64 class QQuickParticleData;
65 class QQuickParticleSystemAnimation;
66 class QQuickStochasticEngine;
67 class QQuickSprite;
68 class QQuickV8ParticleData;
69 class QQuickParticleGroup;
70 class QQuickImageParticle;
71
72 struct QQuickParticleDataHeapNode{
73     int time;//in ms
74     QSet<QQuickParticleData*> data;//Set ptrs instead?
75 };
76
77 class QQuickParticleDataHeap {
78     //Idea is to do a binary heap, but which also stores a set of int,Node* so that if the int already exists, you can
79     //add it to the data* list. Pops return the whole list at once.
80 public:
81     QQuickParticleDataHeap();
82     void insert(QQuickParticleData* data);
83     void insertTimed(QQuickParticleData* data, int time);
84
85     int top();
86
87     QSet<QQuickParticleData*> pop();
88
89     void clear();
90
91     bool contains(QQuickParticleData*);//O(n), for debugging purposes only
92 private:
93     void grow();
94     void swap(int, int);
95     void bubbleUp(int);
96     void bubbleDown(int);
97     int m_size;
98     int m_end;
99     QQuickParticleDataHeapNode m_tmp;
100     QVector<QQuickParticleDataHeapNode> m_data;
101     QHash<int,int> m_lookups;
102 };
103
104 class Q_AUTOTEST_EXPORT QQuickParticleGroupData {
105 public:
106     QQuickParticleGroupData(int id, QQuickParticleSystem* sys);
107     ~QQuickParticleGroupData();
108
109     int size();
110     QString name();
111
112     void setSize(int newSize);
113
114     int index;
115     QSet<QQuickParticlePainter*> painters;//TODO: What if they are dynamically removed?
116
117     //TODO: Refactor particle data list out into a separate class
118     QVector<QQuickParticleData*> data;
119     QQuickParticleDataHeap dataHeap;
120     QSet<int> reusableIndexes;
121     bool recycle(); //Force recycling round, returns true if all indexes are now reusable
122
123     void initList();
124     void kill(QQuickParticleData* d);
125
126     //After calling this, initialize, then call prepareRecycler(d)
127     QQuickParticleData* newDatum(bool respectsLimits);
128
129     //TODO: Find and clean up those that don't get added to the recycler (currently they get lost)
130     void prepareRecycler(QQuickParticleData* d);
131
132 private:
133     int m_size;
134     QQuickParticleSystem* m_system;
135 };
136
137 struct Color4ub {
138     uchar r;
139     uchar g;
140     uchar b;
141     uchar a;
142 };
143
144 class Q_AUTOTEST_EXPORT QQuickParticleData {
145 public:
146     //TODO: QObject like memory management (without the cost, just attached to system)
147     QQuickParticleData(QQuickParticleSystem* sys);
148     ~QQuickParticleData();
149
150     //Convenience functions for working backwards, because parameters are from the start of particle life
151     //If setting multiple parameters at once, doing the conversion yourself will be faster.
152
153     //sets the x accleration without affecting the instantaneous x velocity or position
154     void setInstantaneousAX(qreal ax);
155     //sets the x velocity without affecting the instantaneous x postion
156     void setInstantaneousVX(qreal vx);
157     //sets the instantaneous x postion
158     void setInstantaneousX(qreal x);
159     //sets the y accleration without affecting the instantaneous y velocity or position
160     void setInstantaneousAY(qreal ay);
161     //sets the y velocity without affecting the instantaneous y postion
162     void setInstantaneousVY(qreal vy);
163     //sets the instantaneous Y postion
164     void setInstantaneousY(qreal y);
165
166     //TODO: Slight caching?
167     qreal curX() const;
168     qreal curVX() const;
169     qreal curAX() const { return ax; }
170     qreal curY() const;
171     qreal curVY() const;
172     qreal curAY() const { return ay; }
173
174     int group;
175     QQuickParticleEmitter* e;//### Needed?
176     QQuickParticleSystem* system;
177     int index;
178     int systemIndex;
179
180     //General Position Stuff
181     float x;
182     float y;
183     float t;
184     float lifeSpan;
185     float size;
186     float endSize;
187     float vx;
188     float vy;
189     float ax;
190     float ay;
191
192     //Other stuff, now universally shared
193     Color4ub color;
194     float xx;
195     float xy;
196     float yx;
197     float yy;
198     float rotation;
199     float rotationSpeed;
200     float autoRotate;//Assume that GPUs prefer floats to bools
201     float animIdx;
202     float frameDuration;
203     float frameAt;//Used for duration -1
204     float frameCount;
205     float animT;
206     float animX;
207     float animY;
208     float animWidth;
209     float animHeight;
210     float r;
211     QQuickItem* delegate;
212     int modelIndex;
213     float update;//Used by custom affectors
214
215     //Used by image particle
216     QQuickImageParticle* colorOwner;
217     QQuickImageParticle* rotationOwner;
218     QQuickImageParticle* deformationOwner;
219     QQuickImageParticle* animationOwner;
220
221     void debugDump();
222     bool stillAlive();//Only checks end, because usually that's all you need and it's a little faster.
223     bool alive();
224     float lifeLeft();
225     float curSize();
226     void clone(const QQuickParticleData& other);//Not =, leaves meta-data like index
227     QDeclarativeV8Handle v8Value();
228     void extendLife(float time);
229 private:
230     QQuickV8ParticleData* v8Datum;
231 };
232
233 class Q_AUTOTEST_EXPORT QQuickParticleSystem : public QQuickItem
234 {
235     Q_OBJECT
236     Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
237     Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
238     Q_PROPERTY(bool empty READ isEmpty NOTIFY emptyChanged)
239
240 public:
241     explicit QQuickParticleSystem(QQuickItem *parent = 0);
242     ~QQuickParticleSystem();
243
244     bool isRunning() const
245     {
246         return m_running;
247     }
248
249     int count(){ return particleCount; }
250
251     static const int maxLife = 600000;
252
253 signals:
254
255     void systemInitialized();
256     void runningChanged(bool arg);
257     void pausedChanged(bool arg);
258     void emptyChanged(bool arg);
259
260 public slots:
261     void start(){setRunning(true);}
262     void stop(){setRunning(false);}
263     void restart(){setRunning(false);setRunning(true);}
264     void pause(){setPaused(true);}
265     void resume(){setPaused(false);}
266
267     void reset();
268     void setRunning(bool arg);
269     void setPaused(bool arg);
270
271     virtual int duration() const { return -1; }
272
273
274 protected:
275     //This one only once per frame (effectively)
276     void componentComplete();
277
278 private slots:
279     void emittersChanged();
280     void loadPainter(QObject* p);
281     void createEngine(); //Not invoked by sprite engine, unlike Sprite uses
282     void particleStateChange(int idx);
283
284 public:
285     //These can be called multiple times per frame, performance critical
286     void emitParticle(QQuickParticleData* p);
287     QQuickParticleData* newDatum(int groupId, bool respectLimits = true, int sysIdx = -1);
288     void finishNewDatum(QQuickParticleData*);
289     void moveGroups(QQuickParticleData *d, int newGIdx);
290     int nextSystemIndex();
291
292     //This one only once per painter per frame
293     int systemSync(QQuickParticlePainter* p);
294
295     //Data members here for ease of related class and auto-test usage. Not "public" API. TODO: d_ptrize
296     QSet<QQuickParticleData*> needsReset;
297     QVector<QQuickParticleData*> bySysIdx; //Another reference to the data (data owned by group), but by sysIdx
298     QHash<QString, int> groupIds;
299     QHash<int, QQuickParticleGroupData*> groupData;
300     QQuickStochasticEngine* stateEngine;
301
302     //Also only here for auto-test usage
303     void updateCurrentTime( int currentTime );
304     QQuickParticleSystemAnimation* m_animation;
305     bool m_running;
306     bool m_debugMode;
307
308     int timeInt;
309     bool initialized;
310     int particleCount;
311
312     void registerParticlePainter(QQuickParticlePainter* p);
313     void registerParticleEmitter(QQuickParticleEmitter* e);
314     void registerParticleAffector(QQuickParticleAffector* a);
315     void registerParticleGroup(QQuickParticleGroup* g);
316
317     static void statePropertyRedirect(QDeclarativeListProperty<QObject> *prop, QObject *value);
318     static void stateRedirect(QQuickParticleGroup* group, QQuickParticleSystem* sys, QObject *value);
319     bool isPaused() const
320     {
321         return m_paused;
322     }
323
324     bool isEmpty() const
325     {
326         return m_empty;
327     }
328
329 private:
330     void initializeSystem();
331     void initGroups();
332     QList<QPointer<QQuickParticleEmitter> > m_emitters;
333     QList<QPointer<QQuickParticleAffector> > m_affectors;
334     QList<QPointer<QQuickParticlePainter> > m_painters;
335     QList<QPointer<QQuickParticlePainter> > m_syncList;
336     QList<QQuickParticleGroup*> m_groups;
337     int m_nextGroupId;
338     int m_nextIndex;
339     QSet<int> m_reusableIndexes;
340     bool m_componentComplete;
341
342     QSignalMapper m_painterMapper;
343     QSignalMapper m_emitterMapper;
344     bool m_paused;
345     bool m_allDead;
346     bool m_empty;
347 };
348
349 // Internally, this animation drives all the timing. Painters sync up in their updatePaintNode
350 class QQuickParticleSystemAnimation : public QAbstractAnimation
351 {
352     Q_OBJECT
353 public:
354     QQuickParticleSystemAnimation(QQuickParticleSystem* system)
355         : QAbstractAnimation(static_cast<QObject*>(system)), m_system(system)
356     { }
357 protected:
358     virtual void updateCurrentTime( int t )
359     {
360         m_system->updateCurrentTime(t);
361     }
362
363     virtual int duration() const
364     {
365         return -1;
366     }
367
368 private:
369     QQuickParticleSystem* m_system;
370 };
371
372
373 QT_END_NAMESPACE
374
375 QT_END_HEADER
376
377 #endif // PARTICLESYSTEM_H
378
379