1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsgninepatchnode.h"
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \class QSGNinePatchNode
46 \inmodule QtQuick
47 \since 5.8
48 \internal
49 */
50
51/*!
52 \fn void QSGNinePatchNode::setTexture(QSGTexture *texture)
53 \internal
54 */
55
56/*!
57 \fn void QSGNinePatchNode::setBounds(const QRectF &bounds)
58 \internal
59 */
60
61/*!
62 \fn void QSGNinePatchNode::setDevicePixelRatio(qreal ratio)
63 \internal
64 */
65
66/*!
67 \fn void QSGNinePatchNode::setPadding(qreal left, qreal top, qreal right, qreal bottom)
68 \internal
69 */
70
71
72/*!
73 \fn void QSGNinePatchNode::update()
74 \internal
75 */
76
77void QSGNinePatchNode::rebuildGeometry(QSGTexture *texture, QSGGeometry *geometry, const QVector4D &padding,
78 const QRectF &bounds, qreal dpr)
79{
80 if (padding.x() <= 0 && padding.y() <= 0 && padding.z() <= 0 && padding.w() <= 0) {
81 geometry->allocate(vertexCount: 4, indexCount: 0);
82 QSGGeometry::updateTexturedRectGeometry(g: geometry, rect: bounds, sourceRect: texture->normalizedTextureSubRect());
83 return;
84 }
85
86 QRectF tc = texture->normalizedTextureSubRect();
87 QSize ts = texture->textureSize();
88 ts.setHeight(ts.height() / dpr);
89 ts.setWidth(ts.width() / dpr);
90
91 qreal invtw = tc.width() / ts.width();
92 qreal invth = tc.height() / ts.height();
93
94 struct Coord { qreal p; qreal t; };
95 Coord cx[4] = { { .p: bounds.left(), .t: tc.left() },
96 { .p: bounds.left() + padding.x(), .t: tc.left() + padding.x() * invtw },
97 { .p: bounds.right() - padding.z(), .t: tc.right() - padding.z() * invtw },
98 { .p: bounds.right(), .t: tc.right() }
99 };
100 Coord cy[4] = { { .p: bounds.top(), .t: tc.top() },
101 { .p: bounds.top() + padding.y(), .t: tc.top() + padding.y() * invth },
102 { .p: bounds.bottom() - padding.w(), .t: tc.bottom() - padding.w() * invth },
103 { .p: bounds.bottom(), .t: tc.bottom() }
104 };
105
106 geometry->allocate(vertexCount: 16, indexCount: 28);
107 QSGGeometry::TexturedPoint2D *v = geometry->vertexDataAsTexturedPoint2D();
108 for (int y = 0; y < 4; ++y) {
109 for (int x = 0; x < 4; ++x) {
110 v->set(nx: cx[x].p, ny: cy[y].p, ntx: cx[x].t, nty: cy[y].t);
111 ++v;
112 }
113 }
114
115 quint16 *i = geometry->indexDataAsUShort();
116 for (int r = 0; r < 3; ++r) {
117 if (r > 0)
118 *i++ = 4 * r;
119 for (int c = 0; c < 4; ++c) {
120 i[0] = 4 * r + c;
121 i[1] = 4 * r + c + 4;
122 i += 2;
123 }
124 if (r < 2)
125 *i++ = 4 * r + 3 + 4;
126 }
127}
128
129QT_END_NAMESPACE
130

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