Changeset View
Changeset View
Standalone View
Standalone View
src/scenegraph/shadowedrectanglenode.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 "shadowedrectanglenode.h" | ||||
8 | #include "shadowedrectanglematerial.h" | ||||
9 | #include "shadowedborderrectanglematerial.h" | ||||
10 | | ||||
11 | QColor premultiply(const QColor &color) | ||||
12 | { | ||||
13 | return QColor::fromRgbF( | ||||
14 | color.redF() * color.alphaF(), | ||||
15 | color.greenF() * color.greenF(), | ||||
16 | color.blueF() * color.blueF(), | ||||
17 | color.alphaF() | ||||
18 | ); | ||||
19 | } | ||||
20 | | ||||
21 | ShadowedRectangleNode::ShadowedRectangleNode() | ||||
22 | { | ||||
23 | m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_TexturedPoint2D(), 4}; | ||||
24 | setGeometry(m_geometry); | ||||
25 | | ||||
26 | m_material = new ShadowedRectangleMaterial{}; | ||||
27 | setMaterial(m_material); | ||||
28 | | ||||
29 | setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); | ||||
30 | } | ||||
31 | | ||||
32 | void ShadowedRectangleNode::setRect(const QRectF& rect) | ||||
33 | { | ||||
34 | if (rect == m_rect) { | ||||
35 | return; | ||||
36 | } | ||||
37 | | ||||
38 | m_rect = rect; | ||||
39 | | ||||
40 | QVector2D newAspect{1.0, 1.0}; | ||||
41 | if (m_rect.width() >= m_rect.height()) { | ||||
42 | newAspect.setX(m_rect.width() / m_rect.height()); | ||||
43 | } else { | ||||
44 | newAspect.setY(m_rect.height() / m_rect.width()); | ||||
45 | } | ||||
46 | | ||||
47 | if (m_material->aspect != newAspect) { | ||||
48 | m_material->aspect = newAspect; | ||||
49 | markDirty(QSGNode::DirtyMaterial); | ||||
50 | m_aspect = newAspect; | ||||
51 | } | ||||
52 | } | ||||
53 | | ||||
54 | void ShadowedRectangleNode::setSize(qreal size) | ||||
55 | { | ||||
56 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
57 | float uniformSize = (size / minDimension) * 2.0; | ||||
58 | | ||||
59 | if (!qFuzzyCompare(m_material->size, uniformSize)) { | ||||
60 | m_material->size = uniformSize; | ||||
61 | markDirty(QSGNode::DirtyMaterial); | ||||
62 | m_size = size; | ||||
63 | } | ||||
64 | } | ||||
65 | | ||||
66 | void ShadowedRectangleNode::setRadius(qreal radius) | ||||
67 | { | ||||
68 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
69 | float uniformRadius = radius * 2.0 / minDimension; | ||||
70 | | ||||
71 | if (!qFuzzyCompare(m_material->radius, uniformRadius)) { | ||||
72 | m_material->radius = std::min(uniformRadius, 1.0f); | ||||
73 | markDirty(QSGNode::DirtyMaterial); | ||||
74 | m_radius = radius; | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | void ShadowedRectangleNode::setColor(const QColor &color) | ||||
79 | { | ||||
80 | auto premultiplied = premultiply(color); | ||||
81 | if (m_material->color != premultiplied) { | ||||
82 | m_material->color = premultiplied; | ||||
83 | markDirty(QSGNode::DirtyMaterial); | ||||
84 | } | ||||
85 | } | ||||
86 | | ||||
87 | void ShadowedRectangleNode::setShadowColor(const QColor& color) | ||||
88 | { | ||||
89 | auto premultiplied = premultiply(color); | ||||
90 | if (m_material->shadowColor != premultiplied) { | ||||
91 | m_material->shadowColor = premultiplied; | ||||
92 | markDirty(QSGNode::DirtyMaterial); | ||||
93 | } | ||||
94 | } | ||||
95 | | ||||
96 | void ShadowedRectangleNode::setOffset(const QVector2D& offset) | ||||
97 | { | ||||
98 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
99 | auto uniformOffset = offset / minDimension; | ||||
100 | | ||||
101 | if (m_material->offset != uniformOffset) { | ||||
102 | m_material->offset = uniformOffset; | ||||
103 | markDirty(QSGNode::DirtyMaterial); | ||||
104 | m_offset = offset; | ||||
105 | } | ||||
106 | } | ||||
107 | | ||||
108 | void ShadowedRectangleNode::setBorderWidth(qreal width) | ||||
109 | { | ||||
110 | // We can achieve more performant shaders by splitting the two into separate | ||||
111 | // shaders. This requires separating the materials as well. So when | ||||
112 | // borderWidth is increased to something where the border should be visible, | ||||
113 | // switch to the with-border material. Otherwise use the no-border version. | ||||
114 | | ||||
115 | if (qFuzzyIsNull(width)) { | ||||
116 | if (m_material->type() == &ShadowedBorderRectangleMaterial::staticType) { | ||||
117 | auto newMaterial = new ShadowedRectangleMaterial(); | ||||
118 | setMaterial(newMaterial); | ||||
119 | m_material = newMaterial; | ||||
120 | m_borderWidth = width; | ||||
121 | m_rect = QRectF{}; | ||||
122 | markDirty(QSGNode::DirtyMaterial); | ||||
123 | } | ||||
124 | return; | ||||
125 | } else { | ||||
126 | if (m_material->type() == &ShadowedRectangleMaterial::staticType) { | ||||
127 | auto newMaterial = new ShadowedBorderRectangleMaterial(); | ||||
128 | setMaterial(newMaterial); | ||||
129 | m_material = newMaterial; | ||||
130 | m_rect = QRectF{}; | ||||
131 | markDirty(QSGNode::DirtyMaterial); | ||||
132 | } | ||||
133 | } | ||||
134 | | ||||
135 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
136 | float uniformBorderWidth = width / minDimension; | ||||
137 | | ||||
138 | auto borderMaterial = static_cast<ShadowedBorderRectangleMaterial*>(m_material); | ||||
139 | if (!qFuzzyCompare(borderMaterial->borderWidth, uniformBorderWidth)) { | ||||
140 | borderMaterial->borderWidth = uniformBorderWidth; | ||||
141 | markDirty(QSGNode::DirtyMaterial); | ||||
142 | m_borderWidth = width; | ||||
143 | } | ||||
144 | } | ||||
145 | | ||||
146 | void ShadowedRectangleNode::setBorderColor(const QColor& color) | ||||
147 | { | ||||
148 | if (m_material->type() != &ShadowedBorderRectangleMaterial::staticType) { | ||||
149 | return; | ||||
150 | } | ||||
151 | | ||||
152 | auto borderMaterial = static_cast<ShadowedBorderRectangleMaterial*>(m_material); | ||||
153 | auto premultiplied = premultiply(color); | ||||
154 | if (borderMaterial->borderColor != premultiplied) { | ||||
155 | borderMaterial->borderColor = premultiplied; | ||||
156 | markDirty(QSGNode::DirtyMaterial); | ||||
157 | } | ||||
158 | } | ||||
159 | | ||||
160 | void ShadowedRectangleNode::updateGeometry() | ||||
161 | { | ||||
162 | auto rect = m_rect.adjusted(-m_size * m_aspect.x(), -m_size * m_aspect.y(), | ||||
163 | m_size * m_aspect.x(), m_size * m_aspect.y()); | ||||
164 | | ||||
165 | auto offsetLength = m_offset.length(); | ||||
166 | | ||||
167 | rect = rect.adjusted(-offsetLength * m_aspect.x(), -offsetLength * m_aspect.y(), | ||||
168 | offsetLength * m_aspect.x(), offsetLength * m_aspect.y()); | ||||
169 | | ||||
170 | rect = rect.adjusted(-m_borderWidth * m_aspect.x(), -m_borderWidth * m_aspect.y(), | ||||
171 | m_borderWidth * m_aspect.x(), m_borderWidth * m_aspect.y()); | ||||
172 | | ||||
173 | QSGGeometry::updateTexturedRectGeometry(m_geometry, rect, QRectF{0.0, 0.0, 1.0, 1.0}); | ||||
174 | markDirty(QSGNode::DirtyGeometry); | ||||
175 | } |