Add Move affector
authorAlan Alpert <alan.alpert@nokia.com>
Fri, 14 Oct 2011 07:06:39 +0000 (17:06 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 14 Oct 2011 08:46:03 +0000 (10:46 +0200)
Slated to replace the 'Gravity' affector which only did one specialized
gravity usecase anyways. This one is more generic.

Change-Id: I3cbb975bad24e8f6fca7e07b671aa8ba5a3a916c
Reviewed-on: http://codereview.qt-project.org/6657
Reviewed-by: Bea Lam <bea.lam@nokia.com>

examples/declarative/particles/affectors/gravity.qml
examples/declarative/particles/affectors/move.qml [new file with mode: 0644]
src/declarative/particles/particles.pri
src/declarative/particles/qsggravity.cpp
src/declarative/particles/qsgmove.cpp [new file with mode: 0644]
src/declarative/particles/qsgmove_p.h [new file with mode: 0644]
src/declarative/particles/qsgparticlesmodule.cpp

index 97d9c85..47710e6 100644 (file)
@@ -82,10 +82,12 @@ Item {
     }
 
     ParticleSystem { id: sys }
-    Gravity {
+    Move {
         system: sys
-        angle: ground.rotation + 90
-        acceleration: 32
+        acceleration: AngleDirection {
+            angle: ground.rotation + 90
+            magnitude: 32
+        }
     }
     Emitter {
         system: sys
diff --git a/examples/declarative/particles/affectors/move.qml b/examples/declarative/particles/affectors/move.qml
new file mode 100644 (file)
index 0000000..93ff37b
--- /dev/null
@@ -0,0 +1,142 @@
+/****************************************************************************
+**
+** 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 examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**   * Redistributions of source code must retain the above copyright
+**     notice, this list of conditions and the following disclaimer.
+**   * Redistributions in binary form must reproduce the above copyright
+**     notice, this list of conditions and the following disclaimer in
+**     the documentation and/or other materials provided with the
+**     distribution.
+**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+**     the names of its contributors may be used to endorse or promote
+**     products derived from this software without specific prior written
+**     permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtQuick.Particles 2.0
+
+Rectangle {
+    width: 360
+    height: 540
+    color: "black"
+    ParticleSystem {
+        anchors.fill: parent
+        ImageParticle {
+            groups: ["A"]
+            anchors.fill: parent
+            source: "../images/star.png"
+            color:"#FF1010"
+            redVariation: 0.8
+        }
+
+        Emitter {
+            group: "A"
+            emitRate: 100
+            lifeSpan: 2800
+            size: 32
+            sizeVariation: 8
+            speed: PointDirection{ x: 66; xVariation: 20 }
+            width: 80
+            height: 80
+        }
+
+        Move {
+            groups: ["A"]
+            x: 120
+            width: 80
+            height: 80
+            relative: true
+            once: true
+            position: PointDirection { x: 120; }
+        }
+
+        ImageParticle {
+            groups: ["B"]
+            anchors.fill: parent
+            source: "../images/star.png"
+            color:"#10FF10"
+            greenVariation: 0.8
+        }
+
+        Emitter {
+            group: "B"
+            emitRate: 100
+            lifeSpan: 2800
+            size: 32
+            sizeVariation: 8
+            speed: PointDirection{ x: 240; xVariation: 60 }
+            y: 260
+            width: 10
+            height: 10
+        }
+
+        Move {
+            groups: ["B"]
+            x: 120
+            y: 240
+            width: 80
+            height: 80
+            relative: true
+            once: true
+            speed: AngleDirection { angleVariation:360; magnitude: 72 }
+        }
+
+        ImageParticle {
+            groups: ["C"]
+            anchors.fill: parent
+            source: "../images/star.png"
+            color:"#1010FF"
+            blueVariation: 0.8
+        }
+
+        Emitter {
+            group: "C"
+            y: 400
+            emitRate: 100
+            lifeSpan: 2800
+            size: 32
+            sizeVariation: 8
+            speed: PointDirection{ x: 80; xVariation: 10 }
+            width: 80
+            height: 80
+        }
+
+        Move {
+            groups: ["C"]
+            x: 120
+            y: 400
+            width: 80
+            height: 80
+            relative: true
+            once: true
+            acceleration: PointDirection { y: -80; }
+        }
+
+    }
+}
index bfe1062..4502cc4 100644 (file)
@@ -31,7 +31,8 @@ HEADERS += \
     $$PWD/qsgv8particledata_p.h \
     $$PWD/qsgrectangleextruder_p.h \
     $$PWD/qsgparticlegroup_p.h \
-    $$PWD/qsggroupgoal_p.h
+    $$PWD/qsggroupgoal_p.h \
+    $$PWD/qsgmove_p.h
 
 SOURCES += \
     $$PWD/qsgangledirection.cpp \
@@ -64,7 +65,8 @@ SOURCES += \
     $$PWD/qsgv8particledata.cpp \
     $$PWD/qsgrectangleextruder.cpp \
     $$PWD/qsgparticlegroup.cpp \
-    $$PWD/qsggroupgoal.cpp
+    $$PWD/qsggroupgoal.cpp \
+    $$PWD/qsgmove.cpp
 
 RESOURCES += \
     $$PWD/particles.qrc
index f600726..dd47f66 100644 (file)
@@ -78,6 +78,8 @@ QSGGravityAffector::QSGGravityAffector(QSGItem *parent) :
     connect(this, SIGNAL(angleChanged(qreal)),
             this, SLOT(recalc()));
     recalc();
+    qWarning() << "Gravity has been deprecated. Use Move instead."
+               << "The difference is that you can specify the acceleration with a StochasticDirection instead of just an angle/magnitude pair";
 }
 
 void QSGGravityAffector::recalc()
diff --git a/src/declarative/particles/qsgmove.cpp b/src/declarative/particles/qsgmove.cpp
new file mode 100644 (file)
index 0000000..e885685
--- /dev/null
@@ -0,0 +1,131 @@
+/****************************************************************************
+**
+** 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 "qsgmove_p.h"
+#include <cmath>
+QT_BEGIN_NAMESPACE
+const qreal CONV = 0.017453292520444443;
+/*!
+    \qmlclass Move QSGMoveAffector
+    \inqmlmodule QtQuick.Particles 2
+    \inherits Affector
+    \brief The Move element allows you to set a new position, speed or acceleration on particles
+
+    You'll often want to set the 'once' property to true for efficiency in the case where you just
+    want to set the parameter. Otherwise, the parameter will be needlessly set to the same thing
+    every simulation cycle.
+*/
+
+/*!
+    \qmlproperty StochasticDirection QtQuick.Particles2::Move::position
+
+    Affected particles will have their position set to this direction,
+    relative to the ParticleSystem. When interpreting directions as points,
+    imagine it as an arrow with the base at the 0,0 of the ParticleSystem and the
+    tip at where the specified position will be.
+*/
+
+/*!
+    \qmlproperty StochasticDirection QtQuick.Particles2::Move::speed
+
+    Affected particles will have their speed set to this direction.
+*/
+
+
+/*!
+    \qmlproperty StochasticDirection QtQuick.Particles2::Move::acceleration
+
+    Affected particles will have their acceleration set to this direction.
+*/
+
+
+/*!
+    \qmlproperty bool QtQuick.Particles2::Move::relative
+
+    Whether the affected particles have their existing position/speed/acceleration added
+    to the new one.
+*/
+
+QSGMoveAffector::QSGMoveAffector(QSGItem *parent)
+    : QSGParticleAffector(parent)
+    , m_position(&m_nullVector)
+    , m_speed(&m_nullVector)
+    , m_acceleration(&m_nullVector)
+    , m_relative(false)
+{
+}
+
+bool QSGMoveAffector::affectParticle(QSGParticleData *d, qreal dt)
+{
+    Q_UNUSED(dt);
+    bool changed = false;
+    QPointF curPos(d->curX(), d->curY());
+
+    if (m_position != &m_nullVector){
+        QPointF pos = m_position->sample(curPos);
+        if (m_relative)
+            pos += curPos;
+        d->setInstantaneousX(pos.x());
+        d->setInstantaneousY(pos.y());
+        changed = true;
+    }
+
+    if (m_speed != &m_nullVector){
+        QPointF pos = m_speed->sample(curPos);
+        if (m_relative)
+            pos += QPointF(d->curVX(), d->curVY());
+        d->setInstantaneousVX(pos.x());
+        d->setInstantaneousVY(pos.y());
+        changed = true;
+    }
+
+    if (m_acceleration != &m_nullVector){
+        QPointF pos = m_acceleration->sample(curPos);
+        if (m_relative)
+            pos += QPointF(d->curAX(), d->curAY());
+        d->setInstantaneousAX(pos.x());
+        d->setInstantaneousAY(pos.y());
+        changed = true;
+    }
+
+    return changed;
+}
+QT_END_NAMESPACE
diff --git a/src/declarative/particles/qsgmove_p.h b/src/declarative/particles/qsgmove_p.h
new file mode 100644 (file)
index 0000000..2f7fc83
--- /dev/null
@@ -0,0 +1,155 @@
+/****************************************************************************
+**
+** 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 MOVEAFFECTOR_H
+#define MOVEAFFECTOR_H
+#include "qsgparticleaffector_p.h"
+#include "qsgdirection_p.h"
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Declarative)
+
+
+class QSGMoveAffector : public QSGParticleAffector
+{
+    Q_OBJECT
+    Q_PROPERTY(bool relative READ relative WRITE setRelative NOTIFY relativeChanged)
+    Q_PROPERTY(QSGDirection *position READ position WRITE setPosition NOTIFY positionChanged RESET positionReset)
+    Q_PROPERTY(QSGDirection *speed READ speed WRITE setSpeed NOTIFY speedChanged RESET speedReset)
+    Q_PROPERTY(QSGDirection *acceleration READ acceleration WRITE setAcceleration NOTIFY accelerationChanged RESET accelerationReset)
+
+public:
+    explicit QSGMoveAffector(QSGItem *parent = 0);
+    QSGDirection * position() const
+    {
+        return m_position;
+    }
+
+    QSGDirection * speed() const
+    {
+        return m_speed;
+    }
+
+    QSGDirection * acceleration() const
+    {
+        return m_acceleration;
+    }
+
+    void positionReset()
+    {
+        m_position = &m_nullVector;
+    }
+
+    void speedReset()
+    {
+        m_speed = &m_nullVector;
+    }
+
+    void accelerationReset()
+    {
+        m_acceleration = &m_nullVector;
+    }
+
+    bool relative() const
+    {
+        return m_relative;
+    }
+
+protected:
+    virtual bool affectParticle(QSGParticleData *d, qreal dt);
+signals:
+    void positionChanged(QSGDirection * arg);
+
+    void speedChanged(QSGDirection * arg);
+
+    void accelerationChanged(QSGDirection * arg);
+
+    void relativeChanged(bool arg);
+
+public slots:
+    void setPosition(QSGDirection * arg)
+    {
+        if (m_position != arg) {
+            m_position = arg;
+            emit positionChanged(arg);
+        }
+    }
+
+    void setSpeed(QSGDirection * arg)
+    {
+        if (m_speed != arg) {
+            m_speed = arg;
+            emit speedChanged(arg);
+        }
+    }
+
+    void setAcceleration(QSGDirection * arg)
+    {
+        if (m_acceleration != arg) {
+            m_acceleration = arg;
+            emit accelerationChanged(arg);
+        }
+    }
+
+    void setRelative(bool arg)
+    {
+        if (m_relative != arg) {
+            m_relative = arg;
+            emit relativeChanged(arg);
+        }
+    }
+
+private slots:
+private:
+    QSGDirection * m_position;
+    QSGDirection * m_speed;
+    QSGDirection * m_acceleration;
+
+    QSGDirection m_nullVector;
+    bool m_relative;
+};
+
+QT_END_NAMESPACE
+QT_END_HEADER
+#endif // MOVEAFFECTOR_H
index e5b5680..fe2dec9 100644 (file)
@@ -69,6 +69,7 @@
 #include "qsgrectangleextruder_p.h"
 #include "qsgparticlegroup_p.h"
 #include "qsggroupgoal_p.h"
+#include "qsgmove_p.h"
 
 QT_BEGIN_NAMESPACE
 
@@ -106,6 +107,7 @@ void QSGParticlesModule::defineModule()
     qmlRegisterType<QSGGroupGoalAffector>(uri, 2, 0, "GroupGoal");
     qmlRegisterType<QSGTurbulenceAffector>(uri, 2, 0 , "Turbulence");
     qmlRegisterType<QSGTargetAffector>(uri, 2, 0 , "Target");
+    qmlRegisterType<QSGMoveAffector>(uri, 2, 0, "Move");
 
     //Exposed just for completeness
     qmlRegisterUncreatableType<QSGParticleAffector>(uri, 2, 0, "ParticleAffector",