daccec94186fa3f1aa5505474f220f0d213aee9b
[profile/ivi/qtdeclarative.git] / src / particles / qquickage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickage_p.h"
43 #include "qquickparticleemitter_p.h"
44 QT_BEGIN_NAMESPACE
45 /*!
46     \qmlclass Age QQuickAgeAffector
47     \inqmlmodule QtQuick.Particles 2
48     \inherits Affector
49     \brief For altering particle ages
50     \ingroup qtquick-particles
51
52     The Age affector allows you to alter where the particle is in its lifecycle. Common uses
53     are to expire particles prematurely, possibly giving them time to animate out.
54
55     The Age affector is also sometimes known as a 'Kill' affector, because with the default
56     parameters it will immediately expire all particles which it affects.
57
58     The Age affector only applies to particles which are still alive.
59 */
60 /*!
61     \qmlproperty int QtQuick.Particles2::Age::lifeLeft
62
63     The amount of life to set the particle to have. Affected particles
64     will advance to a point in their life where they will have this many
65     milliseconds left to live.
66 */
67
68 /*!
69     \qmlproperty bool QtQuick.Particles2::Age::advancePosition
70
71     advancePosition determines whether position, veclocity and acceleration are included in
72     the simulated aging done by the affector. If advancePosition is false,
73     then the position, velocity and acceleration will remain the same and only
74     other attributes (such as opacity) will advance in the simulation to where
75     it would normally be for that point in the particle's life. With advancePosition set to
76     true the position, velocity and acceleration will also advance to where it would
77     normally be by that point in the particle's life, making it advance its position
78     on screen.
79
80     Default value is true.
81 */
82
83 QQuickAgeAffector::QQuickAgeAffector(QQuickItem *parent) :
84     QQuickParticleAffector(parent), m_lifeLeft(0), m_advancePosition(true)
85 {
86 }
87
88
89 bool QQuickAgeAffector::affectParticle(QQuickParticleData *d, qreal dt)
90 {
91     Q_UNUSED(dt);
92     if (d->stillAlive()){
93         qreal curT = (qreal)m_system->timeInt/1000.0;
94         qreal ttl = (qreal)m_lifeLeft/1000.0;
95         if (!m_advancePosition && ttl > 0){
96             qreal x = d->curX();
97             qreal vx = d->curVX();
98             qreal ax = d->curAX();
99             qreal y = d->curY();
100             qreal vy = d->curVY();
101             qreal ay = d->curAY();
102             d->t = curT - (d->lifeSpan - ttl);
103             d->setInstantaneousX(x);
104             d->setInstantaneousVX(vx);
105             d->setInstantaneousAX(ax);
106             d->setInstantaneousY(y);
107             d->setInstantaneousVY(vy);
108             d->setInstantaneousAY(ay);
109         } else {
110             d->t = curT - (d->lifeSpan - ttl);
111         }
112         return true;
113     }
114     return false;
115 }
116 QT_END_NAMESPACE