remove some unnecessary CONFIG additions
[profile/ivi/qtdeclarative.git] / src / particles / qquickwander.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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     \qmltype Wander
47     \instantiates QQuickWanderAffector
48     \inqmlmodule QtQuick.Particles 2
49     \ingroup qtquick-particles
50     \inherits Affector
51     \brief For applying random particle trajectory
52
53 */
54 /*!
55     \qmlproperty real QtQuick.Particles2::Wander::pace
56
57     Maximum attribute change per second.
58 */
59 /*!
60     \qmlproperty real QtQuick.Particles2::Wander::xVariance
61
62     Maximum attribute x value (as a result of Wander).
63
64     If unset, Wander will not affect x values.
65 */
66 /*!
67     \qmlproperty real QtQuick.Particles2::Wander::yVariance
68
69     Maximum attribute y value (as a result of Wander).
70
71     If unset, Wander will not affect y values.
72 */
73 /*!
74     \qmlproperty AffectableParameter QtQuick.Particles2::Wander::affectedParameter
75
76     What attribute of particles is directly affected.
77     \list
78     \li PointAttractor.Position
79     \li PointAttractor.Velocity
80     \li PointAttractor.Acceleration
81     \endlist
82 */
83
84 QQuickWanderAffector::QQuickWanderAffector(QQuickItem *parent) :
85     QQuickParticleAffector(parent), m_xVariance(0), m_yVariance(0), m_pace(0)
86     , m_affectedParameter(Velocity)
87 {
88     m_needsReset = true;
89 }
90
91 QQuickWanderAffector::~QQuickWanderAffector()
92 {
93     for (QHash<int, WanderData*>::const_iterator iter=m_wanderData.constBegin();
94         iter != m_wanderData.constEnd(); iter++)
95         delete (*iter);
96 }
97
98 WanderData* QQuickWanderAffector::getData(int idx)
99 {
100     if (m_wanderData.contains(idx))
101         return m_wanderData[idx];
102     WanderData* d = new WanderData;
103     d->x_vel = 0;
104     d->y_vel = 0;
105     d->x_peak = m_xVariance;
106     d->y_peak = m_yVariance;
107     d->x_var = m_pace * qreal(qrand()) / RAND_MAX;
108     d->y_var = m_pace * qreal(qrand()) / RAND_MAX;
109
110     m_wanderData.insert(idx, d);
111     return d;
112 }
113
114 // TODO: see below
115 //void QQuickWanderAffector::reset(int systemIdx)
116 //{
117 //    if (m_wanderData.contains(systemIdx))
118 //        delete m_wanderData[systemIdx];
119 //    m_wanderData.remove(systemIdx);
120 //}
121
122 bool QQuickWanderAffector::affectParticle(QQuickParticleData* data, qreal dt)
123 {
124     /*TODO: Add a mode which does basically this - picking a direction, going in it (random velocity) and then going back
125     WanderData* d = getData(data->systemIndex);
126     if (m_xVariance != 0.) {
127         if ((d->x_vel > d->x_peak && d->x_var > 0.0) || (d->x_vel < -d->x_peak && d->x_var < 0.0)) {
128             d->x_var = -d->x_var;
129             d->x_peak = m_xVariance + m_xVariance * qreal(qrand()) / RAND_MAX;
130         }
131         d->x_vel += d->x_var * dt;
132     }
133     qreal dx = dt * d->x_vel;
134
135     if (m_yVariance != 0.) {
136         if ((d->y_vel > d->y_peak && d->y_var > 0.0) || (d->y_vel < -d->y_peak && d->y_var < 0.0)) {
137             d->y_var = -d->y_var;
138             d->y_peak = m_yVariance + m_yVariance * qreal(qrand()) / RAND_MAX;
139         }
140         d->y_vel += d->y_var * dt;
141     }
142     qreal dy = dt * d->x_vel;
143
144     //### Should we be amending vel instead?
145     ParticleVertex* p = &(data->pv);
146     p->x += dx;
147
148     p->y += dy;
149     return true;
150     */
151     qreal dx = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
152     qreal dy = dt * m_pace * (2 * qreal(qrand())/RAND_MAX - 1);
153     qreal newX, newY;
154     switch (m_affectedParameter){
155     case Position:
156         newX = data->curX() + dx;
157         if (m_xVariance > qAbs(newX) )
158             data->x += dx;
159         newY = data->curY() + dy;
160         if (m_yVariance > qAbs(newY) )
161             data->y += dy;
162         break;
163     default:
164     case Velocity:
165         newX = data->curVX() + dx;
166         if (m_xVariance > qAbs(newX) )
167             data->setInstantaneousVX(newX);
168         newY = data->curVY() + dy;
169         if (m_yVariance > qAbs(newY) )
170             data->setInstantaneousVY(newY);
171         break;
172     case Acceleration:
173         newX = data->ax + dx;
174         if (m_xVariance > qAbs(newX) )
175             data->setInstantaneousAX(newX);
176         newY = data->ay + dy;
177         if (m_yVariance > qAbs(newY) )
178             data->setInstantaneousAY(newY);
179         break;
180     }
181     return true;
182 }
183 QT_END_NAMESPACE