Add Age example
authorAlan Alpert <alan.alpert@nokia.com>
Wed, 14 Sep 2011 08:15:17 +0000 (18:15 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 16 Sep 2011 04:37:43 +0000 (06:37 +0200)
Also added advancePosition property to Age, as it was needed for even this
simple example.

Change-Id: I614a719280efc1bc5140d07ab91ec0f1bc345788
Reviewed-on: http://codereview.qt-project.org/4871
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>
examples/declarative/particles/affectors/age.qml [new file with mode: 0644]
src/declarative/particles/qsgage.cpp
src/declarative/particles/qsgage_p.h

diff --git a/examples/declarative/particles/affectors/age.qml b/examples/declarative/particles/affectors/age.qml
new file mode 100644 (file)
index 0000000..f2361a2
--- /dev/null
@@ -0,0 +1,92 @@
+/****************************************************************************
+**
+** 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 {
+    id: root
+    width: 360
+    height: 600
+    color: "white"
+    ParticleSystem{ id: particles }
+    ImageParticle {
+        system: particles
+        sprites: Sprite{
+            name: "snow"
+            source: "../images/snowflake.png"
+            frames: 51
+            duration: 40
+            durationVariation: 8
+        }
+    }
+    Emitter {
+        system: particles
+        emitRate: 20
+        lifeSpan: 8000
+        speed: PointDirection{ y:80; yVariation: 40; }
+        acceleration: PointDirection{ y: 4 }
+        size: 36
+        endSize: 12
+        sizeVariation: 8
+        width: parent.width
+        height: 100
+    }
+    MouseArea{
+        id: ma
+        anchors.fill: parent
+        hoverEnabled: true
+    }
+    Rectangle{
+        color: "#803333AA"
+        border.color: "black"
+        x: ma.mouseX - 36
+        y: ma.mouseY - 36
+        width: 72
+        height: 72
+        Age{
+            anchors.fill: parent
+            system: particles
+            once: true
+            lifeLeft: 1200
+            advancePosition: false
+        }
+    }
+}
index 2c0678f..0caa8be 100644 (file)
@@ -57,12 +57,27 @@ QT_BEGIN_NAMESPACE
     \qmlproperty int QtQuick.Particles2::Age::lifeLeft
 
     The amount of life to set the particle to have. Affected particles
-    will jump to a point in their life where they will have this many
+    will advance to a point in their life where they will have this many
     milliseconds left to live.
 */
 
+/*!
+    \qmlproperty bool QtQuick.Particles2::Age::advancePosition
+
+    advancePosition determines whether position, veclocity and acceleration are included in
+    the simulated aging done by the affector. If advancePosition is false,
+    then the position, velocity and acceleration will remain the same and only
+    other attributes (such as opacity) will advance in the simulation to where
+    it would normally be for that point in the particle's life. With advancePosition set to
+    true the position, velocity and acceleration will also advance to where it would
+    normally be by that point in the particle's life, making it advance its position
+    on screen.
+
+    Default value is true.
+*/
+
 QSGAgeAffector::QSGAgeAffector(QSGItem *parent) :
-    QSGParticleAffector(parent), m_lifeLeft(0)
+    QSGParticleAffector(parent), m_lifeLeft(0), m_advancePosition(true)
 {
 }
 
@@ -73,7 +88,23 @@ bool QSGAgeAffector::affectParticle(QSGParticleData *d, qreal dt)
     if (d->stillAlive()){
         qreal curT = (qreal)m_system->m_timeInt/1000.0;
         qreal ttl = (qreal)m_lifeLeft/1000.0;
-        d->t = curT - (d->lifeSpan - ttl) + 1;
+        if (!m_advancePosition && ttl > 0){
+            qreal x = d->curX();
+            qreal vx = d->curVX();
+            qreal ax = d->curAX();
+            qreal y = d->curY();
+            qreal vy = d->curVY();
+            qreal ay = d->curAY();
+            d->t = curT - (d->lifeSpan - ttl);
+            d->setInstantaneousX(x);
+            d->setInstantaneousVX(vx);
+            d->setInstantaneousAX(ax);
+            d->setInstantaneousY(y);
+            d->setInstantaneousVY(vy);
+            d->setInstantaneousAY(ay);
+        } else {
+            d->t = curT - (d->lifeSpan - ttl);
+        }
         return true;
     }
     return false;
index c4c9929..6761220 100644 (file)
@@ -54,6 +54,7 @@ class QSGAgeAffector : public QSGParticleAffector
 {
     Q_OBJECT
     Q_PROPERTY(int lifeLeft READ lifeLeft WRITE setLifeLeft NOTIFY lifeLeftChanged)
+    Q_PROPERTY(bool advancePosition READ advancePosition WRITE setAdvancePosition NOTIFY advancePositionChanged)
 
 public:
     explicit QSGAgeAffector(QSGItem *parent = 0);
@@ -63,10 +64,16 @@ public:
         return m_lifeLeft;
     }
 
+    bool advancePosition() const
+    {
+        return m_advancePosition;
+    }
+
 protected:
     virtual bool affectParticle(QSGParticleData *d, qreal dt);
 signals:
     void lifeLeftChanged(int arg);
+    void advancePositionChanged(bool arg);
 
 public slots:
     void setLifeLeft(int arg)
@@ -77,9 +84,17 @@ public slots:
         }
     }
 
-private:
+    void setAdvancePosition(bool arg)
+    {
+        if (m_advancePosition != arg) {
+            m_advancePosition = arg;
+            emit advancePositionChanged(arg);
+        }
+    }
 
-int m_lifeLeft;
+private:
+    int m_lifeLeft;
+    bool m_advancePosition;
 };
 
 QT_END_NAMESPACE