aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/effectmaker/uniform.cpp
blob: 8074c3cc95aae1b8830efc48caa1d774def79a76 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "uniform.h"

#include "propertyeditorvalue.h"

#include <QColor>
#include <QJsonObject>
#include <QVector2D>

namespace QmlDesigner {

Uniform::Uniform(const QJsonObject &propObj)
{
    QString value, defaultValue, minValue, maxValue;

    m_name = propObj.value("name").toString();
    m_description = propObj.value("description").toString();
    m_type = Uniform::typeFromString(propObj.value("type").toString());
    defaultValue = propObj.value("defaultValue").toString();

    m_displayName = propObj.value("displayName").toString();
    if (m_displayName.isEmpty())
        m_displayName = m_name;

    if (m_type == Type::Sampler) {
        if (!defaultValue.isEmpty())
            defaultValue = getResourcePath(defaultValue);
        if (propObj.contains("enableMipmap"))
            m_enableMipmap = getBoolValue(propObj.value("enableMipmap"), false);
        // Update the mipmap property
        QString mipmapProperty = mipmapPropertyName(m_name);
    }
    if (propObj.contains("value")) {
        value = propObj.value("value").toString();
        if (m_type == Type::Sampler && !value.isEmpty())
            value = getResourcePath(value);
    } else {
        // QEN files don't store the current value, so with those use default value
        value = defaultValue;
    }
    minValue = propObj.value("minValue").toString();
    maxValue = propObj.value("maxValue").toString();

    setValueData(value, defaultValue, minValue, maxValue);

    m_backendValue = new PropertyEditorValue(this);
    m_backendValue->setValue(value);
}

Uniform::Type Uniform::type() const
{
    return m_type;
}

// String representation of the type for qml
QString Uniform::typeName() const
{
    return Uniform::stringFromType(m_type);
}

QVariant Uniform::value() const
{
    return m_value;
}

QVariant Uniform::backendValue() const
{
    return QVariant::fromValue(m_backendValue);
}

void Uniform::setValue(const QVariant &newValue)
{
    if (m_value != newValue) {
        m_value = newValue;
        emit uniformValueChanged();
    }
}

QVariant Uniform::defaultValue() const
{
    return m_defaultValue;
}

QVariant Uniform::minValue() const
{
    return m_minValue;
}

QVariant Uniform::maxValue() const
{
    return m_maxValue;
}

QString Uniform::name() const
{
    return m_name;
}

QString Uniform::description() const
{
    return m_description;
}

QString Uniform::customValue() const
{
    return m_customValue;
}

void Uniform::setCustomValue(const QString &newCustomValue)
{
    m_customValue = newCustomValue;
}

bool Uniform::useCustomValue() const
{
    return m_useCustomValue;
}

bool Uniform::enabled() const
{
    return m_enabled;
}

void Uniform::setEnabled(bool newEnabled)
{
    m_enabled = newEnabled;
}

bool Uniform::enableMipmap() const
{
    return m_enableMipmap;
}

// Returns name for image mipmap property.
// e.g. "myImage" -> "myImageMipmap".
QString Uniform::mipmapPropertyName(const QString &name) const
{
    QString simplifiedName = name.simplified();
    simplifiedName = simplifiedName.remove(' ');
    simplifiedName += "Mipmap";
    return simplifiedName;
}

// Returns the boolean value of QJsonValue. It can be either boolean
// (true, false) or string ("true", "false"). Returns the defaultValue
// if QJsonValue is undefined, empty, or some other type.
bool Uniform::getBoolValue(const QJsonValue &jsonValue, bool defaultValue)
{
    if (jsonValue.isBool())
        return jsonValue.toBool();

    if (jsonValue.isString())
        return jsonValue.toString().toLower() == "true";

    return defaultValue;
}

// Returns the path for a shader resource
// Used with sampler types
QString Uniform::getResourcePath(const QString &value) const
{
    Q_UNUSED(value)
    //TODO
    return {};
}

// Validation and setting values
void Uniform::setValueData(const QString &value, const QString &defaultValue,
                           const QString &minValue, const QString &maxValue)
{
    m_value = value.isEmpty() ? getInitializedVariant(false) : valueStringToVariant(value);
    m_defaultValue = defaultValue.isEmpty() ? getInitializedVariant(false)
                                            : valueStringToVariant(defaultValue);
    m_minValue = minValue.isEmpty() ? getInitializedVariant(false) : valueStringToVariant(minValue);
    m_maxValue = maxValue.isEmpty() ? getInitializedVariant(true) : valueStringToVariant(maxValue);
}

// Initialize the value variant with correct type
QVariant Uniform::getInitializedVariant(bool maxValue)
{
    switch (m_type) {
    case Uniform::Type::Bool:
        return maxValue ? true : false;
    case Uniform::Type::Int:
        return maxValue ? 100 : 0;
    case Uniform::Type::Float:
        return maxValue ? 1.0 : 0.0;
    case Uniform::Type::Vec2:
        return maxValue ? QVector2D(1.0, 1.0) : QVector2D(0.0, 0.0);
    case Uniform::Type::Vec3:
        return maxValue ? QVector3D(1.0, 1.0, 1.0) : QVector3D(0.0, 0.0, 0.0);
    case Uniform::Type::Vec4:
        return maxValue ? QVector4D(1.0, 1.0, 1.0, 1.0) : QVector4D(0.0, 0.0, 0.0, 0.0);
    case Uniform::Type::Color:
        return maxValue ? QColor::fromRgbF(1.0f, 1.0f, 1.0f, 1.0f) : QColor::fromRgbF(0.0f, 0.0f, 0.0f, 0.0f);
    default:
        return QVariant();
    }
}

QVariant Uniform::valueStringToVariant(const QString &value)
{
    QVariant variant;
    switch (m_type) {
    case Type::Bool:
        variant = (value == "true");
        break;
    case Type::Int:
    case Type::Float:
        variant = value;
        break;
    case Type::Vec2: {
        QStringList list = value.split(QLatin1Char(','));
        if (list.size() >= 2)
            variant = QVector2D(list.at(0).toDouble(), list.at(1).toDouble());
    }
    break;
    case Type::Vec3: {
        QStringList list = value.split(QLatin1Char(','));
        if (list.size() >= 3)
            variant = QVector3D(list.at(0).toDouble(), list.at(1).toDouble(), list.at(2).toDouble());
    }
    break;
    case Type::Vec4: {
        QStringList list = value.split(QLatin1Char(','));
        if (list.size() >= 4)
            variant = QVector4D(list.at(0).toDouble(), list.at(1).toDouble(),
                                list.at(2).toDouble(), list.at(3).toDouble());
    }
    break;
    case Type::Color: {
        QStringList list = value.split(QLatin1Char(','));
        if (list.size() >= 4)
            variant = QColor::fromRgbF(list.at(0).toDouble(), list.at(1).toDouble(),
                                       list.at(2).toDouble(), list.at(3).toDouble());
    }
    break;
    case Type::Sampler:
        variant = value;
        break;
    case Uniform::Type::Define:
        variant = value;
        break;
    }

    return variant;
}

QString Uniform::stringFromType(Uniform::Type type)
{
    if (type == Type::Bool)
        return "bool";
    else if (type == Type::Int)
        return "int";
    else if (type == Type::Float)
        return "float";
    else if (type == Type::Vec2)
        return "vec2";
    else if (type == Type::Vec3)
        return "vec3";
    else if (type == Type::Vec4)
        return "vec4";
    else if (type == Type::Color)
        return "color";
    else if (type == Type::Sampler)
        return "sampler2D";
    else if (type == Type::Define)
        return "define";

    qWarning() << QString("Unknown type");
    return "float";
}

Uniform::Type Uniform::typeFromString(const QString &typeString)
{
    if (typeString == "bool")
        return Uniform::Type::Bool;
    else if (typeString == "int")
        return Uniform::Type::Int;
    else if (typeString == "float")
        return Uniform::Type::Float;
    else if (typeString == "vec2")
        return Uniform::Type::Vec2;
    else if (typeString == "vec3")
        return Uniform::Type::Vec3;
    else if (typeString == "vec4")
        return Uniform::Type::Vec4;
    else if (typeString == "color")
        return Uniform::Type::Color;
    else if (typeString == "sampler2D")
        return Uniform::Type::Sampler;
    else if (typeString == "define")
        return Uniform::Type::Define;

    qWarning() << QString("Unknown type: %1").arg(typeString).toLatin1();
    return Uniform::Type::Float;
}

QString Uniform::typeToProperty(Uniform::Type type)
{
    if (type == Uniform::Type::Bool)
        return "bool";
    else if (type == Uniform::Type::Int)
        return "int";
    else if (type == Uniform::Type::Float)
        return "real";
    else if (type == Uniform::Type::Vec2)
        return "point";
    else if (type == Uniform::Type::Vec3)
        return "vector3d";
    else if (type == Uniform::Type::Vec4)
        return "vector4d";
    else if (type == Uniform::Type::Color)
        return "color";
    else if (type == Uniform::Type::Sampler || type == Uniform::Type::Define)
        return "var";

    qWarning() << QString("Unhandled const variable type: %1").arg(int(type)).toLatin1();
    return QString();
}

} // namespace QmlDesigner