Qt 6.x
The Qt SDK
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#include <private/qqmlengine_p.h>
6#include <private/qqmlglobal_p.h>
7#include <private/qjsvalue_p.h>
8#include <QQmlEngine>
9#include <QDebug>
11
12//TODO: Move docs (and inheritence) to real base when docs can propagate. Currently this pretends to be the base class!
60 , m_position(&m_nullVector)
61 , m_velocity(&m_nullVector)
62 , m_acceleration(&m_nullVector)
63 , m_relative(true)
64{
65}
66
68{
70}
71
73{
74 //Acts a bit differently, just emits affected for everyone it might affect, when the only thing is connecting to affected(x,y)
75 bool justAffected = (m_acceleration == &m_nullVector
76 && m_velocity == &m_nullVector
77 && m_position == &m_nullVector
79 if (!isAffectConnected() && !justAffected) {
81 return;
82 }
83 if (!m_enabled)
84 return;
86
88 for (const QQuickParticleGroupData *gd : std::as_const(m_system->groupData)) {
89 if (activeGroup(gd->index)) {
90 for (QQuickParticleData *d : gd->data) {
91 if (shouldAffect(d)) {
92 toAffect << d;
93 }
94 }
95 }
96 }
97
98 if (toAffect.isEmpty())
99 return;
100
101 if (justAffected) {
102 for (const QQuickParticleData *d : std::as_const(toAffect)) {//Not postAffect to avoid saying the particle changed
103 if (m_onceOff)
104 m_onceOffed << qMakePair(d->groupId, d->index);
105 emit affected(d->curX(m_system), d->curY(m_system));
106 }
107 return;
108 }
109
110 if (m_onceOff)
111 dt = 1.0;
112
115
116 QV4::Scope scope(v4);
117 QV4::ScopedArrayObject array(scope, v4->newArrayObject(toAffect.size()));
118 QV4::ScopedValue v(scope);
119 for (int i=0; i<toAffect.size(); i++)
120 array->put(i, (v = toAffect[i]->v4Value(m_system)));
121
122 const auto doAffect = [&](qreal dt) {
123 affectProperties(toAffect, dt);
124 QJSValue particles;
125 QJSValuePrivate::setValue(&particles, array);
126 emit affectParticles(particles, dt);
127 };
128
129 if (dt >= simulationCutoff || dt <= simulationDelta) {
130 doAffect(dt);
131 } else {
132 int realTime = m_system->timeInt;
133 m_system->timeInt -= dt * 1000.0;
134 while (dt > simulationDelta) {
135 m_system->timeInt += simulationDelta * 1000.0;
136 dt -= simulationDelta;
137 doAffect(simulationDelta);
138 }
139 m_system->timeInt = realTime;
140 if (dt > 0.0)
141 doAffect(dt);
142 }
143
144 for (QQuickParticleData *d : std::as_const(toAffect))
145 if (d->update == 1.0)
146 postAffect(d);
147}
148
150{
151 //This does the property based affecting, called by superclass if signal isn't hooked up.
152 bool changed = false;
153 QPointF curPos(d->curX(m_system), d->curY(m_system));
154
155 if (m_acceleration != &m_nullVector){
156 QPointF pos = m_acceleration->sample(curPos);
157 QPointF curAcc = QPointF(d->curAX(), d->curAY());
158 if (m_relative) {
159 pos *= dt;
160 pos += curAcc;
161 }
162 if (pos != curAcc) {
163 d->setInstantaneousAX(pos.x(), m_system);
164 d->setInstantaneousAY(pos.y(), m_system);
165 changed = true;
166 }
167 }
168
169 if (m_velocity != &m_nullVector){
170 QPointF pos = m_velocity->sample(curPos);
171 QPointF curVel = QPointF(d->curVX(m_system), d->curVY(m_system));
172 if (m_relative) {
173 pos *= dt;
174 pos += curVel;
175 }
176 if (pos != curVel) {
177 d->setInstantaneousVX(pos.x(), m_system);
178 d->setInstantaneousVY(pos.y(), m_system);
179 changed = true;
180 }
181 }
182
183 if (m_position != &m_nullVector){
184 QPointF pos = m_position->sample(curPos);
185 if (m_relative) {
186 pos *= dt;
187 pos += curPos;
188 }
189 if (pos != curPos) {
190 d->setInstantaneousX(pos.x(), m_system);
191 d->setInstantaneousY(pos.y(), m_system);
192 changed = true;
193 }
194 }
195
196 return changed;
197}
198
199void QQuickCustomAffector::affectProperties(const QList<QQuickParticleData*> &particles, qreal dt)
200{
201 for (QQuickParticleData *d : particles)
202 if ( affectParticle(d, dt) )
203 d->update = 1.0;
204}
205
207
208#include "moc_qquickcustomaffector_p.cpp"
QV4::ExecutionEngine * handle() const
Definition qjsengine.h:292
static void setValue(QJSValue *jsval, const QV4::Value &v)
Definition qjsvalue_p.h:282
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
\inmodule QtCore\reentrant
Definition qpoint.h:214
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
void affectSystem(qreal dt) override
void affectParticles(const QJSValue &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:64
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.
GLsizei const GLfloat * v
[13]
GLenum array
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
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:76
#define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments)
#define emit
double qreal
Definition qtypes.h:92
if(qFloatDistance(a, b)<(1<< 7))
[0]
Heap::ArrayObject * newArrayObject(int count=0)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent