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 | | ||||
10 | QColor premultiply(const QColor &color) | ||||
11 | { | ||||
12 | return QColor::fromRgbF( | ||||
13 | color.redF() * color.alphaF(), | ||||
14 | color.greenF() * color.greenF(), | ||||
15 | color.blueF() * color.blueF(), | ||||
16 | color.alphaF() | ||||
17 | ); | ||||
18 | } | ||||
19 | | ||||
20 | ShadowedRectangleNode::ShadowedRectangleNode() | ||||
21 | { | ||||
22 | m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_TexturedPoint2D(), 4}; | ||||
23 | setGeometry(m_geometry); | ||||
24 | | ||||
25 | m_material = new ShadowedRectangleMaterial{}; | ||||
26 | setMaterial(m_material); | ||||
27 | | ||||
28 | setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); | ||||
29 | } | ||||
30 | | ||||
31 | void ShadowedRectangleNode::setRect(const QRectF& rect) | ||||
32 | { | ||||
33 | if (rect == m_rect) { | ||||
34 | return; | ||||
35 | } | ||||
36 | | ||||
37 | m_rect = rect; | ||||
38 | | ||||
39 | QVector2D newAspect{1.0, 1.0}; | ||||
40 | if (m_rect.width() >= m_rect.height()) { | ||||
41 | newAspect.setX(m_rect.width() / m_rect.height()); | ||||
42 | } else { | ||||
43 | newAspect.setY(m_rect.height() / m_rect.width()); | ||||
44 | } | ||||
45 | | ||||
46 | if (m_material->aspect != newAspect) { | ||||
47 | m_material->aspect = newAspect; | ||||
48 | markDirty(QSGNode::DirtyMaterial); | ||||
49 | m_aspect = newAspect; | ||||
50 | } | ||||
51 | } | ||||
52 | | ||||
53 | void ShadowedRectangleNode::setSize(qreal size) | ||||
54 | { | ||||
55 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
56 | float uniformSize = (size / minDimension) * 2.0; | ||||
57 | | ||||
58 | if (!qFuzzyCompare(m_material->size, uniformSize)) { | ||||
59 | m_material->size = uniformSize; | ||||
60 | markDirty(QSGNode::DirtyMaterial); | ||||
61 | m_size = size; | ||||
62 | } | ||||
63 | } | ||||
64 | | ||||
65 | void ShadowedRectangleNode::setRadius(qreal radius) | ||||
66 | { | ||||
67 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
68 | float uniformRadius = radius * 2.0 / minDimension; | ||||
69 | | ||||
70 | if (!qFuzzyCompare(m_material->radius, uniformRadius)) { | ||||
71 | m_material->radius = std::min(uniformRadius, 1.0f); | ||||
72 | markDirty(QSGNode::DirtyMaterial); | ||||
73 | m_radius = radius; | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
77 | void ShadowedRectangleNode::setColor(const QColor &color) | ||||
78 | { | ||||
79 | if (m_material->color != color) { | ||||
80 | m_material->color = premultiply(color); | ||||
81 | markDirty(QSGNode::DirtyMaterial); | ||||
82 | } | ||||
83 | } | ||||
84 | | ||||
85 | void ShadowedRectangleNode::setShadowColor(const QColor& color) | ||||
86 | { | ||||
87 | if (m_material->shadowColor != color) { | ||||
88 | m_material->shadowColor = premultiply(color); | ||||
89 | markDirty(QSGNode::DirtyMaterial); | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | void ShadowedRectangleNode::setOffset(const QVector2D& offset) | ||||
94 | { | ||||
95 | auto minDimension = std::min(m_rect.width(), m_rect.height()); | ||||
96 | auto uniformOffset = offset / minDimension; | ||||
97 | | ||||
98 | if (m_material->offset != uniformOffset) { | ||||
99 | m_material->offset = uniformOffset; | ||||
100 | markDirty(QSGNode::DirtyMaterial); | ||||
101 | m_offset = offset; | ||||
102 | } | ||||
103 | } | ||||
104 | | ||||
105 | void ShadowedRectangleNode::updateGeometry() | ||||
106 | { | ||||
107 | auto rect = m_rect.adjusted(-m_size * m_aspect.x(), -m_size * m_aspect.y(), | ||||
108 | m_size * m_aspect.x(), m_size * m_aspect.y()); | ||||
109 | | ||||
110 | auto offsetLength = m_offset.length(); | ||||
111 | | ||||
112 | rect = rect.adjusted(-offsetLength * m_aspect.x(), -offsetLength * m_aspect.y(), | ||||
113 | offsetLength * m_aspect.x(), offsetLength * m_aspect.y()); | ||||
114 | | ||||
115 | QSGGeometry::updateTexturedRectGeometry(m_geometry, rect, QRectF{0.0, 0.0, 1.0, 1.0}); | ||||
116 | markDirty(QSGNode::DirtyGeometry); | ||||
117 | } | ||||
118 | | ||||
119 | |