| 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 ITEMPARTICLE_H |
| 5 | #define ITEMPARTICLE_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 | #include "qquickparticlepainter_p.h" |
| 18 | #include <QPointer> |
| 19 | #include <QSet> |
| 20 | #include <private/qquickanimation_p_p.h> |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | class QQuickItemParticleAttached; |
| 24 | |
| 25 | class Q_QUICKPARTICLES_EXPORT QQuickItemParticle : public QQuickParticlePainter |
| 26 | { |
| 27 | Q_OBJECT |
| 28 | Q_PROPERTY(bool fade READ fade WRITE setFade NOTIFY fadeChanged) |
| 29 | Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) |
| 30 | QML_NAMED_ELEMENT(ItemParticle) |
| 31 | QML_ADDED_IN_VERSION(2, 0) |
| 32 | QML_ATTACHED(QQuickItemParticleAttached) |
| 33 | public: |
| 34 | explicit QQuickItemParticle(QQuickItem *parent = nullptr); |
| 35 | ~QQuickItemParticle(); |
| 36 | |
| 37 | bool fade() const { return m_fade; } |
| 38 | |
| 39 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
| 40 | |
| 41 | static QQuickItemParticleAttached *qmlAttachedProperties(QObject *object); |
| 42 | QQmlComponent* delegate() const |
| 43 | { |
| 44 | return m_delegate; |
| 45 | } |
| 46 | |
| 47 | Q_SIGNALS: |
| 48 | void fadeChanged(); |
| 49 | |
| 50 | void delegateChanged(QQmlComponent* arg); |
| 51 | |
| 52 | public Q_SLOTS: |
| 53 | //TODO: Add a follow mode, where moving the delegate causes the logical particle to go with it? |
| 54 | void freeze(QQuickItem* item); |
| 55 | void unfreeze(QQuickItem* item); |
| 56 | void take(QQuickItem* item,bool prioritize=false);//take by modelparticle |
| 57 | void give(QQuickItem* item);//give from modelparticle |
| 58 | |
| 59 | void setFade(bool arg){if (arg == m_fade) return; m_fade = arg; Q_EMIT fadeChanged();} |
| 60 | void setDelegate(QQmlComponent* arg) |
| 61 | { |
| 62 | if (m_delegate != arg) { |
| 63 | m_delegate = arg; |
| 64 | Q_EMIT delegateChanged(arg); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | void reset() override; |
| 70 | void commit(int gIdx, int pIdx) override; |
| 71 | void initialize(int gIdx, int pIdx) override; |
| 72 | void prepareNextFrame(); |
| 73 | private: |
| 74 | bool clockShouldUpdate() const; |
| 75 | void updateClock(); |
| 76 | void reconnectSystem(QQuickParticleSystem *system); |
| 77 | void reconnectParent(QQuickItem *parent); |
| 78 | void processDeletables(); |
| 79 | void tick(int time = 0); |
| 80 | QSet<QQuickItem* > m_deletables; |
| 81 | QList<QQuickItem* > m_managed; |
| 82 | bool m_fade; |
| 83 | |
| 84 | QList<QQuickItem*> m_pendingItems; |
| 85 | QSet<QQuickItem*> m_stasis; |
| 86 | qreal m_lastT; |
| 87 | int m_activeCount; |
| 88 | QQmlComponent* m_delegate; |
| 89 | |
| 90 | typedef QTickAnimationProxy<QQuickItemParticle, &QQuickItemParticle::tick> Clock; |
| 91 | Clock *clock; |
| 92 | QMetaObject::Connection m_systemRunStateConnection; |
| 93 | QMetaObject::Connection m_systemPauseStateConnection; |
| 94 | QMetaObject::Connection m_systemEnabledStateConnection; |
| 95 | QMetaObject::Connection m_parentEnabledStateConnection; |
| 96 | }; |
| 97 | |
| 98 | class QQuickItemParticleAttached : public QObject |
| 99 | { |
| 100 | Q_OBJECT |
| 101 | Q_PROPERTY(QQuickItemParticle* particle READ particle CONSTANT FINAL); |
| 102 | public: |
| 103 | QQuickItemParticleAttached(QObject* parent) |
| 104 | : QObject(parent), m_mp(0), m_parentItem(nullptr) |
| 105 | {;} |
| 106 | QQuickItemParticle* particle() const { return m_mp; } |
| 107 | void detach(){Q_EMIT detached();} |
| 108 | void attach(){Q_EMIT attached();} |
| 109 | private: |
| 110 | QQuickItemParticle* m_mp; |
| 111 | QPointer<QQuickItem> m_parentItem; |
| 112 | friend class QQuickItemParticle; |
| 113 | Q_SIGNALS: |
| 114 | void detached(); |
| 115 | void attached(); |
| 116 | }; |
| 117 | |
| 118 | QT_END_NAMESPACE |
| 119 | |
| 120 | #endif // ITEMPARTICLE_H |
| 121 | |