6c4318180feff5e2d8c88141a6f034f554b690d5
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 The Age affector allows you to prematurely age particles
50
51     The Age affector allows you to alter where the particle is in its lifecycle. Common uses
52     are to expire particles prematurely, possibly giving them time to animate out.
53
54     The Age affector only applies to particles which are still alive.
55 */
56 /*!
57     \qmlproperty int QtQuick.Particles2::Age::lifeLeft
58
59     The amount of life to set the particle to have. Affected particles
60     will advance to a point in their life where they will have this many
61     milliseconds left to live.
62 */
63
64 /*!
65     \qmlproperty bool QtQuick.Particles2::Age::advancePosition
66
67     advancePosition determines whether position, veclocity and acceleration are included in
68     the simulated aging done by the affector. If advancePosition is false,
69     then the position, velocity and acceleration will remain the same and only
70     other attributes (such as opacity) will advance in the simulation to where
71     it would normally be for that point in the particle's life. With advancePosition set to
72     true the position, velocity and acceleration will also advance to where it would
73     normally be by that point in the particle's life, making it advance its position
74     on screen.
75
76     Default value is true.
77 */
78
79 QQuickAgeAffector::QQuickAgeAffector(QQuickItem *parent) :
80     QQuickParticleAffector(parent), m_lifeLeft(0), m_advancePosition(true)
81 {
82 }
83
84
85 bool QQuickAgeAffector::affectParticle(QQuickParticleData *d, qreal dt)
86 {
87     Q_UNUSED(dt);
88     if (d->stillAlive()){
89         qreal curT = (qreal)m_system->timeInt/1000.0;
90         qreal ttl = (qreal)m_lifeLeft/1000.0;
91         if (!m_advancePosition && ttl > 0){
92             qreal x = d->curX();
93             qreal vx = d->curVX();
94             qreal ax = d->curAX();
95             qreal y = d->curY();
96             qreal vy = d->curVY();
97             qreal ay = d->curAY();
98             d->t = curT - (d->lifeSpan - ttl);
99             d->setInstantaneousX(x);
100             d->setInstantaneousVX(vx);
101             d->setInstantaneousAX(ax);
102             d->setInstantaneousY(y);
103             d->setInstantaneousVY(vy);
104             d->setInstantaneousAY(ay);
105         } else {
106             d->t = curT - (d->lifeSpan - ttl);
107         }
108         return true;
109     }
110     return false;
111 }
112 QT_END_NAMESPACE