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 //For internal use
62 Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
63 Q_PROPERTY(int durationVariation READ durationVariance WRITE setDurationVariance NOTIFY durationVarianceChanged)
64 Q_PROPERTY(QVariantMap to READ to WRITE setTo NOTIFY toChanged)
65 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
66 Q_PROPERTY(qreal speedModifiesDuration READ speedModifer WRITE setSpeedModifier NOTIFY speedModifierChanged)
67 Q_PROPERTY(int frames READ frames WRITE setFrames NOTIFY framesChanged)
70 QQuickStochasticState(QObject* parent = 0)
87 QVariantMap to() const
92 qreal speedModifer() const
94 return m_speedModifier;
97 int durationVariance() const
99 return m_durationVariance;
103 int variedDuration() const
106 + (m_durationVariance * ((qreal)qrand()/RAND_MAX) * 2)
107 - m_durationVariance;
116 void durationChanged(int arg);
118 void nameChanged(QString arg);
120 void toChanged(QVariantMap arg);
122 void speedModifierChanged(qreal arg);
124 void durationVarianceChanged(int arg);
126 void entered();//### Just playing around - don't expect full state API
127 void framesChanged(int arg);
130 void setDuration(int arg)
132 if (m_duration != arg) {
134 emit durationChanged(arg);
138 void setName(QString arg)
142 emit nameChanged(arg);
146 void setTo(QVariantMap arg)
154 void setSpeedModifier(qreal arg)
156 if (m_speedModifier != arg) {
157 m_speedModifier = arg;
158 emit speedModifierChanged(arg);
162 void setDurationVariance(int arg)
164 if (m_durationVariance != arg) {
165 m_durationVariance = arg;
166 emit durationVarianceChanged(arg);
170 void setFrames(int arg)
172 if (m_frames != arg) {
174 emit framesChanged(arg);
183 qreal m_speedModifier;
184 int m_durationVariance;
186 friend class QQuickStochasticEngine;
189 class Q_AUTOTEST_EXPORT QQuickStochasticEngine : public QObject
192 //TODO: Optimize single state case?
193 Q_PROPERTY(QString globalGoal READ globalGoal WRITE setGlobalGoal NOTIFY globalGoalChanged)
194 Q_PROPERTY(QDeclarativeListProperty<QQuickStochasticState> states READ states)
196 explicit QQuickStochasticEngine(QObject *parent = 0);
197 QQuickStochasticEngine(QList<QQuickStochasticState*> states, QObject *parent=0);
198 ~QQuickStochasticEngine();
200 QDeclarativeListProperty<QQuickStochasticState> states()
202 return QDeclarativeListProperty<QQuickStochasticState>(this, m_states);
205 QString globalGoal() const
210 int count() const {return m_things.count();}
211 void setCount(int c);
213 void setGoal(int state, int sprite=0, bool jump=false);
214 void start(int index=0, int state=0);
215 void stop(int index=0);
216 int curState(int index=0) {return m_things[index];}
218 QQuickStochasticState* state(int idx){return m_states[idx];}
219 int stateIndex(QQuickStochasticState* s){return m_states.indexOf(s);}
220 int stateIndex(const QString& s) {
221 for (int i=0; i<m_states.count(); i++)
222 if (m_states[i]->name() == s)
227 int stateCount() {return m_states.count();}
231 void globalGoalChanged(QString arg);
232 void stateChanged(int idx);
235 void setGlobalGoal(QString arg)
237 if (m_globalGoal != arg) {
239 emit globalGoalChanged(arg);
243 uint updateSprites(uint time);
246 friend class QQuickParticleSystem;
247 void restart(int index);
248 void addToUpdateList(uint t, int idx);
249 int goalSeek(int curState, int idx, int dist=-1);
250 QList<QQuickStochasticState*> m_states;
251 //### Consider struct or class for the four data variables?
252 QVector<int> m_things;//int is the index in m_states of the current state
253 QVector<int> m_goals;
254 QVector<int> m_duration;
255 QVector<int> m_startTimes;
256 QList<QPair<uint, QList<int> > > m_stateUpdates;//### This could be done faster - priority queue?
260 QString m_globalGoal;
262 int m_imageStateCount;
265 class QQuickSpriteEngine : public QQuickStochasticEngine
268 Q_PROPERTY(QDeclarativeListProperty<QQuickSprite> sprites READ sprites)
270 explicit QQuickSpriteEngine(QObject *parent = 0);
271 QQuickSpriteEngine(QList<QQuickSprite*> sprites, QObject *parent=0);
272 ~QQuickSpriteEngine();
273 QDeclarativeListProperty<QQuickSprite> sprites()
275 return QDeclarativeListProperty<QQuickSprite>(this, m_sprites);
279 int spriteState(int sprite=0);
280 int spriteStart(int sprite=0);
281 int spriteFrames(int sprite=0);
282 int spriteDuration(int sprite=0);
283 int spriteX(int /* sprite */ = 0) { return 0; }//Currently all rows are 0 aligned, if we get more space efficient we might change this
284 int spriteY(int sprite=0);
285 int spriteWidth(int sprite=0);
286 int spriteHeight(int sprite=0);
287 int spriteCount();//Like state count, but for the image states
289 QImage assembledImage();
291 QList<QQuickSprite*> m_sprites;
294 //Common use is to have your own list property which is transparently an engine
295 inline void spriteAppend(QDeclarativeListProperty<QQuickSprite> *p, QQuickSprite* s)
297 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->append(s);
298 p->object->metaObject()->invokeMethod(p->object, "createEngine");
301 inline QQuickSprite* spriteAt(QDeclarativeListProperty<QQuickSprite> *p, int idx)
303 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->at(idx);
306 inline void spriteClear(QDeclarativeListProperty<QQuickSprite> *p)
308 reinterpret_cast<QList<QQuickSprite *> *>(p->data)->clear();
309 p->object->metaObject()->invokeMethod(p->object, "createEngine");
312 inline int spriteCount(QDeclarativeListProperty<QQuickSprite> *p)
314 return reinterpret_cast<QList<QQuickSprite *> *>(p->data)->count();
321 #endif // QQUICKSPRITEENGINE_P_H