Changeset View
Changeset View
Standalone View
Standalone View
src/scenegraph/shadowedtexturenode.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
3 | * | ||||
4 | * SPDX-License-Identifier: LGPL-2.0-or-later | ||||
5 | */ | ||||
6 | | ||||
7 | #include "shadowedtexturenode.h" | ||||
8 | | ||||
9 | #include <QSGTextureProvider> | ||||
10 | #include "shadowedtexturematerial.h" | ||||
11 | #include "shadowedbordertexturematerial.h" | ||||
12 | | ||||
13 | template <typename T> | ||||
14 | inline void preprocessTexture(QSGMaterial *material, QSGTextureProvider *provider) | ||||
15 | { | ||||
16 | auto m = static_cast<T*>(material); | ||||
17 | // Since we handle texture coordinates differently in the shader, we | ||||
18 | // need to remove the texture from the atlas for now. | ||||
19 | if (provider->texture()->isAtlasTexture()) { | ||||
20 | // Blegh, I have no idea why "removedFromAtlas" doesn't just return | ||||
21 | // the texture when it's not an atlas. | ||||
22 | m->textureSource = provider->texture()->removedFromAtlas(); | ||||
23 | } else { | ||||
24 | m->textureSource = provider->texture(); | ||||
25 | } | ||||
26 | if (QSGDynamicTexture *dynamic_texture = qobject_cast<QSGDynamicTexture *>(m->textureSource)) { | ||||
27 | dynamic_texture->updateTexture(); | ||||
28 | } | ||||
29 | } | ||||
30 | | ||||
31 | ShadowedTextureNode::ShadowedTextureNode() | ||||
32 | : ShadowedRectangleNode() | ||||
33 | { | ||||
34 | setFlag(QSGNode::UsePreprocess); | ||||
35 | } | ||||
36 | | ||||
37 | void ShadowedTextureNode::setTextureSource(QSGTextureProvider *source) | ||||
38 | { | ||||
39 | if (m_textureSource == source) { | ||||
40 | return; | ||||
41 | } | ||||
42 | | ||||
43 | if (m_textureSource) { | ||||
44 | m_textureSource->disconnect(); | ||||
45 | } | ||||
46 | | ||||
47 | m_textureSource = source; | ||||
48 | QObject::connect(m_textureSource.data(), &QSGTextureProvider::textureChanged, [this]() { markDirty(QSGNode::DirtyMaterial); }); | ||||
49 | markDirty(QSGNode::DirtyMaterial); | ||||
50 | } | ||||
51 | | ||||
52 | void ShadowedTextureNode::preprocess() | ||||
53 | { | ||||
54 | if (m_textureSource && m_material) { | ||||
55 | if (m_material->type() == borderlessMaterialType()) { | ||||
56 | preprocessTexture<ShadowedTextureMaterial>(m_material, m_textureSource); | ||||
57 | } else { | ||||
58 | preprocessTexture<ShadowedBorderTextureMaterial>(m_material, m_textureSource); | ||||
59 | } | ||||
60 | } | ||||
61 | } | ||||
62 | | ||||
63 | ShadowedRectangleMaterial *ShadowedTextureNode::createBorderlessMaterial() | ||||
64 | { | ||||
65 | return new ShadowedTextureMaterial{}; | ||||
66 | } | ||||
67 | | ||||
68 | ShadowedBorderRectangleMaterial *ShadowedTextureNode::createBorderMaterial() | ||||
69 | { | ||||
70 | return new ShadowedBorderTextureMaterial{}; | ||||
71 | } | ||||
72 | | ||||
73 | QSGMaterialType *ShadowedTextureNode::borderlessMaterialType() | ||||
74 | { | ||||
75 | return &ShadowedTextureMaterial::staticType; | ||||
76 | } | ||||
77 | | ||||
78 | QSGMaterialType *ShadowedTextureNode::borderMaterialType() | ||||
79 | { | ||||
80 | return &ShadowedBorderTextureMaterial::staticType; | ||||
81 | } |