aboutsummaryrefslogtreecommitdiffstats
path: root/src/particles/qquickimageparticle_p.h
blob: b5fdfdadc9224209c4a86e6cde3d9bd62935796f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QQUICKIMAGEPARTICLE_P_H
#define QQUICKIMAGEPARTICLE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qquickparticlepainter_p.h"
#include "qquickdirection_p.h"
#include <private/qquickpixmapcache_p.h>
#include <QQmlListProperty>
#include <QtGui/qcolor.h>
#include <QtQuick/qsgmaterial.h>

QT_BEGIN_NAMESPACE

class ImageMaterialData;
class QSGGeometryNode;
class QSGMaterial;

class QQuickSprite;
class QQuickStochasticEngine;

class QRhi;

struct SimpleVertex {
    float x;
    float y;
    float t;
    float lifeSpan;
    float size;
    float endSize;
    float vx;
    float vy;
    float ax;
    float ay;
};

struct ColoredVertex {
    float x;
    float y;
    float t;
    float lifeSpan;
    float size;
    float endSize;
    float vx;
    float vy;
    float ax;
    float ay;
    Color4ub color;
};

struct DeformableVertex {
    float x;
    float y;
    float tx;
    float ty;
    float t;
    float lifeSpan;
    float size;
    float endSize;
    float vx;
    float vy;
    float ax;
    float ay;
    Color4ub color;
    float xx;
    float xy;
    float yx;
    float yy;
    float rotation;
    float rotationVelocity;
    float autoRotate;//Assumed that GPUs prefer floats to bools
};

struct SpriteVertex {
    float x;
    float y;
    float tx;
    float ty;
    float t;
    float lifeSpan;
    float size;
    float endSize;
    float vx;
    float vy;
    float ax;
    float ay;
    Color4ub color;
    float xx;
    float xy;
    float yx;
    float yy;
    float rotation;
    float rotationVelocity;
    float autoRotate;//Assumed that GPUs prefer floats to bools
    float animW;
    float animH;
    float animProgress;
    float animX1;
    float animY1;
    float animX2;
    float animY2;
};

template <typename Vertex>
struct Vertices {
    Vertex v1;
    Vertex v2;
    Vertex v3;
    Vertex v4;
};

class ImageMaterial : public QSGMaterial
{
public:
    virtual ImageMaterialData *state() = 0;
};

class QQuickImageParticle : public QQuickParticlePainter
{
    Q_OBJECT
    Q_PROPERTY(QUrl source READ image WRITE setImage NOTIFY imageChanged)
    Q_PROPERTY(QQmlListProperty<QQuickSprite> sprites READ sprites)
    Q_PROPERTY(Status status READ status NOTIFY statusChanged)
    //### Is it worth having progress like Image has?
    //Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)

    Q_PROPERTY(QUrl colorTable READ colortable WRITE setColortable NOTIFY colortableChanged)
    Q_PROPERTY(QUrl sizeTable READ sizetable WRITE setSizetable NOTIFY sizetableChanged)
    Q_PROPERTY(QUrl opacityTable READ opacitytable WRITE setOpacitytable NOTIFY opacitytableChanged)

    //###Now just colorize - add a flag for 'solid' color particles(where the img is just a mask?)?
    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged RESET resetColor)
    //Stacks (added) with individual colorVariations
    Q_PROPERTY(qreal colorVariation READ colorVariation WRITE setColorVariation NOTIFY colorVariationChanged RESET resetColor)
    Q_PROPERTY(qreal redVariation READ redVariation WRITE setRedVariation NOTIFY redVariationChanged RESET resetColor)
    Q_PROPERTY(qreal greenVariation READ greenVariation WRITE setGreenVariation NOTIFY greenVariationChanged RESET resetColor)
    Q_PROPERTY(qreal blueVariation READ blueVariation WRITE setBlueVariation NOTIFY blueVariationChanged RESET resetColor)
    //Stacks (multiplies) with the Alpha in the color, mostly here so you can use svg color names (which have full alpha)
    Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha NOTIFY alphaChanged RESET resetColor)
    Q_PROPERTY(qreal alphaVariation READ alphaVariation WRITE setAlphaVariation NOTIFY alphaVariationChanged RESET resetColor)

    Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged RESET resetRotation)
    Q_PROPERTY(qreal rotationVariation READ rotationVariation WRITE setRotationVariation NOTIFY rotationVariationChanged RESET resetRotation)
    Q_PROPERTY(qreal rotationVelocity READ rotationVelocity WRITE setRotationVelocity NOTIFY rotationVelocityChanged RESET resetRotation)
    Q_PROPERTY(qreal rotationVelocityVariation READ rotationVelocityVariation WRITE setRotationVelocityVariation NOTIFY rotationVelocityVariationChanged RESET resetRotation)
    //If true, then will face the direction of motion. Stacks with rotation, e.g. setting rotation
    //to 180 will lead to facing away from the direction of motion
    Q_PROPERTY(bool autoRotation READ autoRotation WRITE setAutoRotation NOTIFY autoRotationChanged RESET resetRotation)

    //xVector is the vector from the top-left point to the top-right point, and is multiplied by current size
    Q_PROPERTY(QQuickDirection* xVector READ xVector WRITE setXVector NOTIFY xVectorChanged RESET resetDeformation)
    //yVector is the same, but top-left to bottom-left. The particle is always a parallelogram.
    Q_PROPERTY(QQuickDirection* yVector READ yVector WRITE setYVector NOTIFY yVectorChanged RESET resetDeformation)
    Q_PROPERTY(bool spritesInterpolate READ spritesInterpolate WRITE setSpritesInterpolate NOTIFY spritesInterpolateChanged)

    Q_PROPERTY(EntryEffect entryEffect READ entryEffect WRITE setEntryEffect NOTIFY entryEffectChanged)
    QML_NAMED_ELEMENT(ImageParticle)
public:
    explicit QQuickImageParticle(QQuickItem *parent = 0);
    virtual ~QQuickImageParticle();

    enum Status { Null, Ready, Loading, Error };
    Q_ENUM(Status)

    QQmlListProperty<QQuickSprite> sprites();
    QQuickStochasticEngine* spriteEngine() {return m_spriteEngine;}

    enum EntryEffect {
        None = 0,
        Fade = 1,
        Scale = 2
    };
    Q_ENUM(EntryEffect)

    enum PerformanceLevel{//TODO: Expose?
        Unknown = 0,
        Simple,
        Colored,
        Deformable,
        Tabled,
        Sprites
    };

    QUrl image() const { return m_image ? m_image->source : QUrl(); }
    void setImage(const QUrl &image);

    QUrl colortable() const { return m_colorTable ? m_colorTable->source : QUrl(); }
    void setColortable(const QUrl &table);

    QUrl sizetable() const { return m_sizeTable ? m_sizeTable->source : QUrl(); }
    void setSizetable (const QUrl &table);

    QUrl opacitytable() const { return m_opacityTable ? m_opacityTable->source : QUrl(); }
    void setOpacitytable(const QUrl &table);

    QColor color() const { return m_color; }
    void setColor(const QColor &color);

    qreal colorVariation() const { return m_color_variation; }
    void setColorVariation(qreal var);

    qreal alphaVariation() const { return m_alphaVariation; }

    qreal alpha() const { return m_alpha; }

    qreal redVariation() const { return m_redVariation; }

    qreal greenVariation() const { return m_greenVariation; }

    qreal blueVariation() const { return m_blueVariation; }

    qreal rotation() const { return m_rotation; }

    qreal rotationVariation() const { return m_rotationVariation; }

    qreal rotationVelocity() const { return m_rotationVelocity; }

    qreal rotationVelocityVariation() const { return m_rotationVelocityVariation; }

    bool autoRotation() const { return m_autoRotation; }

    QQuickDirection* xVector() const { return m_xVector; }

    QQuickDirection* yVector() const { return m_yVector; }

    bool spritesInterpolate() const { return m_spritesInterpolate; }

    bool bypassOptimizations() const { return m_bypassOptimizations; }

    EntryEffect entryEffect() const { return m_entryEffect; }

    Status status() const { return m_status; }

    void resetColor();
    void resetRotation();
    void resetDeformation();

Q_SIGNALS:

    void imageChanged();
    void colortableChanged();
    void sizetableChanged();
    void opacitytableChanged();

    void colorChanged();
    void colorVariationChanged();

    void alphaVariationChanged(qreal arg);

    void alphaChanged(qreal arg);

    void redVariationChanged(qreal arg);

    void greenVariationChanged(qreal arg);

    void blueVariationChanged(qreal arg);

    void rotationChanged(qreal arg);

    void rotationVariationChanged(qreal arg);

    void rotationVelocityChanged(qreal arg);

    void rotationVelocityVariationChanged(qreal arg);

    void autoRotationChanged(bool arg);

    void xVectorChanged(QQuickDirection* arg);

    void yVectorChanged(QQuickDirection* arg);

    void spritesInterpolateChanged(bool arg);

    void bypassOptimizationsChanged(bool arg);

    void entryEffectChanged(EntryEffect arg);

    void statusChanged(Status arg);

public Q_SLOTS:
    void reloadColor(const Color4ub &c, QQuickParticleData* d);
    void setAlphaVariation(qreal arg);

    void setAlpha(qreal arg);

    void setRedVariation(qreal arg);

    void setGreenVariation(qreal arg);

    void setBlueVariation(qreal arg);

    void setRotation(qreal arg);

    void setRotationVariation(qreal arg);

    void setRotationVelocity(qreal arg);

    void setRotationVelocityVariation(qreal arg);

    void setAutoRotation(bool arg);

    void setXVector(QQuickDirection* arg);

    void setYVector(QQuickDirection* arg);

    void setSpritesInterpolate(bool arg);

    void setBypassOptimizations(bool arg);

    void setEntryEffect(EntryEffect arg);

protected:
    void reset() override;
    void initialize(int gIdx, int pIdx) override;
    void commit(int gIdx, int pIdx) override;

    QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
    bool prepareNextFrame(QSGNode**);
    void buildParticleNodes(QSGNode**);

    void sceneGraphInvalidated() override;

private Q_SLOTS:
    void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty

    void spriteAdvance(int spriteIndex);
    void spritesUpdate(qreal time = 0 );
    void mainThreadFetchImageData();
    void finishBuildParticleNodes(QSGNode **n);
private:
    struct ImageData {
        QUrl source;
        QQuickPixmap pix;
    };
    QScopedPointer<ImageData> m_image;
    QScopedPointer<ImageData> m_colorTable;
    QScopedPointer<ImageData> m_sizeTable;
    QScopedPointer<ImageData> m_opacityTable;
    bool loadingSomething();


    QColor m_color;
    qreal m_color_variation;

    QSGNode *m_outgoingNode;
    QHash<int, QSGGeometryNode *> m_nodes;
    QHash<int, int> m_idxStarts;//TODO: Proper resizing will lead to needing a spriteEngine per particle - do this after sprite engine gains transparent sharing?
    QList<QPair<int, int> > m_startsIdx;//Same data, optimized for alternate retrieval

    int m_lastIdxStart;
    QSGMaterial *m_material;

    // derived values...

    qreal m_alphaVariation;
    qreal m_alpha;
    qreal m_redVariation;
    qreal m_greenVariation;
    qreal m_blueVariation;
    qreal m_rotation;
    qreal m_rotationVariation;
    qreal m_rotationVelocity;
    qreal m_rotationVelocityVariation;
    bool m_autoRotation;
    QQuickDirection* m_xVector;
    QQuickDirection* m_yVector;

    QList<QQuickSprite*> m_sprites;
    QQuickSpriteEngine* m_spriteEngine;
    bool m_spritesInterpolate;

    bool m_explicitColor;
    bool m_explicitRotation;
    bool m_explicitDeformation;
    bool m_explicitAnimation;
    QHash<int, QVector<QQuickParticleData*> > m_shadowData;
    void clearShadows();
    QQuickParticleData* getShadowDatum(QQuickParticleData* datum);

    bool m_bypassOptimizations;
    PerformanceLevel perfLevel;

    PerformanceLevel m_lastLevel;
    bool m_debugMode;

    template<class Vertex>
    void initTexCoords(Vertex* v, int count){
        Vertex* end = v + count;
        while (v < end){
            v[0].tx = 0;
            v[0].ty = 0;

            v[1].tx = 1;
            v[1].ty = 0;

            v[2].tx = 0;
            v[2].ty = 1;

            v[3].tx = 1;
            v[3].ty = 1;

            v += 4;
        }
    }

    ImageMaterialData *getState(QSGMaterial *m) {
        return static_cast<ImageMaterial *>(m)->state();
    }

    EntryEffect m_entryEffect;
    Status m_status;
    int m_startedImageLoading;
    QRhi *m_rhi;
    bool m_apiChecked;
    bool m_previousActive;
};

QT_END_NAMESPACE

#endif // QQUICKIMAGEPARTICLE_P_H