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