Split GroupGoal out of SpriteGoal
authorAlan Alpert <alan.alpert@nokia.com>
Tue, 4 Oct 2011 04:39:21 +0000 (14:39 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 5 Oct 2011 05:33:29 +0000 (07:33 +0200)
Leaving SpriteGoal alone for a few weeks with a deprecation message, but
a functioning GroupGoal has been added.

Change-Id: I395046c00526e7caa38bdbc499a8191308c476fa
Reviewed-on: http://codereview.qt-project.org/5950
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>
examples/declarative/particles/affectors/groupgoal.qml
src/declarative/items/qsgspriteengine_p.h
src/declarative/particles/particles.pri
src/declarative/particles/qsggroupgoal.cpp [new file with mode: 0644]
src/declarative/particles/qsggroupgoal_p.h [new file with mode: 0644]
src/declarative/particles/qsgparticlesmodule.cpp
src/declarative/particles/qsgspritegoal.cpp
src/declarative/particles/qsgspritegoal_p.h

index 35febf3..836392c 100644 (file)
@@ -67,11 +67,10 @@ Rectangle {
                 colorVariation: 0.1
                 color: "#2060160f"
             }
-            SpriteGoal {
+            GroupGoal {
                 whenCollidingWith: ["lit"]
                 goalState: "lighting"
                 jump: true
-                systemStates: true
             }
         }
         ParticleGroup {
@@ -163,11 +162,10 @@ Rectangle {
             sizeVariation: 2
             endSize: 0
             speed: PointDirection { y:-100; yVariation: 4; xVariation: 4 }
-            SpriteGoal {
+            GroupGoal {
                 groups: ["unlit"]
                 goalState: "lit"
                 jump: true
-                systemStates: true
                 system: particles
                 x: -15
                 y: -55
@@ -177,11 +175,10 @@ Rectangle {
             }
         }
         //Click to enflame
-        SpriteGoal {//TODO: Aux emiiters in the state definition (which allows the occasional ball to spontaneously combust)
+        GroupGoal {
             groups: ["unlit"]
             goalState: "lighting"
             jump: true
-            systemStates: true
             enabled: ma.pressed
             width: 18
             height: 18
index b2a06f2..0561849 100644 (file)
@@ -58,7 +58,7 @@ QT_BEGIN_NAMESPACE
 QT_MODULE(Declarative)
 
 class QSGSprite;
-class QSGStochasticState : public QObject //For internal use
+class Q_AUTOTEST_EXPORT QSGStochasticState : public QObject //For internal use
 {
     Q_OBJECT
     Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
@@ -188,7 +188,7 @@ private:
     friend class QSGStochasticEngine;
 };
 
-class QSGStochasticEngine : public QObject
+class Q_AUTOTEST_EXPORT QSGStochasticEngine : public QObject
 {
     Q_OBJECT
     //TODO: Optimize single state case?
index 8676e52..bfe1062 100644 (file)
@@ -30,7 +30,8 @@ HEADERS += \
     $$PWD/qsgcumulativedirection_p.h \
     $$PWD/qsgv8particledata_p.h \
     $$PWD/qsgrectangleextruder_p.h \
-    $$PWD/qsgparticlegroup_p.h
+    $$PWD/qsgparticlegroup_p.h \
+    $$PWD/qsggroupgoal_p.h
 
 SOURCES += \
     $$PWD/qsgangledirection.cpp \
@@ -62,7 +63,8 @@ SOURCES += \
     $$PWD/qsgcumulativedirection.cpp \
     $$PWD/qsgv8particledata.cpp \
     $$PWD/qsgrectangleextruder.cpp \
-    $$PWD/qsgparticlegroup.cpp
+    $$PWD/qsgparticlegroup.cpp \
+    $$PWD/qsggroupgoal.cpp
 
 RESOURCES += \
     $$PWD/particles.qrc
diff --git a/src/declarative/particles/qsggroupgoal.cpp b/src/declarative/particles/qsggroupgoal.cpp
new file mode 100644 (file)
index 0000000..7f0b938
--- /dev/null
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Declarative module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qsggroupgoal_p.h"
+#include "private/qsgspriteengine_p.h"
+#include "private/qsgsprite_p.h"
+#include "qsgimageparticle_p.h"
+#include <QDebug>
+
+QT_BEGIN_NAMESPACE
+
+/*!
+    \qmlclass GroupGoal QSGGroupGoalAffector
+    \inqmlmodule QtQuick.Particles 2
+    \inherits Affector
+    \brief The GroupGoal Affector allows you to change the state of a group of a particle.
+
+*/
+/*!
+    \qmlproperty string QtQuick.Particles2::GroupGoal::goalState
+
+    The name of the group which the affected particles should move to.
+
+    Groups can have defined durations and transitions between them, setting goalState
+    will cause it to disregard any path weightings (including 0) and head down the path
+    which will reach the goalState quickest. It will pass through intermediate groups
+    on that path for their respective durations.
+*/
+/*!
+    \qmlproperty bool QtQuick.Particles2::GroupGoal::jump
+
+    If true, affected particles will jump directly to the target group instead of taking the
+    the shortest valid path to get there. They will also not finish their current state,
+    but immediately move to the beginning of the goal state.
+
+    Default is false.
+*/
+
+QSGGroupGoalAffector::QSGGroupGoalAffector(QSGItem *parent) :
+    QSGParticleAffector(parent), m_jump(false)
+{
+}
+
+void QSGGroupGoalAffector::setGoalState(QString arg)
+{
+    if (m_goalState != arg) {
+        m_goalState = arg;
+        emit goalStateChanged(arg);
+    }
+}
+
+bool QSGGroupGoalAffector::affectParticle(QSGParticleData *d, qreal dt)
+{
+    Q_UNUSED(dt);
+    QSGStochasticEngine *engine = m_system->stateEngine;
+    bool notUsingEngine = false;
+    if (!engine)
+        notUsingEngine = true;
+
+    int index = d->systemIndex;
+    int goalIdx = m_system->groupIds[m_goalState];
+    if (notUsingEngine){//no stochastic states defined. So cut out the engine
+        //TODO: It's possible to move to a group that is intermediate and not used by painters or emitters - but right now that will redirect to the default group
+        m_system->moveGroups(d, goalIdx);
+        return true;
+    }else if (engine->curState(index) != goalIdx){
+        engine->setGoal(goalIdx, index, m_jump);
+        return true;
+    }
+    return false;
+}
+
+QT_END_NAMESPACE
diff --git a/src/declarative/particles/qsggroupgoal_p.h b/src/declarative/particles/qsggroupgoal_p.h
new file mode 100644 (file)
index 0000000..6357c3f
--- /dev/null
@@ -0,0 +1,102 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Declarative module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef GROUPGOALAFFECTOR_H
+#define GROUPGOALAFFECTOR_H
+#include "qsgparticleaffector_p.h"
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Declarative)
+
+class QSGStochasticEngine;
+
+class QSGGroupGoalAffector : public QSGParticleAffector
+{
+    Q_OBJECT
+    Q_PROPERTY(QString goalState READ goalState WRITE setGoalState NOTIFY goalStateChanged)
+    Q_PROPERTY(bool jump READ jump WRITE setJump NOTIFY jumpChanged)
+public:
+    explicit QSGGroupGoalAffector(QSGItem *parent = 0);
+
+    QString goalState() const
+    {
+        return m_goalState;
+    }
+
+    bool jump() const
+    {
+        return m_jump;
+    }
+
+protected:
+    virtual bool affectParticle(QSGParticleData *d, qreal dt);
+
+signals:
+
+    void goalStateChanged(QString arg);
+
+    void jumpChanged(bool arg);
+
+public slots:
+
+    void setGoalState(QString arg);
+
+    void setJump(bool arg)
+    {
+        if (m_jump != arg) {
+            m_jump = arg;
+            emit jumpChanged(arg);
+        }
+    }
+
+private:
+    QString m_goalState;
+    bool m_jump;
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // GROUPGOALAFFECTOR_H
index 362a361..e5b5680 100644 (file)
@@ -68,6 +68,7 @@
 #include "qsgcustomaffector_p.h"
 #include "qsgrectangleextruder_p.h"
 #include "qsgparticlegroup_p.h"
+#include "qsggroupgoal_p.h"
 
 QT_BEGIN_NAMESPACE
 
@@ -102,6 +103,7 @@ void QSGParticlesModule::defineModule()
     qmlRegisterType<QSGGravityAffector>(uri, 2, 0, "Gravity");
     qmlRegisterType<QSGAgeAffector>(uri, 2, 0, "Age");
     qmlRegisterType<QSGSpriteGoalAffector>(uri, 2, 0, "SpriteGoal");
+    qmlRegisterType<QSGGroupGoalAffector>(uri, 2, 0, "GroupGoal");
     qmlRegisterType<QSGTurbulenceAffector>(uri, 2, 0 , "Turbulence");
     qmlRegisterType<QSGTargetAffector>(uri, 2, 0 , "Target");
 
index fd919e8..1e8feec 100644 (file)
@@ -51,17 +51,32 @@ QT_BEGIN_NAMESPACE
     \qmlclass SpriteGoal QSGSpriteGoalAffector
     \inqmlmodule QtQuick.Particles 2
     \inherits Affector
-    \brief The SpriteGoal Affector allows you to change the state of a sprite or group of a particle.
+    \brief The SpriteGoal Affector allows you to change the state of a sprite particle.
 
 */
 /*!
     \qmlproperty string QtQuick.Particles2::SpriteGoal::goalState
+
+    The name of the Sprite which the affected particles should move to.
+
+    Sprite states have defined durations and transitions between them, setting goalState
+    will cause it to disregard any path weightings (including 0) and head down the path
+    which will reach the goalState quickest. It will pass through intermediate states
+    on that path.
 */
 /*!
     \qmlproperty bool QtQuick.Particles2::SpriteGoal::jump
+
+    If true, affected sprites will jump directly to the goal state instead of taking the
+    the shortest valid path to get there. They will also not finish their current state,
+    but immediately move to the beginning of the goal state.
+
+    Default is false.
 */
 /*!
     \qmlproperty bool QtQuick.Particles2::SpriteGoal::systemStates
+
+    deprecated, use GroupGoal instead
 */
 
 QSGSpriteGoalAffector::QSGSpriteGoalAffector(QSGItem *parent) :
@@ -124,7 +139,6 @@ bool QSGSpriteGoalAffector::affectParticle(QSGParticleData *d, qreal dt)
         m_system->moveGroups(d, m_goalIdx);
     }else if (engine->curState(index) != m_goalIdx){
         engine->setGoal(m_goalIdx, index, m_jump);
-        emit affected(QPointF(d->curX(), d->curY()));//###Expensive if unconnected? Move to Affector?
         return true; //Doesn't affect particle data, but necessary for onceOff
     }
     return false;
index 043970b..7f20b1e 100644 (file)
@@ -42,6 +42,7 @@
 #ifndef SPRITEGOALAFFECTOR_H
 #define SPRITEGOALAFFECTOR_H
 #include "qsgparticleaffector_p.h"
+#include <QtDeclarative/qdeclarativeinfo.h>
 
 QT_BEGIN_HEADER
 
@@ -82,7 +83,6 @@ signals:
 
     void jumpChanged(bool arg);
 
-    void affected(const QPointF &pos);
     void systemStatesChanged(bool arg);
 
 public slots:
@@ -100,6 +100,8 @@ void setJump(bool arg)
 void setSystemStates(bool arg)
 {
     if (m_systemStates != arg) {
+        //TODO: GroupGoal was added (and this deprecated) Oct 4 - remove it in a few weeks.
+        qmlInfo(this) << "systemStates is deprecated and will be removed soon. Use GroupGoal instead.";
         m_systemStates = arg;
         emit systemStatesChanged(arg);
     }