| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QQUICKSPRITE_P_H |
| 41 | #define QQUICKSPRITE_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <private/qtquickglobal_p.h> |
| 55 | |
| 56 | QT_REQUIRE_CONFIG(quick_sprite); |
| 57 | |
| 58 | #include <QObject> |
| 59 | #include <QUrl> |
| 60 | #include <QVariantMap> |
| 61 | #include <QQmlListProperty> |
| 62 | #include <QtQuick/private/qquickpixmapcache_p.h> |
| 63 | #include "qquickspriteengine_p.h" |
| 64 | #include <QDebug> |
| 65 | |
| 66 | QT_BEGIN_NAMESPACE |
| 67 | |
| 68 | // exported, since it's used in QtQuickParticles |
| 69 | class Q_QUICK_EXPORT QQuickSprite : public QQuickStochasticState |
| 70 | { |
| 71 | Q_OBJECT |
| 72 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
| 73 | //Renderers have to query this hint when advancing frames |
| 74 | Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged) |
| 75 | Q_PROPERTY(bool frameSync READ frameSync WRITE setFrameSync NOTIFY frameSyncChanged) |
| 76 | Q_PROPERTY(int frames READ frames WRITE setFrames NOTIFY frameCountChanged) |
| 77 | Q_PROPERTY(int frameCount READ frameCount WRITE setFrameCount NOTIFY frameCountChanged) |
| 78 | //If frame height or width is not specified, it is assumed to be a single long row of square frames. |
| 79 | //Otherwise, it can be multiple contiguous rows, when one row runs out the next will be used. |
| 80 | Q_PROPERTY(int frameHeight READ frameHeight WRITE setFrameHeight NOTIFY frameHeightChanged) |
| 81 | Q_PROPERTY(int frameWidth READ frameWidth WRITE setFrameWidth NOTIFY frameWidthChanged) |
| 82 | Q_PROPERTY(int frameX READ frameX WRITE setFrameX NOTIFY frameXChanged) |
| 83 | Q_PROPERTY(int frameY READ frameY WRITE setFrameY NOTIFY frameYChanged) |
| 84 | //Precedence order: frameRate, frameDuration, duration |
| 85 | Q_PROPERTY(qreal frameRate READ frameRate WRITE setFrameRate NOTIFY frameRateChanged RESET resetFrameRate) |
| 86 | Q_PROPERTY(qreal frameRateVariation READ frameRateVariation WRITE setFrameRateVariation NOTIFY frameRateVariationChanged) |
| 87 | Q_PROPERTY(int frameDuration READ frameDuration WRITE setFrameDuration NOTIFY frameDurationChanged RESET resetFrameDuration) |
| 88 | Q_PROPERTY(int frameDurationVariation READ frameDurationVariation WRITE setFrameDurationVariation NOTIFY frameDurationVariationChanged) |
| 89 | QML_NAMED_ELEMENT(Sprite) |
| 90 | |
| 91 | public: |
| 92 | explicit QQuickSprite(QObject *parent = nullptr); |
| 93 | ~QQuickSprite() override; |
| 94 | |
| 95 | QUrl source() const |
| 96 | { |
| 97 | return m_source; |
| 98 | } |
| 99 | |
| 100 | int frameHeight() const |
| 101 | { |
| 102 | return m_frameHeight; |
| 103 | } |
| 104 | |
| 105 | int frameWidth() const |
| 106 | { |
| 107 | return m_frameWidth; |
| 108 | } |
| 109 | |
| 110 | bool reverse() const |
| 111 | { |
| 112 | return m_reverse; |
| 113 | } |
| 114 | |
| 115 | int frames() const |
| 116 | { |
| 117 | return m_frames; |
| 118 | } |
| 119 | |
| 120 | int frameCount() const |
| 121 | { |
| 122 | return m_frames; |
| 123 | } |
| 124 | |
| 125 | int frameX() const |
| 126 | { |
| 127 | return m_frameX; |
| 128 | } |
| 129 | |
| 130 | int frameY() const |
| 131 | { |
| 132 | return m_frameY; |
| 133 | } |
| 134 | |
| 135 | void resetFrameRate() |
| 136 | { |
| 137 | setFrameRate(-1); |
| 138 | } |
| 139 | |
| 140 | qreal frameRate() const |
| 141 | { |
| 142 | return m_frameRate; |
| 143 | } |
| 144 | |
| 145 | qreal frameRateVariation() const |
| 146 | { |
| 147 | return m_frameRateVariation; |
| 148 | } |
| 149 | |
| 150 | void resetFrameDuration() |
| 151 | { |
| 152 | setFrameDuration(-1); |
| 153 | } |
| 154 | |
| 155 | int frameDuration() const |
| 156 | { |
| 157 | return m_frameDuration; |
| 158 | } |
| 159 | |
| 160 | int frameDurationVariation() const |
| 161 | { |
| 162 | return m_frameDurationVariation; |
| 163 | } |
| 164 | |
| 165 | int variedDuration() const override; |
| 166 | |
| 167 | bool frameSync() const |
| 168 | { |
| 169 | return m_frameSync; |
| 170 | } |
| 171 | |
| 172 | void setDevicePixelRatio(qreal dpr) |
| 173 | { |
| 174 | m_devicePixelRatio = dpr; |
| 175 | } |
| 176 | |
| 177 | qreal devicePixelRatio() const |
| 178 | { |
| 179 | return m_devicePixelRatio; |
| 180 | } |
| 181 | |
| 182 | Q_SIGNALS: |
| 183 | |
| 184 | void sourceChanged(QUrl arg); |
| 185 | |
| 186 | void frameHeightChanged(int arg); |
| 187 | |
| 188 | void frameWidthChanged(int arg); |
| 189 | |
| 190 | void reverseChanged(bool arg); |
| 191 | |
| 192 | void frameCountChanged(int arg); |
| 193 | |
| 194 | void frameXChanged(int arg); |
| 195 | |
| 196 | void frameYChanged(int arg); |
| 197 | |
| 198 | void frameRateChanged(qreal arg); |
| 199 | |
| 200 | void frameRateVariationChanged(qreal arg); |
| 201 | |
| 202 | void frameDurationChanged(int arg); |
| 203 | |
| 204 | void frameDurationVariationChanged(int arg); |
| 205 | |
| 206 | void frameSyncChanged(bool arg); |
| 207 | |
| 208 | public Q_SLOTS: |
| 209 | |
| 210 | void setSource(QUrl arg) |
| 211 | { |
| 212 | if (m_source != arg) { |
| 213 | m_source = arg; |
| 214 | Q_EMIT sourceChanged(arg); |
| 215 | startImageLoading(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void setFrameHeight(int arg) |
| 220 | { |
| 221 | if (m_frameHeight != arg) { |
| 222 | m_frameHeight = arg; |
| 223 | Q_EMIT frameHeightChanged(arg); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void setFrameWidth(int arg) |
| 228 | { |
| 229 | if (m_frameWidth != arg) { |
| 230 | m_frameWidth = arg; |
| 231 | Q_EMIT frameWidthChanged(arg); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void setReverse(bool arg) |
| 236 | { |
| 237 | if (m_reverse != arg) { |
| 238 | m_reverse = arg; |
| 239 | Q_EMIT reverseChanged(arg); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void setFrames(int arg) |
| 244 | { |
| 245 | qWarning() << "Sprite::frames has been renamed Sprite::frameCount" ; |
| 246 | setFrameCount(arg); |
| 247 | } |
| 248 | |
| 249 | void setFrameCount(int arg) |
| 250 | { |
| 251 | if (m_frames != arg) { |
| 252 | m_frames = arg; |
| 253 | Q_EMIT frameCountChanged(arg); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void setFrameX(int arg) |
| 258 | { |
| 259 | if (m_frameX != arg) { |
| 260 | m_frameX = arg; |
| 261 | Q_EMIT frameXChanged(arg); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void setFrameY(int arg) |
| 266 | { |
| 267 | if (m_frameY != arg) { |
| 268 | m_frameY = arg; |
| 269 | Q_EMIT frameYChanged(arg); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void setFrameRate(qreal arg) |
| 274 | { |
| 275 | if (m_frameRate != arg) { |
| 276 | m_frameRate = arg; |
| 277 | Q_EMIT frameRateChanged(arg); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void setFrameRateVariation(qreal arg) |
| 282 | { |
| 283 | if (m_frameRateVariation != arg) { |
| 284 | m_frameRateVariation = arg; |
| 285 | Q_EMIT frameRateVariationChanged(arg); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void setFrameDuration(int arg) |
| 290 | { |
| 291 | if (m_frameDuration != arg) { |
| 292 | m_frameDuration = arg; |
| 293 | Q_EMIT frameDurationChanged(arg); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void setFrameDurationVariation(int arg) |
| 298 | { |
| 299 | if (m_frameDurationVariation != arg) { |
| 300 | m_frameDurationVariation = arg; |
| 301 | Q_EMIT frameDurationVariationChanged(arg); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void setFrameSync(bool arg) |
| 306 | { |
| 307 | if (m_frameSync != arg) { |
| 308 | m_frameSync = arg; |
| 309 | Q_EMIT frameSyncChanged(arg); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private Q_SLOTS: |
| 314 | void startImageLoading(); |
| 315 | |
| 316 | private: |
| 317 | friend class QQuickImageParticle; |
| 318 | //friend class QQuickSpriteSequence; |
| 319 | friend class QQuickAnimatedSprite; |
| 320 | friend class QQuickSpriteEngine; |
| 321 | friend class QQuickStochasticEngine; |
| 322 | |
| 323 | int m_generatedCount; |
| 324 | int m_framesPerRow; |
| 325 | int m_rowY; |
| 326 | int m_rowStartX; |
| 327 | |
| 328 | QUrl m_source; |
| 329 | bool m_reverse; |
| 330 | int m_frameHeight; |
| 331 | int m_frameWidth; |
| 332 | int m_frames; |
| 333 | int m_frameX; |
| 334 | int m_frameY; |
| 335 | qreal m_frameRate; |
| 336 | qreal m_frameRateVariation; |
| 337 | int m_frameDuration; |
| 338 | int m_frameDurationVariation; |
| 339 | bool m_frameSync; |
| 340 | qreal m_devicePixelRatio; |
| 341 | QQuickPixmap m_pix; |
| 342 | }; |
| 343 | |
| 344 | QT_END_NAMESPACE |
| 345 | |
| 346 | #endif // QQUICKSPRITE_P_H |
| 347 | |