aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgcurvefillnode_p.h
blob: d96a78cde94be62d9044da827830b393b6c7b435 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSGCURVEFILLNODE_P_H
#define QSGCURVEFILLNODE_P_H

#include <QtGui/qbrush.h>

#include <QtQuick/qtquickexports.h>
#include <QtQuick/private/qsggradientcache_p.h>
#include <QtQuick/qsgnode.h>

#include "qsgcurveabstractnode_p.h"

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of a number of Qt sources files.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

QT_BEGIN_NAMESPACE

class Q_QUICK_EXPORT QSGCurveFillNode : public QSGCurveAbstractNode
{
public:
    QSGCurveFillNode();

    void setColor(QColor col) override
    {
        if (m_color == col)
            return;
        m_color = col;
        updateMaterial();
    }

    QColor color() const
    {
        return m_color;
    }

    void setStrokeColor(QColor col)
    {
        const bool hadStroke = hasStroke();
        m_strokeColor = col;
        if (hadStroke != hasStroke())
            updateMaterial();
    }

    QColor strokeColor() const
    {
        return m_strokeColor;
    }

    void setStrokeWidth(float width)
    {
        const bool hadStroke = hasStroke();
        m_strokeWidth = width;
        if (hadStroke != hasStroke())
            updateMaterial();
    }

    float strokeWidth() const
    {
        return m_strokeWidth;
    }

    void setFillGradient(const QSGGradientCache::GradientDesc &fillGradient)
    {
        m_fillGradient = fillGradient;
    }

    QSGGradientCache::GradientDesc fillGradient() const
    {
        return m_fillGradient;
    }

    void setGradientType(QGradient::Type type)
    {
        if (m_gradientType != type) {
            m_gradientType = type;
            updateMaterial();
        }
    }

    QGradient::Type gradientType() const
    {
        return m_gradientType;
    }

    float debug() const
    {
        return m_debug;
    }

    void setDebug(float newDebug)
    {
        m_debug = newDebug;
    }


    bool hasStroke() const
    {
        return m_strokeWidth > 0.0f && m_strokeColor.alpha() > 0;
    }

    void appendTriangle(const std::array<QVector2D, 3> &v, // triangle vertices
                        const std::array<QVector2D, 3> &n, // vertex normals
                        std::function<QVector3D(QVector2D)> uvForPoint
                        )
    {
        QVector3D uv1 = uvForPoint(v[0]);
        QVector3D uv2 = uvForPoint(v[1]);
        QVector3D uv3 = uvForPoint(v[2]);

        QVector2D duvdx = QVector2D(uvForPoint(v[0] + QVector2D(1, 0))) - QVector2D(uv1);
        QVector2D duvdy = QVector2D(uvForPoint(v[0] + QVector2D(0, 1))) - QVector2D(uv1);

        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v[0].x(), v[0].y(),
            uv1.x(), uv1.y(), uv1.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n[0].x(), n[0].y()
        });

        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v[1].x(), v[1].y(),
            uv2.x(), uv2.y(), uv2.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n[1].x(), n[1].y()
        });

        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v[2].x(), v[2].y(),
            uv3.x(), uv3.y(), uv3.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n[2].x(), n[2].y()
        });
    }

    void appendTriangle(const QVector2D &v1,
                        const QVector2D &v2,
                        const QVector2D &v3,
                        const QVector3D &uv1,
                        const QVector3D &uv2,
                        const QVector3D &uv3,
                        const QVector2D &n1,
                        const QVector2D &n2,
                        const QVector2D &n3,
                        const QVector2D &duvdx,
                        const QVector2D &duvdy)
    {
        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v1.x(), v1.y(),
            uv1.x(), uv1.y(), uv1.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n1.x(), n1.y()
        });

        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v2.x(), v2.y(),
            uv2.x(), uv2.y(), uv2.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n2.x(), n2.y()
        });

        m_uncookedIndexes.append(m_uncookedVertexes.size());
        m_uncookedVertexes.append( { v3.x(), v3.y(),
            uv3.x(), uv3.y(), uv3.z(),
            duvdx.x(), duvdx.y(),
            duvdy.x(), duvdy.y(),
            n3.x(), n3.y()
        });
    }

    void appendTriangle(const QVector2D &v1,
                        const QVector2D &v2,
                        const QVector2D &v3,
                        std::function<QVector3D(QVector2D)> uvForPoint)
    {
        appendTriangle({v1, v2, v3}, {}, uvForPoint);
    }

    QVector<quint32> uncookedIndexes() const
    {
        return m_uncookedIndexes;
    }

    void cookGeometry() override;

    void reserve(qsizetype size)
    {
        m_uncookedIndexes.reserve(size);
        m_uncookedVertexes.reserve(size);
    }

private:
    struct CurveNodeVertex
    {
        float x, y, u, v, w;
        float dudx, dvdx, dudy, dvdy; // Size of pixel in curve space (must be same for all vertices in triangle)
        float nx, ny; // normal vector describing the direction to shift the vertex for AA
    };

    void updateMaterial();
    static const QSGGeometry::AttributeSet &attributes();

    QColor m_color = Qt::white;
    QColor m_strokeColor = Qt::transparent;
    float m_strokeWidth = 0.0f;
    float m_debug = 0.0f;
    QSGGradientCache::GradientDesc m_fillGradient;
    QGradient::Type m_gradientType = QGradient::NoGradient;

    QScopedPointer<QSGMaterial> m_material;

    QVector<CurveNodeVertex> m_uncookedVertexes;
    QVector<quint32> m_uncookedIndexes;
};

QT_END_NAMESPACE

#endif // QSGCURVEFILLNODE_P_H