| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef UNIFORMMODEL_H |
| 5 | #define UNIFORMMODEL_H |
| 6 | |
| 7 | #include <QAbstractTableModel> |
| 8 | #include <QUrl> |
| 9 | #include <QVector2D> |
| 10 | #include <QList> |
| 11 | #include <QImage> |
| 12 | #include <QtQml/qqmlregistration.h> |
| 13 | |
| 14 | class EffectManager; |
| 15 | class UniformModel : public QAbstractListModel |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | public: |
| 19 | struct Uniform |
| 20 | { |
| 21 | enum class Type |
| 22 | { |
| 23 | Bool, |
| 24 | Int, |
| 25 | Float, |
| 26 | Vec2, |
| 27 | Vec3, |
| 28 | Vec4, |
| 29 | Color, |
| 30 | Sampler, |
| 31 | Define |
| 32 | }; |
| 33 | |
| 34 | Type type; |
| 35 | QVariant value; |
| 36 | QVariant defaultValue; |
| 37 | QVariant minValue; |
| 38 | QVariant maxValue; |
| 39 | QByteArray name; |
| 40 | QString description; |
| 41 | QString customValue; |
| 42 | bool useCustomValue = false; |
| 43 | bool visible = true; |
| 44 | bool exportProperty = true; |
| 45 | bool canMoveUp = false; |
| 46 | bool canMoveDown = false; |
| 47 | bool enableMipmap = false; |
| 48 | bool exportImage = true; |
| 49 | // The effect node which owns this uniform |
| 50 | int nodeId = -1; |
| 51 | bool operator==(const Uniform& rhs) const noexcept |
| 52 | { |
| 53 | return this->name == rhs.name; |
| 54 | } |
| 55 | |
| 56 | }; |
| 57 | using UniformTable = QList<Uniform>; |
| 58 | |
| 59 | enum UniformModelRoles { |
| 60 | Type = Qt::UserRole + 1, |
| 61 | NodeId, |
| 62 | Name, |
| 63 | Value, |
| 64 | DefaultValue, |
| 65 | Description, |
| 66 | CustomValue, |
| 67 | UseCustomValue, |
| 68 | MinValue, |
| 69 | MaxValue, |
| 70 | Visible, |
| 71 | ExportProperty, |
| 72 | CanMoveUp, |
| 73 | CanMoveDown, |
| 74 | EnableMipmap, |
| 75 | ExportImage |
| 76 | }; |
| 77 | |
| 78 | explicit UniformModel(QObject *parent = nullptr); |
| 79 | |
| 80 | void setModelData(UniformTable *data); |
| 81 | int rowCount(const QModelIndex & = QModelIndex()) const final; |
| 82 | QVariant data(const QModelIndex &index, int role) const final; |
| 83 | QHash<int, QByteArray> roleNames() const final; |
| 84 | bool setData(const QModelIndex &index, const QVariant &value, int role) final; |
| 85 | |
| 86 | static QVariant getInitializedVariant(Uniform::Type type, bool maxValue = false); |
| 87 | static QString getImageElementName(const Uniform &uniform); |
| 88 | static QString typeToProperty(Uniform::Type type); |
| 89 | QString typeToUniform(Uniform::Type type); |
| 90 | QString valueAsString(const Uniform &uniform); |
| 91 | QString valueAsVariable(const Uniform &uniform); |
| 92 | QString valueAsBinding(const Uniform &uniform); |
| 93 | QString variantAsDataString(const Uniform::Type type, const QVariant &variant); |
| 94 | QVariant valueStringToVariant(const Uniform::Type type, const QString &value); |
| 95 | |
| 96 | Q_INVOKABLE bool updateRow(int nodeId, int rowIndex, int type, const QString &id, |
| 97 | const QVariant &defaultValue, const QString &description, const QString &customValue, |
| 98 | bool useCustomValue, const QVariant &minValue, const QVariant &maxValue, |
| 99 | bool enableMipmap, bool exportImage); |
| 100 | Q_INVOKABLE void removeRow(int rowIndex, int rows = 1); |
| 101 | Q_INVOKABLE bool resetValue(int rowIndex); |
| 102 | Q_INVOKABLE bool setImage(int rowIndex, const QVariant &value); |
| 103 | Q_INVOKABLE void moveIndex(int rowIndex, int direction); |
| 104 | |
| 105 | void appendUniform(Uniform uniform); |
| 106 | |
| 107 | void setUniformValueData(Uniform *uniform, const QString &value, const QString &defaultValue, const QString &minValue, const QString &maxValue); |
| 108 | // These convert between uniform type & string name in JSON |
| 109 | UniformModel::Uniform::Type typeFromString(const QString &typeString) const; |
| 110 | QString stringFromType(Uniform::Type type); |
| 111 | |
| 112 | Q_SIGNALS: |
| 113 | void uniformsChanged(); |
| 114 | void qmlComponentChanged(); |
| 115 | void addFSCode(const QString &code); |
| 116 | |
| 117 | private: |
| 118 | friend class EffectManager; |
| 119 | bool validateUniformName(const QString &uniformName); |
| 120 | void updateCanMoveStatus(); |
| 121 | UniformTable *m_uniformTable = nullptr; |
| 122 | EffectManager *m_effectManager = nullptr; |
| 123 | }; |
| 124 | |
| 125 | #endif // UNIFORMMODEL_H |
| 126 | |