Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickparticleaffector.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QDebug>
6#include <private/qqmlglobal_p.h>
8
12
99 QQuickItem(parent), m_needsReset(false), m_ignoresTime(false), m_onceOff(false), m_enabled(true)
100 , m_system(nullptr), m_updateIntSet(false), m_shape(new QQuickParticleExtruder(this))
101{
102}
103
105{
107}
108
109
111{
112 if (!m_system && qobject_cast<QQuickParticleSystem*>(parentItem()))
113 setSystem(qobject_cast<QQuickParticleSystem*>(parentItem()));
115}
116
118 if (!m_system)
119 return false;
120
121 if (m_updateIntSet){ //This can occur before group ids are properly assigned, but that resets the flag
122 m_groupIds.clear();
123 foreach (const QString &p, m_groups)
124 m_groupIds << m_system->groupIds[p];
125 m_updateIntSet = false;
126 }
127 return m_groupIds.isEmpty() || m_groupIds.contains(g);
128}
129
131{
132 if (!d)
133 return false;
134 if (!m_system)
135 return false;
136
137 if (activeGroup(d->groupId)){
138 if ((m_onceOff && m_onceOffed.contains(qMakePair(d->groupId, d->index)))
139 || !d->stillAlive(m_system))
140 return false;
141 //Need to have previous location for affected anyways
142 if (width() == 0 || height() == 0
143 || m_shape->contains(QRectF(m_offset.x(), m_offset.y(), width(), height()), QPointF(d->curX(m_system), d->curY(m_system)))){
144 if (m_whenCollidingWith.isEmpty() || isColliding(d)){
145 return true;
146 }
147 }
148 }
149 return false;
150
151}
152
154{
155 if (!m_system)
156 return;
157
159 if (m_onceOff)
160 m_onceOffed << qMakePair(d->groupId, d->index);
162 emit affected(d->curX(m_system), d->curY(m_system));
163}
164
166const qreal QQuickParticleAffector::simulationCutoff = 1.000;//If this goes above 1.0, then m_once behaviour needs special codepath
167
169{
170 if (!m_enabled)
171 return;
172 if (!m_system)
173 return;
174
175 //If not reimplemented, calls affectParticle per particle
176 //But only on particles in targeted system/area
177 updateOffsets();//### Needed if an ancestor is transformed.
178 if (m_onceOff)
179 dt = 1.0;
180 for (QQuickParticleGroupData* gd : std::as_const(m_system->groupData)) {
181 if (activeGroup(gd->index)) {
182 for (QQuickParticleData* d : std::as_const(gd->data)) {
183 if (shouldAffect(d)) {
184 bool affected = false;
185 qreal myDt = dt;
186 if (!m_ignoresTime && myDt < simulationCutoff) {
187 int realTime = m_system->timeInt;
188 m_system->timeInt -= myDt * 1000.0;
189 while (myDt > simulationDelta) {
190 m_system->timeInt += simulationDelta * 1000.0;
191 if (d->alive(m_system))//Only affect during the parts it was alive for
193 myDt -= simulationDelta;
194 }
195 m_system->timeInt = realTime;
196 }
197 if (myDt > 0.0)
198 affected = affectParticle(d, myDt) || affected;
199 if (affected)
200 postAffect(d);
201 }
202 }
203 }
204 }
205}
206
208{
209 return true;
210}
211
213{//TODO: This, among other ones, should be restructured so they don't all need to remember to call the superclass
214 if (m_onceOff)
215 if (activeGroup(pd->groupId))
217}
218
220{
221 if (m_system)
222 m_offset = m_system->mapFromItem(this, QPointF(0, 0));
223}
224
225bool QQuickParticleAffector::isColliding(QQuickParticleData *d) const
226{
227 if (!m_system)
228 return false;
229
230 qreal myCurX = d->curX(m_system);
231 qreal myCurY = d->curY(m_system);
232 qreal myCurSize = d->curSize(m_system) / 2;
233 foreach (const QString &group, m_whenCollidingWith){
235 if (!other->stillAlive(m_system))
236 continue;
237 qreal otherCurX = other->curX(m_system);
238 qreal otherCurY = other->curY(m_system);
239 qreal otherCurSize = other->curSize(m_system) / 2;
240 if ((myCurX + myCurSize > otherCurX - otherCurSize
241 && myCurX - myCurSize < otherCurX + otherCurSize)
242 && (myCurY + myCurSize > otherCurY - otherCurSize
243 && myCurY - myCurSize < otherCurY + otherCurSize))
244 return true;
245 }
246 }
247 return false;
248}
249
251
252#include "moc_qquickparticleaffector_p.cpp"
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
Q_INVOKABLE QPointF mapFromItem(const QQuickItem *item, const QPointF &point) const
Maps the given point in item's coordinate system to the equivalent point within this item's coordinat...
void componentComplete() override
\reimp Derived classes should call the base class method before adding their own actions to perform a...
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
QQuickItem * parentItem() const
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
virtual void reset(QQuickParticleData *)
virtual void affectSystem(qreal dt)
void setSystem(QQuickParticleSystem *arg)
virtual bool affectParticle(QQuickParticleData *d, qreal dt)
void postAffect(QQuickParticleData *datum)
QQuickParticleSystem * m_system
bool shouldAffect(QQuickParticleData *datum)
QSet< QPair< int, int > > m_onceOffed
void componentComplete() override
\reimp Derived classes should call the base class method before adding their own actions to perform a...
QQuickParticleAffector(QQuickItem *parent=nullptr)
Applies alterations to the attributes of logical particles at any point in their lifetime.
void affected(qreal x, qreal y)
QQuickParticleGroupData::ID groupId
virtual bool contains(const QRectF &bounds, const QPointF &point)
QVarLengthArray< QQuickParticleGroupData *, 32 > groupData
QHash< QString, int > groupIds
QSet< QQuickParticleData * > needsReset
\inmodule QtCore\reentrant
Definition qrect.h:483
bool remove(const T &value)
Definition qset.h:63
bool isEmpty() const
Definition qset.h:52
void clear()
Definition qset.h:61
bool contains(const T &value) const
Definition qset.h:71
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
T * data() noexcept
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
GLint GLsizei width
GLboolean GLuint group
GLboolean GLboolean g
GLfloat GLfloat p
[1]
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition qpair.h:19
#define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments)
#define emit
double qreal
Definition qtypes.h:92
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent