aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3dparticles/qquick3dparticleaffector.cpp
blob: 949fc6a78e4042314df0c708483312abe8877061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dparticleaffector_p.h"
#include "qquick3dparticleutils_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype Affector3D
    \inherits Node
    \inqmlmodule QtQuick3D.Particles3D
    \brief Affectors modify the attributes of particles during their lifetime.
    \since 6.2

    The Affector3D is an abstract base class of affectors like \l Gravity3D, \l Wander3D, and \l PointRotator3D.

    By default affectors affect all particles in the system, but this can be limited by defining
    the \l particles list. If the system has multiple affectors, the order of affectors may
    result in different outcome, as affectors are applied one after another.
*/

QQuick3DParticleAffector::QQuick3DParticleAffector(QQuick3DNode *parent)
    : QQuick3DNode(parent)
{
}

QQuick3DParticleAffector::~QQuick3DParticleAffector()
{
    for (const auto &connection : std::as_const(m_connections))
        QObject::disconnect(connection);
    if (m_system)
        m_system->unRegisterParticleAffector(this);
}

/*!
    \qmlproperty ParticleSystem3D Affector3D::system

    This property defines the \l ParticleSystem3D for the affector. If system is direct parent of the affector,
    this property does not need to be defined.
*/
QQuick3DParticleSystem *QQuick3DParticleAffector::system() const
{
    return m_system;
}

void QQuick3DParticleAffector::setSystem(QQuick3DParticleSystem *system)
{
    if (m_system == system)
        return;

    if (m_system)
        m_system->unRegisterParticleAffector(this);

    m_system = system;
    if (m_system)
        m_system->registerParticleAffector(this);

    m_systemSharedParent = getSharedParentNode(this, m_system);

    Q_EMIT systemChanged();
    Q_EMIT update();
}

/*!
    \qmlproperty bool Affector3D::enabled

    If enabled is set to \c false, this affector will not alter any particles.
    Usually this is used to conditionally turn an affector on or off.

    The default value is \c true.
*/
bool QQuick3DParticleAffector::enabled() const
{
    return m_enabled;
}

void QQuick3DParticleAffector::setEnabled(bool enabled)
{
    if (m_enabled == enabled)
        return;

    m_enabled = enabled;
    Q_EMIT enabledChanged();
    Q_EMIT update();
}

void QQuick3DParticleAffector::componentComplete()
{
    if (!m_system && qobject_cast<QQuick3DParticleSystem*>(parent()))
        setSystem(qobject_cast<QQuick3DParticleSystem*>(parent()));
}

void QQuick3DParticleAffector::prepareToAffect()
{
}

// Particles

/*!
    \qmlproperty List<Particle3D> Affector3D::particles

    This list controls which logical particles will be affected.
    When empty, all particles in the system are affected.
*/
QQmlListProperty<QQuick3DParticle> QQuick3DParticleAffector::particles()
{
    return {this, this,
             &QQuick3DParticleAffector::appendParticle,
             &QQuick3DParticleAffector::particleCount,
             &QQuick3DParticleAffector::particle,
             &QQuick3DParticleAffector::clearParticles,
             &QQuick3DParticleAffector::replaceParticle,
             &QQuick3DParticleAffector::removeLastParticle};
}

void QQuick3DParticleAffector::appendParticle(QQuick3DParticle *n) {
    m_particles.append(n);
    m_connections.insert(n, QObject::connect(n, &QObject::destroyed, this, [this](QObject *obj) {
        QQuick3DParticle *particle = qobject_cast<QQuick3DParticle *>(obj);
        m_particles.removeAll(particle);
        QObject::disconnect(m_connections[particle]);
        m_connections.remove(particle);
    }));
}

qsizetype QQuick3DParticleAffector::particleCount() const
{
    return m_particles.size();
}

QQuick3DParticle *QQuick3DParticleAffector::particle(qsizetype index) const
{
    return m_particles.at(index);
}

void QQuick3DParticleAffector::clearParticles() {
    m_particles.clear();
}

void QQuick3DParticleAffector::replaceParticle(qsizetype index, QQuick3DParticle *n)
{
    QQuick3DParticle *remove = m_particles[index];
    QObject::disconnect(m_connections[remove]);
    m_connections.remove(remove);
    m_particles[index] = n;
    m_connections.insert(n, QObject::connect(n, &QObject::destroyed, this, [this](QObject *obj) {
        QQuick3DParticle *particle = qobject_cast<QQuick3DParticle *>(obj);
        m_particles.removeAll(particle);
        QObject::disconnect(m_connections[particle]);
        m_connections.remove(particle);
    }));
}

void QQuick3DParticleAffector::removeLastParticle()
{
    QQuick3DParticle *last = m_particles.last();
    QObject::disconnect(m_connections[last]);
    m_connections.remove(last);
    m_particles.removeLast();
}

// Particles - static
void QQuick3DParticleAffector::appendParticle(QQmlListProperty<QQuick3DParticle> *list, QQuick3DParticle *p) {
    reinterpret_cast<QQuick3DParticleAffector *>(list->data)->appendParticle(p);
}

void QQuick3DParticleAffector::clearParticles(QQmlListProperty<QQuick3DParticle> *list) {
    reinterpret_cast<QQuick3DParticleAffector *>(list->data)->clearParticles();
}

void QQuick3DParticleAffector::replaceParticle(QQmlListProperty<QQuick3DParticle> *list, qsizetype i, QQuick3DParticle *p)
{
    reinterpret_cast<QQuick3DParticleAffector *>(list->data)->replaceParticle(i, p);
}

void QQuick3DParticleAffector::removeLastParticle(QQmlListProperty<QQuick3DParticle> *list)
{
    reinterpret_cast<QQuick3DParticleAffector *>(list->data)->removeLastParticle();
}

QQuick3DParticle *QQuick3DParticleAffector::particle(QQmlListProperty<QQuick3DParticle> *list, qsizetype i) {
    return reinterpret_cast<QQuick3DParticleAffector *>(list->data)->particle(i);
}

qsizetype QQuick3DParticleAffector::particleCount(QQmlListProperty<QQuick3DParticle> *list) {
    return reinterpret_cast<QQuick3DParticleAffector *>(list->data)->particleCount();
}

QT_END_NAMESPACE