From a7486d3b3bec3df9b7a036da7e331565770cbc49 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Fri, 14 Oct 2011 17:06:39 +1000 Subject: Add Move affector Slated to replace the 'Gravity' affector which only did one specialized gravity usecase anyways. This one is more generic. Change-Id: I3cbb975bad24e8f6fca7e07b671aa8ba5a3a916c Reviewed-on: http://codereview.qt-project.org/6657 Reviewed-by: Bea Lam --- src/declarative/particles/particles.pri | 6 +- src/declarative/particles/qsggravity.cpp | 2 + src/declarative/particles/qsgmove.cpp | 131 +++++++++++++++++++ src/declarative/particles/qsgmove_p.h | 155 +++++++++++++++++++++++ src/declarative/particles/qsgparticlesmodule.cpp | 2 + 5 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 src/declarative/particles/qsgmove.cpp create mode 100644 src/declarative/particles/qsgmove_p.h (limited to 'src') diff --git a/src/declarative/particles/particles.pri b/src/declarative/particles/particles.pri index bfe1062338..4502cc499c 100644 --- a/src/declarative/particles/particles.pri +++ b/src/declarative/particles/particles.pri @@ -31,7 +31,8 @@ HEADERS += \ $$PWD/qsgv8particledata_p.h \ $$PWD/qsgrectangleextruder_p.h \ $$PWD/qsgparticlegroup_p.h \ - $$PWD/qsggroupgoal_p.h + $$PWD/qsggroupgoal_p.h \ + $$PWD/qsgmove_p.h SOURCES += \ $$PWD/qsgangledirection.cpp \ @@ -64,7 +65,8 @@ SOURCES += \ $$PWD/qsgv8particledata.cpp \ $$PWD/qsgrectangleextruder.cpp \ $$PWD/qsgparticlegroup.cpp \ - $$PWD/qsggroupgoal.cpp + $$PWD/qsggroupgoal.cpp \ + $$PWD/qsgmove.cpp RESOURCES += \ $$PWD/particles.qrc diff --git a/src/declarative/particles/qsggravity.cpp b/src/declarative/particles/qsggravity.cpp index f6007269b4..dd47f66443 100644 --- a/src/declarative/particles/qsggravity.cpp +++ b/src/declarative/particles/qsggravity.cpp @@ -78,6 +78,8 @@ QSGGravityAffector::QSGGravityAffector(QSGItem *parent) : connect(this, SIGNAL(angleChanged(qreal)), this, SLOT(recalc())); recalc(); + qWarning() << "Gravity has been deprecated. Use Move instead." + << "The difference is that you can specify the acceleration with a StochasticDirection instead of just an angle/magnitude pair"; } void QSGGravityAffector::recalc() diff --git a/src/declarative/particles/qsgmove.cpp b/src/declarative/particles/qsgmove.cpp new file mode 100644 index 0000000000..e885685d6d --- /dev/null +++ b/src/declarative/particles/qsgmove.cpp @@ -0,0 +1,131 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Declarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsgmove_p.h" +#include +QT_BEGIN_NAMESPACE +const qreal CONV = 0.017453292520444443; +/*! + \qmlclass Move QSGMoveAffector + \inqmlmodule QtQuick.Particles 2 + \inherits Affector + \brief The Move element allows you to set a new position, speed or acceleration on particles + + You'll often want to set the 'once' property to true for efficiency in the case where you just + want to set the parameter. Otherwise, the parameter will be needlessly set to the same thing + every simulation cycle. +*/ + +/*! + \qmlproperty StochasticDirection QtQuick.Particles2::Move::position + + Affected particles will have their position set to this direction, + relative to the ParticleSystem. When interpreting directions as points, + imagine it as an arrow with the base at the 0,0 of the ParticleSystem and the + tip at where the specified position will be. +*/ + +/*! + \qmlproperty StochasticDirection QtQuick.Particles2::Move::speed + + Affected particles will have their speed set to this direction. +*/ + + +/*! + \qmlproperty StochasticDirection QtQuick.Particles2::Move::acceleration + + Affected particles will have their acceleration set to this direction. +*/ + + +/*! + \qmlproperty bool QtQuick.Particles2::Move::relative + + Whether the affected particles have their existing position/speed/acceleration added + to the new one. +*/ + +QSGMoveAffector::QSGMoveAffector(QSGItem *parent) + : QSGParticleAffector(parent) + , m_position(&m_nullVector) + , m_speed(&m_nullVector) + , m_acceleration(&m_nullVector) + , m_relative(false) +{ +} + +bool QSGMoveAffector::affectParticle(QSGParticleData *d, qreal dt) +{ + Q_UNUSED(dt); + bool changed = false; + QPointF curPos(d->curX(), d->curY()); + + if (m_position != &m_nullVector){ + QPointF pos = m_position->sample(curPos); + if (m_relative) + pos += curPos; + d->setInstantaneousX(pos.x()); + d->setInstantaneousY(pos.y()); + changed = true; + } + + if (m_speed != &m_nullVector){ + QPointF pos = m_speed->sample(curPos); + if (m_relative) + pos += QPointF(d->curVX(), d->curVY()); + d->setInstantaneousVX(pos.x()); + d->setInstantaneousVY(pos.y()); + changed = true; + } + + if (m_acceleration != &m_nullVector){ + QPointF pos = m_acceleration->sample(curPos); + if (m_relative) + pos += QPointF(d->curAX(), d->curAY()); + d->setInstantaneousAX(pos.x()); + d->setInstantaneousAY(pos.y()); + changed = true; + } + + return changed; +} +QT_END_NAMESPACE diff --git a/src/declarative/particles/qsgmove_p.h b/src/declarative/particles/qsgmove_p.h new file mode 100644 index 0000000000..2f7fc831f5 --- /dev/null +++ b/src/declarative/particles/qsgmove_p.h @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Declarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MOVEAFFECTOR_H +#define MOVEAFFECTOR_H +#include "qsgparticleaffector_p.h" +#include "qsgdirection_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + + +class QSGMoveAffector : public QSGParticleAffector +{ + Q_OBJECT + Q_PROPERTY(bool relative READ relative WRITE setRelative NOTIFY relativeChanged) + Q_PROPERTY(QSGDirection *position READ position WRITE setPosition NOTIFY positionChanged RESET positionReset) + Q_PROPERTY(QSGDirection *speed READ speed WRITE setSpeed NOTIFY speedChanged RESET speedReset) + Q_PROPERTY(QSGDirection *acceleration READ acceleration WRITE setAcceleration NOTIFY accelerationChanged RESET accelerationReset) + +public: + explicit QSGMoveAffector(QSGItem *parent = 0); + QSGDirection * position() const + { + return m_position; + } + + QSGDirection * speed() const + { + return m_speed; + } + + QSGDirection * acceleration() const + { + return m_acceleration; + } + + void positionReset() + { + m_position = &m_nullVector; + } + + void speedReset() + { + m_speed = &m_nullVector; + } + + void accelerationReset() + { + m_acceleration = &m_nullVector; + } + + bool relative() const + { + return m_relative; + } + +protected: + virtual bool affectParticle(QSGParticleData *d, qreal dt); +signals: + void positionChanged(QSGDirection * arg); + + void speedChanged(QSGDirection * arg); + + void accelerationChanged(QSGDirection * arg); + + void relativeChanged(bool arg); + +public slots: + void setPosition(QSGDirection * arg) + { + if (m_position != arg) { + m_position = arg; + emit positionChanged(arg); + } + } + + void setSpeed(QSGDirection * arg) + { + if (m_speed != arg) { + m_speed = arg; + emit speedChanged(arg); + } + } + + void setAcceleration(QSGDirection * arg) + { + if (m_acceleration != arg) { + m_acceleration = arg; + emit accelerationChanged(arg); + } + } + + void setRelative(bool arg) + { + if (m_relative != arg) { + m_relative = arg; + emit relativeChanged(arg); + } + } + +private slots: +private: + QSGDirection * m_position; + QSGDirection * m_speed; + QSGDirection * m_acceleration; + + QSGDirection m_nullVector; + bool m_relative; +}; + +QT_END_NAMESPACE +QT_END_HEADER +#endif // MOVEAFFECTOR_H diff --git a/src/declarative/particles/qsgparticlesmodule.cpp b/src/declarative/particles/qsgparticlesmodule.cpp index e5b5680435..fe2dec9c48 100644 --- a/src/declarative/particles/qsgparticlesmodule.cpp +++ b/src/declarative/particles/qsgparticlesmodule.cpp @@ -69,6 +69,7 @@ #include "qsgrectangleextruder_p.h" #include "qsgparticlegroup_p.h" #include "qsggroupgoal_p.h" +#include "qsgmove_p.h" QT_BEGIN_NAMESPACE @@ -106,6 +107,7 @@ void QSGParticlesModule::defineModule() qmlRegisterType(uri, 2, 0, "GroupGoal"); qmlRegisterType(uri, 2, 0 , "Turbulence"); qmlRegisterType(uri, 2, 0 , "Target"); + qmlRegisterType(uri, 2, 0, "Move"); //Exposed just for completeness qmlRegisterUncreatableType(uri, 2, 0, "ParticleAffector", -- cgit v1.2.3