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 "qabstractspritesheet.h"
5#include "qabstractspritesheet_p.h"
6
7#include <Qt3DRender/qabstracttexture.h>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt3DCore;
12
13namespace Qt3DExtras {
14
15QAbstractSpriteSheetPrivate::QAbstractSpriteSheetPrivate()
16 : QNodePrivate()
17 , m_texture(nullptr)
18 , m_currentIndex(-1)
19{
20}
21
22void QAbstractSpriteSheetPrivate::init()
23{
24 m_textureTransform.setToIdentity();
25}
26
27void QAbstractSpriteSheetPrivate::updateTexture(Qt3DRender::QAbstractTexture *texture)
28{
29 if (m_texture) {
30 disconnect(sender: m_texture, signal: &Qt3DRender::QAbstractTexture::widthChanged,
31 receiverPrivate: this, slot: &QAbstractSpriteSheetPrivate::updateSizes);
32 disconnect(sender: m_texture, signal: &Qt3DRender::QAbstractTexture::heightChanged,
33 receiverPrivate: this, slot: &QAbstractSpriteSheetPrivate::updateSizes);
34 }
35 m_texture = texture;
36 if (m_texture) {
37 connect(sender: m_texture, signal: &Qt3DRender::QAbstractTexture::widthChanged,
38 receiverPrivate: this, slot: &QAbstractSpriteSheetPrivate::updateSizes);
39 connect(sender: m_texture, signal: &Qt3DRender::QAbstractTexture::heightChanged,
40 receiverPrivate: this, slot: &QAbstractSpriteSheetPrivate::updateSizes);
41 }
42}
43
44void QAbstractSpriteSheetPrivate::updateIndex(int newIndex)
45{
46 Q_Q(QAbstractSpriteSheet);
47 if (newIndex > maxIndex())
48 newIndex = 0;
49
50 m_currentIndex = newIndex;
51 emit q->currentIndexChanged(currentIndex: newIndex);
52 updateTransform();
53}
54
55QAbstractSpriteSheet::QAbstractSpriteSheet(QAbstractSpriteSheetPrivate &dd, QNode *parent)
56 : Qt3DCore::QNode(dd, parent)
57{
58 Q_D(QAbstractSpriteSheet);
59 d->init();
60}
61
62QAbstractSpriteSheet::~QAbstractSpriteSheet()
63{
64}
65
66/*!
67 \property Qt3DExtras::QAbstractSpriteSheet::texture
68
69 Holds the current texture used by the material.
70*/
71Qt3DRender::QAbstractTexture *QAbstractSpriteSheet::texture() const
72{
73 Q_D(const QAbstractSpriteSheet);
74 return d->m_texture;
75}
76
77void QAbstractSpriteSheet::setTexture(Qt3DRender::QAbstractTexture *texture)
78{
79 Q_D(QAbstractSpriteSheet);
80 if (d->m_texture != texture) {
81 d->updateTexture(texture);
82 d->updateSizes();
83 emit textureChanged(texture: d->m_texture);
84 }
85}
86
87QMatrix3x3 QAbstractSpriteSheet::textureTransform() const
88{
89 Q_D(const QAbstractSpriteSheet);
90 return d->m_textureTransform;
91}
92
93int QAbstractSpriteSheet::currentIndex() const
94{
95 Q_D(const QAbstractSpriteSheet);
96 return d->m_currentIndex;
97}
98
99void QAbstractSpriteSheet::setCurrentIndex(int currentIndex)
100{
101 Q_D(QAbstractSpriteSheet);
102 d->updateIndex(newIndex: currentIndex);
103}
104
105} // namespace Qt3DExtras
106
107QT_END_NAMESPACE
108
109#include "moc_qabstractspritesheet.cpp"
110

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