Add some more properties to JS particle type
[profile/ivi/qtdeclarative.git] / src / declarative / particles / qsgwander.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 "qsgwander_p.h"
43 #include "qsgparticlesystem_p.h"//for ParticlesVertices
44 QT_BEGIN_NAMESPACE
45 /*!
46     \qmlclass Wander QSGWanderAffector
47     \inqmlmodule QtQuick.Particles 2
48     \since QtQuick.Particles 2.0
49     \inherits Affector
50     \brief The Wander affector allows particles to randomly vary their trajectory.
51
52 */
53
54 QSGWanderAffector::QSGWanderAffector(QSGItem *parent) :
55     QSGParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
56     , m_physics(Velocity)
57 {
58     m_needsReset = true;
59 }
60
61 QSGWanderAffector::~QSGWanderAffector()
62 {
63     for (QHash<int, WanderData*>::const_iterator iter=m_wanderData.constBegin();
64         iter != m_wanderData.constEnd(); iter++)
65         delete (*iter);
66 }
67
68 WanderData* QSGWanderAffector::getData(int idx)
69 {
70     if (m_wanderData.contains(idx))
71         return m_wanderData[idx];
72     WanderData* d = new WanderData;
73     d->x_vel = 0;
74     d->y_vel = 0;
75     d->x_peak = m_xVariance;
76     d->y_peak = m_yVariance;
77     d->x_var = m_pace * qreal(qrand()) / RAND_MAX;
78     d->y_var = m_pace * qreal(qrand()) / RAND_MAX;
79
80     m_wanderData.insert(idx, d);
81     return d;
82 }
83
84 void QSGWanderAffector::reset(int systemIdx)
85 {
86     if (m_wanderData.contains(systemIdx))
87         delete m_wanderData[systemIdx];
88     m_wanderData.remove(systemIdx);
89 }
90
91 bool QSGWanderAffector::affectParticle(QSGParticleData* data, qreal dt)
92 {
93     /*TODO: Add a mode which does basically this - picking a direction, going in it (random speed) and then going back
94     WanderData* d = getData(data->systemIndex);
95     if (m_xVariance != 0.) {
96         if ((d->x_vel > d->x_peak && d->x_var > 0.0) || (d->x_vel < -d->x_peak && d->x_var < 0.0)) {
97             d->x_var = -d->x_var;
98             d->x_peak = m_xVariance + m_xVariance * qreal(qrand()) / RAND_MAX;
99         }
100         d->x_vel += d->x_var * dt;
101     }
102     qreal dx = dt * d->x_vel;
103
104     if (m_yVariance != 0.) {
105         if ((d->y_vel > d->y_peak && d->y_var > 0.0) || (d->y_vel < -d->y_peak && d->y_var < 0.0)) {
106             d->y_var = -d->y_var;
107             d->y_peak = m_yVariance + m_yVariance * qreal(qrand()) / RAND_MAX;
108         }
109         d->y_vel += d->y_var * dt;
110     }
111     qreal dy = dt * d->x_vel;
112
113     //### Should we be amending vel instead?
114     ParticleVertex* p = &(data->pv);
115     p->x += dx;
116
117     p->y += dy;
118     return true;
119     */
120     qreal dx = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
121     qreal dy = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
122     qreal newX, newY;
123     switch (m_physics){
124     case Position:
125         newX = data->curX() + dx;
126         if (m_xVariance > qAbs(newX) )
127             data->x += dx;
128         newY = data->curY() + dy;
129         if (m_yVariance > qAbs(newY) )
130             data->y += dy;
131         break;
132     default:
133     case Velocity:
134         newX = data->curVX() + dx;
135         if (m_xVariance > qAbs(newX) )
136             data->setInstantaneousVX(newX);
137         newY = data->curVY() + dy;
138         if (m_yVariance > qAbs(newY) )
139             data->setInstantaneousVY(newY);
140         break;
141     case Acceleration:
142         newX = data->ax + dx;
143         if (m_xVariance > qAbs(newX) )
144             data->setInstantaneousAX(newX);
145         newY = data->ay + dy;
146         if (m_yVariance > qAbs(newY) )
147             data->setInstantaneousAY(newY);
148         break;
149     }
150     return true;
151 }
152 QT_END_NAMESPACE