File indexing completed on 2024-05-12 15:42:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QColor>
0010 #include <QSGMaterial>
0011 #include <QSGMaterialShader>
0012 
0013 /**
0014  * A material rendering a rectangle with a shadow.
0015  *
0016  * This material uses a distance field shader to render a rectangle with a
0017  * shadow below it, optionally with rounded corners.
0018  */
0019 class ShadowedRectangleMaterial : public QSGMaterial
0020 {
0021 public:
0022     enum class ShaderType {
0023         Standard,
0024         LowPower,
0025     };
0026 
0027     ShadowedRectangleMaterial();
0028 
0029 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0030     QSGMaterialShader *createShader() const override;
0031 #else
0032     QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override;
0033 #endif
0034     QSGMaterialType *type() const override;
0035     int compare(const QSGMaterial *other) const override;
0036 
0037     QVector2D aspect = QVector2D{1.0, 1.0};
0038     float size = 0.0;
0039     QVector4D radius = QVector4D{0.0, 0.0, 0.0, 0.0};
0040     QColor color = Qt::white;
0041     QColor shadowColor = Qt::black;
0042     QVector2D offset;
0043     ShaderType shaderType = ShaderType::Standard;
0044 
0045     static QSGMaterialType staticType;
0046 };
0047 
0048 class ShadowedRectangleShader : public QSGMaterialShader
0049 {
0050 public:
0051     ShadowedRectangleShader(ShadowedRectangleMaterial::ShaderType shaderType);
0052 
0053 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0054     char const *const *attributeNames() const override;
0055 
0056     void initialize() override;
0057     void updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
0058 #else
0059     bool updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
0060 #endif
0061 
0062 protected:
0063     void setShader(ShadowedRectangleMaterial::ShaderType shaderType, const QString &shader);
0064 
0065 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0066 private:
0067     int m_matrixLocation = -1;
0068     int m_opacityLocation = -1;
0069     int m_aspectLocation = -1;
0070     int m_sizeLocation = -1;
0071     int m_radiusLocation = -1;
0072     int m_colorLocation = -1;
0073     int m_shadowColorLocation = -1;
0074     int m_offsetLocation = -1;
0075 #endif
0076 };