aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/util/qsgninepatchnode.cpp
blob: b6f56dcaec11864ae782330c75a8be0f004934f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qsgninepatchnode.h"

QT_BEGIN_NAMESPACE

/*!
  \class QSGNinePatchNode
  \inmodule QtQuick
  \since 5.8
  \internal
 */

/*!
    \fn void QSGNinePatchNode::setTexture(QSGTexture *texture)
    \internal
 */

/*!
    \fn void QSGNinePatchNode::setBounds(const QRectF &bounds)
    \internal
 */

/*!
    \fn void QSGNinePatchNode::setDevicePixelRatio(qreal ratio)
    \internal
 */

/*!
    \fn void QSGNinePatchNode::setPadding(qreal left, qreal top, qreal right, qreal bottom)
    \internal
 */


/*!
    \fn void QSGNinePatchNode::update()
    \internal
 */

void QSGNinePatchNode::rebuildGeometry(QSGTexture *texture, QSGGeometry *geometry, const QVector4D &padding,
                                       const QRectF &bounds, qreal dpr)
{
    if (padding.x() <= 0 && padding.y() <= 0 && padding.z() <= 0 && padding.w() <= 0) {
        geometry->allocate(4, 0);
        QSGGeometry::updateTexturedRectGeometry(geometry, bounds, texture->normalizedTextureSubRect());
        return;
    }

    QRectF tc = texture->normalizedTextureSubRect();
    QSize ts = texture->textureSize();
    ts.setHeight(ts.height() / dpr);
    ts.setWidth(ts.width() / dpr);

    qreal invtw = tc.width() / ts.width();
    qreal invth = tc.height() / ts.height();

    struct Coord { qreal p; qreal t; };
    Coord cx[4] = { { bounds.left(), tc.left() },
                    { bounds.left() + padding.x(), tc.left() + padding.x() * invtw },
                    { bounds.right() - padding.z(), tc.right() - padding.z() * invtw },
                    { bounds.right(), tc.right() }
                  };
    Coord cy[4] = { { bounds.top(), tc.top() },
                    { bounds.top() + padding.y(), tc.top() + padding.y() * invth },
                    { bounds.bottom() - padding.w(), tc.bottom() - padding.w() * invth },
                    { bounds.bottom(), tc.bottom() }
                  };

    geometry->allocate(16, 28);
    QSGGeometry::TexturedPoint2D *v = geometry->vertexDataAsTexturedPoint2D();
    for (int y = 0; y < 4; ++y) {
        for (int x = 0; x < 4; ++x) {
            v->set(cx[x].p, cy[y].p, cx[x].t, cy[y].t);
            ++v;
        }
    }

    quint16 *i = geometry->indexDataAsUShort();
    for (int r = 0; r < 3; ++r) {
        if (r > 0)
            *i++ = 4 * r;
        for (int c = 0; c < 4; ++c) {
            i[0] = 4 * r + c;
            i[1] = 4 * r + c + 4;
            i += 2;
        }
        if (r < 2)
            *i++ = 4 * r + 3 + 4;
    }
}

QT_END_NAMESPACE