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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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