From 4588034490d53759a9b6cf2e225113b4035e9dfa Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Tue, 20 Sep 2011 10:46:34 +1000 Subject: [PATCH] Refactor QSGParticleAffector internals Putting more of the logic in protected subroutines makes it a lot easier for subclasses to reimplement affectSystem. Change-Id: I07f6553228064f1c9b68c6f55628b12b5c78013b Reviewed-on: http://codereview.qt-project.org/5172 Reviewed-by: Qt Sanity Bot Reviewed-by: Martin Jones --- src/declarative/particles/qsgparticleaffector.cpp | 63 +++++++++++--------- src/declarative/particles/qsgparticleaffector_p.h | 2 + src/declarative/particles/qsgturbulence.cpp | 8 +-- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/declarative/particles/qsgparticleaffector.cpp b/src/declarative/particles/qsgparticleaffector.cpp index 7cb4869..ac2d0ab 100644 --- a/src/declarative/particles/qsgparticleaffector.cpp +++ b/src/declarative/particles/qsgparticleaffector.cpp @@ -150,41 +150,48 @@ bool QSGParticleAffector::activeGroup(int g) { return m_groupIds.isEmpty() || m_groupIds.contains(g); } +bool QSGParticleAffector::shouldAffect(QSGParticleData* d) +{ + if (!d) + return false; + if (activeGroup(d->group)){ + if ((m_onceOff && m_onceOffed.contains(qMakePair(d->group, d->index))) + || !d->stillAlive()) + return false; + //Need to have previous location for affected anyways + if (width() == 0 || height() == 0 + || m_shape->contains(QRectF(m_offset.x(), m_offset.y(), width(), height()), QPointF(d->curX(), d->curY()))){ + if (m_whenCollidingWith.isEmpty() || isColliding(d)){ + return true; + } + } + } + return false; + +} + +void QSGParticleAffector::postAffect(QSGParticleData* d) +{ + m_system->m_needsReset << d; + if (m_onceOff) + m_onceOffed << qMakePair(d->group, d->index); + if (isAffectedConnected()) + emit affected(d->curX(), d->curY()); +} + void QSGParticleAffector::affectSystem(qreal dt) { if (!m_enabled) return; //If not reimplemented, calls affect particle per particle //But only on particles in targeted system/area - bool affectedConnected = isAffectedConnected(); updateOffsets();//### Needed if an ancestor is transformed. - foreach (QSGParticleGroupData* gd, m_system->m_groupData){ - foreach (QSGParticleData* d, gd->data){ - if (!d) - continue; - if (activeGroup(d->group)){ - if ((m_onceOff && m_onceOffed.contains(qMakePair(d->group, d->index))) - || !d->stillAlive()) - continue; - //Need to have previous location for affected anyways - QPointF curPos; - if (affectedConnected || (width() && height())) - curPos = QPointF(d->curX(), d->curY()); - if (width() == 0 || height() == 0 - || m_shape->contains(QRectF(m_offset.x(), m_offset.y(), width(), height()),curPos)){ - if (m_whenCollidingWith.isEmpty() || isColliding(d)){ - if (affectParticle(d, dt)){ - m_system->m_needsReset << d; - if (m_onceOff) - m_onceOffed << qMakePair(d->group, d->index); - if (affectedConnected) - emit affected(curPos.x(), curPos.y()); - } - } - } - } - } - } + foreach (QSGParticleGroupData* gd, m_system->m_groupData) + if (activeGroup(m_system->m_groupData.key(gd))) + foreach (QSGParticleData* d, gd->data) + if (shouldAffect(d)) + if (affectParticle(d, dt)) + postAffect(d); } bool QSGParticleAffector::affectParticle(QSGParticleData *, qreal ) diff --git a/src/declarative/particles/qsgparticleaffector_p.h b/src/declarative/particles/qsgparticleaffector_p.h index 0dadeff..c67912f 100644 --- a/src/declarative/particles/qsgparticleaffector_p.h +++ b/src/declarative/particles/qsgparticleaffector_p.h @@ -171,6 +171,8 @@ protected: QSGParticleSystem* m_system; QStringList m_groups; bool activeGroup(int g); + bool shouldAffect(QSGParticleData* datum);//Call to do the logic on whether it is affecting that datum + void postAffect(QSGParticleData* datum);//Call to do the post-affect logic on particles which WERE affected(once off, needs reset, affected signal) bool m_enabled; virtual void componentComplete(); QPointF m_offset; diff --git a/src/declarative/particles/qsgturbulence.cpp b/src/declarative/particles/qsgturbulence.cpp index c24af1b..7792c61 100644 --- a/src/declarative/particles/qsgturbulence.cpp +++ b/src/declarative/particles/qsgturbulence.cpp @@ -180,14 +180,12 @@ void QSGTurbulenceAffector::affectSystem(qreal dt) QRectF boundsRect(0, 0, width()-1, height()-1); foreach (QSGParticleGroupData *gd, m_system->m_groupData){ - if (!activeGroup(m_system->m_groupData.key(gd)))//TODO: Surely this can be done better + if (!activeGroup(m_system->m_groupData.key(gd))) continue; foreach (QSGParticleData *d, gd->data){ - if (!d || !activeGroup(d->group) || !d->stillAlive()) + if (!shouldAffect(d)) continue; QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint(); - if (!boundsRect.contains(pos)) - continue; qreal fx = 0.0; qreal fy = 0.0; fx += m_vectorField[pos.x()][pos.y()].x() * m_strength; @@ -195,7 +193,7 @@ void QSGTurbulenceAffector::affectSystem(qreal dt) if (fx || fy){ d->setInstantaneousVX(d->curVX()+ fx * dt); d->setInstantaneousVY(d->curVY()+ fy * dt); - m_system->m_needsReset << d; + postAffect(d); } } } -- 1.7.2.5