Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/particles/qquickparticleaffector.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 "qquickparticleaffector_p.h" | - | ||||||||||||||||||
41 | #include <QDebug> | - | ||||||||||||||||||
42 | #include <private/qqmlglobal_p.h> | - | ||||||||||||||||||
43 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
44 | - | |||||||||||||||||||
45 | /*! | - | ||||||||||||||||||
46 | \qmltype Affector | - | ||||||||||||||||||
47 | \instantiates QQuickParticleAffector | - | ||||||||||||||||||
48 | \inqmlmodule QtQuick.Particles | - | ||||||||||||||||||
49 | \brief Applies alterations to the attributes of logical particles at any | - | ||||||||||||||||||
50 | point in their lifetime. | - | ||||||||||||||||||
51 | \ingroup qtquick-particles | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | The base Affector does not alter any attributes, but can be used to emit a signal | - | ||||||||||||||||||
54 | when a particle meets certain conditions. | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | If an affector has a defined size, then it will only affect particles within its size and position on screen. | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | Affectors have different performance characteristics to the other particle system elements. In particular, | - | ||||||||||||||||||
59 | they have some simplifications to try to maintain a simulation at real-time or faster. When running a system | - | ||||||||||||||||||
60 | with Affectors, irregular frame timings that grow too large ( > one second per frame) will cause the Affectors | - | ||||||||||||||||||
61 | to try and cut corners with a faster but less accurate simulation. If the system has multiple affectors the order | - | ||||||||||||||||||
62 | in which they are applied is not guaranteed, and when simulating larger time shifts they will simulate the whole | - | ||||||||||||||||||
63 | shift each, which can lead to different results compared to smaller time shifts. | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | Accurate simulation for large numbers of particles (hundreds) with multiple affectors may be possible on some hardware, | - | ||||||||||||||||||
66 | but on less capable hardware you should expect small irregularties in the simulation as simulates with worse granularity. | - | ||||||||||||||||||
67 | */ | - | ||||||||||||||||||
68 | /*! | - | ||||||||||||||||||
69 | \qmlproperty ParticleSystem QtQuick.Particles::Affector::system | - | ||||||||||||||||||
70 | This is the system which will be affected by the element. | - | ||||||||||||||||||
71 | If the Affector is a direct child of a ParticleSystem, it will automatically be associated with it. | - | ||||||||||||||||||
72 | */ | - | ||||||||||||||||||
73 | /*! | - | ||||||||||||||||||
74 | \qmlproperty list<string> QtQuick.Particles::Affector::groups | - | ||||||||||||||||||
75 | Which logical particle groups will be affected. | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | If empty, it will affect all particles. | - | ||||||||||||||||||
78 | */ | - | ||||||||||||||||||
79 | /*! | - | ||||||||||||||||||
80 | \qmlproperty list<string> QtQuick.Particles::Affector::whenCollidingWith | - | ||||||||||||||||||
81 | If any logical particle groups are specified here, then the affector | - | ||||||||||||||||||
82 | will only be triggered if the particle being examined intersects with | - | ||||||||||||||||||
83 | a particle of one of these groups. | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | This is different from the groups property. The groups property selects which | - | ||||||||||||||||||
86 | particles might be examined, and if they meet other criteria (including being | - | ||||||||||||||||||
87 | within the bounds of the Affector, modified by shape) then they will be tested | - | ||||||||||||||||||
88 | again to see if they intersect with a particles from one of the particle groups | - | ||||||||||||||||||
89 | in whenCollidingWith. | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | By default, no groups are specified. | - | ||||||||||||||||||
92 | */ | - | ||||||||||||||||||
93 | /*! | - | ||||||||||||||||||
94 | \qmlproperty bool QtQuick.Particles::Affector::enabled | - | ||||||||||||||||||
95 | If enabled is set to false, this affector will not affect any particles. | - | ||||||||||||||||||
96 | - | |||||||||||||||||||
97 | Usually this is used to conditionally turn an affector on or off. | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | Default value is true. | - | ||||||||||||||||||
100 | */ | - | ||||||||||||||||||
101 | /*! | - | ||||||||||||||||||
102 | \qmlproperty bool QtQuick.Particles::Affector::once | - | ||||||||||||||||||
103 | If once is set to true, this affector will only affect each particle | - | ||||||||||||||||||
104 | once in their lifetimes. If the affector normally simulates a continuous | - | ||||||||||||||||||
105 | effect over time, then it will simulate the effect of one second of time | - | ||||||||||||||||||
106 | the one instant it affects the particle. | - | ||||||||||||||||||
107 | - | |||||||||||||||||||
108 | Default value is false. | - | ||||||||||||||||||
109 | */ | - | ||||||||||||||||||
110 | /*! | - | ||||||||||||||||||
111 | \qmlproperty Shape QtQuick.Particles::Affector::shape | - | ||||||||||||||||||
112 | If a size has been defined, the shape property can be used to affect a | - | ||||||||||||||||||
113 | non-rectangular area. | - | ||||||||||||||||||
114 | */ | - | ||||||||||||||||||
115 | /*! | - | ||||||||||||||||||
116 | \qmlsignal QtQuick.Particles::Affector::affected(real x, real y) | - | ||||||||||||||||||
117 | - | |||||||||||||||||||
118 | This signal is emitted when a particle is selected to be affected. It will not be emitted | - | ||||||||||||||||||
119 | if a particle is considered by the Affector but not actually altered in any way. | - | ||||||||||||||||||
120 | - | |||||||||||||||||||
121 | In the special case where an Affector has no possible effect (e.g. Affector {}), this signal | - | ||||||||||||||||||
122 | will be emitted for all particles being considered if you connect to it. This allows you to | - | ||||||||||||||||||
123 | execute arbitrary code in response to particles (use the Affector::onAffectParticles | - | ||||||||||||||||||
124 | signal handler if you want to execute code which affects the particles | - | ||||||||||||||||||
125 | themselves). As this executes JavaScript code per particle, it is not recommended to use this | - | ||||||||||||||||||
126 | signal with a high-volume particle system. | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | x,y is the particle's current position. | - | ||||||||||||||||||
129 | - | |||||||||||||||||||
130 | The corresponding handler is \c onAffected. | - | ||||||||||||||||||
131 | */ | - | ||||||||||||||||||
132 | QQuickParticleAffector::QQuickParticleAffector(QQuickItem *parent) : | - | ||||||||||||||||||
133 | QQuickItem(parent), m_needsReset(false), m_ignoresTime(false), m_onceOff(false), m_enabled(true) | - | ||||||||||||||||||
134 | , m_system(nullptr), m_updateIntSet(false), m_shape(new QQuickParticleExtruder(this)) | - | ||||||||||||||||||
135 | { | - | ||||||||||||||||||
136 | } executed 94 times by 11 tests: end of block Executed by:
| 94 | ||||||||||||||||||
137 | - | |||||||||||||||||||
138 | bool QQuickParticleAffector::isAffectedConnected() | - | ||||||||||||||||||
139 | { | - | ||||||||||||||||||
140 | IS_SIGNAL_CONNECTED(this, QQuickParticleAffector, affected, (qreal,qreal)); executed 171823 times by 9 tests: return QObjectPrivate::get(sender)->isSignalConnected(signalIdx); Executed by:
| 171823 | ||||||||||||||||||
141 | } never executed: end of block | 0 | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | - | |||||||||||||||||||
144 | void QQuickParticleAffector::componentComplete() | - | ||||||||||||||||||
145 | { | - | ||||||||||||||||||
146 | if (!m_system && qobject_cast<QQuickParticleSystem*>(parentItem()))
| 0-62 | ||||||||||||||||||
147 | setSystem(qobject_cast<QQuickParticleSystem*>(parentItem())); executed 62 times by 11 tests: setSystem(qobject_cast<QQuickParticleSystem*>(parentItem())); Executed by:
| 62 | ||||||||||||||||||
148 | QQuickItem::componentComplete(); | - | ||||||||||||||||||
149 | } executed 94 times by 11 tests: end of block Executed by:
| 94 | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | bool QQuickParticleAffector::activeGroup(int g) { | - | ||||||||||||||||||
152 | if (m_updateIntSet){ //This can occur before group ids are properly assigned, but that resets the flag
| 34-726824 | ||||||||||||||||||
153 | m_groupIds.clear(); | - | ||||||||||||||||||
154 | foreach (const QString &p, m_groups)
| 0-38 | ||||||||||||||||||
155 | m_groupIds << m_system->groupIds[p]; executed 4 times by 2 tests: m_groupIds << m_system->groupIds[p]; Executed by:
| 4 | ||||||||||||||||||
156 | m_updateIntSet = false; | - | ||||||||||||||||||
157 | } executed 34 times by 9 tests: end of block Executed by:
| 34 | ||||||||||||||||||
158 | return m_groupIds.isEmpty() || m_groupIds.contains(g); executed 726858 times by 9 tests: return m_groupIds.isEmpty() || m_groupIds.contains(g); Executed by:
| 726858 | ||||||||||||||||||
159 | } | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | bool QQuickParticleAffector::shouldAffect(QQuickParticleData* d) | - | ||||||||||||||||||
162 | { | - | ||||||||||||||||||
163 | if (!d)
| 0-718940 | ||||||||||||||||||
164 | return false; never executed: return false; | 0 | ||||||||||||||||||
165 | if (activeGroup(d->groupId)){
| 0-718940 | ||||||||||||||||||
166 | if ((m_onceOff && m_onceOffed.contains(qMakePair(d->groupId, d->index)))
| 91881-516440 | ||||||||||||||||||
167 | || !d->stillAlive(m_system))
| 290102-336957 | ||||||||||||||||||
168 | return false; executed 428838 times by 9 tests: return false; Executed by:
| 428838 | ||||||||||||||||||
169 | //Need to have previous location for affected anyways | - | ||||||||||||||||||
170 | if (width() == 0 || height() == 0
| 0-266258 | ||||||||||||||||||
171 | || m_shape->contains(QRectF(m_offset.x(), m_offset.y(), width(), height()), QPointF(d->curX(m_system), d->curY(m_system)))){
| 0-23844 | ||||||||||||||||||
172 | if (m_whenCollidingWith.isEmpty() || isColliding(d)){
| 0-290102 | ||||||||||||||||||
173 | return true; executed 290102 times by 9 tests: return true; Executed by:
| 290102 | ||||||||||||||||||
174 | } | - | ||||||||||||||||||
175 | } never executed: end of block | 0 | ||||||||||||||||||
176 | } never executed: end of block | 0 | ||||||||||||||||||
177 | return false; never executed: return false; | 0 | ||||||||||||||||||
178 | - | |||||||||||||||||||
179 | } | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | void QQuickParticleAffector::postAffect(QQuickParticleData* d) | - | ||||||||||||||||||
182 | { | - | ||||||||||||||||||
183 | m_system->needsReset << d; | - | ||||||||||||||||||
184 | if (m_onceOff)
| 3760-167902 | ||||||||||||||||||
185 | m_onceOffed << qMakePair(d->groupId, d->index); executed 3760 times by 2 tests: m_onceOffed << qMakePair(d->groupId, d->index); Executed by:
| 3760 | ||||||||||||||||||
186 | if (isAffectedConnected())
| 0-171662 | ||||||||||||||||||
187 | emit affected(d->curX(m_system), d->curY(m_system)); never executed: affected(d->curX(m_system), d->curY(m_system)); | 0 | ||||||||||||||||||
188 | } executed 171662 times by 9 tests: end of block Executed by:
| 171662 | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | const qreal QQuickParticleAffector::simulationDelta = 0.020; | - | ||||||||||||||||||
191 | const qreal QQuickParticleAffector::simulationCutoff = 1.000;//If this goes above 1.0, then m_once behaviour needs special codepath | - | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | void QQuickParticleAffector::affectSystem(qreal dt) | - | ||||||||||||||||||
194 | { | - | ||||||||||||||||||
195 | if (!m_enabled)
| 0-1116 | ||||||||||||||||||
196 | return; never executed: return; | 0 | ||||||||||||||||||
197 | //If not reimplemented, calls affectParticle per particle | - | ||||||||||||||||||
198 | //But only on particles in targeted system/area | - | ||||||||||||||||||
199 | updateOffsets();//### Needed if an ancestor is transformed. | - | ||||||||||||||||||
200 | if (m_onceOff)
| 244-872 | ||||||||||||||||||
201 | dt = 1.0; executed 244 times by 2 tests: dt = 1.0; Executed by:
| 244 | ||||||||||||||||||
202 | foreach (QQuickParticleGroupData* gd, m_system->groupData) {
| 0-2473 | ||||||||||||||||||
203 | if (activeGroup(gd->index)) {
| 163-1194 | ||||||||||||||||||
204 | foreach (QQuickParticleData* d, gd->data) {
| 0-599634 | ||||||||||||||||||
205 | if (shouldAffect(d)) {
| 263746-334694 | ||||||||||||||||||
206 | bool affected = false; | - | ||||||||||||||||||
207 | qreal myDt = dt; | - | ||||||||||||||||||
208 | if (!m_ignoresTime && myDt < simulationCutoff) {
| 23676-240070 | ||||||||||||||||||
209 | int realTime = m_system->timeInt; | - | ||||||||||||||||||
210 | m_system->timeInt -= myDt * 1000.0; | - | ||||||||||||||||||
211 | while (myDt > simulationDelta) {
| 0-212702 | ||||||||||||||||||
212 | m_system->timeInt += simulationDelta * 1000.0; | - | ||||||||||||||||||
213 | if (d->alive(m_system))//Only affect during the parts it was alive for
| 0 | ||||||||||||||||||
214 | affected = affectParticle(d, simulationDelta) || affected; never executed: affected = affectParticle(d, simulationDelta) || affected;
| 0 | ||||||||||||||||||
215 | myDt -= simulationDelta; | - | ||||||||||||||||||
216 | } never executed: end of block | 0 | ||||||||||||||||||
217 | m_system->timeInt = realTime; | - | ||||||||||||||||||
218 | } executed 212702 times by 6 tests: end of block Executed by:
| 212702 | ||||||||||||||||||
219 | if (myDt > 0.0)
| 0-263746 | ||||||||||||||||||
220 | affected = affectParticle(d, myDt) || affected; executed 263746 times by 8 tests: affected = affectParticle(d, myDt) || affected; Executed by:
| 0-263746 | ||||||||||||||||||
221 | if (affected)
| 114024-149722 | ||||||||||||||||||
222 | postAffect(d); executed 149722 times by 8 tests: postAffect(d); Executed by:
| 149722 | ||||||||||||||||||
223 | } executed 263746 times by 8 tests: end of block Executed by:
| 263746 | ||||||||||||||||||
224 | } executed 598440 times by 8 tests: end of block Executed by:
| 598440 | ||||||||||||||||||
225 | } executed 1194 times by 8 tests: end of block Executed by:
| 1194 | ||||||||||||||||||
226 | } executed 1357 times by 8 tests: end of block Executed by:
| 1357 | ||||||||||||||||||
227 | } executed 1116 times by 8 tests: end of block Executed by:
| 1116 | ||||||||||||||||||
228 | - | |||||||||||||||||||
229 | bool QQuickParticleAffector::affectParticle(QQuickParticleData *, qreal ) | - | ||||||||||||||||||
230 | { | - | ||||||||||||||||||
231 | return true; never executed: return true; | 0 | ||||||||||||||||||
232 | } | - | ||||||||||||||||||
233 | - | |||||||||||||||||||
234 | void QQuickParticleAffector::reset(QQuickParticleData* pd) | - | ||||||||||||||||||
235 | {//TODO: This, among other ones, should be restructured so they don't all need to remember to call the superclass | - | ||||||||||||||||||
236 | if (m_onceOff)
| 1200-6320 | ||||||||||||||||||
237 | if (activeGroup(pd->groupId))
| 0-6320 | ||||||||||||||||||
238 | m_onceOffed.remove(qMakePair(pd->groupId, pd->index)); executed 6320 times by 2 tests: m_onceOffed.remove(qMakePair(pd->groupId, pd->index)); Executed by:
| 6320 | ||||||||||||||||||
239 | } executed 7520 times by 3 tests: end of block Executed by:
| 7520 | ||||||||||||||||||
240 | - | |||||||||||||||||||
241 | void QQuickParticleAffector::updateOffsets() | - | ||||||||||||||||||
242 | { | - | ||||||||||||||||||
243 | if (m_system)
| 0-1357 | ||||||||||||||||||
244 | m_offset = m_system->mapFromItem(this, QPointF(0, 0)); executed 1357 times by 9 tests: m_offset = m_system->mapFromItem(this, QPointF(0, 0)); Executed by:
| 1357 | ||||||||||||||||||
245 | } executed 1357 times by 9 tests: end of block Executed by:
| 1357 | ||||||||||||||||||
246 | - | |||||||||||||||||||
247 | bool QQuickParticleAffector::isColliding(QQuickParticleData *d) const | - | ||||||||||||||||||
248 | { | - | ||||||||||||||||||
249 | qreal myCurX = d->curX(m_system); | - | ||||||||||||||||||
250 | qreal myCurY = d->curY(m_system); | - | ||||||||||||||||||
251 | qreal myCurSize = d->curSize(m_system) / 2; | - | ||||||||||||||||||
252 | foreach (const QString &group, m_whenCollidingWith){
| 0 | ||||||||||||||||||
253 | foreach (QQuickParticleData* other, m_system->groupData[m_system->groupIds[group]]->data){
| 0 | ||||||||||||||||||
254 | if (!other->stillAlive(m_system))
| 0 | ||||||||||||||||||
255 | continue; never executed: continue; | 0 | ||||||||||||||||||
256 | qreal otherCurX = other->curX(m_system); | - | ||||||||||||||||||
257 | qreal otherCurY = other->curY(m_system); | - | ||||||||||||||||||
258 | qreal otherCurSize = other->curSize(m_system) / 2; | - | ||||||||||||||||||
259 | if ((myCurX + myCurSize > otherCurX - otherCurSize
| 0 | ||||||||||||||||||
260 | && myCurX - myCurSize < otherCurX + otherCurSize)
| 0 | ||||||||||||||||||
261 | && (myCurY + myCurSize > otherCurY - otherCurSize
| 0 | ||||||||||||||||||
262 | && myCurY - myCurSize < otherCurY + otherCurSize))
| 0 | ||||||||||||||||||
263 | return true; never executed: return true; | 0 | ||||||||||||||||||
264 | } never executed: end of block | 0 | ||||||||||||||||||
265 | } never executed: end of block | 0 | ||||||||||||||||||
266 | return false; never executed: return false; | 0 | ||||||||||||||||||
267 | } | - | ||||||||||||||||||
268 | - | |||||||||||||||||||
269 | QT_END_NAMESPACE | - | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | #include "moc_qquickparticleaffector_p.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |