| 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 | |
| 5 | #include "qquickclipnode_p.h" |
| 6 | |
| 7 | #include <QtGui/qvector2d.h> |
| 8 | #include <QtCore/qmath.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QQuickDefaultClipNode::QQuickDefaultClipNode(const QRectF &rect) |
| 13 | : m_rect(rect) |
| 14 | , m_radius(0) |
| 15 | , m_dirty_geometry(true) |
| 16 | , m_geometry(QSGGeometry::defaultAttributes_Point2D(), 0) |
| 17 | { |
| 18 | Q_UNUSED(m_reserved); |
| 19 | setGeometry(&m_geometry); |
| 20 | setIsRectangular(true); |
| 21 | } |
| 22 | |
| 23 | void QQuickDefaultClipNode::setRect(const QRectF &rect) |
| 24 | { |
| 25 | m_rect = rect; |
| 26 | m_dirty_geometry = true; |
| 27 | } |
| 28 | |
| 29 | void QQuickDefaultClipNode::setRadius(qreal radius) |
| 30 | { |
| 31 | m_radius = radius; |
| 32 | m_dirty_geometry = true; |
| 33 | setIsRectangular(radius == 0); |
| 34 | } |
| 35 | |
| 36 | void QQuickDefaultClipNode::update() |
| 37 | { |
| 38 | if (m_dirty_geometry) { |
| 39 | updateGeometry(); |
| 40 | m_dirty_geometry = false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void QQuickDefaultClipNode::updateGeometry() |
| 45 | { |
| 46 | QSGGeometry *g = geometry(); |
| 47 | |
| 48 | if (qFuzzyIsNull(d: m_radius)) { |
| 49 | g->allocate(vertexCount: 4); |
| 50 | QSGGeometry::updateRectGeometry(g, rect: m_rect); |
| 51 | |
| 52 | } else { |
| 53 | int vertexCount = 0; |
| 54 | |
| 55 | // Radius should never exceeds half of the width or half of the height |
| 56 | qreal radius = qMin(a: qMin(a: m_rect.width() / 2, b: m_rect.height() / 2), b: m_radius); |
| 57 | QRectF rect = m_rect; |
| 58 | rect.adjust(xp1: radius, yp1: radius, xp2: -radius, yp2: -radius); |
| 59 | |
| 60 | int segments = qMin(a: 30, b: qCeil(v: radius)); // Number of segments per corner. |
| 61 | |
| 62 | g->allocate(vertexCount: (segments + 1) * 4); |
| 63 | |
| 64 | QVector2D *vertices = (QVector2D *)g->vertexData(); |
| 65 | |
| 66 | for (int part = 0; part < 2; ++part) { |
| 67 | for (int i = 0; i <= segments; ++i) { |
| 68 | //### Should change to calculate sin/cos only once. |
| 69 | qreal angle = qreal(0.5 * M_PI) * (part + i / qreal(segments)); |
| 70 | qreal s = qFastSin(x: angle); |
| 71 | qreal c = qFastCos(x: angle); |
| 72 | qreal y = (part ? rect.bottom() : rect.top()) - radius * c; // current inner y-coordinate. |
| 73 | qreal lx = rect.left() - radius * s; // current inner left x-coordinate. |
| 74 | qreal rx = rect.right() + radius * s; // current inner right x-coordinate. |
| 75 | |
| 76 | vertices[vertexCount++] = QVector2D(rx, y); |
| 77 | vertices[vertexCount++] = QVector2D(lx, y); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | #ifdef QSG_RUNTIME_DESCRIPTION |
| 83 | #ifndef QT_NO_DEBUG_STREAM |
| 84 | QString desc; |
| 85 | { |
| 86 | QDebug dbg(&desc); |
| 87 | dbg << m_rect; |
| 88 | if (!qFuzzyIsNull(d: m_radius)) |
| 89 | dbg << "radius" << m_radius; |
| 90 | } |
| 91 | qsgnode_set_description(node: this, description: desc); |
| 92 | #endif |
| 93 | #endif |
| 94 | setClipRect(m_rect); |
| 95 | markDirty(bits: DirtyGeometry); |
| 96 | } |
| 97 | |
| 98 | QT_END_NAMESPACE |
| 99 | |