7f0c5d2cf2f129a967cc91d3638470ac4ae4a1ba
[profile/ivi/qtdeclarative.git] / src / quick / particles / qquickwander.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 "qquickwander_p.h"
43 #include "qquickparticlesystem_p.h"//for ParticlesVertices
44 QT_BEGIN_NAMESPACE
45 /*!
46     \qmlclass Wander QQuickWanderAffector
47     \inqmlmodule QtQuick.Particles 2
48     \inherits Affector
49     \brief The Wander affector allows particles to randomly vary their trajectory.
50
51 */
52 /*!
53     \qmlproperty real QtQuick.Particles2::Wander::pace
54
55     Maximum attribute change per second.
56 */
57 /*!
58     \qmlproperty real QtQuick.Particles2::Wander::xVariance
59
60     Maximum attribute x value (as a result of Wander).
61
62     If unset, Wander will not affect x values.
63 */
64 /*!
65     \qmlproperty real QtQuick.Particles2::Wander::yVariance
66
67     Maximum attribute y value (as a result of Wander).
68
69     If unset, Wander will not affect y values.
70 */
71 /*!
72     \qmlproperty AffectableParameter QtQuick.Particles2::Wander::affectedParameter
73
74     What attribute of particles is directly affected.
75     \list
76     \o PointAttractor.Position
77     \o PointAttractor.Velocity
78     \o PointAttractor.Acceleration
79     \endlist
80 */
81
82 QQuickWanderAffector::QQuickWanderAffector(QQuickItem *parent) :
83     QQuickParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
84     , m_affectedParameter(Velocity)
85 {
86     m_needsReset = true;
87 }
88
89 QQuickWanderAffector::~QQuickWanderAffector()
90 {
91     for (QHash<int, WanderData*>::const_iterator iter=m_wanderData.constBegin();
92         iter != m_wanderData.constEnd(); iter++)
93         delete (*iter);
94 }
95
96 WanderData* QQuickWanderAffector::getData(int idx)
97 {
98     if (m_wanderData.contains(idx))
99         return m_wanderData[idx];
100     WanderData* d = new WanderData;
101     d->x_vel = 0;
102     d->y_vel = 0;
103     d->x_peak = m_xVariance;
104     d->y_peak = m_yVariance;
105     d->x_var = m_pace * qreal(qrand()) / RAND_MAX;
106     d->y_var = m_pace * qreal(qrand()) / RAND_MAX;
107
108     m_wanderData.insert(idx, d);
109     return d;
110 }
111
112 void QQuickWanderAffector::reset(int systemIdx)
113 {
114     if (m_wanderData.contains(systemIdx))
115         delete m_wanderData[systemIdx];
116     m_wanderData.remove(systemIdx);
117 }
118
119 bool QQuickWanderAffector::affectParticle(QQuickParticleData* data, qreal dt)
120 {
121     /*TODO: Add a mode which does basically this - picking a direction, going in it (random speed) and then going back
122     WanderData* d = getData(data->systemIndex);
123     if (m_xVariance != 0.) {
124         if ((d->x_vel > d->x_peak && d->x_var > 0.0) || (d->x_vel < -d->x_peak && d->x_var < 0.0)) {
125             d->x_var = -d->x_var;
126             d->x_peak = m_xVariance + m_xVariance * qreal(qrand()) / RAND_MAX;
127         }
128         d->x_vel += d->x_var * dt;
129     }
130     qreal dx = dt * d->x_vel;
131
132     if (m_yVariance != 0.) {
133         if ((d->y_vel > d->y_peak && d->y_var > 0.0) || (d->y_vel < -d->y_peak && d->y_var < 0.0)) {
134             d->y_var = -d->y_var;
135             d->y_peak = m_yVariance + m_yVariance * qreal(qrand()) / RAND_MAX;
136         }
137         d->y_vel += d->y_var * dt;
138     }
139     qreal dy = dt * d->x_vel;
140
141     //### Should we be amending vel instead?
142     ParticleVertex* p = &(data->pv);
143     p->x += dx;
144
145     p->y += dy;
146     return true;
147     */
148     qreal dx = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
149     qreal dy = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
150     qreal newX, newY;
151     switch (m_affectedParameter){
152     case Position:
153         newX = data->curX() + dx;
154         if (m_xVariance > qAbs(newX) )
155             data->x += dx;
156         newY = data->curY() + dy;
157         if (m_yVariance > qAbs(newY) )
158             data->y += dy;
159         break;
160     default:
161     case Velocity:
162         newX = data->curVX() + dx;
163         if (m_xVariance > qAbs(newX) )
164             data->setInstantaneousVX(newX);
165         newY = data->curVY() + dy;
166         if (m_yVariance > qAbs(newY) )
167             data->setInstantaneousVY(newY);
168         break;
169     case Acceleration:
170         newX = data->ax + dx;
171         if (m_xVariance > qAbs(newX) )
172             data->setInstantaneousAX(newX);
173         newY = data->ay + dy;
174         if (m_yVariance > qAbs(newY) )
175             data->setInstantaneousAY(newY);
176         break;
177     }
178     return true;
179 }
180 QT_END_NAMESPACE