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 "qspritegrid.h" |
5 | #include "qspritegrid_p.h" |
6 | |
7 | #include <Qt3DRender/qabstracttexture.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | using namespace Qt3DCore; |
12 | |
13 | namespace Qt3DExtras { |
14 | |
15 | QSpriteGridPrivate::QSpriteGridPrivate() |
16 | : QAbstractSpriteSheetPrivate() |
17 | , m_numColumns(1) |
18 | , m_numRows(1) |
19 | { |
20 | } |
21 | |
22 | int QSpriteGridPrivate::maxIndex() const |
23 | { |
24 | return m_numColumns * m_numRows - 1; |
25 | } |
26 | |
27 | void QSpriteGridPrivate::updateSizes() |
28 | { |
29 | Q_Q(QSpriteGrid); |
30 | if (m_texture && m_numColumns && m_numRows) { |
31 | m_textureSize = QSize(m_texture->width(), m_texture->height()); |
32 | m_cellSize = QSizeF((float) m_texture->width() / m_numColumns, (float) m_texture->height() / m_numRows); |
33 | } else { |
34 | m_textureSize = QSize(); |
35 | m_cellSize = QSizeF(); |
36 | } |
37 | |
38 | if (m_cellSize.isEmpty() || m_numColumns == 0 || m_numRows == 0) { |
39 | if (m_currentIndex != -1) { |
40 | m_currentIndex = -1; |
41 | emit q->currentIndexChanged(currentIndex: m_currentIndex); |
42 | } |
43 | m_textureTransform.setToIdentity(); |
44 | emit q->textureTransformChanged(textureTransform: m_textureTransform); |
45 | return; |
46 | } |
47 | |
48 | if (m_currentIndex == -1) { |
49 | m_currentIndex = 0; |
50 | emit q->currentIndexChanged(currentIndex: m_currentIndex); |
51 | } |
52 | updateTransform(); |
53 | } |
54 | |
55 | void QSpriteGridPrivate::updateTransform() |
56 | { |
57 | Q_Q(QSpriteGrid); |
58 | const float xScale = (float) m_cellSize.width() / (float) m_textureSize.width(); |
59 | const float yScale = (float) m_cellSize.height() / (float) m_textureSize.height(); |
60 | |
61 | const int currentRow = m_currentIndex / m_numColumns; |
62 | const int currentColumn = m_currentIndex % m_numColumns; |
63 | const float xTranslate = currentColumn * xScale; |
64 | const float yTranslate = currentRow * yScale; |
65 | |
66 | m_textureTransform.setToIdentity(); |
67 | m_textureTransform(0, 0) = xScale; |
68 | m_textureTransform(1, 1) = yScale; |
69 | m_textureTransform(0, 2) = xTranslate; |
70 | m_textureTransform(1, 2) = yTranslate; |
71 | emit q->textureTransformChanged(textureTransform: m_textureTransform); |
72 | } |
73 | |
74 | /*! |
75 | Constructs a new QSpriteGrid instance with parent object \a parent. |
76 | */ |
77 | QSpriteGrid::QSpriteGrid(QNode *parent) |
78 | : QAbstractSpriteSheet(*new QSpriteGridPrivate, parent) |
79 | { |
80 | } |
81 | |
82 | QSpriteGrid::~QSpriteGrid() |
83 | { |
84 | } |
85 | |
86 | int QSpriteGrid::rows() const |
87 | { |
88 | Q_D(const QSpriteGrid); |
89 | return d->m_numRows; |
90 | } |
91 | |
92 | void QSpriteGrid::setRows(int rows) |
93 | { |
94 | Q_D(QSpriteGrid); |
95 | if (d->m_numRows != rows) { |
96 | d->m_numRows = rows; |
97 | d->updateSizes(); |
98 | emit rowsChanged(rows); |
99 | } |
100 | } |
101 | |
102 | int QSpriteGrid::columns() const |
103 | { |
104 | Q_D(const QSpriteGrid); |
105 | return d->m_numColumns; |
106 | } |
107 | |
108 | void QSpriteGrid::setColumns(int columns) |
109 | { |
110 | Q_D(QSpriteGrid); |
111 | if (d->m_numColumns != columns) { |
112 | d->m_numColumns = columns; |
113 | d->updateSizes(); |
114 | emit columnsChanged(columns); |
115 | } |
116 | } |
117 | |
118 | } // namespace Qt3DExtras |
119 | |
120 | QT_END_NAMESPACE |
121 | |
122 | #include "moc_qspritegrid.cpp" |
123 |