| 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 | #ifndef PARTICLEEMITTER_H |
| 41 | #define PARTICLEEMITTER_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtQuick/QQuickItem> |
| 55 | #include <QDebug> |
| 56 | #include "qquickparticlesystem_p.h" |
| 57 | #include "qquickparticleextruder_p.h" |
| 58 | #include "qquickdirection_p.h" |
| 59 | |
| 60 | #include <QList> |
| 61 | #include <QPair> |
| 62 | #include <QPointF> |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | class Q_QUICKPARTICLES_PRIVATE_EXPORT QQuickParticleEmitter : public QQuickItem |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | Q_PROPERTY(QQuickParticleSystem* system READ system WRITE setSystem NOTIFY systemChanged) |
| 69 | Q_PROPERTY(QString group READ group WRITE setGroup NOTIFY groupChanged) |
| 70 | Q_PROPERTY(QQuickParticleExtruder* shape READ extruder WRITE setExtruder NOTIFY extruderChanged) |
| 71 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) |
| 72 | Q_PROPERTY(int startTime READ startTime WRITE setStartTime NOTIFY startTimeChanged) |
| 73 | |
| 74 | Q_PROPERTY(qreal emitRate READ particlesPerSecond WRITE setParticlesPerSecond NOTIFY particlesPerSecondChanged) |
| 75 | Q_PROPERTY(int lifeSpan READ particleDuration WRITE setParticleDuration NOTIFY particleDurationChanged) |
| 76 | Q_PROPERTY(int lifeSpanVariation READ particleDurationVariation WRITE setParticleDurationVariation NOTIFY particleDurationVariationChanged) |
| 77 | Q_PROPERTY(int maximumEmitted READ maxParticleCount WRITE setMaxParticleCount NOTIFY maximumEmittedChanged) |
| 78 | |
| 79 | Q_PROPERTY(qreal size READ particleSize WRITE setParticleSize NOTIFY particleSizeChanged) |
| 80 | Q_PROPERTY(qreal endSize READ particleEndSize WRITE setParticleEndSize NOTIFY particleEndSizeChanged) |
| 81 | Q_PROPERTY(qreal sizeVariation READ particleSizeVariation WRITE setParticleSizeVariation NOTIFY particleSizeVariationChanged) |
| 82 | |
| 83 | Q_PROPERTY(QQuickDirection *velocity READ velocity WRITE setVelocity NOTIFY velocityChanged) |
| 84 | Q_PROPERTY(QQuickDirection *acceleration READ acceleration WRITE setAcceleration NOTIFY accelerationChanged) |
| 85 | Q_PROPERTY(qreal velocityFromMovement READ velocityFromMovement WRITE setVelocityFromMovement NOTIFY velocityFromMovementChanged) |
| 86 | QML_NAMED_ELEMENT(Emitter) |
| 87 | |
| 88 | public: |
| 89 | explicit QQuickParticleEmitter(QQuickItem *parent = 0); |
| 90 | virtual ~QQuickParticleEmitter(); |
| 91 | virtual void emitWindow(int timeStamp); |
| 92 | |
| 93 | enum Lifetime { |
| 94 | InfiniteLife = QQuickParticleSystem::maxLife |
| 95 | }; |
| 96 | Q_ENUM(Lifetime) |
| 97 | |
| 98 | bool enabled() const |
| 99 | { |
| 100 | return m_enabled; |
| 101 | } |
| 102 | |
| 103 | qreal particlesPerSecond() const |
| 104 | { |
| 105 | return m_particlesPerSecond; |
| 106 | } |
| 107 | |
| 108 | int particleDuration() const |
| 109 | { |
| 110 | return m_particleDuration; |
| 111 | } |
| 112 | |
| 113 | QQuickParticleSystem* system() const |
| 114 | { |
| 115 | return m_system; |
| 116 | } |
| 117 | |
| 118 | QString group() const |
| 119 | { |
| 120 | return m_group; |
| 121 | } |
| 122 | |
| 123 | QQuickParticleGroupData::ID groupId() const |
| 124 | { |
| 125 | if (m_groupIdNeedRecalculation) |
| 126 | reclaculateGroupId(); |
| 127 | return m_groupId; |
| 128 | } |
| 129 | |
| 130 | int particleDurationVariation() const |
| 131 | { |
| 132 | return m_particleDurationVariation; |
| 133 | } |
| 134 | |
| 135 | qreal velocityFromMovement() const { return m_velocity_from_movement; } |
| 136 | void setVelocityFromMovement(qreal s); |
| 137 | void componentComplete() override; |
| 138 | Q_SIGNALS: |
| 139 | void emitParticles(const QJSValue &particles); |
| 140 | void particlesPerSecondChanged(qreal); |
| 141 | void particleDurationChanged(int); |
| 142 | void enabledChanged(bool); |
| 143 | |
| 144 | void systemChanged(QQuickParticleSystem* arg); |
| 145 | |
| 146 | void groupChanged(const QString &arg); |
| 147 | |
| 148 | void particleDurationVariationChanged(int arg); |
| 149 | |
| 150 | void extruderChanged(QQuickParticleExtruder* arg); |
| 151 | |
| 152 | void particleSizeChanged(qreal arg); |
| 153 | |
| 154 | void particleEndSizeChanged(qreal arg); |
| 155 | |
| 156 | void particleSizeVariationChanged(qreal arg); |
| 157 | |
| 158 | void velocityChanged(QQuickDirection * arg); |
| 159 | |
| 160 | void accelerationChanged(QQuickDirection * arg); |
| 161 | |
| 162 | void maximumEmittedChanged(int arg); |
| 163 | void particleCountChanged(); |
| 164 | |
| 165 | void velocityFromMovementChanged(); |
| 166 | |
| 167 | void startTimeChanged(int arg); |
| 168 | |
| 169 | public Q_SLOTS: |
| 170 | void pulse(int milliseconds); |
| 171 | void burst(int num); |
| 172 | void burst(int num, qreal x, qreal y); |
| 173 | |
| 174 | void setEnabled(bool arg); |
| 175 | |
| 176 | void setParticlesPerSecond(qreal arg) |
| 177 | { |
| 178 | if (m_particlesPerSecond != arg) { |
| 179 | m_particlesPerSecond = arg; |
| 180 | Q_EMIT particlesPerSecondChanged(arg); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void setParticleDuration(int arg) |
| 185 | { |
| 186 | if (m_particleDuration != arg) { |
| 187 | m_particleDuration = arg; |
| 188 | Q_EMIT particleDurationChanged(arg); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void setSystem(QQuickParticleSystem* arg) |
| 193 | { |
| 194 | if (m_system != arg) { |
| 195 | m_system = arg; |
| 196 | m_groupIdNeedRecalculation = true; |
| 197 | if (m_system) |
| 198 | m_system->registerParticleEmitter(e: this); |
| 199 | Q_EMIT systemChanged(arg); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void setGroup(const QString &arg) |
| 204 | { |
| 205 | if (m_group != arg) { |
| 206 | m_group = arg; |
| 207 | m_groupIdNeedRecalculation = true; |
| 208 | Q_EMIT groupChanged(arg); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void setParticleDurationVariation(int arg) |
| 213 | { |
| 214 | if (m_particleDurationVariation != arg) { |
| 215 | m_particleDurationVariation = arg; |
| 216 | Q_EMIT particleDurationVariationChanged(arg); |
| 217 | } |
| 218 | } |
| 219 | void setExtruder(QQuickParticleExtruder* arg) |
| 220 | { |
| 221 | if (m_extruder != arg) { |
| 222 | m_extruder = arg; |
| 223 | Q_EMIT extruderChanged(arg); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void setParticleSize(qreal arg) |
| 228 | { |
| 229 | if (m_particleSize != arg) { |
| 230 | m_particleSize = arg; |
| 231 | Q_EMIT particleSizeChanged(arg); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void setParticleEndSize(qreal arg) |
| 236 | { |
| 237 | if (m_particleEndSize != arg) { |
| 238 | m_particleEndSize = arg; |
| 239 | Q_EMIT particleEndSizeChanged(arg); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void setParticleSizeVariation(qreal arg) |
| 244 | { |
| 245 | if (m_particleSizeVariation != arg) { |
| 246 | m_particleSizeVariation = arg; |
| 247 | Q_EMIT particleSizeVariationChanged(arg); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void setVelocity(QQuickDirection * arg) |
| 252 | { |
| 253 | if (m_velocity != arg) { |
| 254 | m_velocity = arg; |
| 255 | Q_EMIT velocityChanged(arg); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void setAcceleration(QQuickDirection * arg) |
| 260 | { |
| 261 | if (m_acceleration != arg) { |
| 262 | m_acceleration = arg; |
| 263 | Q_EMIT accelerationChanged(arg); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void setMaxParticleCount(int arg); |
| 268 | |
| 269 | void setStartTime(int arg) |
| 270 | { |
| 271 | if (m_startTime != arg) { |
| 272 | m_startTime = arg; |
| 273 | Q_EMIT startTimeChanged(arg); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | virtual void reset(); |
| 278 | public: |
| 279 | int particleCount() const |
| 280 | { |
| 281 | if (m_maxParticleCount >= 0) |
| 282 | return m_maxParticleCount; |
| 283 | return m_particlesPerSecond*((m_particleDuration+m_particleDurationVariation)/1000.0); |
| 284 | } |
| 285 | |
| 286 | QQuickParticleExtruder* extruder() const |
| 287 | { |
| 288 | return m_extruder; |
| 289 | } |
| 290 | |
| 291 | qreal particleSize() const |
| 292 | { |
| 293 | return m_particleSize; |
| 294 | } |
| 295 | |
| 296 | qreal particleEndSize() const |
| 297 | { |
| 298 | return m_particleEndSize; |
| 299 | } |
| 300 | |
| 301 | qreal particleSizeVariation() const |
| 302 | { |
| 303 | return m_particleSizeVariation; |
| 304 | } |
| 305 | |
| 306 | QQuickDirection * velocity() const |
| 307 | { |
| 308 | return m_velocity; |
| 309 | } |
| 310 | |
| 311 | QQuickDirection * acceleration() const |
| 312 | { |
| 313 | return m_acceleration; |
| 314 | } |
| 315 | |
| 316 | int maxParticleCount() const |
| 317 | { |
| 318 | return m_maxParticleCount; |
| 319 | } |
| 320 | |
| 321 | int startTime() const |
| 322 | { |
| 323 | return m_startTime; |
| 324 | } |
| 325 | |
| 326 | void reclaculateGroupId() const; |
| 327 | |
| 328 | protected: |
| 329 | qreal m_particlesPerSecond; |
| 330 | int m_particleDuration; |
| 331 | int m_particleDurationVariation; |
| 332 | bool m_enabled; |
| 333 | QQuickParticleSystem* m_system; |
| 334 | QQuickParticleExtruder* m_extruder; |
| 335 | QQuickParticleExtruder* m_defaultExtruder; |
| 336 | QQuickParticleExtruder* effectiveExtruder(); |
| 337 | QQuickDirection * m_velocity; |
| 338 | QQuickDirection * m_acceleration; |
| 339 | qreal m_particleSize; |
| 340 | qreal m_particleEndSize; |
| 341 | qreal m_particleSizeVariation; |
| 342 | |
| 343 | qreal m_velocityFromMovement; |
| 344 | int m_startTime; |
| 345 | bool m_overwrite; |
| 346 | |
| 347 | int m_pulseLeft; |
| 348 | QList<QPair<int, QPointF > > m_burstQueue; |
| 349 | int m_maxParticleCount; |
| 350 | |
| 351 | //Used in default implementation, but might be useful |
| 352 | qreal m_velocity_from_movement; |
| 353 | |
| 354 | int m_emitCap; |
| 355 | bool m_reset_last; |
| 356 | qreal m_last_timestamp; |
| 357 | qreal m_last_emission; |
| 358 | |
| 359 | QPointF m_last_emitter; |
| 360 | QPointF m_last_last_emitter; |
| 361 | QPointF m_last_last_last_emitter; |
| 362 | |
| 363 | bool isEmitConnected(); |
| 364 | |
| 365 | private: // data |
| 366 | QString m_group; |
| 367 | mutable bool m_groupIdNeedRecalculation; |
| 368 | mutable QQuickParticleGroupData::ID m_groupId; |
| 369 | QQuickDirection m_nullVector; |
| 370 | |
| 371 | }; |
| 372 | |
| 373 | QT_END_NAMESPACE |
| 374 | |
| 375 | #endif // PARTICLEEMITTER_H |
| 376 |
