aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/effectcomposer/uniform.cpp
blob: 590b38b423b83162fcaf7d241607a13b2f9e7305 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// 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 <qmldesignerplugin.h>

#include "propertyhandler.h"

#include <modelnodeoperations.h>

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

namespace EffectComposer {

Uniform::Uniform(const QString &effectName, const QJsonObject &propObj, const QString &qenPath)
    : m_qenPath(qenPath)
{
    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;

    value = propObj.contains("value") ? propObj.value("value").toString() : defaultValue;

    if (m_type == Type::Sampler) {
        if (!defaultValue.isEmpty())
            defaultValue = getResourcePath(effectName, defaultValue, qenPath);
        if (!value.isEmpty())
            value = getResourcePath(effectName, value, qenPath);
        if (value.isEmpty())
            value = defaultValue;
        if (propObj.contains("enableMipmap"))
            m_enableMipmap = getBoolValue(propObj.value("enableMipmap"), false);
        // Update the mipmap property
        QString mipmapProperty = mipmapPropertyName(m_name);
        g_propertyData[mipmapProperty] = m_enableMipmap;
    }

    QString controlType = propObj.value("controlType").toString();
    m_controlType = controlType.isEmpty() ? m_type : Uniform::typeFromString(controlType);

    m_customValue = propObj.value("customValue").toString();
    m_useCustomValue = getBoolValue(propObj.value("useCustomValue"), false);

    minValue = propObj.value("minValue").toString();
    maxValue = propObj.value("maxValue").toString();

    setValueData(value, defaultValue, minValue, maxValue);

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

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

Uniform::Type Uniform::controlType() const
{
    return m_controlType;
}

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

QString Uniform::controlTypeName() const
{
    return Uniform::stringFromType(m_controlType);
}

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();

        if (m_type == Type::Sampler) {
            m_backendValue->setValue(newValue);
            emit uniformBackendValueChanged();
        }
    }
}

void Uniform::setDefaultValue(const QVariant &newValue)
{
    if (m_defaultValue != newValue) {
        m_defaultValue = newValue;
        emit uniformDefaultValueChanged();
    }
}

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::displayName() const
{
    return m_displayName;
}

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;
}

QString Uniform::getDesignerSpecifics() const
{
    QString specs;

    // Uniforms with custom values or define type do not result in exported properties
    if (!m_customValue.isEmpty() || m_type == Type::Define)
        return specs;

    auto appendVectorSpinbox = [this, &specs](const QString subProp, const QString &label,
                                              float minVal, float maxVal, bool firstCol) {
        QString vecSpec =
R"(
                SpinBox {
                    minimumValue: %4
                    maximumValue: %5
                    decimals: 2
                    stepSize: .01
                    backendValue: backendValues.%1_%2
                    implicitWidth: StudioTheme.Values.twoControlColumnWidth
                                   + StudioTheme.Values.actionIndicatorWidth
                }

                Spacer { implicitWidth: StudioTheme.Values.controlLabelGap }

                ControlLabel {
                    text: "%3"
                }
)";
        specs += vecSpec.arg(m_name).arg(subProp).arg(label).arg(minVal).arg(maxVal);
        if (firstCol)
            specs += "                Spacer { implicitWidth: StudioTheme.Values.controlGap }\n";
    };

    auto appendVectorSeparator = [&specs]() {
        specs +=
R"(
                ExpandingSpacer {}
            }

            PropertyLabel {}

            SecondColumnLayout {
)";

    };

    specs +=
R"(
            PropertyLabel {
                text: "%1"
                tooltip: "%2"
            }

            SecondColumnLayout {
)";
    QString desc = m_description;
    desc.replace("\n", "\\n");
    desc.replace("\"", "\\\"");
    specs = specs.arg(m_displayName, desc);

    switch (m_type) {
    case Type::Bool: {
        QString typeSpec =
R"(
                CheckBox {
                    text: backendValues.%1.valueToString
                    backendValue: backendValues.%1
                    implicitWidth: StudioTheme.Values.twoControlColumnWidth
                                + StudioTheme.Values.actionIndicatorWidth
                }
)";
        specs += typeSpec.arg(m_name);
        break;
    }
    case Type::Int: {
        if (m_controlType == Uniform::Type::Int) {
            QString typeSpec =
    R"(
                SpinBox {
                    minimumValue: %1
                    maximumValue: %2
                    decimals: 0
                    stepSize: 1
                    sliderIndicatorVisible: true
                    backendValue: backendValues.%3
                    implicitWidth: StudioTheme.Values.singleControlColumnWidth
                                   + StudioTheme.Values.actionIndicatorWidth
                }
    )";
            specs += typeSpec.arg(m_minValue.toString(), m_maxValue.toString(), m_name);
        } else if (m_controlType == Uniform::Type::Channel) {
            QString typeSpec =
    R"(
                ComboBox {
                    model: ["R", "G", "B", "A"]
                    backendValue: backendValues.%1
                    manualMapping: true

                    onCompressedActivated: backendValue.value = currentIndex
                    onValueFromBackendChanged: currentIndex = backendValue.value
                    Component.onCompleted: currentIndex = backendValue.value
                }
    )";
            specs += typeSpec.arg(m_name);
        }
    }
    break;
    case Type::Float: {
        QString typeSpec =
R"(
                SpinBox {
                    minimumValue: %1
                    maximumValue: %2
                    decimals: 2
                    stepSize: .01
                    sliderIndicatorVisible: true
                    backendValue: backendValues.%3
                    implicitWidth: StudioTheme.Values.singleControlColumnWidth
                                   + StudioTheme.Values.actionIndicatorWidth
                }
)";
        specs += typeSpec.arg(m_minValue.toString(), m_maxValue.toString(), m_name);
        break;
    }
    case Type::Vec2: {
        QVector2D minVal = m_minValue.value<QVector2D>();
        QVector2D maxVal = m_maxValue.value<QVector2D>();
        appendVectorSpinbox("x", tr("X"), minVal.x(), maxVal.x(), true);
        appendVectorSpinbox("y", tr("Y"), minVal.y(), maxVal.y(), false);
        break;
    }
    case Type::Vec3: {
        QVector3D minVal = m_minValue.value<QVector3D>();
        QVector3D maxVal = m_maxValue.value<QVector3D>();
        appendVectorSpinbox("x", tr("X"), minVal.x(), maxVal.x(), true);
        appendVectorSpinbox("y", tr("Y"), minVal.y(), maxVal.y(), false);
        appendVectorSeparator();
        appendVectorSpinbox("z", tr("Z"), minVal.z(), maxVal.z(), true);
        break;
    }
    case Type::Vec4: {
        QVector4D minVal = m_minValue.value<QVector4D>();
        QVector4D maxVal = m_maxValue.value<QVector4D>();
        appendVectorSpinbox("x", tr("X"), minVal.x(), maxVal.x(), true);
        appendVectorSpinbox("y", tr("Y"), minVal.y(), maxVal.y(), false);
        appendVectorSeparator();
        appendVectorSpinbox("z", tr("Z"), minVal.z(), maxVal.z(), true);
        appendVectorSpinbox("w", tr("W"), minVal.w(), maxVal.w(), false);
        break;
    }
    case Type::Color: {
        QString typeSpec =
R"(
                ColorEditor {
                    backendValue: backendValues.%1
                    supportGradient: false
                }
)";
        specs += typeSpec.arg(m_name);
        break;
    }
    case Type::Sampler: {
        QString typeSpec =
R"(
                UrlChooser {
                    backendValue: backendValues.%1
                }
)";
        specs += typeSpec.arg(m_name + "Url");
        break;
    }
    case Type::Define:
    default:
        break;
    }

    specs +=
R"(
                ExpandingSpacer {}
            }
)";

    return specs;
}

// 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 &effectName, const QString &value, const QString &qenPath) const
{
    QString filePath = value;
    if (qenPath.isEmpty()) {
        const Utils::FilePath effectsResDir = QmlDesigner::ModelNodeOperations::getEffectsImportDirectory();
        return effectsResDir.pathAppended(effectName).pathAppended(value).toString();
    } else {
        QDir dir(m_qenPath);
        dir.cdUp();
        QString absPath = dir.absoluteFilePath(filePath);
        absPath = QDir::cleanPath(absPath);
        absPath = QUrl::fromLocalFile(absPath).toString();
        return absPath;
    }
}

// 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);
    case Uniform::Type::Channel:
        return 3; // sets default channel to alpha
    case Uniform::Type::Define:
        if (m_controlType == Uniform::Type::Bool)
            return maxValue ? true : false;
        else if (m_controlType == Uniform::Type::Int)
            return maxValue ? 100 : 0;
        else
            return QVariant();
    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:
    case Type::Channel:
        variant = value;
        break;
    case Uniform::Type::Define:
        if (m_controlType == Uniform::Type::Bool)
            variant = (value == "true");
        else
            variant = value;
        break;
    }

    return variant;
}

QString Uniform::stringFromType(Uniform::Type type, bool isShader)
{
    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 isShader ? QString("vec4") : QString("color");
    else if (type == Type::Sampler)
        return "sampler2D";
    else if (type == Type::Channel)
        return "channel";
    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" || typeString == "image") //TODO: change image to sample2D in all QENs
        return Uniform::Type::Sampler;
    else if (typeString == "channel")
        return Uniform::Type::Channel;
    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::Channel)
        return "channel";
    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 EffectComposer