1 | // Copyright (C) 2016 The Qt Company Ltd. |
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 "qsgdefaultrectanglenode_p.h" |
5 | #include "qsgflatcolormaterial.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | // Unlike our predecessor, QSGSimpleRectNode, use QSGVertexColorMaterial |
10 | // instead of Flat in order to allow better batching in the renderer. |
11 | |
12 | QSGDefaultRectangleNode::QSGDefaultRectangleNode() |
13 | : m_geometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 4) |
14 | { |
15 | QSGGeometry::updateColoredRectGeometry(g: &m_geometry, rect: QRectF()); |
16 | setMaterial(&m_material); |
17 | setGeometry(&m_geometry); |
18 | setColor(QColor(255, 255, 255)); |
19 | #ifdef QSG_RUNTIME_DESCRIPTION |
20 | qsgnode_set_description(node: this, description: QLatin1String("rectangle" )); |
21 | #endif |
22 | } |
23 | |
24 | void QSGDefaultRectangleNode::setRect(const QRectF &rect) |
25 | { |
26 | QSGGeometry::updateColoredRectGeometry(g: &m_geometry, rect); |
27 | markDirty(bits: QSGNode::DirtyGeometry); |
28 | } |
29 | |
30 | QRectF QSGDefaultRectangleNode::rect() const |
31 | { |
32 | const QSGGeometry::ColoredPoint2D *pts = m_geometry.vertexDataAsColoredPoint2D(); |
33 | return QRectF(pts[0].x, |
34 | pts[0].y, |
35 | pts[3].x - pts[0].x, |
36 | pts[3].y - pts[0].y); |
37 | } |
38 | |
39 | void QSGDefaultRectangleNode::setColor(const QColor &color) |
40 | { |
41 | if (color != m_color) { |
42 | float r, g, b, a; |
43 | color.getRgbF(r: &r, g: &g, b: &b, a: &a); |
44 | QSGGeometry::ColoredPoint2D *pts = m_geometry.vertexDataAsColoredPoint2D(); |
45 | for (int i = 0; i < 4; ++i) { |
46 | pts[i].r = uchar(qRound(f: r * a * 255)); |
47 | pts[i].g = uchar(qRound(f: g * a * 255)); |
48 | pts[i].b = uchar(qRound(f: b * a * 255)); |
49 | pts[i].a = uchar(qRound(f: a * 255)); |
50 | } |
51 | markDirty(bits: QSGNode::DirtyGeometry); |
52 | } |
53 | } |
54 | |
55 | QColor QSGDefaultRectangleNode::color() const |
56 | { |
57 | return m_color; |
58 | } |
59 | |
60 | QT_END_NAMESPACE |
61 | |