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 QQUICKSPRITEENGINE_P_H
43 #define QQUICKSPRITEENGINE_P_H
50 #include <QDeclarativeListProperty>
59 class Q_AUTOTEST_EXPORT QQuickStochasticState : public QObject //Currently for internal use only - Sprite and ParticleGroup
62 Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
63 Q_PROPERTY(int durationVariation READ durationVariation WRITE setDurationVariation NOTIFY durationVariationChanged)
64 //Note than manually advanced sprites need to query this variable and implement own behaviour for it
65 Q_PROPERTY(bool randomStart READ randomStart WRITE setRandomStart NOTIFY randomStartChanged)
66 Q_PROPERTY(QVariantMap to READ to WRITE setTo NOTIFY toChanged)
67 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
70 QQuickStochasticState(QObject* parent = 0)
73 , m_durationVariation(0)
74 , m_randomStart(false)
88 QVariantMap to() const
93 int durationVariation() const
95 return m_durationVariation;
99 virtual int variedDuration() const
101 return qMax(qreal(0.0) , m_duration
102 + (m_durationVariation * ((qreal)qrand()/RAND_MAX) * 2)
103 - m_durationVariation);
106 bool randomStart() const
108 return m_randomStart;
112 void durationChanged(int arg);
114 void nameChanged(QString arg);
116 void toChanged(QVariantMap arg);
118 void durationVariationChanged(int arg);
120 void entered();//### Just playing around - don't expect full state API
122 void randomStartChanged(bool arg);
125 void setDuration(int arg)
127 if (m_duration != arg) {
129 emit durationChanged(arg);
133 void setName(QString arg)
137 emit nameChanged(arg);
141 void setTo(QVariantMap arg)
149 void setDurationVariation(int arg)
151 if (m_durationVariation != arg) {
152 m_durationVariation = arg;
153 emit durationVariationChanged(arg);
157 void setRandomStart(bool arg)
159 if (m_randomStart != arg) {
161 emit randomStartChanged(arg);
169 int m_durationVariation;
171 friend class QQuickStochasticEngine;
175 class Q_AUTOTEST_EXPORT QQuickStochasticEngine : public QObject
178 //TODO: Optimize single state case?
179 Q_PROPERTY(QString globalGoal READ globalGoal WRITE setGlobalGoal NOTIFY globalGoalChanged)
180 Q_PROPERTY(QDeclarativeListProperty<QQuickStochasticState> states READ states)
182 explicit QQuickStochasticEngine(QObject *parent = 0);
183 QQuickStochasticEngine(QList<QQuickStochasticState*> states, QObject *parent=0);
184 ~QQuickStochasticEngine();
186 QDeclarativeListProperty<QQuickStochasticState> states()
188 return QDeclarativeListProperty<QQuickStochasticState>(this, m_states);
191 QString globalGoal() const
196 int count() const {return m_things.count();}
197 void setCount(int c);
199 void setGoal(int state, int sprite=0, bool jump=false);
200 void start(int index=0, int state=0);
201 virtual void restart(int index=0);
202 virtual void advance(int index=0);//Sends state to the next chosen state, unlike goal.
203 void stop(int index=0);
204 int curState(int index=0) {return m_things[index];}
206 QQuickStochasticState* state(int idx){return m_states[idx];}
207 int stateIndex(QQuickStochasticState* s){return m_states.indexOf(s);}
208 int stateIndex(const QString& s) {
209 for (int i=0; i<m_states.count(); i++)
210 if (m_states[i]->name() == s)
215 int stateCount() {return m_states.count();}
219 void globalGoalChanged(QString arg);
220 void stateChanged(int idx);
223 void setGlobalGoal(QString arg)
225 if (m_globalGoal != arg) {
227 emit globalGoalChanged(arg);
231 uint updateSprites(uint time);
234 friend class QQuickParticleSystem;
235 void addToUpdateList(uint t, int idx);
236 int nextState(int curState, int idx=0);
237 int goalSeek(int curState, int idx, int dist=-1);
238 QList<QQuickStochasticState*> m_states;
239 //### Consider struct or class for the four data variables?
240 QVector<int> m_things;//int is the index in m_states of the current state
241 QVector<int> m_goals;
242 QVector<int> m_duration;
243 QVector<int> m_startTimes;
244 QList<QPair<uint, QList<int> > > m_stateUpdates;//### This could be done faster - priority queue?
248 QString m_globalGoal;
250 int m_imageStateCount;
254 class QQuickSpriteEngine : public QQuickStochasticEngine
257 Q_PROPERTY(QDeclarativeListProperty<QQuickSprite> sprites READ sprites)
259 explicit QQuickSpriteEngine(QObject *parent = 0);
260 QQuickSpriteEngine(QList<QQuickSprite*> sprites, QObject *parent=0);
261 ~QQuickSpriteEngine();
262 QDeclarativeListProperty<QQuickSprite> sprites()
264 return QDeclarativeListProperty<QQuickSprite>(this, m_sprites);
267 QQuickSprite* sprite(int sprite=0);
268 int spriteState(int sprite=0);
269 int spriteStart(int sprite=0);
270 int spriteFrames(int sprite=0);
271 int spriteDuration(int sprite=0);//Full duration, not per frame
272 int spriteX(int sprite=0);
273 int spriteY(int sprite=0);
274 int spriteWidth(int sprite=0);
275 int spriteHeight(int sprite=0);
276 int spriteCount();//Like state count
278 QString realName(int sprite=0);//Gives the parent sprite name for pseudosprites
279 QImage assembledImage();
281 virtual void restart(int index=0);
282 virtual void advance(int index=0);
284 int pseudospriteProgress(int,int,int*rd=0);
285 QList<QQuickSprite*> m_sprites;
288 //Common use is to have your own list property which is transparently an engine
289 inline void spriteAppend(QDeclarativeListProperty<QQuickSprite> *p, QQuickSprite* s)
291 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->append(s);
292 p->object->metaObject()->invokeMethod(p->object, "createEngine");
295 inline QQuickSprite* spriteAt(QDeclarativeListProperty<QQuickSprite> *p, int idx)
297 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->at(idx);
300 inline void spriteClear(QDeclarativeListProperty<QQuickSprite> *p)
302 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->clear();
303 p->object->metaObject()->invokeMethod(p->object, "createEngine");
306 inline int spriteCount(QDeclarativeListProperty<QQuickSprite> *p)
308 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->count();
315 #endif // QQUICKSPRITEENGINE_P_H