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 "qsgninepatchnode.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \class QSGNinePatchNode
10 \inmodule QtQuick
11 \since 5.8
12 \internal
13 */
14
15/*!
16 \fn void QSGNinePatchNode::setTexture(QSGTexture *texture)
17 \internal
18 */
19
20/*!
21 \fn void QSGNinePatchNode::setBounds(const QRectF &bounds)
22 \internal
23 */
24
25/*!
26 \fn void QSGNinePatchNode::setDevicePixelRatio(qreal ratio)
27 \internal
28 */
29
30/*!
31 \fn void QSGNinePatchNode::setPadding(qreal left, qreal top, qreal right, qreal bottom)
32 \internal
33 */
34
35
36/*!
37 \fn void QSGNinePatchNode::update()
38 \internal
39 */
40
41void QSGNinePatchNode::rebuildGeometry(QSGTexture *texture, QSGGeometry *geometry, const QVector4D &padding,
42 const QRectF &bounds, qreal dpr)
43{
44 if (padding.x() <= 0 && padding.y() <= 0 && padding.z() <= 0 && padding.w() <= 0) {
45 geometry->allocate(vertexCount: 4, indexCount: 0);
46 QSGGeometry::updateTexturedRectGeometry(g: geometry, rect: bounds, sourceRect: texture->normalizedTextureSubRect());
47 return;
48 }
49
50 QRectF tc = texture->normalizedTextureSubRect();
51 QSize ts = texture->textureSize();
52 ts.setHeight(ts.height() / dpr);
53 ts.setWidth(ts.width() / dpr);
54
55 qreal invtw = tc.width() / ts.width();
56 qreal invth = tc.height() / ts.height();
57
58 struct Coord { qreal p; qreal t; };
59 Coord cx[4] = { { .p: bounds.left(), .t: tc.left() },
60 { .p: bounds.left() + padding.x(), .t: tc.left() + padding.x() * invtw },
61 { .p: bounds.right() - padding.z(), .t: tc.right() - padding.z() * invtw },
62 { .p: bounds.right(), .t: tc.right() }
63 };
64 Coord cy[4] = { { .p: bounds.top(), .t: tc.top() },
65 { .p: bounds.top() + padding.y(), .t: tc.top() + padding.y() * invth },
66 { .p: bounds.bottom() - padding.w(), .t: tc.bottom() - padding.w() * invth },
67 { .p: bounds.bottom(), .t: tc.bottom() }
68 };
69
70 geometry->allocate(vertexCount: 16, indexCount: 28);
71 QSGGeometry::TexturedPoint2D *v = geometry->vertexDataAsTexturedPoint2D();
72 for (int y = 0; y < 4; ++y) {
73 for (int x = 0; x < 4; ++x) {
74 v->set(nx: cx[x].p, ny: cy[y].p, ntx: cx[x].t, nty: cy[y].t);
75 ++v;
76 }
77 }
78
79 quint16 *i = geometry->indexDataAsUShort();
80 for (int r = 0; r < 3; ++r) {
81 if (r > 0)
82 *i++ = 4 * r;
83 for (int c = 0; c < 4; ++c) {
84 i[0] = 4 * r + c;
85 i[1] = 4 * r + c + 4;
86 i += 2;
87 }
88 if (r < 2)
89 *i++ = 4 * r + 3 + 4;
90 }
91}
92
93QT_END_NAMESPACE
94

source code of qtdeclarative/src/quick/scenegraph/util/qsgninepatchnode.cpp