| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D 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 | #include "qspritesheet.h" |
| 41 | #include "qspritesheetitem.h" |
| 42 | #include "qspritesheet_p.h" |
| 43 | |
| 44 | #include <Qt3DRender/qabstracttexture.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | using namespace Qt3DCore; |
| 49 | |
| 50 | namespace Qt3DExtras { |
| 51 | |
| 52 | QSpriteSheetPrivate::() |
| 53 | : QAbstractSpriteSheetPrivate() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | int QSpriteSheetPrivate::() const |
| 58 | { |
| 59 | return m_sprites.count() - 1; |
| 60 | } |
| 61 | |
| 62 | void QSpriteSheetPrivate::() |
| 63 | { |
| 64 | Q_Q(QSpriteSheet); |
| 65 | if (m_texture) |
| 66 | m_textureSize = QSize(m_texture->width(), m_texture->height()); |
| 67 | else |
| 68 | m_textureSize = QSize(); |
| 69 | |
| 70 | if (m_textureSize.isEmpty() || m_sprites.isEmpty()) { |
| 71 | if (m_currentIndex != -1) { |
| 72 | m_currentIndex = -1; |
| 73 | emit q->currentIndexChanged(currentIndex: m_currentIndex); |
| 74 | } |
| 75 | m_textureTransform.setToIdentity(); |
| 76 | emit q->textureTransformChanged(textureTransform: m_textureTransform); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (m_currentIndex < 0 || m_currentIndex >= m_sprites.size()) { |
| 81 | m_currentIndex = 0; |
| 82 | emit q->currentIndexChanged(currentIndex: m_currentIndex); |
| 83 | } |
| 84 | updateTransform(); |
| 85 | } |
| 86 | |
| 87 | void QSpriteSheetPrivate::() |
| 88 | { |
| 89 | Q_Q(QSpriteSheet); |
| 90 | if (m_currentIndex < 0 || m_currentIndex >= m_sprites.size()) |
| 91 | return; |
| 92 | |
| 93 | const QSpriteSheetItem *r = m_sprites.at(i: m_currentIndex); |
| 94 | const float xScale = static_cast<float>(r->width()) / static_cast<float>(m_textureSize.width()); |
| 95 | const float yScale = static_cast<float>(r->height()) / static_cast<float>(m_textureSize.height()); |
| 96 | |
| 97 | const float xTranslate = static_cast<float>(r->x()) / static_cast<float>(m_textureSize.width()); |
| 98 | const float yTranslate = static_cast<float>(r->y()) / static_cast<float>(m_textureSize.height()); |
| 99 | |
| 100 | m_textureTransform.setToIdentity(); |
| 101 | m_textureTransform(0, 0) = xScale; |
| 102 | m_textureTransform(1, 1) = yScale; |
| 103 | m_textureTransform(0, 2) = xTranslate; |
| 104 | m_textureTransform(1, 2) = yTranslate; |
| 105 | emit q->textureTransformChanged(textureTransform: m_textureTransform); |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | Constructs a new QSpriteSheet instance with parent object \a parent. |
| 110 | */ |
| 111 | QSpriteSheet::(QNode *parent) |
| 112 | : QAbstractSpriteSheet(*new QSpriteSheetPrivate, parent) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | QSpriteSheet::() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | QVector<QSpriteSheetItem *> QSpriteSheet::() const |
| 121 | { |
| 122 | Q_D(const QSpriteSheet); |
| 123 | return d->m_sprites; |
| 124 | } |
| 125 | |
| 126 | QSpriteSheetItem *QSpriteSheet::(int x, int y, int width, int height) |
| 127 | { |
| 128 | QSpriteSheetItem *item = new QSpriteSheetItem(this); |
| 129 | item->setX(x); |
| 130 | item->setX(y); |
| 131 | item->setWidth(width); |
| 132 | item->setHeight(height); |
| 133 | addSprite(sprite: item); |
| 134 | return item; |
| 135 | } |
| 136 | |
| 137 | void QSpriteSheet::(QSpriteSheetItem *sprite) |
| 138 | { |
| 139 | Q_ASSERT(sprite); |
| 140 | Q_D(QSpriteSheet); |
| 141 | if (!d->m_sprites.contains(t: sprite)) { |
| 142 | d->m_sprites << sprite; |
| 143 | |
| 144 | // Ensures proper bookkeeping |
| 145 | d->registerDestructionHelper(node: sprite, func: &QSpriteSheet::removeSprite, d->m_sprites); |
| 146 | if (!sprite->parent()) |
| 147 | sprite->setParent(this); |
| 148 | |
| 149 | emit spritesChanged(sprites: d->m_sprites); |
| 150 | d->updateSizes(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void QSpriteSheet::(QSpriteSheetItem *sprite) |
| 155 | { |
| 156 | Q_ASSERT(sprite); |
| 157 | Q_D(QSpriteSheet); |
| 158 | d->m_sprites.removeOne(t: sprite); |
| 159 | // Remove bookkeeping connection |
| 160 | d->unregisterDestructionHelper(node: sprite); |
| 161 | } |
| 162 | |
| 163 | void QSpriteSheet::(QVector<QSpriteSheetItem *> sprites) |
| 164 | { |
| 165 | Q_D(QSpriteSheet); |
| 166 | d->m_sprites = sprites; |
| 167 | emit spritesChanged(sprites); |
| 168 | d->updateSizes(); |
| 169 | } |
| 170 | |
| 171 | } // namespace Qt3DExtras |
| 172 | |
| 173 | QT_END_NAMESPACE |
| 174 | |