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 QQUICKSPRITEENGINE_P_H
43 #define QQUICKSPRITEENGINE_P_H
50 #include <QQmlListProperty>
53 #include <QtQuick/private/qquickpixmapcache_p.h>
60 class Q_AUTOTEST_EXPORT QQuickStochasticState : public QObject //Currently for internal use only - Sprite and ParticleGroup
63 Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
64 Q_PROPERTY(int durationVariation READ durationVariation WRITE setDurationVariation NOTIFY durationVariationChanged)
65 //Note that manually advanced sprites need to query this variable and implement own behaviour for it
66 Q_PROPERTY(bool randomStart READ randomStart WRITE setRandomStart NOTIFY randomStartChanged)
67 Q_PROPERTY(QVariantMap to READ to WRITE setTo NOTIFY toChanged)
68 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
71 QQuickStochasticState(QObject* parent = 0)
74 , m_durationVariation(0)
75 , m_randomStart(false)
89 QVariantMap to() const
94 int durationVariation() const
96 return m_durationVariation;
100 virtual int variedDuration() const
102 return qMax(qreal(0.0) , m_duration
103 + (m_durationVariation * ((qreal)qrand()/RAND_MAX) * 2)
104 - m_durationVariation);
107 bool randomStart() const
109 return m_randomStart;
113 void durationChanged(int arg);
115 void nameChanged(QString arg);
117 void toChanged(QVariantMap arg);
119 void durationVariationChanged(int arg);
121 void entered();//### Just playing around - don't expect full state API
123 void randomStartChanged(bool arg);
126 void setDuration(int arg)
128 if (m_duration != arg) {
130 emit durationChanged(arg);
134 void setName(QString arg)
138 emit nameChanged(arg);
142 void setTo(QVariantMap arg)
150 void setDurationVariation(int arg)
152 if (m_durationVariation != arg) {
153 m_durationVariation = arg;
154 emit durationVariationChanged(arg);
158 void setRandomStart(bool arg)
160 if (m_randomStart != arg) {
162 emit randomStartChanged(arg);
170 int m_durationVariation;
172 friend class QQuickStochasticEngine;
176 class Q_AUTOTEST_EXPORT QQuickStochasticEngine : public QObject
179 //TODO: Optimize single state case?
180 Q_PROPERTY(QString globalGoal READ globalGoal WRITE setGlobalGoal NOTIFY globalGoalChanged)
181 Q_PROPERTY(QQmlListProperty<QQuickStochasticState> states READ states)
183 explicit QQuickStochasticEngine(QObject *parent = 0);
184 QQuickStochasticEngine(QList<QQuickStochasticState*> states, QObject *parent=0);
185 ~QQuickStochasticEngine();
187 QQmlListProperty<QQuickStochasticState> states()
189 return QQmlListProperty<QQuickStochasticState>(this, m_states);
192 QString globalGoal() const
197 int count() const {return m_things.count();}
198 void setCount(int c);
200 void setGoal(int state, int sprite=0, bool jump=false);
201 void start(int index=0, int state=0);
202 virtual void restart(int index=0);
203 virtual void advance(int index=0);//Sends state to the next chosen state, unlike goal.
204 void stop(int index=0);
205 int curState(int index=0) {return m_things[index];}
207 QQuickStochasticState* state(int idx){return m_states[idx];}
208 int stateIndex(QQuickStochasticState* s){return m_states.indexOf(s);}
209 int stateIndex(const QString& s) {
210 for (int i=0; i<m_states.count(); i++)
211 if (m_states[i]->name() == s)
216 int stateCount() {return m_states.count();}
220 void globalGoalChanged(QString arg);
221 void stateChanged(int idx);
224 void setGlobalGoal(QString arg)
226 if (m_globalGoal != arg) {
228 emit globalGoalChanged(arg);
232 uint updateSprites(uint time);
235 friend class QQuickParticleSystem;
236 void addToUpdateList(uint t, int idx);
237 int nextState(int curState, int idx=0);
238 int goalSeek(int curState, int idx, int dist=-1);
239 QList<QQuickStochasticState*> m_states;
240 //### Consider struct or class for the four data variables?
241 QVector<int> m_things;//int is the index in m_states of the current state
242 QVector<int> m_goals;
243 QVector<int> m_duration;
244 QVector<int> m_startTimes;
245 QList<QPair<uint, QList<int> > > m_stateUpdates;//### This could be done faster - priority queue?
249 QString m_globalGoal;
251 int m_imageStateCount;
255 class QQuickSpriteEngine : public QQuickStochasticEngine
258 Q_PROPERTY(QQmlListProperty<QQuickSprite> sprites READ sprites)
260 explicit QQuickSpriteEngine(QObject *parent = 0);
261 QQuickSpriteEngine(QList<QQuickSprite*> sprites, QObject *parent=0);
262 ~QQuickSpriteEngine();
263 QQmlListProperty<QQuickSprite> sprites()
265 return QQmlListProperty<QQuickSprite>(this, m_sprites);
268 QQuickSprite* sprite(int sprite=0);
269 int spriteState(int sprite=0);
270 int spriteStart(int sprite=0);
271 int spriteFrames(int sprite=0);
272 int spriteDuration(int sprite=0);
273 int spriteX(int sprite=0);
274 int spriteY(int sprite=0);
275 int spriteWidth(int sprite=0);
276 int spriteHeight(int sprite=0);
277 int spriteCount();//Like state count
280 virtual void restart(int index=0);
281 virtual void advance(int index=0);
283 //Similar API to QQuickPixmap for async loading convenience
284 bool isNull() { return status() == QQuickPixmap::Null; }
285 bool isReady() { return status() == QQuickPixmap::Ready; }
286 bool isLoading() { return status() == QQuickPixmap::Loading; }
287 bool isError() { return status() == QQuickPixmap::Error; }
288 QQuickPixmap::Status status();//Composed status of all Sprites
289 void startAssemblingImage();
290 QImage assembledImage();
293 int pseudospriteProgress(int,int,int*rd=0);
294 QList<QQuickSprite*> m_sprites;
295 bool m_startedImageAssembly;
298 //Common use is to have your own list property which is transparently an engine
299 inline void spriteAppend(QQmlListProperty<QQuickSprite> *p, QQuickSprite* s)
301 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->append(s);
302 p->object->metaObject()->invokeMethod(p->object, "createEngine");
305 inline QQuickSprite* spriteAt(QQmlListProperty<QQuickSprite> *p, int idx)
307 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->at(idx);
310 inline void spriteClear(QQmlListProperty<QQuickSprite> *p)
312 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->clear();
313 p->object->metaObject()->invokeMethod(p->object, "createEngine");
316 inline int spriteCount(QQmlListProperty<QQuickSprite> *p)
318 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->count();
325 #endif // QQUICKSPRITEENGINE_P_H