Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/particles/qquickcustomaffector.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
24 | ** | - | ||||||||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
35 | ** | - | ||||||||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
37 | ** | - | ||||||||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include "qquickcustomaffector_p.h" | - | ||||||||||||||||||
41 | #include <private/qv8engine_p.h> | - | ||||||||||||||||||
42 | #include <private/qqmlengine_p.h> | - | ||||||||||||||||||
43 | #include <private/qqmlglobal_p.h> | - | ||||||||||||||||||
44 | #include <QQmlEngine> | - | ||||||||||||||||||
45 | #include <QDebug> | - | ||||||||||||||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | //TODO: Move docs (and inheritence) to real base when docs can propagate. Currently this pretends to be the base class! | - | ||||||||||||||||||
49 | /*! | - | ||||||||||||||||||
50 | \qmlsignal QtQuick.Particles::Affector::affectParticles(Array particles, real dt) | - | ||||||||||||||||||
51 | - | |||||||||||||||||||
52 | This signal is emitted when particles are selected to be affected. particles contains | - | ||||||||||||||||||
53 | an array of particle objects which can be directly manipulated. | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | dt is the time since the last time it was affected. Use dt to normalize | - | ||||||||||||||||||
56 | trajectory manipulations to real time. | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | Note that JavaScript is slower to execute, so it is not recommended to use this in | - | ||||||||||||||||||
59 | high-volume particle systems. | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | The corresponding handler is \c onAffectParticles. | - | ||||||||||||||||||
62 | */ | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | /*! | - | ||||||||||||||||||
65 | \qmlproperty StochasticDirection QtQuick.Particles::Affector::position | - | ||||||||||||||||||
66 | - | |||||||||||||||||||
67 | Affected particles will have their position set to this direction, | - | ||||||||||||||||||
68 | relative to the ParticleSystem. When interpreting directions as points, | - | ||||||||||||||||||
69 | imagine it as an arrow with the base at the 0,0 of the ParticleSystem and the | - | ||||||||||||||||||
70 | tip at where the specified position will be. | - | ||||||||||||||||||
71 | */ | - | ||||||||||||||||||
72 | - | |||||||||||||||||||
73 | /*! | - | ||||||||||||||||||
74 | \qmlproperty StochasticDirection QtQuick.Particles::Affector::velocity | - | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | Affected particles will have their velocity set to this direction. | - | ||||||||||||||||||
77 | */ | - | ||||||||||||||||||
78 | - | |||||||||||||||||||
79 | - | |||||||||||||||||||
80 | /*! | - | ||||||||||||||||||
81 | \qmlproperty StochasticDirection QtQuick.Particles::Affector::acceleration | - | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | Affected particles will have their acceleration set to this direction. | - | ||||||||||||||||||
84 | */ | - | ||||||||||||||||||
85 | - | |||||||||||||||||||
86 | - | |||||||||||||||||||
87 | /*! | - | ||||||||||||||||||
88 | \qmlproperty bool QtQuick.Particles::Affector::relative | - | ||||||||||||||||||
89 | - | |||||||||||||||||||
90 | Whether the affected particles have their existing position/velocity/acceleration added | - | ||||||||||||||||||
91 | to the new one. | - | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | Default is true. | - | ||||||||||||||||||
94 | */ | - | ||||||||||||||||||
95 | QQuickCustomAffector::QQuickCustomAffector(QQuickItem *parent) : | - | ||||||||||||||||||
96 | QQuickParticleAffector(parent) | - | ||||||||||||||||||
97 | , m_position(&m_nullVector) | - | ||||||||||||||||||
98 | , m_velocity(&m_nullVector) | - | ||||||||||||||||||
99 | , m_acceleration(&m_nullVector) | - | ||||||||||||||||||
100 | , m_relative(true) | - | ||||||||||||||||||
101 | { | - | ||||||||||||||||||
102 | } executed 24 times by 3 tests: end of block Executed by:
| 24 | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | bool QQuickCustomAffector::isAffectConnected() | - | ||||||||||||||||||
105 | { | - | ||||||||||||||||||
106 | IS_SIGNAL_CONNECTED(this, QQuickCustomAffector, affectParticles, (QQmlV4Handle,qreal)); executed 324 times by 1 test: return QObjectPrivate::get(sender)->isSignalConnected(signalIdx); Executed by:
| 324 | ||||||||||||||||||
107 | } never executed: end of block | 0 | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | void QQuickCustomAffector::affectSystem(qreal dt) | - | ||||||||||||||||||
110 | { | - | ||||||||||||||||||
111 | //Acts a bit differently, just emits affected for everyone it might affect, when the only thing is connecting to affected(x,y) | - | ||||||||||||||||||
112 | bool justAffected = (m_acceleration == &m_nullVector
| 81-243 | ||||||||||||||||||
113 | && m_velocity == &m_nullVector
| 0-243 | ||||||||||||||||||
114 | && m_position == &m_nullVector
| 82-161 | ||||||||||||||||||
115 | && isAffectedConnected());
| 79-82 | ||||||||||||||||||
116 | if (!isAffectConnected() && !justAffected) {
| 79-245 | ||||||||||||||||||
117 | QQuickParticleAffector::affectSystem(dt); | - | ||||||||||||||||||
118 | return; executed 163 times by 1 test: return; Executed by:
| 163 | ||||||||||||||||||
119 | } | - | ||||||||||||||||||
120 | if (!m_enabled)
| 0-161 | ||||||||||||||||||
121 | return; never executed: return; | 0 | ||||||||||||||||||
122 | updateOffsets(); | - | ||||||||||||||||||
123 | - | |||||||||||||||||||
124 | QList<QQuickParticleData*> toAffect; | - | ||||||||||||||||||
125 | foreach (QQuickParticleGroupData* gd, m_system->groupData) {
| 0-322 | ||||||||||||||||||
126 | if (activeGroup(gd->index)) {
| 0-161 | ||||||||||||||||||
127 | foreach (QQuickParticleData* d, gd->data) {
| 0-80661 | ||||||||||||||||||
128 | if (shouldAffect(d)) {
| 2512-77988 | ||||||||||||||||||
129 | toAffect << d; | - | ||||||||||||||||||
130 | } executed 2512 times by 1 test: end of block Executed by:
| 2512 | ||||||||||||||||||
131 | } executed 80500 times by 1 test: end of block Executed by:
| 80500 | ||||||||||||||||||
132 | } executed 161 times by 1 test: end of block Executed by:
| 161 | ||||||||||||||||||
133 | } executed 161 times by 1 test: end of block Executed by:
| 161 | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | if (toAffect.isEmpty())
| 4-157 | ||||||||||||||||||
136 | return; executed 4 times by 1 test: return; Executed by:
| 4 | ||||||||||||||||||
137 | - | |||||||||||||||||||
138 | if (justAffected) {
| 77-80 | ||||||||||||||||||
139 | foreach (QQuickParticleData* d, toAffect) {//Not postAffect to avoid saying the particle changed
| 0-1360 | ||||||||||||||||||
140 | if (m_onceOff)
| 0-1280 | ||||||||||||||||||
141 | m_onceOffed << qMakePair(d->groupId, d->index); executed 1280 times by 1 test: m_onceOffed << qMakePair(d->groupId, d->index); Executed by:
| 1280 | ||||||||||||||||||
142 | emit affected(d->curX(m_system), d->curY(m_system)); | - | ||||||||||||||||||
143 | } executed 1280 times by 1 test: end of block Executed by:
| 1280 | ||||||||||||||||||
144 | return; executed 80 times by 1 test: return; Executed by:
| 80 | ||||||||||||||||||
145 | } | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | if (m_onceOff)
| 0-77 | ||||||||||||||||||
148 | dt = 1.0; executed 77 times by 1 test: dt = 1.0; Executed by:
| 77 | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | QQmlEngine *qmlEngine = ::qmlEngine(this); | - | ||||||||||||||||||
151 | QV4::ExecutionEngine *v4 = qmlEngine->handle(); | - | ||||||||||||||||||
152 | - | |||||||||||||||||||
153 | QV4::Scope scope(v4); | - | ||||||||||||||||||
154 | QV4::ScopedArrayObject array(scope, v4->newArrayObject(toAffect.size())); | - | ||||||||||||||||||
155 | QV4::ScopedValue v(scope); | - | ||||||||||||||||||
156 | for (int i=0; i<toAffect.size(); i++)
| 77-1232 | ||||||||||||||||||
157 | array->put(i, (v = toAffect[i]->v4Value(m_system))); executed 1232 times by 1 test: array->put(i, (v = toAffect[i]->v4Value(m_system))); Executed by:
| 1232 | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | if (dt >= simulationCutoff || dt <= simulationDelta) {
| 0-77 | ||||||||||||||||||
160 | affectProperties(toAffect, dt); | - | ||||||||||||||||||
161 | emit affectParticles(QQmlV4Handle(array), dt); | - | ||||||||||||||||||
162 | } else { executed 77 times by 1 test: end of block Executed by:
| 77 | ||||||||||||||||||
163 | int realTime = m_system->timeInt; | - | ||||||||||||||||||
164 | m_system->timeInt -= dt * 1000.0; | - | ||||||||||||||||||
165 | while (dt > simulationDelta) {
| 0 | ||||||||||||||||||
166 | m_system->timeInt += simulationDelta * 1000.0; | - | ||||||||||||||||||
167 | dt -= simulationDelta; | - | ||||||||||||||||||
168 | affectProperties(toAffect, simulationDelta); | - | ||||||||||||||||||
169 | emit affectParticles(QQmlV4Handle(array), simulationDelta); | - | ||||||||||||||||||
170 | } never executed: end of block | 0 | ||||||||||||||||||
171 | m_system->timeInt = realTime; | - | ||||||||||||||||||
172 | if (dt > 0.0) {
| 0 | ||||||||||||||||||
173 | affectProperties(toAffect, dt); | - | ||||||||||||||||||
174 | emit affectParticles(QQmlV4Handle(array), dt); | - | ||||||||||||||||||
175 | } never executed: end of block | 0 | ||||||||||||||||||
176 | } never executed: end of block | 0 | ||||||||||||||||||
177 | - | |||||||||||||||||||
178 | foreach (QQuickParticleData* d, toAffect)
| 0-1309 | ||||||||||||||||||
179 | if (d->update == 1.0)
| 0-1232 | ||||||||||||||||||
180 | postAffect(d); executed 1232 times by 1 test: postAffect(d); Executed by:
| 1232 | ||||||||||||||||||
181 | } executed 77 times by 1 test: end of block Executed by:
| 77 | ||||||||||||||||||
182 | - | |||||||||||||||||||
183 | bool QQuickCustomAffector::affectParticle(QQuickParticleData *d, qreal dt) | - | ||||||||||||||||||
184 | { | - | ||||||||||||||||||
185 | //This does the property based affecting, called by superclass if signal isn't hooked up. | - | ||||||||||||||||||
186 | bool changed = false; | - | ||||||||||||||||||
187 | QPointF curPos(d->curX(m_system), d->curY(m_system)); | - | ||||||||||||||||||
188 | - | |||||||||||||||||||
189 | if (m_acceleration != &m_nullVector){
| 24342-26072 | ||||||||||||||||||
190 | QPointF pos = m_acceleration->sample(curPos); | - | ||||||||||||||||||
191 | QPointF curAcc = QPointF(d->curAX(), d->curAY()); | - | ||||||||||||||||||
192 | if (m_relative) {
| 0-24342 | ||||||||||||||||||
193 | pos *= dt; | - | ||||||||||||||||||
194 | pos += curAcc; | - | ||||||||||||||||||
195 | } never executed: end of block | 0 | ||||||||||||||||||
196 | if (pos != curAcc) {
| 1264-23078 | ||||||||||||||||||
197 | d->setInstantaneousAX(pos.x(), m_system); | - | ||||||||||||||||||
198 | d->setInstantaneousAY(pos.y(), m_system); | - | ||||||||||||||||||
199 | changed = true; | - | ||||||||||||||||||
200 | } executed 1264 times by 1 test: end of block Executed by:
| 1264 | ||||||||||||||||||
201 | } executed 24342 times by 1 test: end of block Executed by:
| 24342 | ||||||||||||||||||
202 | - | |||||||||||||||||||
203 | if (m_velocity != &m_nullVector){
| 24342-26072 | ||||||||||||||||||
204 | QPointF pos = m_velocity->sample(curPos); | - | ||||||||||||||||||
205 | QPointF curVel = QPointF(d->curVX(m_system), d->curVY(m_system)); | - | ||||||||||||||||||
206 | if (m_relative) {
| 0-24342 | ||||||||||||||||||
207 | pos *= dt; | - | ||||||||||||||||||
208 | pos += curVel; | - | ||||||||||||||||||
209 | } never executed: end of block | 0 | ||||||||||||||||||
210 | if (pos != curVel) {
| 0-24342 | ||||||||||||||||||
211 | d->setInstantaneousVX(pos.x(), m_system); | - | ||||||||||||||||||
212 | d->setInstantaneousVY(pos.y(), m_system); | - | ||||||||||||||||||
213 | changed = true; | - | ||||||||||||||||||
214 | } executed 24342 times by 1 test: end of block Executed by:
| 24342 | ||||||||||||||||||
215 | } executed 24342 times by 1 test: end of block Executed by:
| 24342 | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | if (m_position != &m_nullVector){
| 1232-49182 | ||||||||||||||||||
218 | QPointF pos = m_position->sample(curPos); | - | ||||||||||||||||||
219 | if (m_relative) {
| 0-49182 | ||||||||||||||||||
220 | pos *= dt; | - | ||||||||||||||||||
221 | pos += curPos; | - | ||||||||||||||||||
222 | } never executed: end of block | 0 | ||||||||||||||||||
223 | if (pos != curPos) {
| 24342-24840 | ||||||||||||||||||
224 | d->setInstantaneousX(pos.x(), m_system); | - | ||||||||||||||||||
225 | d->setInstantaneousY(pos.y(), m_system); | - | ||||||||||||||||||
226 | changed = true; | - | ||||||||||||||||||
227 | } executed 24342 times by 1 test: end of block Executed by:
| 24342 | ||||||||||||||||||
228 | } executed 49182 times by 1 test: end of block Executed by:
| 49182 | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | return changed; executed 50414 times by 1 test: return changed; Executed by:
| 50414 | ||||||||||||||||||
231 | } | - | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | void QQuickCustomAffector::affectProperties(const QList<QQuickParticleData*> particles, qreal dt) | - | ||||||||||||||||||
234 | { | - | ||||||||||||||||||
235 | foreach (QQuickParticleData* d, particles)
| 0-1309 | ||||||||||||||||||
236 | if ( affectParticle(d, dt) )
| 0-1232 | ||||||||||||||||||
237 | d->update = 1.0; never executed: d->update = 1.0; | 0 | ||||||||||||||||||
238 | } executed 77 times by 1 test: end of block Executed by:
| 77 | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | QT_END_NAMESPACE | - | ||||||||||||||||||
241 | - | |||||||||||||||||||
242 | #include "moc_qquickcustomaffector_p.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |