Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickcustomaffector.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
6#include <private/qquickv4particledata_p.h>
7#include <private/qqmlglobal_p.h>
8
9#include <QtCore/qdebug.h>
10
12
13//TODO: Move docs (and inheritence) to real base when docs can propagate. Currently this pretends to be the base class!
61 , m_position(&m_nullVector)
62 , m_velocity(&m_nullVector)
63 , m_acceleration(&m_nullVector)
64 , m_relative(true)
65{
66}
67
69{
72 (const QList<QQuickV4ParticleData> &, qreal));
73}
74
76{
77 //Acts a bit differently, just emits affected for everyone it might affect, when the only thing is connecting to affected(x,y)
78 bool justAffected = (m_acceleration == &m_nullVector
79 && m_velocity == &m_nullVector
80 && m_position == &m_nullVector
82 if (!isAffectConnected() && !justAffected) {
84 return;
85 }
86 if (!m_enabled)
87 return;
89
90 QList<QQuickParticleData*> toAffect;
91 for (const QQuickParticleGroupData *gd : std::as_const(m_system->groupData)) {
92 if (activeGroup(gd->index)) {
93 for (QQuickParticleData *d : gd->data) {
94 if (shouldAffect(d)) {
95 toAffect << d;
96 }
97 }
98 }
99 }
100
101 if (toAffect.isEmpty())
102 return;
103
104 if (justAffected) {
105 for (const QQuickParticleData *d : std::as_const(toAffect)) {//Not postAffect to avoid saying the particle changed
106 if (m_onceOff)
107 m_onceOffed << qMakePair(d->groupId, d->index);
108 emit affected(d->curX(m_system), d->curY(m_system));
109 }
110 return;
111 }
112
113 if (m_onceOff)
114 dt = 1.0;
115
116 QList<QQuickV4ParticleData> particles;
117 particles.reserve(toAffect.size());
118 for (QQuickParticleData *data: std::as_const(toAffect))
119 particles.push_back(data->v4Value(m_system));
120
121 const auto doAffect = [&](qreal dt) {
122 affectProperties(toAffect, dt);
123 emit affectParticles(particles, dt);
124 };
125
126 if (dt >= simulationCutoff || dt <= simulationDelta) {
127 doAffect(dt);
128 } else {
129 int realTime = m_system->timeInt;
130 m_system->timeInt -= dt * 1000.0;
131 while (dt > simulationDelta) {
132 m_system->timeInt += simulationDelta * 1000.0;
133 dt -= simulationDelta;
134 doAffect(simulationDelta);
135 }
136 m_system->timeInt = realTime;
137 if (dt > 0.0)
138 doAffect(dt);
139 }
140
141 for (QQuickParticleData *d : std::as_const(toAffect))
142 if (d->update == 1.0)
143 postAffect(d);
144}
145
147{
148 //This does the property based affecting, called by superclass if signal isn't hooked up.
149 bool changed = false;
150 QPointF curPos(d->curX(m_system), d->curY(m_system));
151
152 if (m_acceleration != &m_nullVector){
153 QPointF pos = m_acceleration->sample(curPos);
154 QPointF curAcc = QPointF(d->curAX(), d->curAY());
155 if (m_relative) {
156 pos *= dt;
157 pos += curAcc;
158 }
159 if (pos != curAcc) {
160 d->setInstantaneousAX(pos.x(), m_system);
161 d->setInstantaneousAY(pos.y(), m_system);
162 changed = true;
163 }
164 }
165
166 if (m_velocity != &m_nullVector){
167 QPointF pos = m_velocity->sample(curPos);
168 QPointF curVel = QPointF(d->curVX(m_system), d->curVY(m_system));
169 if (m_relative) {
170 pos *= dt;
171 pos += curVel;
172 }
173 if (pos != curVel) {
174 d->setInstantaneousVX(pos.x(), m_system);
175 d->setInstantaneousVY(pos.y(), m_system);
176 changed = true;
177 }
178 }
179
180 if (m_position != &m_nullVector){
181 QPointF pos = m_position->sample(curPos);
182 if (m_relative) {
183 pos *= dt;
184 pos += curPos;
185 }
186 if (pos != curPos) {
187 d->setInstantaneousX(pos.x(), m_system);
188 d->setInstantaneousY(pos.y(), m_system);
189 changed = true;
190 }
191 }
192
193 return changed;
194}
195
196void QQuickCustomAffector::affectProperties(const QList<QQuickParticleData*> &particles, qreal dt)
197{
198 for (QQuickParticleData *d : particles)
199 if ( affectParticle(d, dt) )
200 d->update = 1.0;
201}
202
204
205#include "moc_qquickcustomaffector_p.cpp"
\inmodule QtCore\reentrant
Definition qpoint.h:217
void affectSystem(qreal dt) override
void affectParticles(const QList< QQuickV4ParticleData > &particles, qreal dt)
QQuickCustomAffector(QQuickItem *parent=nullptr)
\qmlsignal QtQuick.Particles::Affector::affectParticles(Array particles, real dt)
bool affectParticle(QQuickParticleData *d, qreal dt) override
virtual QPointF sample(const QPointF &from)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void update()
Schedules a call to updatePaintNode() for this item.
virtual void affectSystem(qreal dt)
void postAffect(QQuickParticleData *datum)
QQuickParticleSystem * m_system
bool shouldAffect(QQuickParticleData *datum)
QSet< QPair< int, int > > m_onceOffed
void affected(qreal x, qreal y)
QVarLengthArray< QQuickParticleGroupData *, 32 > groupData
Combined button and popup list for selecting options.
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
QT_BEGIN_NAMESPACE 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:187
if(qFloatDistance(a, b)<(1<< 7))
[0]