diff --git a/src/plugins/qmldesigner/CMakeLists.txt b/src/plugins/qmldesigner/CMakeLists.txt index 268e40fc8a6..59f15ef159b 100644 --- a/src/plugins/qmldesigner/CMakeLists.txt +++ b/src/plugins/qmldesigner/CMakeLists.txt @@ -717,6 +717,7 @@ extend_qtc_plugin(QmlDesigner effectnodescategory.cpp effectnodescategory.h compositionnode.cpp compositionnode.h uniform.cpp uniform.h + effectutils.cpp effectutils.h ) extend_qtc_plugin(QmlDesigner diff --git a/src/plugins/qmldesigner/components/effectmaker/compositionnode.cpp b/src/plugins/qmldesigner/components/effectmaker/compositionnode.cpp index 7f1f84f8d8b..0fdf105407f 100644 --- a/src/plugins/qmldesigner/components/effectmaker/compositionnode.cpp +++ b/src/plugins/qmldesigner/components/effectmaker/compositionnode.cpp @@ -3,6 +3,7 @@ #include "compositionnode.h" +#include "effectutils.h" #include "uniform.h" #include @@ -64,10 +65,10 @@ void CompositionNode::parse(const QString &qenPath) return; } - m_name = json.value("name").toString(); //TODO: there is a difference between name and type + m_name = json.value("name").toString(); m_description = json.value("description").toString(); - m_fragmentCode = codeFromJsonArray(json.value("fragmentCode").toArray()); - m_vertexCode = codeFromJsonArray(json.value("vertexCode").toArray()); + m_fragmentCode = EffectUtils::codeFromJsonArray(json.value("fragmentCode").toArray()); + m_vertexCode = EffectUtils::codeFromJsonArray(json.value("vertexCode").toArray()); // parse properties QJsonArray properties = json.value("properties").toArray(); @@ -84,17 +85,4 @@ void CompositionNode::parse(const QString &qenPath) } } -QString CompositionNode::codeFromJsonArray(const QJsonArray &codeArray) -{ - if (codeArray.isEmpty()) - return {}; - - QString codeString; - for (const auto &element : codeArray) - codeString += element.toString() + '\n'; - - codeString.chop(1); // Remove last '\n' - return codeString; -} - } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/effectmaker/compositionnode.h b/src/plugins/qmldesigner/components/effectmaker/compositionnode.h index 9cb8ae85506..cc878a428c9 100644 --- a/src/plugins/qmldesigner/components/effectmaker/compositionnode.h +++ b/src/plugins/qmldesigner/components/effectmaker/compositionnode.h @@ -22,7 +22,6 @@ public: private: void parse(const QString &qenPath); - QString codeFromJsonArray(const QJsonArray &codeArray); QString m_name; QString m_fragmentCode; diff --git a/src/plugins/qmldesigner/components/effectmaker/effectutils.cpp b/src/plugins/qmldesigner/components/effectmaker/effectutils.cpp new file mode 100644 index 00000000000..8f45b9a1370 --- /dev/null +++ b/src/plugins/qmldesigner/components/effectmaker/effectutils.cpp @@ -0,0 +1,23 @@ +// 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 "effectutils.h" + +#include + +namespace QmlDesigner { + +QString EffectUtils::codeFromJsonArray(const QJsonArray &codeArray) +{ + if (codeArray.isEmpty()) + return {}; + + QString codeString; + for (const auto &element : codeArray) + codeString += element.toString() + '\n'; + + codeString.chop(1); // Remove last '\n' + return codeString; +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/effectmaker/effectutils.h b/src/plugins/qmldesigner/components/effectmaker/effectutils.h new file mode 100644 index 00000000000..0abe4d64e6b --- /dev/null +++ b/src/plugins/qmldesigner/components/effectmaker/effectutils.h @@ -0,0 +1,20 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#pragma once + +#include + +QT_FORWARD_DECLARE_CLASS(QJsonArray) + +namespace QmlDesigner { + +class EffectUtils +{ +public: + EffectUtils() = delete; + + static QString codeFromJsonArray(const QJsonArray &codeArray); +}; + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/effectmaker/uniform.cpp b/src/plugins/qmldesigner/components/effectmaker/uniform.cpp index a5d1be5b600..df1436d5c4f 100644 --- a/src/plugins/qmldesigner/components/effectmaker/uniform.cpp +++ b/src/plugins/qmldesigner/components/effectmaker/uniform.cpp @@ -7,9 +7,40 @@ namespace QmlDesigner { -Uniform::Uniform(const QJsonObject &props) +Uniform::Uniform(const QJsonObject &propObj) { - Q_UNUSED(props) + //TODO: some cases such as missing values or default values not yet implemented + + QString value, defaultValue; + + m_name = propObj.value("name").toString(); + m_description = propObj.value("description").toString(); + m_type = typeFromString(propObj.value("type").toString()); + defaultValue = propObj.value("defaultValue").toString(); + + 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; + } + m_customValue = propObj.value("customValue").toString(); + m_useCustomValue = getBoolValue(propObj.value("useCustomValue"), false); + m_minValue = propObj.value("minValue").toString(); + m_maxValue = propObj.value("maxValue").toString(); + //TODO: set uniform value data after validating, for now just set to current value + m_defaultValue = defaultValue; + m_value = value; } Uniform::Type Uniform::type() const @@ -47,11 +78,6 @@ QString Uniform::name() const return m_name; } -void Uniform::setName(const QString &newName) -{ - m_name = newName; -} - QString Uniform::description() const { return m_description; @@ -87,4 +113,62 @@ 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; +} + +Uniform::Type Uniform::typeFromString(const QString &typeString) const +{ + if (typeString == "bool") + return Type::Bool; + if (typeString == "int") + return Type::Int; + if (typeString == "float") + return Type::Float; + if (typeString == "vec2") + return Type::Vec2; + if (typeString == "vec3") + return Type::Vec3; + if (typeString == "vec4") + return Type::Vec4; + if (typeString == "color") + return Type::Color; + if (typeString == "image") + return Type::Sampler; + if (typeString == "define") + return Type::Define; + + qWarning() << QString("Unknown type: %1").arg(typeString).toLatin1(); + return Type::Float; +} + +// 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 {}; +} + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/effectmaker/uniform.h b/src/plugins/qmldesigner/components/effectmaker/uniform.h index 652b4d5f1ca..c3e4256e431 100644 --- a/src/plugins/qmldesigner/components/effectmaker/uniform.h +++ b/src/plugins/qmldesigner/components/effectmaker/uniform.h @@ -6,6 +6,8 @@ #include #include +QT_FORWARD_DECLARE_CLASS(QJsonObject) + namespace QmlDesigner { class Uniform : public QObject @@ -36,17 +38,13 @@ public: QVariant defaultValue() const; QVariant minValue() const; - QVariant maxValue() const; QString name() const; - void setName(const QString &newName); - QString description() const; QString customValue() const; void setCustomValue(const QString &newCustomValue); - bool useCustomValue() const; bool enabled() const; @@ -55,6 +53,12 @@ public: bool enableMipmap() const; private: + QString mipmapPropertyName(const QString &name) const; + + Uniform::Type typeFromString(const QString &typeString) const; + bool getBoolValue(const QJsonValue &jsonValue, bool defaultValue); + QString getResourcePath(const QString &value) const; + Type m_type; QVariant m_value; QVariant m_defaultValue;