1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the Declarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qquickcustomaffector_p.h"
43 #include <private/qv8engine_p.h>
44 #include <private/qdeclarativeengine_p.h>
45 #include <QDeclarativeEngine>
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(QDeclarativeV8Handle,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(QDeclarativeEnginePrivate::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(QDeclarativeV8Handle::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(QDeclarativeV8Handle::fromHandle(array), simulationDelta);
150 m_system->timeInt = realTime;
152 affectProperties(toAffect, dt);
153 emit affectParticles(QDeclarativeV8Handle::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) )