1// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qspritesheet.h"
5#include "qspritesheetitem.h"
6#include "qspritesheet_p.h"
7
8#include <Qt3DRender/qabstracttexture.h>
9
10QT_BEGIN_NAMESPACE
11
12using namespace Qt3DCore;
13
14namespace Qt3DExtras {
15
16QSpriteSheetPrivate::QSpriteSheetPrivate()
17 : QAbstractSpriteSheetPrivate()
18{
19}
20
21int QSpriteSheetPrivate::maxIndex() const
22{
23 return m_sprites.size() - 1;
24}
25
26void QSpriteSheetPrivate::updateSizes()
27{
28 Q_Q(QSpriteSheet);
29 if (m_texture)
30 m_textureSize = QSize(m_texture->width(), m_texture->height());
31 else
32 m_textureSize = QSize();
33
34 if (m_textureSize.isEmpty() || m_sprites.isEmpty()) {
35 if (m_currentIndex != -1) {
36 m_currentIndex = -1;
37 emit q->currentIndexChanged(currentIndex: m_currentIndex);
38 }
39 m_textureTransform.setToIdentity();
40 emit q->textureTransformChanged(textureTransform: m_textureTransform);
41 return;
42 }
43
44 if (m_currentIndex < 0 || m_currentIndex >= m_sprites.size()) {
45 m_currentIndex = 0;
46 emit q->currentIndexChanged(currentIndex: m_currentIndex);
47 }
48 updateTransform();
49}
50
51void QSpriteSheetPrivate::updateTransform()
52{
53 Q_Q(QSpriteSheet);
54 if (m_currentIndex < 0 || m_currentIndex >= m_sprites.size())
55 return;
56
57 const QSpriteSheetItem *r = m_sprites.at(i: m_currentIndex);
58 const float xScale = static_cast<float>(r->width()) / static_cast<float>(m_textureSize.width());
59 const float yScale = static_cast<float>(r->height()) / static_cast<float>(m_textureSize.height());
60
61 const float xTranslate = static_cast<float>(r->x()) / static_cast<float>(m_textureSize.width());
62 const float yTranslate = static_cast<float>(r->y()) / static_cast<float>(m_textureSize.height());
63
64 m_textureTransform.setToIdentity();
65 m_textureTransform(0, 0) = xScale;
66 m_textureTransform(1, 1) = yScale;
67 m_textureTransform(0, 2) = xTranslate;
68 m_textureTransform(1, 2) = yTranslate;
69 emit q->textureTransformChanged(textureTransform: m_textureTransform);
70}
71
72/*!
73 Constructs a new QSpriteSheet instance with parent object \a parent.
74 */
75QSpriteSheet::QSpriteSheet(QNode *parent)
76 : QAbstractSpriteSheet(*new QSpriteSheetPrivate, parent)
77{
78}
79
80QSpriteSheet::~QSpriteSheet()
81{
82}
83
84QList<QSpriteSheetItem *> QSpriteSheet::sprites() const
85{
86 Q_D(const QSpriteSheet);
87 return d->m_sprites;
88}
89
90QSpriteSheetItem *QSpriteSheet::addSprite(int x, int y, int width, int height)
91{
92 QSpriteSheetItem *item = new QSpriteSheetItem(this);
93 item->setX(x);
94 item->setX(y);
95 item->setWidth(width);
96 item->setHeight(height);
97 addSprite(sprite: item);
98 return item;
99}
100
101void QSpriteSheet::addSprite(QSpriteSheetItem *sprite)
102{
103 Q_ASSERT(sprite);
104 Q_D(QSpriteSheet);
105 if (!d->m_sprites.contains(t: sprite)) {
106 d->m_sprites << sprite;
107
108 // Ensures proper bookkeeping
109 d->registerDestructionHelper(node: sprite, func: &QSpriteSheet::removeSprite, d->m_sprites);
110 if (!sprite->parent())
111 sprite->setParent(this);
112
113 emit spritesChanged(sprites: d->m_sprites);
114 d->updateSizes();
115 }
116}
117
118void QSpriteSheet::removeSprite(QSpriteSheetItem *sprite)
119{
120 Q_ASSERT(sprite);
121 Q_D(QSpriteSheet);
122 d->m_sprites.removeOne(t: sprite);
123 // Remove bookkeeping connection
124 d->unregisterDestructionHelper(node: sprite);
125}
126
127void QSpriteSheet::setSprites(QList<QSpriteSheetItem *> sprites)
128{
129 Q_D(QSpriteSheet);
130 d->m_sprites = sprites;
131 emit spritesChanged(sprites);
132 d->updateSizes();
133}
134
135} // namespace Qt3DExtras
136
137QT_END_NAMESPACE
138
139#include "moc_qspritesheet.cpp"
140

source code of qt3d/src/extras/defaults/qspritesheet.cpp