1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Declarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qquickcustomaffector_p.h"
43 #include <private/qv8engine_p.h>
44 #include <private/qqmlengine_p.h>
49 //TODO: Move docs (and inheritence) to real base when docs can propagate. Currently this pretends to be the base class!
51 \qmlsignal QtQuick.Particles2::Affector::affectParticles(Array particles, real dt)
53 This handler is called when particles are selected to be affected. particles contains
54 an array of particle objects which can be directly manipulated.
56 dt is the time since the last time it was affected. Use dt to normalize
57 trajectory manipulations to real time.
59 Note that JS is slower to execute, so it is not recommended to use this in
60 high-volume particle systems.
64 \qmlproperty StochasticDirection QtQuick.Particles2::Affector::position
66 Affected particles will have their position set to this direction,
67 relative to the ParticleSystem. When interpreting directions as points,
68 imagine it as an arrow with the base at the 0,0 of the ParticleSystem and the
69 tip at where the specified position will be.
73 \qmlproperty StochasticDirection QtQuick.Particles2::Affector::speed
75 Affected particles will have their speed set to this direction.
80 \qmlproperty StochasticDirection QtQuick.Particles2::Affector::acceleration
82 Affected particles will have their acceleration set to this direction.
87 \qmlproperty bool QtQuick.Particles2::Affector::relative
89 Whether the affected particles have their existing position/speed/acceleration added
94 QQuickCustomAffector::QQuickCustomAffector(QQuickItem *parent) :
95 QQuickParticleAffector(parent)
96 , m_position(&m_nullVector)
97 , m_speed(&m_nullVector)
98 , m_acceleration(&m_nullVector)
103 bool QQuickCustomAffector::isAffectConnected()
105 static int idx = QObjectPrivate::get(this)->signalIndex("affectParticles(QQmlV8Handle,qreal)");
106 return QObjectPrivate::get(this)->isSignalConnected(idx);
109 void QQuickCustomAffector::affectSystem(qreal dt)
111 if (!isAffectConnected()) {
112 QQuickParticleAffector::affectSystem(dt);
119 QList<QQuickParticleData*> toAffect;
120 foreach (QQuickParticleGroupData* gd, m_system->groupData)
121 if (activeGroup(m_system->groupData.key(gd)))
122 foreach (QQuickParticleData* d, gd->data)
126 if (toAffect.isEmpty())
132 v8::HandleScope handle_scope;
133 v8::Context::Scope scope(QQmlEnginePrivate::getV8Engine(qmlEngine(this))->context());
134 v8::Handle<v8::Array> array = v8::Array::New(toAffect.size());
135 for (int i=0; i<toAffect.size(); i++)
136 array->Set(i, toAffect[i]->v8Value().toHandle());
138 if (dt >= simulationCutoff || dt <= simulationDelta) {
139 affectProperties(toAffect, dt);
140 emit affectParticles(QQmlV8Handle::fromHandle(array), dt);
142 int realTime = m_system->timeInt;
143 m_system->timeInt -= dt * 1000.0;
144 while (dt > simulationDelta) {
145 m_system->timeInt += simulationDelta * 1000.0;
146 dt -= simulationDelta;
147 affectProperties(toAffect, simulationDelta);
148 emit affectParticles(QQmlV8Handle::fromHandle(array), simulationDelta);
150 m_system->timeInt = realTime;
152 affectProperties(toAffect, dt);
153 emit affectParticles(QQmlV8Handle::fromHandle(array), dt);
157 foreach (QQuickParticleData* d, toAffect)
158 if (d->update == 1.0)
162 bool QQuickCustomAffector::affectParticle(QQuickParticleData *d, qreal dt)
164 //This does the property based affecting, called by superclass if signal isn't hooked up.
165 bool changed = false;
166 QPointF curPos(d->curX(), d->curY());
168 if (m_acceleration != &m_nullVector){
169 QPointF pos = m_acceleration->sample(curPos);
172 pos += QPointF(d->curAX(), d->curAY());
174 d->setInstantaneousAX(pos.x());
175 d->setInstantaneousAY(pos.y());
179 if (m_speed != &m_nullVector){
180 QPointF pos = m_speed->sample(curPos);
183 pos += QPointF(d->curVX(), d->curVY());
185 d->setInstantaneousVX(pos.x());
186 d->setInstantaneousVY(pos.y());
190 if (m_position != &m_nullVector){
191 QPointF pos = m_position->sample(curPos);
196 d->setInstantaneousX(pos.x());
197 d->setInstantaneousY(pos.y());
204 void QQuickCustomAffector::affectProperties(const QList<QQuickParticleData*> particles, qreal dt)
206 foreach (QQuickParticleData* d, particles)
207 if ( affectParticle(d, dt) )