summaryrefslogtreecommitdiffstats
path: root/tools/qqem/uniformmodel.h
blob: 3db9433b284b4a0f288a69504d6ba06df0062710 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef UNIFORMMODEL_H
#define UNIFORMMODEL_H

#include <QAbstractTableModel>
#include <QUrl>
#include <QVector2D>
#include <QList>
#include <QImage>
#include <QtQml/qqmlregistration.h>

class EffectManager;
class UniformModel : public QAbstractListModel
{
    Q_OBJECT
public:
    struct Uniform
    {
        enum class Type
        {
            Bool,
            Int,
            Float,
            Vec2,
            Vec3,
            Vec4,
            Color,
            Sampler,
            Define
        };

        Type type;
        QVariant value;
        QVariant defaultValue;
        QVariant minValue;
        QVariant maxValue;
        QByteArray name;
        QString description;
        QString customValue;
        bool useCustomValue = false;
        bool visible = true;
        bool exportProperty = true;
        bool canMoveUp = false;
        bool canMoveDown = false;
        bool enableMipmap = false;
        bool exportImage = true;
        // The effect node which owns this uniform
        int nodeId = -1;
        bool operator==(const Uniform& rhs) const noexcept
        {
           return this->name == rhs.name;
        }

    };
    using UniformTable = QList<Uniform>;

    enum UniformModelRoles {
        Type = Qt::UserRole + 1,
        NodeId,
        Name,
        Value,
        DefaultValue,
        Description,
        CustomValue,
        UseCustomValue,
        MinValue,
        MaxValue,
        Visible,
        ExportProperty,
        CanMoveUp,
        CanMoveDown,
        EnableMipmap,
        ExportImage
    };

    explicit UniformModel(QObject *parent = nullptr);

    void setModelData(UniformTable *data);
    int rowCount(const QModelIndex & = QModelIndex()) const final;
    QVariant data(const QModelIndex &index, int role) const final;
    QHash<int, QByteArray> roleNames() const final;
    bool setData(const QModelIndex &index, const QVariant &value, int role) final;

    static QVariant getInitializedVariant(Uniform::Type type, bool maxValue = false);
    static QString getImageElementName(const Uniform &uniform);
    static QString typeToProperty(Uniform::Type type);
    QString typeToUniform(Uniform::Type type);
    QString valueAsString(const Uniform &uniform);
    QString valueAsVariable(const Uniform &uniform);
    QString valueAsBinding(const Uniform &uniform);
    QString variantAsDataString(const Uniform::Type type, const QVariant &variant);
    QVariant valueStringToVariant(const Uniform::Type type, const QString &value);

    Q_INVOKABLE bool updateRow(int nodeId, int rowIndex, int type, const QString &id,
                               const QVariant &defaultValue, const QString &description, const QString &customValue,
                               bool useCustomValue, const QVariant &minValue, const QVariant &maxValue,
                               bool enableMipmap, bool exportImage);
    Q_INVOKABLE void removeRow(int rowIndex, int rows = 1);
    Q_INVOKABLE bool resetValue(int rowIndex);
    Q_INVOKABLE bool setImage(int rowIndex, const QVariant &value);
    Q_INVOKABLE void moveIndex(int rowIndex, int direction);

    void appendUniform(Uniform uniform);

    void setUniformValueData(Uniform *uniform, const QString &value, const QString &defaultValue, const QString &minValue, const QString &maxValue);
    // These convert between uniform type & string name in JSON
    UniformModel::Uniform::Type typeFromString(const QString &typeString) const;
    QString stringFromType(Uniform::Type type);

Q_SIGNALS:
    void uniformsChanged();
    void qmlComponentChanged();
    void addFSCode(const QString &code);

private:
    friend class EffectManager;
    bool validateUniformName(const QString &uniformName);
    void updateCanMoveStatus();
    UniformTable *m_uniformTable = nullptr;
    EffectManager *m_effectManager = nullptr;
};

#endif // UNIFORMMODEL_H