Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dparticlespritesequence.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
8
10
11/*!
12 \qmltype SpriteSequence3D
13 \inherits QtObject
14 \inqmlmodule QtQuick3D.Particles3D
15 \brief Provides image sequence features for the Sprite particles.
16 \since 6.2
17
18 The SpriteSequence3D element provides support for animated images with multiple frames. The frames
19 should be aligned horizontally in the image, first frame being on the left and last on the right.
20
21 To make a \l SpriteParticle3D an animated sequence, set its \l {SpriteParticle3D::spriteSequence}{spriteSequence} property.
22*/
23
24QQuick3DParticleSpriteSequence::QQuick3DParticleSpriteSequence(QObject *parent)
25 : QObject(parent)
26{
27
28}
29
30QQuick3DParticleSpriteSequence::~QQuick3DParticleSpriteSequence()
31{
32 if (m_parentParticle)
33 m_parentParticle->setSpriteSequence(nullptr);
34}
35
36/*!
37 \qmlproperty int SpriteSequence3D::frameCount
38
39 This property defines the amount of image frames in \l {SpriteParticle3D::}{sprite}.
40 Particle animates through these frames during its \l duration.
41 The frames should be laid out horizontally in the same image file. For example,
42 \e sprite could be a \c {512x64} image, with \c frameCount of \c 8. This would make
43 each particle frame size \c {64x64} pixels.
44
45 The default value is \c 1.
46
47 \note If your image only has a single sprite frame, don't define the
48 \l {SpriteParticle3D::}{spriteSequence} property at all.
49
50 \sa interpolate
51*/
52int QQuick3DParticleSpriteSequence::frameCount() const
53{
54 return m_frameCount;
55}
56
57/*!
58 \qmlproperty int SpriteSequence3D::frameIndex
59
60 This property defines the initial index of the frame. This is the position in between frames
61 where the animation is started. For example when the \c frameIndex is 5 and the \l animationDirection
62 is \c Normal, the first rendered frame is 5. If the \l animationDirection is \c Reverse, the
63 first rendered frame is 4.
64
65 The value of frameIndex must be between 0 and \l{frameCount} - \c 1. When the \l animationDirection
66 is \c SingleFrame and \l randomStart is \c false, all the particles will render sprites with the
67 \c frameIndex.
68
69 The default value is \c 0.
70
71 \sa randomStart, animationDirection
72*/
73int QQuick3DParticleSpriteSequence::frameIndex() const
74{
75 return m_frameIndex;
76}
77
78/*!
79 \qmlproperty bool SpriteSequence3D::interpolate
80
81 This property defines if the sprites are interpolated (blended) between frames
82 to make the animation appear smoother.
83
84 The default value is \c true.
85
86 \sa frameCount
87*/
88bool QQuick3DParticleSpriteSequence::interpolate() const
89{
90 return m_interpolate;
91}
92
93/*!
94 \qmlproperty int SpriteSequence3D::duration
95
96 This property defines the duration in milliseconds how long it takes for the
97 sprite sequence to animate. For example, if the \l duration is \c 400 and the
98 \l frameCount is 8, each frame will be shown for 50 milliseconds. When the
99 value is -1, the particle lifeSpan is used as the duration.
100
101 The default value is \c -1.
102*/
103int QQuick3DParticleSpriteSequence::duration() const
104{
105 return m_duration;
106}
107
108/*!
109 \qmlproperty int SpriteSequence3D::durationVariation
110
111 This property defines the duration variation in milliseconds. The actual duration
112 of the animation is between \c duration - \c durationVariation and \c duration +
113 \c durationVariation.
114
115 The default value is \c 0 (no variation).
116*/
117int QQuick3DParticleSpriteSequence::durationVariation() const
118{
119 return m_durationVariation;
120}
121
122/*!
123 \qmlproperty bool SpriteSequence3D::randomStart
124
125 This property defines if the animation should start from a random frame between \c 0 and \l frameCount - \c 1.
126 This allows animations to not look like they all just started when the animation begins.
127
128 The default value is \c false.
129
130 \sa animationDirection
131*/
132bool QQuick3DParticleSpriteSequence::randomStart() const
133{
134 return m_randomStart;
135}
136
137/*!
138 \qmlproperty AnimationDirection SpriteSequence3D::animationDirection
139
140 This property defines the animation direction of the sequence.
141
142 The default value is \c SpriteSequence3D.Normal.
143
144 \sa randomStart
145*/
146
147/*!
148 \qmlproperty enumeration SpriteSequence3D::AnimationDirection
149
150 Defines the animation playback direction of the sequence.
151
152 \value SpriteSequence3D.Normal
153 Animate from the first frame to the last frame. When the last frame is reached, jump back to the first frame.
154 \value SpriteSequence3D.Reverse
155 Animate from the last frame to the first frame. When the first frame is reached, jump back to the last frame.
156 \value SpriteSequence3D.Alternate
157 Animate from the first frame to the last frame. When the last or first frame is reached, switch the animation direction.
158 This makes the sequence animation smooth even when the first and the last frames don't match.
159 \value SpriteSequence3D.AlternateReverse
160 Animate from the last frame to the first frame. When the last or first frame is reached, switch the animation direction.
161 This makes the sequence animation smooth even when the first and the last frames don't match.
162 \value SpriteSequence3D.SingleFrame
163 Don't animate the frame. When the \l randomStart is false, \l frameIndex frame is rendered.
164 When the \l randomStart is true, each particle renders a random frame.
165*/
166QQuick3DParticleSpriteSequence::AnimationDirection QQuick3DParticleSpriteSequence::animationDirection() const
167{
168 return m_animationDirection;
169}
170
171void QQuick3DParticleSpriteSequence::setFrameCount(int frameCount)
172{
173 if (m_frameCount == frameCount)
174 return;
175 m_frameCount = std::max(1, frameCount);
176 markNodesDirty();
177 Q_EMIT frameCountChanged();
178}
179
180void QQuick3DParticleSpriteSequence::setFrameIndex(int frameIndex)
181{
182 if (m_frameIndex == frameIndex)
183 return;
184 m_frameIndex = std::max(0, frameIndex);
185 markNodesDirty();
186 Q_EMIT frameIndexChanged();
187}
188
189void QQuick3DParticleSpriteSequence::setInterpolate(bool interpolate)
190{
191 if (m_interpolate == interpolate)
192 return;
193 m_interpolate = interpolate;
194 markNodesDirty();
195 Q_EMIT interpolateChanged();
196}
197
198void QQuick3DParticleSpriteSequence::setDuration(int duration)
199{
200 if (m_duration == duration)
201 return;
202
203 m_duration = duration;
204 markNodesDirty();
205 Q_EMIT durationChanged();
206}
207
208void QQuick3DParticleSpriteSequence::setDurationVariation(int durationVariation)
209{
210 if (m_durationVariation == durationVariation)
211 return;
212
213 m_durationVariation = durationVariation;
214 markNodesDirty();
215 Q_EMIT durationVariationChanged();
216}
217
218void QQuick3DParticleSpriteSequence::setRandomStart(bool randomStart)
219{
220 if (m_randomStart == randomStart)
221 return;
222 m_randomStart = randomStart;
223 markNodesDirty();
224 Q_EMIT randomStartChanged();
225}
226
227void QQuick3DParticleSpriteSequence::setAnimationDirection(QQuick3DParticleSpriteSequence::AnimationDirection animationDirection)
228{
229 if (m_animationDirection == animationDirection)
230 return;
231 m_animationDirection = animationDirection;
232 markNodesDirty();
233 Q_EMIT animationDirectionChanged();
234}
235
236void QQuick3DParticleSpriteSequence::componentComplete()
237{
238 m_parentParticle = qobject_cast<QQuick3DParticleSpriteParticle *>(parent());
239 if (!m_parentParticle)
240 qWarning() << "SpriteSequence3D requires parent SpriteParticle3D to function correctly!";
241}
242
243void QQuick3DParticleSpriteSequence::markNodesDirty()
244{
245 if (m_parentParticle)
246 m_parentParticle->markNodesDirty();
247}
248
249// Returns the first frame of the sequence.
250// Return range [0..1) where 0.0 is the first frame and 0.9999 is the last.
251float QQuick3DParticleSpriteSequence::firstFrame(int index, bool singleFrame)
252{
253 float firstFrame = 0.0f;
254 if (m_randomStart) {
255 if (!m_parentParticle || !m_parentParticle->m_system)
256 return firstFrame;
257 auto rand = m_parentParticle->m_system->rand();
258 firstFrame = rand->get(index, QPRand::SpriteAnimationI);
259 } else if (m_frameCount > 1 && m_frameIndex > 0) {
260 int frameIndex = std::min(m_frameIndex, m_frameCount - 1);
261 if (singleFrame)
262 firstFrame = float(frameIndex) / (float(m_frameCount - 1) + 0.0001f);
263 else
264 firstFrame = float(frameIndex) / float(m_frameCount);
265 }
266 return firstFrame;
267}
268
269QT_END_NAMESPACE
Combined button and popup list for selecting options.