| 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 QQUICKSPRITE_P_H |
| 5 | #define QQUICKSPRITE_P_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 | |
| 18 | #include <private/qtquickglobal_p.h> |
| 19 | |
| 20 | QT_REQUIRE_CONFIG(quick_sprite); |
| 21 | |
| 22 | #include <QObject> |
| 23 | #include <QUrl> |
| 24 | #include <QVariantMap> |
| 25 | #include <QQmlListProperty> |
| 26 | #include <QtQuick/private/qquickpixmap_p.h> |
| 27 | #include "qquickspriteengine_p.h" |
| 28 | #include <QDebug> |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | // exported, since it's used in QtQuickParticles |
| 33 | class Q_QUICK_EXPORT QQuickSprite : public QQuickStochasticState |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
| 37 | //Renderers have to query this hint when advancing frames |
| 38 | Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged) |
| 39 | Q_PROPERTY(bool frameSync READ frameSync WRITE setFrameSync NOTIFY frameSyncChanged) |
| 40 | Q_PROPERTY(int frames READ frames WRITE setFrames NOTIFY frameCountChanged) |
| 41 | Q_PROPERTY(int frameCount READ frameCount WRITE setFrameCount NOTIFY frameCountChanged) |
| 42 | //If frame height or width is not specified, it is assumed to be a single long row of square frames. |
| 43 | //Otherwise, it can be multiple contiguous rows, when one row runs out the next will be used. |
| 44 | Q_PROPERTY(int frameHeight READ frameHeight WRITE setFrameHeight NOTIFY frameHeightChanged) |
| 45 | Q_PROPERTY(int frameWidth READ frameWidth WRITE setFrameWidth NOTIFY frameWidthChanged) |
| 46 | Q_PROPERTY(int frameX READ frameX WRITE setFrameX NOTIFY frameXChanged) |
| 47 | Q_PROPERTY(int frameY READ frameY WRITE setFrameY NOTIFY frameYChanged) |
| 48 | //Precedence order: frameRate, frameDuration, duration |
| 49 | Q_PROPERTY(qreal frameRate READ frameRate WRITE setFrameRate NOTIFY frameRateChanged RESET resetFrameRate) |
| 50 | Q_PROPERTY(qreal frameRateVariation READ frameRateVariation WRITE setFrameRateVariation NOTIFY frameRateVariationChanged) |
| 51 | Q_PROPERTY(int frameDuration READ frameDuration WRITE setFrameDuration NOTIFY frameDurationChanged RESET resetFrameDuration) |
| 52 | Q_PROPERTY(int frameDurationVariation READ frameDurationVariation WRITE setFrameDurationVariation NOTIFY frameDurationVariationChanged) |
| 53 | QML_NAMED_ELEMENT(Sprite) |
| 54 | QML_ADDED_IN_VERSION(2, 0) |
| 55 | |
| 56 | public: |
| 57 | explicit QQuickSprite(QObject *parent = nullptr); |
| 58 | ~QQuickSprite() override; |
| 59 | |
| 60 | QUrl source() const |
| 61 | { |
| 62 | return m_source; |
| 63 | } |
| 64 | |
| 65 | int frameHeight() const |
| 66 | { |
| 67 | return m_frameHeight; |
| 68 | } |
| 69 | |
| 70 | int frameWidth() const |
| 71 | { |
| 72 | return m_frameWidth; |
| 73 | } |
| 74 | |
| 75 | bool reverse() const |
| 76 | { |
| 77 | return m_reverse; |
| 78 | } |
| 79 | |
| 80 | int frames() const |
| 81 | { |
| 82 | return m_frames; |
| 83 | } |
| 84 | |
| 85 | int frameCount() const |
| 86 | { |
| 87 | return m_frames; |
| 88 | } |
| 89 | |
| 90 | int frameX() const |
| 91 | { |
| 92 | return m_frameX; |
| 93 | } |
| 94 | |
| 95 | int frameY() const |
| 96 | { |
| 97 | return m_frameY; |
| 98 | } |
| 99 | |
| 100 | void resetFrameRate() |
| 101 | { |
| 102 | setFrameRate(-1); |
| 103 | } |
| 104 | |
| 105 | qreal frameRate() const |
| 106 | { |
| 107 | return m_frameRate; |
| 108 | } |
| 109 | |
| 110 | qreal frameRateVariation() const |
| 111 | { |
| 112 | return m_frameRateVariation; |
| 113 | } |
| 114 | |
| 115 | void resetFrameDuration() |
| 116 | { |
| 117 | setFrameDuration(-1); |
| 118 | } |
| 119 | |
| 120 | int frameDuration() const |
| 121 | { |
| 122 | return m_frameDuration; |
| 123 | } |
| 124 | |
| 125 | int frameDurationVariation() const |
| 126 | { |
| 127 | return m_frameDurationVariation; |
| 128 | } |
| 129 | |
| 130 | int variedDuration() const override; |
| 131 | |
| 132 | bool frameSync() const |
| 133 | { |
| 134 | return m_frameSync; |
| 135 | } |
| 136 | |
| 137 | void setDevicePixelRatio(qreal dpr) |
| 138 | { |
| 139 | m_devicePixelRatio = dpr; |
| 140 | } |
| 141 | |
| 142 | qreal devicePixelRatio() const |
| 143 | { |
| 144 | return m_devicePixelRatio; |
| 145 | } |
| 146 | |
| 147 | Q_SIGNALS: |
| 148 | |
| 149 | void sourceChanged(QUrl arg); |
| 150 | |
| 151 | void frameHeightChanged(int arg); |
| 152 | |
| 153 | void frameWidthChanged(int arg); |
| 154 | |
| 155 | void reverseChanged(bool arg); |
| 156 | |
| 157 | void frameCountChanged(int arg); |
| 158 | |
| 159 | void frameXChanged(int arg); |
| 160 | |
| 161 | void frameYChanged(int arg); |
| 162 | |
| 163 | void frameRateChanged(qreal arg); |
| 164 | |
| 165 | void frameRateVariationChanged(qreal arg); |
| 166 | |
| 167 | void frameDurationChanged(int arg); |
| 168 | |
| 169 | void frameDurationVariationChanged(int arg); |
| 170 | |
| 171 | void frameSyncChanged(bool arg); |
| 172 | |
| 173 | public Q_SLOTS: |
| 174 | |
| 175 | void setSource(QUrl arg) |
| 176 | { |
| 177 | if (m_source != arg) { |
| 178 | m_source = arg; |
| 179 | Q_EMIT sourceChanged(arg); |
| 180 | startImageLoading(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void setFrameHeight(int arg) |
| 185 | { |
| 186 | if (m_frameHeight != arg) { |
| 187 | m_frameHeight = arg; |
| 188 | Q_EMIT frameHeightChanged(arg); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void setFrameWidth(int arg) |
| 193 | { |
| 194 | if (m_frameWidth != arg) { |
| 195 | m_frameWidth = arg; |
| 196 | Q_EMIT frameWidthChanged(arg); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void setReverse(bool arg) |
| 201 | { |
| 202 | if (m_reverse != arg) { |
| 203 | m_reverse = arg; |
| 204 | Q_EMIT reverseChanged(arg); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void setFrames(int arg) |
| 209 | { |
| 210 | qWarning() << "Sprite::frames has been renamed Sprite::frameCount"; |
| 211 | setFrameCount(arg); |
| 212 | } |
| 213 | |
| 214 | void setFrameCount(int arg) |
| 215 | { |
| 216 | if (m_frames != arg) { |
| 217 | m_frames = arg; |
| 218 | Q_EMIT frameCountChanged(arg); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void setFrameX(int arg) |
| 223 | { |
| 224 | if (m_frameX != arg) { |
| 225 | m_frameX = arg; |
| 226 | Q_EMIT frameXChanged(arg); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void setFrameY(int arg) |
| 231 | { |
| 232 | if (m_frameY != arg) { |
| 233 | m_frameY = arg; |
| 234 | Q_EMIT frameYChanged(arg); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void setFrameRate(qreal arg) |
| 239 | { |
| 240 | if (m_frameRate != arg) { |
| 241 | m_frameRate = arg; |
| 242 | Q_EMIT frameRateChanged(arg); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void setFrameRateVariation(qreal arg) |
| 247 | { |
| 248 | if (m_frameRateVariation != arg) { |
| 249 | m_frameRateVariation = arg; |
| 250 | Q_EMIT frameRateVariationChanged(arg); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void setFrameDuration(int arg) |
| 255 | { |
| 256 | if (m_frameDuration != arg) { |
| 257 | m_frameDuration = arg; |
| 258 | Q_EMIT frameDurationChanged(arg); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void setFrameDurationVariation(int arg) |
| 263 | { |
| 264 | if (m_frameDurationVariation != arg) { |
| 265 | m_frameDurationVariation = arg; |
| 266 | Q_EMIT frameDurationVariationChanged(arg); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void setFrameSync(bool arg) |
| 271 | { |
| 272 | if (m_frameSync != arg) { |
| 273 | m_frameSync = arg; |
| 274 | Q_EMIT frameSyncChanged(arg); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | private Q_SLOTS: |
| 279 | void startImageLoading(); |
| 280 | |
| 281 | private: |
| 282 | friend class QQuickImageParticle; |
| 283 | //friend class QQuickSpriteSequence; |
| 284 | friend class QQuickAnimatedSprite; |
| 285 | friend class QQuickSpriteEngine; |
| 286 | friend class QQuickStochasticEngine; |
| 287 | |
| 288 | int m_generatedCount; |
| 289 | int m_framesPerRow; |
| 290 | int m_rowY; |
| 291 | int m_rowStartX; |
| 292 | |
| 293 | QUrl m_source; |
| 294 | bool m_reverse; |
| 295 | int m_frameHeight; |
| 296 | int m_frameWidth; |
| 297 | int m_frames; |
| 298 | int m_frameX; |
| 299 | int m_frameY; |
| 300 | qreal m_frameRate; |
| 301 | qreal m_frameRateVariation; |
| 302 | int m_frameDuration; |
| 303 | int m_frameDurationVariation; |
| 304 | bool m_frameSync; |
| 305 | qreal m_devicePixelRatio; |
| 306 | QQuickPixmap m_pix; |
| 307 | }; |
| 308 | |
| 309 | QT_END_NAMESPACE |
| 310 | |
| 311 | #endif // QQUICKSPRITE_P_H |
| 312 |
