343d48b654aafa398ef7265f9d8a2edc30c1c5f4
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickparticlesystem_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 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 frameCount;
204     float animT;
205     float animX;
206     float animY;
207     float animWidth;
208     float animHeight;
209     float r;
210     QQuickItem* delegate;
211     int modelIndex;
212     float update;//Used by custom affectors
213
214     //Used by image particle
215     QQuickImageParticle* colorOwner;
216     QQuickImageParticle* rotationOwner;
217     QQuickImageParticle* deformationOwner;
218     QQuickImageParticle* animationOwner;
219
220     void debugDump();
221     bool stillAlive();//Only checks end, because usually that's all you need and it's a little faster.
222     bool alive();
223     float lifeLeft();
224     float curSize();
225     void clone(const QQuickParticleData& other);//Not =, leaves meta-data like index
226     QDeclarativeV8Handle v8Value();
227     void extendLife(float time);
228 private:
229     QQuickV8ParticleData* v8Datum;
230 };
231
232 class Q_AUTOTEST_EXPORT QQuickParticleSystem : public QQuickItem
233 {
234     Q_OBJECT
235     Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
236     Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
237     Q_PROPERTY(bool empty READ isEmpty NOTIFY emptyChanged)
238
239 public:
240     explicit QQuickParticleSystem(QQuickItem *parent = 0);
241     ~QQuickParticleSystem();
242
243     bool isRunning() const
244     {
245         return m_running;
246     }
247
248     int count(){ return particleCount; }
249
250     static const int maxLife = 600000;
251
252 signals:
253
254     void systemInitialized();
255     void runningChanged(bool arg);
256     void pausedChanged(bool arg);
257     void emptyChanged(bool arg);
258
259 public slots:
260     void start(){setRunning(true);}
261     void stop(){setRunning(false);}
262     void restart(){setRunning(false);setRunning(true);}
263     void pause(){setPaused(true);}
264     void resume(){setPaused(false);}
265
266     void reset();
267     void setRunning(bool arg);
268     void setPaused(bool arg);
269
270     virtual int duration() const { return -1; }
271
272
273 protected:
274     //This one only once per frame (effectively)
275     void componentComplete();
276
277 private slots:
278     void emittersChanged();
279     void loadPainter(QObject* p);
280     void createEngine(); //Not invoked by sprite engine, unlike Sprite uses
281     void particleStateChange(int idx);
282
283 public:
284     //These can be called multiple times per frame, performance critical
285     void emitParticle(QQuickParticleData* p);
286     QQuickParticleData* newDatum(int groupId, bool respectLimits = true, int sysIdx = -1);
287     void finishNewDatum(QQuickParticleData*);
288     void moveGroups(QQuickParticleData *d, int newGIdx);
289     int nextSystemIndex();
290
291     //This one only once per painter per frame
292     int systemSync(QQuickParticlePainter* p);
293
294     //Data members here for ease of related class and auto-test usage. Not "public" API. TODO: d_ptrize
295     QSet<QQuickParticleData*> needsReset;
296     QVector<QQuickParticleData*> bySysIdx; //Another reference to the data (data owned by group), but by sysIdx
297     QHash<QString, int> groupIds;
298     QHash<int, QQuickParticleGroupData*> groupData;
299     QQuickStochasticEngine* stateEngine;
300
301     //Also only here for auto-test usage
302     void updateCurrentTime( int currentTime );
303     QQuickParticleSystemAnimation* m_animation;
304     bool m_running;
305     bool m_debugMode;
306
307     int timeInt;
308     bool initialized;
309     int particleCount;
310
311     void registerParticlePainter(QQuickParticlePainter* p);
312     void registerParticleEmitter(QQuickParticleEmitter* e);
313     void registerParticleAffector(QQuickParticleAffector* a);
314     void registerParticleGroup(QQuickParticleGroup* g);
315
316     static void statePropertyRedirect(QDeclarativeListProperty<QObject> *prop, QObject *value);
317     static void stateRedirect(QQuickParticleGroup* group, QQuickParticleSystem* sys, QObject *value);
318     bool isPaused() const
319     {
320         return m_paused;
321     }
322
323     bool isEmpty() const
324     {
325         return m_empty;
326     }
327
328 private:
329     void initializeSystem();
330     void initGroups();
331     QList<QPointer<QQuickParticleEmitter> > m_emitters;
332     QList<QPointer<QQuickParticleAffector> > m_affectors;
333     QList<QPointer<QQuickParticlePainter> > m_painters;
334     QList<QPointer<QQuickParticlePainter> > m_syncList;
335     QList<QQuickParticleGroup*> m_groups;
336     int m_nextGroupId;
337     int m_nextIndex;
338     QSet<int> m_reusableIndexes;
339     bool m_componentComplete;
340
341     QSignalMapper m_painterMapper;
342     QSignalMapper m_emitterMapper;
343     bool m_paused;
344     bool m_allDead;
345     bool m_empty;
346 };
347
348 // Internally, this animation drives all the timing. Painters sync up in their updatePaintNode
349 class QQuickParticleSystemAnimation : public QAbstractAnimation
350 {
351     Q_OBJECT
352 public:
353     QQuickParticleSystemAnimation(QQuickParticleSystem* system)
354         : QAbstractAnimation(static_cast<QObject*>(system)), m_system(system)
355     { }
356 protected:
357     virtual void updateCurrentTime( int t )
358     {
359         m_system->updateCurrentTime(t);
360     }
361
362     virtual int duration() const
363     {
364         return -1;
365     }
366
367 private:
368     QQuickParticleSystem* m_system;
369 };
370
371
372 QT_END_NAMESPACE
373
374 QT_END_HEADER
375
376 #endif // PARTICLESYSTEM_H
377
378