diff --git a/CMakeLists.txt b/CMakeLists.txt index 49e64a0..9f31433 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,19 +400,6 @@ add_executable( src/WallpaperEngine/WebBrowser/CWebBrowserContext.cpp src/WallpaperEngine/WebBrowser/CWebBrowserContext.h - src/WallpaperEngine/Core/Projects/CProperty.h - src/WallpaperEngine/Core/Projects/CProperty.cpp - src/WallpaperEngine/Core/Projects/CPropertyColor.h - src/WallpaperEngine/Core/Projects/CPropertyColor.cpp - src/WallpaperEngine/Core/Projects/CPropertyBoolean.h - src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp - src/WallpaperEngine/Core/Projects/CPropertySlider.h - src/WallpaperEngine/Core/Projects/CPropertySlider.cpp - src/WallpaperEngine/Core/Projects/CPropertyCombo.h - src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp - src/WallpaperEngine/Core/Projects/CPropertyText.h - src/WallpaperEngine/Core/Projects/CPropertyText.cpp - src/WallpaperEngine/Data/Model/Types.h src/WallpaperEngine/Data/Model/Project.h src/WallpaperEngine/Data/Model/Wallpaper.h @@ -423,6 +410,7 @@ add_executable( src/WallpaperEngine/Data/Model/UserSetting.h src/WallpaperEngine/Data/Model/DynamicValue.h src/WallpaperEngine/Data/Model/DynamicValue.cpp + src/WallpaperEngine/Data/Model/Property.h src/WallpaperEngine/Data/Utils/TypeCaster.cpp src/WallpaperEngine/Data/Utils/TypeCaster.h src/WallpaperEngine/Data/Utils/SFINAE.h @@ -440,6 +428,8 @@ add_executable( src/WallpaperEngine/Data/Parsers/ModelParser.h src/WallpaperEngine/Data/Parsers/ShaderConstantParser.cpp src/WallpaperEngine/Data/Parsers/ShaderConstantParser.h + src/WallpaperEngine/Data/Parsers/PropertyParser.cpp + src/WallpaperEngine/Data/Parsers/PropertyParser.h src/WallpaperEngine/Data/Builders/UserSettingBuilder.h src/WallpaperEngine/Data/Builders/VectorBuilder.cpp src/WallpaperEngine/Data/Builders/VectorBuilder.h diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.cpp b/src/WallpaperEngine/Application/CWallpaperApplication.cpp index a32ec59..cfdabc3 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.cpp +++ b/src/WallpaperEngine/Application/CWallpaperApplication.cpp @@ -10,12 +10,11 @@ #include "WallpaperEngine/Render/CRenderContext.h" #include "WallpaperEngine/Render/Drivers/CVideoFactories.h" -#include "WallpaperEngine/Core/Projects/CProperty.h" - #include "WallpaperEngine/Data/Parsers/ProjectParser.h" #include "WallpaperEngine/Data/Dumpers/StringPrinter.h" #include "WallpaperEngine/Data/Model/Wallpaper.h" +#include "WallpaperEngine/Data/Model/Property.h" #if DEMOMODE #include "recording.h" @@ -227,11 +226,11 @@ void CWallpaperApplication::setupPropertiesForProject (const Project& project) { if (override != this->m_context.settings.general.properties.end ()) { sLog.out ("Applying override value for ", key); - cur->set (override->second); + cur->update (override->second); } if (this->m_context.settings.general.onlyListProperties) - sLog.out (cur->dump ()); + sLog.out (cur->toString ()); } } diff --git a/src/WallpaperEngine/Core/Projects/CProperty.cpp b/src/WallpaperEngine/Core/Projects/CProperty.cpp deleted file mode 100644 index 0b40e9d..0000000 --- a/src/WallpaperEngine/Core/Projects/CProperty.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "CProperty.h" -#include "CPropertyBoolean.h" -#include "CPropertyColor.h" -#include "CPropertyCombo.h" -#include "CPropertySlider.h" -#include "CPropertyText.h" -#include "WallpaperEngine/Logging/CLog.h" -#include -#include - -using namespace WallpaperEngine::Core::Projects; - -std::shared_ptr CProperty::fromJSON (const JSON& data, const std::string& name) { - const auto type = data.require ("type", "Project properties must have the type field"); - - if (type == "color") - return CPropertyColor::fromJSON (data, name); - if (type == "bool") - return CPropertyBoolean::fromJSON (data, name); - if (type == "slider") - return CPropertySlider::fromJSON (data, name); - if (type == "combo") - return CPropertyCombo::fromJSON (data, name); - if (type == "text") - return CPropertyText::fromJSON (data, name); - - if (type != "group") { - // show the error and ignore this property - sLog.error ("Unexpected type for property: ", type); - sLog.error (data); - } - - return nullptr; -} - -CProperty::CProperty (std::string name, std::string text) : - m_name (std::move(name)), - m_text (std::move(text)) {} - -const std::string& CProperty::getName () const { - return this->m_name; -} - -const std::string& CProperty::getText () const { - return this->m_text; -} \ No newline at end of file diff --git a/src/WallpaperEngine/Core/Projects/CProperty.h b/src/WallpaperEngine/Core/Projects/CProperty.h deleted file mode 100644 index 80f49a4..0000000 --- a/src/WallpaperEngine/Core/Projects/CProperty.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "WallpaperEngine/Data/Utils/TypeCaster.h" -#include "WallpaperEngine/Data/Model/DynamicValue.h" -#include "WallpaperEngine/Data/JSON.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON; -using namespace WallpaperEngine::Data::Model; -using namespace WallpaperEngine::Data::Utils; -/** - * Represents a property in a background - * - * Properties are settings that alter how the background looks or works - * and are configurable by the user so they can customize it to their likings - */ -class CProperty : public DynamicValue, public TypeCaster { - public: - using UniquePtr = std::unique_ptr; - using SharedPtr = std::shared_ptr; - using WeakPtr = std::weak_ptr; - - static std::shared_ptr fromJSON (const JSON& data, const std::string& name); - - /** - * @return Representation of what the property does and the default values - */ - [[nodiscard]] virtual std::string dump () const = 0; - /** - * Updates the value of the property with the one in the string - * - * @param value New value for the property - */ - virtual void set (const std::string& value) = 0; - /** - * @return Name of the property - */ - [[nodiscard]] const std::string& getName () const; - /** - * @return Textual type representation of this property - */ - [[nodiscard]] virtual const char* getPropertyType () const = 0; - /** - * @return Text of the property - */ - [[nodiscard]] const std::string& getText () const; - - protected: - CProperty (std::string name, std::string text); - /** Name of the property */ - const std::string m_name; - /** Description of the property for the user */ - mutable std::string m_text; -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp deleted file mode 100644 index dc1d0f6..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -#include "CPropertyBoolean.h" - -using namespace WallpaperEngine::Core::Projects; - -std::shared_ptr CPropertyBoolean::fromJSON (const JSON& data, std::string name) { - return std::make_shared ( - data.require ("value", "Boolean property must have a value"), - std::move(name), - data.optional ("text", "") - ); -} - - -void CPropertyBoolean::set (const std::string& value) { - this->update (value == "1" || value == "true" || value == "on"); -} - -std::string CPropertyBoolean::dump () const { - std::stringstream ss; - - ss << this->m_name << " - boolean" << std::endl - << "\t" - << "Description: " << this->m_text << std::endl - << "\t" - << "Value: " << &this->getBool (); - - return ss.str (); -} - -const char* CPropertyBoolean::getPropertyType () const { - return "bool"; -} - -CPropertyBoolean::CPropertyBoolean (bool value, std::string name, std::string text) : - CProperty (std::move(name), std::move(text)) {} diff --git a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h deleted file mode 100644 index 72f3f85..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "CProperty.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON; - -/** - * Represents a boolean property - */ -class CPropertyBoolean final : public CProperty { - public: - CPropertyBoolean (bool value, std::string name, std::string text); - - static std::shared_ptr fromJSON (const JSON& data, std::string name); - [[nodiscard]] std::string dump () const override; - void set (const std::string& value) override; - - [[nodiscard]] const char* getPropertyType () const override; -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp b/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp deleted file mode 100644 index 045f6f9..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include - -#include "CPropertyColor.h" - -using namespace WallpaperEngine::Data::Builders; -using namespace WallpaperEngine::Core::Projects; - -glm::vec3 ParseColor (std::string value) { - // TODO: ENSURE THIS PARSING IS ACTUALLY ACCURATE - if (value.find (',') != std::string::npos) { - // replace commas with dots so it can be parsed - std::replace (value.begin (), value.end (), ',', ' '); - } - - if (value.find ('.') == std::string::npos && value != "0 0 0" && value != "1 1 1") { - if (value.find ('#') == 0) { - // hex color, parse it to int color and then convert it to float - auto number = value.substr (1); - - if (number.size () == 3) { - // CSS short-number, expand to the proper size - number = number[0] + number[0] + number[1] + number[1] + number[2] + number[2]; - } - - // remove alpha if it's present, should look into it more closely - if (number.size () > 6) { - sLog.error ("Color value has alpha channel, which is not supported"); - number = number.substr (0, 6); - } - - const auto color = std::stoi (number, nullptr, 16); - - return { - (((color >> 16) & 0xFF) / 255.0), - (((color >> 8) & 0xFF) / 255.0), - (((color >> 0) & 0xFF) / 255.0) - }; - } - - const auto intcolor = VectorBuilder::parse (value); - - return {intcolor.r / 255.0, intcolor.g / 255.0, intcolor.b / 255.0}; - } - - return VectorBuilder::parse (value); -} - -std::shared_ptr CPropertyColor::fromJSON (const JSON& data, std::string name) { - return std::make_shared ( - data.require ("value", "Color property must have a value"), - std::move(name), - data.optional ("text", "") - ); -} - -void CPropertyColor::set (const std::string& value) { - this->update (ParseColor (std::string (value))); -} - -std::string CPropertyColor::dump () const { - const auto color = this->getVec3 (); - std::stringstream ss; - - ss << this->m_name << " - color" << std::endl - << "\t" - << "Description: " << this->m_text << std::endl - << "\t" - << "R: " << color.r << " G: " << color.g << " B: " << color.b; - - return ss.str (); -} - -const char* CPropertyColor::getPropertyType () const { - return "color"; -} - -CPropertyColor::CPropertyColor (const std::string& color, std::string name, std::string text) : - CProperty (std::move(name), std::move(text)) { - this->set (color); -} diff --git a/src/WallpaperEngine/Core/Projects/CPropertyColor.h b/src/WallpaperEngine/Core/Projects/CPropertyColor.h deleted file mode 100644 index 4718e59..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyColor.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "CProperty.h" - -#include "WallpaperEngine/Data/JSON.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON; - -/** - * Represents a color property - */ -class CPropertyColor final : public CProperty { - public: - CPropertyColor (const std::string& color, std::string name, std::string text); - - static std::shared_ptr fromJSON (const JSON& data, std::string name); - [[nodiscard]] std::string dump () const override; - void set (const std::string& value) override; - - [[nodiscard]] const char* getPropertyType () const override; -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp b/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp deleted file mode 100644 index 2784802..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include -#include - -#include "CPropertyCombo.h" - -#include "WallpaperEngine/Logging/CLog.h" - -using namespace WallpaperEngine::Core::Projects; - -std::shared_ptr CPropertyCombo::fromJSON (const JSON& data, std::string name) { - std::vector values; - const auto options = data.require ("options", "Options for a property combo is required"); - - if (!options.is_array ()) - sLog.exception ("Property combo options should be an array"); - - for (auto& cur : options) { - // TODO: PROPERLY REPORT THESE ISSUES - if (!cur.is_object ()) - continue; - - const auto valueIt = cur.require ("value", "Value is required for a property combo option"); - auto value = valueIt.is_number () ? std::to_string (valueIt.get ()) : valueIt.get (); - - // check for label and value to ensure they're there - values.push_back ({ - .label = cur.require ("label", "Label is required for a property combo option"), - .value = value - }); - } - - const auto valueIt = data.require ("value", "Value is required for a property combo"); - auto value = valueIt.is_number () ? std::to_string (valueIt.get ()) : valueIt.get (); - - return std::make_shared ( - std::move(name), - data.optional ("text", ""), - value, - values - ); -} - -CPropertyCombo::CPropertyCombo ( - std::string name, std::string text, const std::string& defaultValue, - std::vector values -) : - CProperty (std::move(name), std::move(text)), - m_values (std::move(values)) { - this->set (defaultValue); -} - -std::string CPropertyCombo::dump () const { - std::stringstream ss; - - ss << this->m_name << " - combolist" << std::endl - << "\t" - << "Description: " << this->m_text << std::endl - << "\t" - << "Value: " << &this->getInt () << std::endl - << "\t\t" - << "Posible values:" << std::endl; - - for (const auto& cur : this->m_values) - ss << "\t\t" << cur.label << " -> " << cur.value << std::endl; - - return ss.str (); -} - -void CPropertyCombo::set (const std::string& value) { - bool found = false; - int index = 0; - - // ensure the value is present somewhere in the value list - for (const auto& cur : this->m_values) { - if (cur.value == value) { - found = true; - break; - } - - index ++; - } - - if (!found) - sLog.exception ("Assigning invalid value to property ", this->m_name); - - this->update (index); -} - -int CPropertyCombo::translateValueToIndex (const std::string& value) const { - bool found = false; - int index = 0; - - // ensure the value is present somewhere in the value list - for (const auto& cur : this->m_values) { - if (cur.value == value) { - found = true; - break; - } - - index ++; - } - - if (!found) { - return -1; - } - - return index; -} - -const char* CPropertyCombo::getPropertyType () const { - return "combo"; -} diff --git a/src/WallpaperEngine/Core/Projects/CPropertyCombo.h b/src/WallpaperEngine/Core/Projects/CPropertyCombo.h deleted file mode 100644 index 9d7b323..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyCombo.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "CProperty.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON;; - -/** - * Represents different combo values - */ -struct CPropertyComboValue { - public: - const std::string label; - const std::string value; -}; - -/** - * Represents a combo property - * - * Combos are properties that have different values available and only one can be selected at once - * this limits the user's possibilities, used for things like the amount of samples to use in audioprocessing - * backgrounds - */ -class CPropertyCombo final : public CProperty { - public: - static std::shared_ptr fromJSON (const JSON& data, std::string name); - - CPropertyCombo ( - std::string name, std::string text, const std::string& defaultValue, - std::vector values); - - [[nodiscard]] std::string dump () const override; - void set (const std::string& value) override; - int translateValueToIndex (const std::string& value) const; - - [[nodiscard]] const char* getPropertyType () const override; - - private: - /** List of values available to select */ - const std::vector m_values; -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp b/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp deleted file mode 100644 index f8b7d47..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "CPropertySlider.h" -#include "WallpaperEngine/Logging/CLog.h" -#include - -using namespace WallpaperEngine::Core::Projects; - -std::shared_ptr CPropertySlider::fromJSON (const JSON& data, const std::string& name) { - const auto value = data.find ("value"); - const auto text = data.optional ("text", ""); - const auto min = data.optional ("min", 0.0f); - const auto max = data.optional ("max", 0.0f); - const auto step = data.optional ("step", 0.0f); - - return std::make_shared (*value, name, text, min, max, step); -} - -const float& CPropertySlider::getMinValue () const { - return this->m_min; -} - -const float& CPropertySlider::getMaxValue () const { - return this->m_max; -} - -const float& CPropertySlider::getStep () const { - return this->m_step; -} - -std::string CPropertySlider::dump () const { - std::stringstream ss; - - ss << this->m_name << " - slider" << std::endl - << "\t" - << "Description: " << this->m_text << std::endl - << "\t" - << "Value: " << &this->getFloat () << std::endl - << "\t" - << "Minimum value: " << this->m_min << std::endl - << "\t" - << "Maximum value: " << this->m_max << std::endl - << "\t" - << "Step: " << this->m_step << std::endl; - - return ss.str (); -} - -void CPropertySlider::set (const std::string& value) { - const auto newValue = strtof (value.c_str (), nullptr); - - if (newValue < this->m_min || newValue > this->m_max) - sLog.exception ("Slider value (", newValue, ") is out of range (", this->m_min, ",", this->m_max, ")"); - - this->update (newValue); -} - -const char* CPropertySlider::getPropertyType () const { - return "slider"; -} - -CPropertySlider::CPropertySlider (float value, const std::string& name, const std::string& text, float min, - float max, float step) : - CProperty (name, text), - m_min (min), - m_max (max), - m_step (step) { - this->update (value); -} diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.h b/src/WallpaperEngine/Core/Projects/CPropertySlider.h deleted file mode 100644 index bfb13f2..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertySlider.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include "CProperty.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON; - -/** - * Represents a slider value with a minimum and maximum value - */ -class CPropertySlider final : public CProperty { - public: - CPropertySlider (float value, const std::string& name, const std::string& text, float min, float max, float step); - - static std::shared_ptr fromJSON (const JSON& data, const std::string& name); - /** - * @return The slider's minimum value - */ - [[nodiscard]] const float& getMinValue () const; - /** - * @return The slider's maximum value - */ - [[nodiscard]] const float& getMaxValue () const; - /** - * @return The slider's value increment steps, only really used in the UI - */ - [[nodiscard]] const float& getStep () const; - [[nodiscard]] std::string dump () const override; - void set (const std::string& value) override; - - [[nodiscard]] const char* getPropertyType () const override; - - private: - /** Minimum value */ - const float m_min; - /** Maximum value */ - const float m_max; - /** Increment steps for the slider in the UI */ - const float m_step; -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Core/Projects/CPropertyText.cpp b/src/WallpaperEngine/Core/Projects/CPropertyText.cpp deleted file mode 100644 index 6e27910..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyText.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "CPropertyText.h" -#include -#include - -using namespace WallpaperEngine::Core::Projects; - -std::shared_ptr CPropertyText::fromJSON (const JSON& data, std::string name) { - //TODO: VALIDATE THIS IS RIGHT - return std::make_shared ( - std::move(name), - data.optional ("text", "") - ); -} - -std::string CPropertyText::dump () const { - std::stringstream ss; - - ss << this->m_name << " - text" << std::endl - << "\t" - << "Value: " << this->m_text; - - return ss.str (); -} - -void CPropertyText::set (const std::string& value) { - this->m_text = value; -} - -const char* CPropertyText::getPropertyType () const { - return "text"; -} - -CPropertyText::CPropertyText (std::string name, std::string text) : - CProperty (std::move(name), std::move(text)) {} diff --git a/src/WallpaperEngine/Core/Projects/CPropertyText.h b/src/WallpaperEngine/Core/Projects/CPropertyText.h deleted file mode 100644 index b64e88d..0000000 --- a/src/WallpaperEngine/Core/Projects/CPropertyText.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "CProperty.h" - -namespace WallpaperEngine::Core::Projects { -using JSON = WallpaperEngine::Data::JSON::JSON; - -/** - * Represents a text property - */ -class CPropertyText final : public CProperty { - public: - CPropertyText (std::string name, std::string text); - - static std::shared_ptr fromJSON (const JSON& data, std::string name); - [[nodiscard]] std::string dump () const override; - void set (const std::string& value) override; - - [[nodiscard]] const char* getPropertyType () const override; - private: -}; -} // namespace WallpaperEngine::Core::Projects diff --git a/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h b/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h index 82c55cc..e3d4335 100644 --- a/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h +++ b/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h @@ -13,7 +13,7 @@ class UserSettingBuilder { return std::make_unique (UserSetting { .value = std::move (value), - .property = PropertyWeakPtr (), + .property = PropertySharedPtr (), .condition = std::nullopt, }); } diff --git a/src/WallpaperEngine/Data/Model/DynamicValue.cpp b/src/WallpaperEngine/Data/Model/DynamicValue.cpp index 7450ac3..bc99a6e 100644 --- a/src/WallpaperEngine/Data/Model/DynamicValue.cpp +++ b/src/WallpaperEngine/Data/Model/DynamicValue.cpp @@ -271,6 +271,9 @@ void DynamicValue::connect (DynamicValue* other) { this->update (other); }); + // update our value on connection + this->update (*other); + this->m_connections.push_back (deregisterFunction); } diff --git a/src/WallpaperEngine/Data/Model/DynamicValue.h b/src/WallpaperEngine/Data/Model/DynamicValue.h index 6d6f1d9..7bf8ccc 100644 --- a/src/WallpaperEngine/Data/Model/DynamicValue.h +++ b/src/WallpaperEngine/Data/Model/DynamicValue.h @@ -45,7 +45,7 @@ class DynamicValue { [[nodiscard]] const int& getInt () const; [[nodiscard]] const bool& getBool () const; [[nodiscard]] UnderlyingType getType () const; - [[nodiscard]] std::string toString () const; + [[nodiscard]] virtual std::string toString () const; void update (float newValue); void update (int newValue); diff --git a/src/WallpaperEngine/Data/Model/Object.h b/src/WallpaperEngine/Data/Model/Object.h index 0dfdf8b..8e3e9b1 100644 --- a/src/WallpaperEngine/Data/Model/Object.h +++ b/src/WallpaperEngine/Data/Model/Object.h @@ -65,7 +65,7 @@ struct ImageEffect { /** Effect's name for the editor */ std::string name; /** If this effect is visible or not */ - UserSettingSharedPtr visible; + UserSettingUniquePtr visible; /** Pass overrides to apply to the effect's passes */ std::vector passOverrides; /** The effect definition */ @@ -74,17 +74,17 @@ struct ImageEffect { struct ImageData { /** The point of origin of the image */ - UserSettingSharedPtr origin; + UserSettingUniquePtr origin; /** The scale of the image */ - UserSettingSharedPtr scale; + UserSettingUniquePtr scale; /** The rotation of the image */ - UserSettingSharedPtr angles; + UserSettingUniquePtr angles; /** If the image is visible or not */ - UserSettingSharedPtr visible; + UserSettingUniquePtr visible; /** The alpha of the image */ - UserSettingSharedPtr alpha; + UserSettingUniquePtr alpha; /** The color of the image */ - UserSettingSharedPtr color; + UserSettingUniquePtr color; // TODO: WRITE A COUPLE OF ENUMS FOR THIS /** The alignment of the image */ std::string alignment; diff --git a/src/WallpaperEngine/Data/Model/Property.h b/src/WallpaperEngine/Data/Model/Property.h new file mode 100644 index 0000000..d919e9c --- /dev/null +++ b/src/WallpaperEngine/Data/Model/Property.h @@ -0,0 +1,138 @@ +#pragma once + +#include +#include +#include +#include "DynamicValue.h" +#include "../Utils/TypeCaster.h" + +namespace WallpaperEngine::Data::Model { +using namespace WallpaperEngine::Data::Utils; +using namespace WallpaperEngine::Data::Builders; + +struct PropertyData { + std::string name; + std::string text; +}; + +struct SliderData { + float min; + float max; + float step; +}; + +struct ComboData { + std::map values; +}; + +class Property : public DynamicValue, public TypeCaster, public PropertyData { + public: + explicit Property (PropertyData data) : PropertyData (std::move(data)), TypeCaster (), DynamicValue () {} + + using DynamicValue::update; + virtual void update(const std::string& value) = 0; +}; + +class PropertySlider : public Property, SliderData { + public: + PropertySlider (PropertyData data, SliderData sliderData, float value) : Property (std::move(data)), SliderData (sliderData) { + this->update (value); + } + + using Property::update; + void update(const std::string& value) override { + this->update (std::stof (value)); + } +}; + +class PropertyBoolean : public Property { + public: + explicit PropertyBoolean (PropertyData data, bool value) : Property (std::move(data)) { + this->update (value); + } + + using Property::update; + void update(const std::string& value) override { + this->update (value == "true" || value == "1"); + } +}; + +class PropertyColor : public Property { + public: + explicit PropertyColor (PropertyData data, std::string value) : Property (std::move(data)) { + this->update (value); + } + + using Property::update; + void update(const std::string& value) override { + auto copy = value; + + // TODO: ENSURE ALL THIS PARSING IS CORRECT + if (copy.find (',') != std::string::npos) { + // replace comma separator with spaces so it's + std::replace (copy.begin (), copy.end (), ',', ' '); + } + + // hex colors should be converted to int colors + if (copy.find ('#') == 0) { + auto number = copy.substr (1); + + // support for css notation + if (number.size () == 3) { + number = number[0] + number[0] + number[1] + number[1] + number[2] + number[2]; + } + + // remove alpha if it's present, should look into it more closely + if (number.size () > 6) { + sLog.error ("Color value has alpha channel, which is not supported"); + number = number.substr (0, 6); + } + + const auto color = std::stoi (number, nullptr, 16); + + // format the number as float vector + copy = + std::to_string (((color >> 16) & 0xFF) / 255.0) + " " + + std::to_string (((color >> 8) & 0xFF) / 255.0) + " " + + std::to_string ((color & 0xFF) / 255.0); + } else if (copy.find ('.') == std::string::npos) { + // integer vector, convert it to float vector + const auto intcolor = VectorBuilder::parse (copy); + + copy = + std::to_string (intcolor.r / 255.0) + " " + + std::to_string (intcolor.g / 255.0) + " " + + std::to_string (intcolor.b / 255.0); + } + + // finally parse the string as a float vector + this->update (VectorBuilder::parse (copy)); + } +}; + +class PropertyCombo : public Property, ComboData { + public: + PropertyCombo (PropertyData data, ComboData comboData, std::string value) : Property (std::move(data)), ComboData (std::move(comboData)) { + this->update (value); + } + + using Property::update; + void update(const std::string& value) override { + this->update (std::stoi (value)); + } +}; + +class PropertyText : public Property { + public: + explicit PropertyText (PropertyData data) : Property (std::move(data)) {} + + using Property::update; + void update(const std::string& value) override { + throw std::runtime_error ("PropertyText::update() is not implemented"); + } + + [[nodiscard]] std::string toString () const override { + return this->text; + } +}; +} \ No newline at end of file diff --git a/src/WallpaperEngine/Data/Model/Types.h b/src/WallpaperEngine/Data/Model/Types.h index 6fff328..4d4e374 100644 --- a/src/WallpaperEngine/Data/Model/Types.h +++ b/src/WallpaperEngine/Data/Model/Types.h @@ -8,10 +8,6 @@ #include "WallpaperEngine/Assets/CContainer.h" -namespace WallpaperEngine::Core::Projects { -class CProperty; -} - namespace WallpaperEngine::Data::Model { struct Project; class Wallpaper; @@ -26,6 +22,12 @@ struct ImageEffect; struct ImageEffectPassOverride; class Particle; class DynamicValue; +class Property; +class PropertySlider; +class PropertyBoolean; +class PropertyCombo; +class PropertyText; +class PropertyColor; struct Material; struct MaterialPass; struct FBO; @@ -36,16 +38,10 @@ struct ModelStruct; // TODO: REMOVE ONCE THESE ARE RENAMED AND MOVED using Container = WallpaperEngine::Assets::CContainer; -using Property = WallpaperEngine::Core::Projects::CProperty; using PropertySharedPtr = std::shared_ptr ; -using PropertyWeakPtr = std::weak_ptr ; using Properties = std::map ; using DynamicValueUniquePtr = std::unique_ptr ; -using DynamicValueSharedPtr = std::shared_ptr ; -using DynamicValueWeakPtr = std::weak_ptr ; -using UserSettingSharedPtr = std::shared_ptr ; using UserSettingUniquePtr = std::unique_ptr ; -using UserSettingWeakPtr = std::weak_ptr ; // TODO: UP TO THIS POINT using ShaderConstantMap = std::map ; diff --git a/src/WallpaperEngine/Data/Model/UserSetting.h b/src/WallpaperEngine/Data/Model/UserSetting.h index 7ef7c71..8ad1c9c 100644 --- a/src/WallpaperEngine/Data/Model/UserSetting.h +++ b/src/WallpaperEngine/Data/Model/UserSetting.h @@ -22,7 +22,7 @@ struct UserSetting { */ DynamicValueUniquePtr value; /** The property this setting takes the value from (if specified) */ - PropertyWeakPtr property; + PropertySharedPtr property; /** Condition required for this setting, this should be possible to run in JS' V8 */ std::optional condition; /** TODO: Value might come from a script and not have conditions, implement this later */ diff --git a/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp b/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp index 6b6b325..8ccc31f 100644 --- a/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp @@ -5,8 +5,8 @@ #include "WallpaperParser.h" +#include "PropertyParser.h" #include "WallpaperEngine/Data/Model/Wallpaper.h" -#include "WallpaperEngine/Core/Projects/CProperty.h" using namespace WallpaperEngine::Data::Parsers; @@ -70,18 +70,15 @@ Properties ProjectParser::parseProperties (const std::optional & data) { Properties result = {}; - // TODO: CHANGE THIS ONCE THE PROPERTIES PARSING IS HANDLED IN THE NEW TYPES - // THESE ARE COMPLEX TYPES THAT INCLUDE SOME RUNTIME INFO THAT ISN'T EXPLICITLY DATA - // SO THIS WILL NEED A RETHINK IN THE FUTURE for (const auto& cur : properties.value ().items ()) { - const auto& property = Property::fromJSON (cur.value (), cur.key ()); + const auto& property = PropertyParser::parse (cur.value (), cur.key ()); // ignore properties that failed, these are generally groups if (property == nullptr) { continue; } - result.emplace (property->getName (), property); + result.emplace (cur.key (), property); } return result; diff --git a/src/WallpaperEngine/Data/Parsers/PropertyParser.cpp b/src/WallpaperEngine/Data/Parsers/PropertyParser.cpp new file mode 100644 index 0000000..93f1247 --- /dev/null +++ b/src/WallpaperEngine/Data/Parsers/PropertyParser.cpp @@ -0,0 +1,98 @@ +#include "PropertyParser.h" +#include "../Model/Property.h" + +using namespace WallpaperEngine::Data::Parsers; +using namespace WallpaperEngine::Data::Model; + +PropertySharedPtr PropertyParser::parse (const JSON& it, std::string name) { + const auto type = it.require ("type", "Property type is required"); + + if (type == "color") { + return parseColor (it, name); + } + if (type == "bool") { + return parseBoolean (it, name); + } + if (type == "slider") { + return parseSlider (it, name); + } + if (type == "combo") { + return parseCombo (it, name); + } + if (type == "text") { + return parseText (it, name); + } + + if (type != "group") { + // show the error and ignore this property + sLog.error ("Unexpected type for property: ", type); + sLog.error (it.dump ()); + } + + return nullptr; +} + + +PropertySharedPtr PropertyParser::parseCombo (const JSON& it, std::string name) { + std::map optionsMap = {}; + + const auto options = it.require ("options", "Combo property must have options"); + + if (!options.is_array ()) { + sLog.exception ("Property combo options should be an array"); + } + + for (auto& cur : options) { + if (!cur.is_object ()) { + continue; + } + + const auto value = cur.require ("value", "Combo option must have a value"); + + optionsMap.emplace ( + cur.require ("label", "Combo option must have a label"), + value.is_number () ? std::to_string (value.get ()) : value.get () + ); + } + + const auto value = it.require ("value", "Combo property must have a value"); + + return std::make_shared (PropertyData { + .name = name, + .text = it.optional ("text", ""), + }, ComboData { + .values = optionsMap + }, value.is_number () ? std::to_string (value.get ()) : value.get ()); +} + +PropertySharedPtr PropertyParser::parseColor (const JSON& it, std::string name) { + return std::make_shared (PropertyData { + .name = name, + .text = it.optional ("text", ""), + }, it.require ("value", "Property must have a value")); +} + +PropertySharedPtr PropertyParser::parseBoolean (const JSON& it, std::string name) { + return std::make_shared (PropertyData { + .name = name, + .text = it.optional ("text", ""), + }, it.require ("value", "Property must have a value")); +} + +PropertySharedPtr PropertyParser::parseSlider (const JSON& it, std::string name) { + return std::make_shared (PropertyData { + .name = name, + .text = it.optional ("text", ""), + }, SliderData { + .min = it.optional ("min", 0.0f), + .max = it.optional ("max", 0.0f), + .step = it.optional ("step", 0.0f), + }, it.require ("value", "Property must have a value")); +} + +PropertySharedPtr PropertyParser::parseText (const JSON& it, std::string name) { + return std::make_shared (PropertyData { + .name = name, + .text = it.optional ("text", ""), + }); +} \ No newline at end of file diff --git a/src/WallpaperEngine/Data/Parsers/PropertyParser.h b/src/WallpaperEngine/Data/Parsers/PropertyParser.h new file mode 100644 index 0000000..5dd2e83 --- /dev/null +++ b/src/WallpaperEngine/Data/Parsers/PropertyParser.h @@ -0,0 +1,20 @@ +#pragma once + +#include "WallpaperEngine/Data/JSON.h" + +namespace WallpaperEngine::Data::Parsers { +using JSON = WallpaperEngine::Data::JSON::JSON; +using namespace WallpaperEngine::Data::Model; + +class PropertyParser { + public: + static PropertySharedPtr parse (const JSON& it, std::string name); + + private: + static PropertySharedPtr parseCombo (const JSON& it, std::string name); + static PropertySharedPtr parseColor (const JSON& it, std::string name); + static PropertySharedPtr parseBoolean (const JSON& it, std::string name); + static PropertySharedPtr parseSlider (const JSON& it, std::string name); + static PropertySharedPtr parseText (const JSON& it, std::string name); +}; +} \ No newline at end of file diff --git a/src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp b/src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp index 9d7224a..9fe7016 100644 --- a/src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp @@ -1,13 +1,14 @@ #include "UserSettingParser.h" #include "WallpaperEngine/Data/Model/UserSetting.h" +#include "WallpaperEngine/Data/Model/Property.h" using namespace WallpaperEngine::Data::Parsers; using namespace WallpaperEngine::Data::Builders; UserSettingUniquePtr UserSettingParser::parse (const json& data, const Properties& properties) { DynamicValueUniquePtr value = std::make_unique (); - PropertyWeakPtr property; + PropertySharedPtr property; std::optional condition; auto valueIt = data; std::string content = data.dump (); @@ -64,6 +65,9 @@ UserSettingUniquePtr UserSettingParser::parse (const json& data, const Propertie value->update (valueIt.get ()); } else if (valueIt.is_boolean ()) { value->update (valueIt.get ()); + } else if (valueIt.is_null () && property != nullptr) { + // null values are directly connected to the property + value->connect (property.get()); } else { sLog.exception ("Unsupported user setting type ", valueIt.type_name ()); } diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index 03a0d81..7f3cfba 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -10,12 +10,6 @@ #include "WallpaperEngine/Render/Objects/CImage.h" #include "WallpaperEngine/Render/CFBO.h" -#include "WallpaperEngine/Core/Projects/CProperty.h" -#include "WallpaperEngine/Core/Projects/CPropertyColor.h" -#include "WallpaperEngine/Core/Projects/CPropertyCombo.h" -#include "WallpaperEngine/Core/Projects/CPropertySlider.h" -#include "WallpaperEngine/Core/Projects/CPropertyBoolean.h" - #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h" #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableFloat.h" #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableInteger.h" diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.h b/src/WallpaperEngine/Render/Objects/Effects/CPass.h index 553617c..a4a6efc 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.h +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.h @@ -17,7 +17,6 @@ class CImage; namespace WallpaperEngine::Render::Objects::Effects { using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Render::Shaders::Variables; -using namespace WallpaperEngine::Core::Projects; using namespace WallpaperEngine::Data::Model; class CPass final : public Helpers::CContextAware { diff --git a/src/WallpaperEngine/Render/Shaders/CShader.cpp b/src/WallpaperEngine/Render/Shaders/CShader.cpp index 947b9fd..75e7fbe 100644 --- a/src/WallpaperEngine/Render/Shaders/CShader.cpp +++ b/src/WallpaperEngine/Render/Shaders/CShader.cpp @@ -10,7 +10,6 @@ #include "CGLSLContext.h" -using namespace WallpaperEngine::Core; using namespace WallpaperEngine::Assets; namespace WallpaperEngine::Render::Shaders { diff --git a/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp b/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp index e9dba1a..c0b4f85 100644 --- a/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp +++ b/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp @@ -47,7 +47,6 @@ "#define varying out\n" #define DEFINE_COMBO(name, value) "#define " + name + " " + std::to_string (value) + "\n"; -using namespace WallpaperEngine::Core; using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Data::Builders; using namespace WallpaperEngine::Render::Shaders;