1 | // Copyright (C) 2018 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef BMANIMATION_H |
5 | #define BMANIMATION_H |
6 | |
7 | #include <QQuickPaintedItem> |
8 | #include <QByteArray> |
9 | #include <QList> |
10 | #include <QImage> |
11 | #include <QThread> |
12 | #include <QMetaObject> |
13 | |
14 | #include <QtBodymovin/private/bmconstants_p.h> |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | class QQmlFile; |
19 | |
20 | class BMBase; |
21 | class BMLayer; |
22 | class BatchRenderer; |
23 | |
24 | class LottieAnimation : public QQuickPaintedItem |
25 | { |
26 | Q_OBJECT |
27 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
28 | Q_PROPERTY(int frameRate READ frameRate WRITE setFrameRate RESET resetFrameRate NOTIFY frameRateChanged) |
29 | Q_PROPERTY(int startFrame READ startFrame NOTIFY startFrameChanged) |
30 | Q_PROPERTY(int endFrame READ endFrame NOTIFY endFrameChanged) |
31 | Q_PROPERTY(Status status READ status WRITE setStatus NOTIFY statusChanged) |
32 | Q_PROPERTY(Quality quality READ quality WRITE setQuality NOTIFY qualityChanged) |
33 | Q_PROPERTY(bool autoPlay MEMBER m_autoPlay NOTIFY autoPlayChanged) |
34 | Q_PROPERTY(int loops MEMBER m_loops NOTIFY loopsChanged) |
35 | Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
36 | |
37 | QML_ELEMENT |
38 | QML_ADDED_IN_VERSION(1, 0) |
39 | |
40 | public: |
41 | enum Status{Null, Loading, Ready, Error}; |
42 | Q_ENUM(Status) |
43 | |
44 | enum Quality{LowQuality, MediumQuality, HighQuality}; |
45 | Q_ENUM(Quality) |
46 | |
47 | enum Direction{Forward = 1, Reverse = -1}; |
48 | Q_ENUM(Direction) |
49 | |
50 | enum LoopCount{Infinite = -1}; |
51 | Q_ENUM(LoopCount) |
52 | |
53 | explicit LottieAnimation(QQuickItem *parent = nullptr); |
54 | ~LottieAnimation() override; |
55 | |
56 | void paint(QPainter *painter) override; |
57 | |
58 | Status status() const; |
59 | |
60 | QUrl source() const; |
61 | void setSource(const QUrl &source); |
62 | |
63 | int frameRate() const; |
64 | void setFrameRate(int frameRate); |
65 | void resetFrameRate(); |
66 | |
67 | Quality quality() const; |
68 | void setQuality(Quality quality); |
69 | |
70 | Direction direction() const; |
71 | void setDirection(Direction direction); |
72 | |
73 | int startFrame() const; |
74 | int endFrame() const; |
75 | int currentFrame() const; |
76 | |
77 | QVersionNumber version() const; |
78 | |
79 | Q_INVOKABLE void start(); |
80 | |
81 | Q_INVOKABLE void play(); |
82 | Q_INVOKABLE void pause(); |
83 | Q_INVOKABLE void togglePause(); |
84 | Q_INVOKABLE void stop(); |
85 | Q_INVOKABLE void gotoAndPlay(int frame); |
86 | Q_INVOKABLE bool gotoAndPlay(const QString &frameMarker); |
87 | Q_INVOKABLE void gotoAndStop(int frame); |
88 | Q_INVOKABLE bool gotoAndStop(const QString &frameMarker); |
89 | Q_INVOKABLE double getDuration(bool inFrames = false); |
90 | |
91 | QByteArray jsonSource() const; |
92 | |
93 | signals: |
94 | void statusChanged(); |
95 | void qualityChanged(); |
96 | void sourceChanged(); |
97 | void finished(); |
98 | void frameRateChanged(); |
99 | void autoPlayChanged(); |
100 | void loopsChanged(); |
101 | void directionChanged(); |
102 | void startFrameChanged(); |
103 | void endFrameChanged(); |
104 | |
105 | protected slots: |
106 | void loadFinished(); |
107 | |
108 | void renderNextFrame(); |
109 | |
110 | protected: |
111 | void componentComplete() override; |
112 | |
113 | void setStatus(Status status); |
114 | |
115 | void setStartFrame(int startFrame); |
116 | void setEndFrame(int endFrame); |
117 | |
118 | void load(); |
119 | |
120 | virtual int parse(QByteArray jsonSource); |
121 | |
122 | protected: |
123 | BatchRenderer *m_frameRenderThread = nullptr; |
124 | QMetaObject::Connection m_waitForFrameConn; |
125 | |
126 | Status m_status = Null; |
127 | QVersionNumber m_version = QVersionNumber(); |
128 | int m_startFrame = 0; |
129 | int m_endFrame = 0; |
130 | int m_currentFrame = 0; |
131 | int m_frameRate = 30; |
132 | int m_animFrameRate = 30; |
133 | qreal m_animWidth = 0; |
134 | qreal m_animHeight = 0; |
135 | QHash<QString, int> m_markers; |
136 | QUrl m_source; |
137 | QScopedPointer<QQmlFile> m_file; |
138 | QTimer *m_frameAdvance = nullptr; |
139 | |
140 | void gotoFrame(int frame); |
141 | void reset(); |
142 | |
143 | private: |
144 | Quality m_quality = HighQuality; |
145 | bool m_autoPlay = true; |
146 | int m_loops = 1; |
147 | int m_currentLoop = 0; |
148 | int m_direction = Forward; |
149 | QByteArray m_jsonSource; |
150 | }; |
151 | |
152 | QT_END_NAMESPACE |
153 | |
154 | Q_DECLARE_METATYPE(LottieAnimation*) |
155 | |
156 | #endif // BMANIMATION_H |
157 | |