Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickitemparticle.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 <QtQuick/qsgnode.h>
6#include <QTimer>
7#include <QQmlComponent>
8#include <QDebug>
9
11
93 QQuickParticlePainter(parent), m_fade(true), m_lastT(0), m_activeCount(0), m_delegate(nullptr)
94{
96 clock = new Clock(this);
97 clock->start();
98}
99
101{
102 delete clock;
103 qDeleteAll(m_managed);
104}
105
107{
108 m_stasis << item;
109}
110
111
113{
114 m_stasis.remove(item);
115}
116
118{
119 if (prioritize)
120 m_pendingItems.push_front(item);
121 else
122 m_pendingItems.push_back(item);
123}
124
126{
127 for (auto groupId : groupIds()) {
128 for (QQuickParticleData* data : std::as_const(m_system->groupData[groupId]->data)) {
129 if (data->delegate == item){
130 m_deletables << item;
131 data->delegate = nullptr;
132 m_system->groupData[groupId]->kill(data);
133 return;
134 }
135 }
136 }
137}
138
139void QQuickItemParticle::initialize(int gIdx, int pIdx)
140{
141 Q_UNUSED(gIdx);
142 Q_UNUSED(pIdx);
143}
144
146{
147}
148
149void QQuickItemParticle::processDeletables()
150{
151 foreach (QQuickItem* item, m_deletables){
152 if (m_fade)
153 item->setOpacity(0.);
154 item->setVisible(false);
156 if ((mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(item)))) {
157 if (mpa->m_parentItem != nullptr)
158 item->setParentItem(mpa->m_parentItem);
159 mpa->detach();
160 }
161 int idx = -1;
162 if ((idx = m_managed.indexOf(item)) != -1) {
163 m_managed.takeAt(idx);
164 delete item;
165 }
166 m_activeCount--;
167 }
168 m_deletables.clear();
169}
170
171void QQuickItemParticle::tick(int time)
172{
173 Q_UNUSED(time);//only needed because QTickAnimationProxy expects one
174 processDeletables();
175 for (auto groupId : groupIds()) {
176 for (QQuickParticleData* d : std::as_const(m_system->groupData[groupId]->data)) {
177 if (!d->delegate && d->t != -1 && d->stillAlive(m_system)) {
178 QQuickItem* parentItem = nullptr;
179 if (!m_pendingItems.isEmpty()){
180 QQuickItem *item = m_pendingItems.front();
181 m_pendingItems.pop_front();
183 d->delegate = item;
184 }else if (m_delegate){
185 d->delegate = qobject_cast<QQuickItem*>(m_delegate->create(qmlContext(this)));
186 if (d->delegate)
187 m_managed << d->delegate;
188 }
189 if (d && d->delegate){//###Data can be zero if creating an item leads to a reset - this screws things up.
190 d->delegate->setX(d->curX(m_system) - d->delegate->width() / 2); //TODO: adjust for system?
191 d->delegate->setY(d->curY(m_system) - d->delegate->height() / 2);
192 QQuickItemParticleAttached* mpa = qobject_cast<QQuickItemParticleAttached*>(qmlAttachedPropertiesObject<QQuickItemParticle>(d->delegate));
193 if (mpa){
194 mpa->m_parentItem = parentItem;
195 mpa->m_mp = this;
196 mpa->attach();
197 }
198 d->delegate->setParentItem(this);
199 if (m_fade)
200 d->delegate->setOpacity(0.);
201 d->delegate->setVisible(false);//Will be set to true when we prepare the next frame
202 m_activeCount++;
203 }
204 }
205 }
206 }
207}
208
210{
212
213 // delete all managed items which had their logical particles cleared
214 // but leave it alone if the logical particle is maintained
215 QSet<QQuickItem*> lost = QSet<QQuickItem*>(m_managed.cbegin(), m_managed.cend());
216 for (auto groupId : groupIds()) {
217 for (QQuickParticleData* d : std::as_const(m_system->groupData[groupId]->data)) {
218 lost.remove(d->delegate);
219 }
220 }
221 m_deletables.unite(lost);
222 //TODO: This doesn't yet handle calling detach on taken particles in the system reset case
223 processDeletables();
224}
225
226
228{
229 //Dummy update just to get painting tick
230 if (m_pleaseReset)
231 m_pleaseReset = false;
232
234
235 update();//Get called again
236 if (n)
237 n->markDirty(QSGNode::DirtyMaterial);
239}
240
242{
243 if (!m_system)
244 return;
245 qint64 timeStamp = m_system->systemSync(this);
246 qreal curT = timeStamp/1000.0;
247 qreal dt = curT - m_lastT;
248 m_lastT = curT;
249 if (!m_activeCount)
250 return;
251
252 //TODO: Size, better fade?
253 for (auto groupId : groupIds()) {
254 for (QQuickParticleData* data : std::as_const(m_system->groupData[groupId]->data)) {
255 QQuickItem* item = data->delegate;
256 if (!item)
257 continue;
258 float t = ((timeStamp / 1000.0f) - data->t) / data->lifeSpan;
259 if (m_stasis.contains(item)) {
260 data->t += dt;//Stasis effect
261 continue;
262 }
263 if (t >= 1.0f){//Usually happens from load
264 m_deletables << item;
265 data->delegate = nullptr;
266 }else{//Fade
267 data->delegate->setVisible(true);
268 if (m_fade){
269 float o = 1.f;
270 if (t <0.2f)
271 o = t * 5;
272 if (t > 0.8f)
273 o = (1-t)*5;
274 item->setOpacity(o);
275 }
276 }
277 item->setX(data->curX(m_system) - item->width() / 2 - m_systemOffset.x());
278 item->setY(data->curY(m_system) - item->height() / 2 - m_systemOffset.y());
279 }
280 }
281}
282
284{
285 return new QQuickItemParticleAttached(object);
286}
287
289
290#include "moc_qquickitemparticle_p.cpp"
DarwinBluetooth::DeviceInquiryDelegate * m_delegate
void setX(qreal x)
void setOpacity(qreal opacity)
void setParentItem(QGraphicsItem *parent)
Sets this item's parent item to newParent.
QGraphicsItem * parentItem() const
Returns a pointer to this item's parent item.
void setVisible(bool visible)
If visible is true, the item is made visible.
void setY(qreal y)
void push_front(rvalue_ref t)
Definition qlist.h:674
bool isEmpty() const noexcept
Definition qlist.h:390
void push_back(parameter_type t)
Definition qlist.h:672
T takeAt(qsizetype i)
Definition qlist.h:592
reference front()
Definition qlist.h:684
void pop_front() noexcept
Definition qlist.h:677
const_iterator cend() const noexcept
Definition qlist.h:614
const_iterator cbegin() const noexcept
Definition qlist.h:613
\inmodule QtCore
Definition qobject.h:90
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
virtual QObject * create(QQmlContext *context=nullptr)
Create an object instance from this component, within the specified context.
static QQuickItemParticleAttached * qmlAttachedProperties(QObject *object)
void commit(int gIdx, int pIdx) override
void give(QQuickItem *item)
void initialize(int gIdx, int pIdx) override
QQuickItemParticle(QQuickItem *parent=nullptr)
\qmltype ItemParticle \instantiates QQuickItemParticle \inqmlmodule QtQuick.Particles \inherits Parti...
QSGNode * updatePaintNode(QSGNode *, UpdatePaintNodeData *) override
Called on the render thread when it is time to sync the state of the item with the scene graph.
void freeze(QQuickItem *item)
void take(QQuickItem *item, bool prioritize=false)
void unfreeze(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
virtual QSGNode * updatePaintNode(QSGNode *, UpdatePaintNodeData *)
Called on the render thread when it is time to sync the state of the item with the scene graph.
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
QQuickItem * parentItem() const
void update()
Schedules a call to updatePaintNode() for this item.
const GroupIDs & groupIds() const
QQuickParticleSystem * m_system
QVarLengthArray< QQuickParticleGroupData *, 32 > groupData
int systemSync(QQuickParticlePainter *p)
\group qtquick-scenegraph-nodes \title Qt Quick Scene Graph Node classes
Definition qsgnode.h:37
@ DirtyMaterial
Definition qsgnode.h:75
Definition qset.h:18
bool remove(const T &value)
Definition qset.h:63
void clear()
Definition qset.h:61
QSet< T > & unite(const QSet< T > &other)
Definition qset.h:225
bool contains(const T &value) const
Definition qset.h:71
T * data() noexcept
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
GLdouble GLdouble t
Definition qopenglext.h:243
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:71
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92
QObject::connect nullptr
QGraphicsItem * item
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:955
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent