Kirigami2

shadowedtexturenode.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "shadowedtexturenode.h"
8 
9 #include "shadowedbordertexturematerial.h"
10 
11 template<typename T>
12 inline void preprocessTexture(QSGMaterial *material, QSGTextureProvider *provider)
13 {
14  auto m = static_cast<T *>(material);
15  // Since we handle texture coordinates differently in the shader, we
16  // need to remove the texture from the atlas for now.
17  if (provider->texture()->isAtlasTexture()) {
18  // Blegh, I have no idea why "removedFromAtlas" doesn't just return
19  // the texture when it's not an atlas.
20  m->textureSource = provider->texture()->removedFromAtlas();
21  } else {
22  m->textureSource = provider->texture();
23  }
24  if (QSGDynamicTexture *dynamic_texture = qobject_cast<QSGDynamicTexture *>(m->textureSource)) {
25  dynamic_texture->updateTexture();
26  }
27 }
28 
29 ShadowedTextureNode::ShadowedTextureNode()
31 {
33 }
34 
35 ShadowedTextureNode::~ShadowedTextureNode()
36 {
37  QObject::disconnect(m_textureChangeConnectionHandle);
38 }
39 
40 void ShadowedTextureNode::setTextureSource(QSGTextureProvider *source)
41 {
42  if (m_textureSource == source) {
43  return;
44  }
45 
46  if (m_textureSource) {
47  m_textureSource->disconnect();
48  }
49 
50  m_textureSource = source;
51  m_textureChangeConnectionHandle = QObject::connect(m_textureSource.data(), &QSGTextureProvider::textureChanged, [this] {
52  markDirty(QSGNode::DirtyMaterial);
53  });
55 }
56 
57 void ShadowedTextureNode::preprocess()
58 {
59  if (m_textureSource && m_material && m_textureSource->texture()) {
60  if (m_material->type() == borderlessMaterialType()) {
61  preprocessTexture<ShadowedTextureMaterial>(m_material, m_textureSource);
62  } else {
63  preprocessTexture<ShadowedBorderTextureMaterial>(m_material, m_textureSource);
64  }
65  }
66 }
67 
68 ShadowedRectangleMaterial *ShadowedTextureNode::createBorderlessMaterial()
69 {
70  return new ShadowedTextureMaterial{};
71 }
72 
73 ShadowedBorderRectangleMaterial *ShadowedTextureNode::createBorderMaterial()
74 {
75  return new ShadowedBorderTextureMaterial{};
76 }
77 
78 QSGMaterialType *ShadowedTextureNode::borderlessMaterialType()
79 {
80  return &ShadowedTextureMaterial::staticType;
81 }
82 
83 QSGMaterialType *ShadowedTextureNode::borderMaterialType()
84 {
85  return &ShadowedBorderTextureMaterial::staticType;
86 }
Scene graph node for a shadowed rectangle.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void setFlag(QSGMaterial::Flags flags, bool on)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A material rendering a rectangle with a shadow.
A material rendering a rectangle with a shadow and a border.
virtual QSGTexture * texture() const const=0
virtual QSGTexture * removedFromAtlas() const const
void markDirty(QSGNode::DirtyState bits)
A material rendering a rectangle with a shadow.
virtual bool isAtlasTexture() const const
T * data() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.