| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquick3dparticleemitburst_p.h" |
| 5 | #include "qquick3dparticleemitter_p.h" |
| 6 | #include <qdebug.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | /*! |
| 11 | \qmltype EmitBurst3D |
| 12 | \inherits QtObject |
| 13 | \inqmlmodule QtQuick3D.Particles3D |
| 14 | \brief Declarative emitter bursts. |
| 15 | \since 6.2 |
| 16 | |
| 17 | This element defines particle bursts in the \l ParticleEmitter3D. These bursts are |
| 18 | static, meaning that they are evaluated when the particlesystem starts. This allows |
| 19 | better performance than \l DynamicBurst3D and bursting outside of the particlesystem |
| 20 | time (so e.g. burst at 1000ms while system time starts from 2000ms). |
| 21 | \note EmitBurst3D uses emitter properties (position, rotation etc.) at the |
| 22 | particlesystem start. For dynamic emitters, use \l DynamicBurst3D instead. |
| 23 | |
| 24 | For example, to emit 100 particles at the beginning, and 50 particles at 2 seconds, |
| 25 | so that both bursts take 200 milliseconds: |
| 26 | |
| 27 | \qml |
| 28 | ParticleEmitter3D { |
| 29 | ... |
| 30 | emitBursts: [ |
| 31 | EmitBurst3D { |
| 32 | time: 0 |
| 33 | amount: 100 |
| 34 | duration: 200 |
| 35 | }, |
| 36 | EmitBurst3D { |
| 37 | time: 2000 |
| 38 | amount: 50 |
| 39 | duration: 200 |
| 40 | } |
| 41 | ] |
| 42 | } |
| 43 | \endqml |
| 44 | */ |
| 45 | |
| 46 | QQuick3DParticleEmitBurst::QQuick3DParticleEmitBurst(QObject *parent) |
| 47 | : QObject(parent) |
| 48 | { |
| 49 | m_parentEmitter = qobject_cast<QQuick3DParticleEmitter *>(object: parent); |
| 50 | } |
| 51 | |
| 52 | QQuick3DParticleEmitBurst::~QQuick3DParticleEmitBurst() |
| 53 | { |
| 54 | if (m_parentEmitter) |
| 55 | m_parentEmitter->unRegisterEmitBurst(emitBurst: this); |
| 56 | } |
| 57 | |
| 58 | /*! |
| 59 | \qmlproperty int EmitBurst3D::time |
| 60 | |
| 61 | This property defines the time in milliseconds when emitting the burst starts. |
| 62 | |
| 63 | The default value is \c 0. |
| 64 | */ |
| 65 | int QQuick3DParticleEmitBurst::time() const |
| 66 | { |
| 67 | return m_time; |
| 68 | } |
| 69 | |
| 70 | /*! |
| 71 | \qmlproperty int EmitBurst3D::amount |
| 72 | |
| 73 | This property defines the amount of particles emitted during the burst. |
| 74 | |
| 75 | The default value is \c 0. |
| 76 | */ |
| 77 | int QQuick3DParticleEmitBurst::amount() const |
| 78 | { |
| 79 | return m_amount; |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | \qmlproperty int EmitBurst3D::duration |
| 84 | |
| 85 | This property defines the duration of the burst. The default value is 0, |
| 86 | meaning all particles will burst at the beginning of \l time. |
| 87 | If the duration is set, particles emitting is distributed between \c time |
| 88 | and \c time + \c duration. |
| 89 | |
| 90 | For example, to have emit rate of 400 between 1000 and 1200 milliseconds: |
| 91 | \qml |
| 92 | EmitBurst3D { |
| 93 | time: 1000 |
| 94 | amount: 80 |
| 95 | duration: 1200 |
| 96 | } |
| 97 | \endqml |
| 98 | |
| 99 | The default value is \c 0. |
| 100 | */ |
| 101 | int QQuick3DParticleEmitBurst::duration() const |
| 102 | { |
| 103 | return m_duration; |
| 104 | } |
| 105 | |
| 106 | void QQuick3DParticleEmitBurst::setTime(int time) |
| 107 | { |
| 108 | if (m_time == time) |
| 109 | return; |
| 110 | |
| 111 | m_time = time; |
| 112 | Q_EMIT timeChanged(); |
| 113 | } |
| 114 | |
| 115 | void QQuick3DParticleEmitBurst::setAmount(int amount) |
| 116 | { |
| 117 | if (m_amount == amount) |
| 118 | return; |
| 119 | if (amount < 0) { |
| 120 | qWarning () << "EmitBurst3D: Amount must be positive." ; |
| 121 | return; |
| 122 | } |
| 123 | m_amount = amount; |
| 124 | Q_EMIT amountChanged(); |
| 125 | } |
| 126 | |
| 127 | void QQuick3DParticleEmitBurst::setDuration(int duration) |
| 128 | { |
| 129 | if (m_duration == duration) |
| 130 | return; |
| 131 | if (duration < 0) { |
| 132 | qWarning () << "EmitBurst3D: Duration must be positive." ; |
| 133 | return; |
| 134 | } |
| 135 | m_duration = duration; |
| 136 | Q_EMIT durationChanged(); |
| 137 | } |
| 138 | |
| 139 | void QQuick3DParticleEmitBurst::componentComplete() |
| 140 | { |
| 141 | m_parentEmitter = qobject_cast<QQuick3DParticleEmitter *>(object: parent()); |
| 142 | if (m_parentEmitter) |
| 143 | m_parentEmitter->registerEmitBurst(emitBurst: this); |
| 144 | else |
| 145 | qWarning() << "EmitBurst requires parent Emitter to function correctly!" ; |
| 146 | } |
| 147 | |
| 148 | QT_END_NAMESPACE |
| 149 | |