Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dparticleaffector.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
8
10
11/*!
12 \qmltype Affector3D
13 \inherits Node
14 \inqmlmodule QtQuick3D.Particles3D
15 \brief Affectors modify the attributes of particles during their lifetime.
16 \since 6.2
17
18 The Affector3D is an abstract base class of affectors like \l Gravity3D, \l Wander3D, and \l PointRotator3D.
19
20 By default affectors affect all particles in the system, but this can be limited by defining
21 the \l particles list. If the system has multiple affectors, the order of affectors may
22 result in different outcome, as affectors are applied one after another.
23*/
24
25QQuick3DParticleAffector::QQuick3DParticleAffector(QQuick3DNode *parent)
26 : QQuick3DNode(parent)
27{
28}
29
30QQuick3DParticleAffector::~QQuick3DParticleAffector()
31{
32 for (const auto &connection : std::as_const(m_connections))
33 QObject::disconnect(connection);
34 if (m_system)
35 m_system->unRegisterParticleAffector(this);
36}
37
38/*!
39 \qmlproperty ParticleSystem3D Affector3D::system
40
41 This property defines the \l ParticleSystem3D for the affector. If system is direct parent of the affector,
42 this property does not need to be defined.
43*/
44QQuick3DParticleSystem *QQuick3DParticleAffector::system() const
45{
46 return m_system;
47}
48
49void QQuick3DParticleAffector::setSystem(QQuick3DParticleSystem *system)
50{
51 if (m_system == system)
52 return;
53
54 if (m_system)
55 m_system->unRegisterParticleAffector(this);
56
57 m_system = system;
58 if (m_system)
59 m_system->registerParticleAffector(this);
60
61 m_systemSharedParent = getSharedParentNode(this, m_system);
62
63 Q_EMIT systemChanged();
64 Q_EMIT update();
65}
66
67/*!
68 \qmlproperty bool Affector3D::enabled
69
70 If enabled is set to \c false, this affector will not alter any particles.
71 Usually this is used to conditionally turn an affector on or off.
72
73 The default value is \c true.
74*/
75bool QQuick3DParticleAffector::enabled() const
76{
77 return m_enabled;
78}
79
80void QQuick3DParticleAffector::setEnabled(bool enabled)
81{
82 if (m_enabled == enabled)
83 return;
84
85 m_enabled = enabled;
86 Q_EMIT enabledChanged();
87 Q_EMIT update();
88}
89
90void QQuick3DParticleAffector::componentComplete()
91{
92 if (!m_system && qobject_cast<QQuick3DParticleSystem*>(parent()))
93 setSystem(qobject_cast<QQuick3DParticleSystem*>(parent()));
94}
95
96void QQuick3DParticleAffector::prepareToAffect()
97{
98}
99
100// Particles
101
102/*!
103 \qmlproperty List<Particle3D> Affector3D::particles
104
105 This list controls which logical particles will be affected.
106 When empty, all particles in the system are affected.
107*/
108QQmlListProperty<QQuick3DParticle> QQuick3DParticleAffector::particles()
109{
110 return {this, this,
111 &QQuick3DParticleAffector::appendParticle,
112 &QQuick3DParticleAffector::particleCount,
113 &QQuick3DParticleAffector::particle,
114 &QQuick3DParticleAffector::clearParticles,
115 &QQuick3DParticleAffector::replaceParticle,
116 &QQuick3DParticleAffector::removeLastParticle};
117}
118
119void QQuick3DParticleAffector::appendParticle(QQuick3DParticle *n) {
120 m_particles.append(n);
121 m_connections.insert(n, QObject::connect(n, &QObject::destroyed, this, [this](QObject *obj) {
122 QQuick3DParticle *particle = qobject_cast<QQuick3DParticle *>(obj);
123 m_particles.removeAll(particle);
124 QObject::disconnect(m_connections[particle]);
125 m_connections.remove(particle);
126 }));
127}
128
129qsizetype QQuick3DParticleAffector::particleCount() const
130{
131 return m_particles.size();
132}
133
134QQuick3DParticle *QQuick3DParticleAffector::particle(qsizetype index) const
135{
136 return m_particles.at(index);
137}
138
139void QQuick3DParticleAffector::clearParticles() {
140 m_particles.clear();
141}
142
143void QQuick3DParticleAffector::replaceParticle(qsizetype index, QQuick3DParticle *n)
144{
145 QQuick3DParticle *remove = m_particles[index];
146 QObject::disconnect(m_connections[remove]);
147 m_connections.remove(remove);
148 m_particles[index] = n;
149 m_connections.insert(n, QObject::connect(n, &QObject::destroyed, this, [this](QObject *obj) {
150 QQuick3DParticle *particle = qobject_cast<QQuick3DParticle *>(obj);
151 m_particles.removeAll(particle);
152 QObject::disconnect(m_connections[particle]);
153 m_connections.remove(particle);
154 }));
155}
156
157void QQuick3DParticleAffector::removeLastParticle()
158{
159 QQuick3DParticle *last = m_particles.last();
160 QObject::disconnect(m_connections[last]);
161 m_connections.remove(last);
162 m_particles.removeLast();
163}
164
165// Particles - static
166void QQuick3DParticleAffector::appendParticle(QQmlListProperty<QQuick3DParticle> *list, QQuick3DParticle *p) {
167 reinterpret_cast<QQuick3DParticleAffector *>(list->data)->appendParticle(p);
168}
169
170void QQuick3DParticleAffector::clearParticles(QQmlListProperty<QQuick3DParticle> *list) {
171 reinterpret_cast<QQuick3DParticleAffector *>(list->data)->clearParticles();
172}
173
174void QQuick3DParticleAffector::replaceParticle(QQmlListProperty<QQuick3DParticle> *list, qsizetype i, QQuick3DParticle *p)
175{
176 reinterpret_cast<QQuick3DParticleAffector *>(list->data)->replaceParticle(i, p);
177}
178
179void QQuick3DParticleAffector::removeLastParticle(QQmlListProperty<QQuick3DParticle> *list)
180{
181 reinterpret_cast<QQuick3DParticleAffector *>(list->data)->removeLastParticle();
182}
183
184QQuick3DParticle *QQuick3DParticleAffector::particle(QQmlListProperty<QQuick3DParticle> *list, qsizetype i) {
185 return reinterpret_cast<QQuick3DParticleAffector *>(list->data)->particle(i);
186}
187
188qsizetype QQuick3DParticleAffector::particleCount(QQmlListProperty<QQuick3DParticle> *list) {
189 return reinterpret_cast<QQuick3DParticleAffector *>(list->data)->particleCount();
190}
191
192QT_END_NAMESPACE
Combined button and popup list for selecting options.