From 7db36183508a7057c91f8a7f244fb30cec837e04 Mon Sep 17 00:00:00 2001 From: Alexis Maiquez Date: Mon, 29 Nov 2021 13:56:50 +0100 Subject: [PATCH] + Added parsing for shader constants that have extra information + Added support for slider properties Signed-off-by: Alexis Maiquez --- CMakeLists.txt | 2 + src/WallpaperEngine/Core/Objects/CEffect.cpp | 29 ++++++++--- .../Core/Projects/CProperty.cpp | 8 +-- .../Core/Projects/CPropertySlider.cpp | 49 +++++++++++++++++++ .../Core/Projects/CPropertySlider.h | 32 ++++++++++++ 5 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 src/WallpaperEngine/Core/Projects/CPropertySlider.cpp create mode 100644 src/WallpaperEngine/Core/Projects/CPropertySlider.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f4fa94d..c360555 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,8 @@ add_executable( 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/Scenes/CCamera.cpp src/WallpaperEngine/Core/Scenes/CCamera.h diff --git a/src/WallpaperEngine/Core/Objects/CEffect.cpp b/src/WallpaperEngine/Core/Objects/CEffect.cpp index 859c021..b58797f 100644 --- a/src/WallpaperEngine/Core/Objects/CEffect.cpp +++ b/src/WallpaperEngine/Core/Objects/CEffect.cpp @@ -1,6 +1,7 @@ #include "CEffect.h" #include +#include #include "WallpaperEngine/Core/Objects/CImage.h" #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h" @@ -149,20 +150,36 @@ void CEffect::constantsFromJSON (json::const_iterator constants_it, Core::Object for (; cur != end; cur ++) { + json::const_iterator val = cur; + Effects::Constants::CShaderConstant* constant = nullptr; - if ((*cur).is_number_float () == true) + // if the constant is an object, that means the constant has some extra information + // for the UI, take the value, which is what we need + + if ((*cur).is_object () == true) { - constant = new Effects::Constants::CShaderConstantFloat ((*cur).get ()); + val = (*cur).find ("value"); + + if (val == (*cur).end ()) + { + std::cerr << "Found object for shader constant without \"value\" member" << std::endl; + continue; + } } - else if ((*cur).is_number_integer () == true) + + if ((*val).is_number_float () == true) { - constant = new Effects::Constants::CShaderConstantInteger ((*cur).get ()); + constant = new Effects::Constants::CShaderConstantFloat ((*val).get ()); } - else if ((*cur).is_string () == true) + else if ((*val).is_number_integer () == true) + { + constant = new Effects::Constants::CShaderConstantInteger ((*val).get ()); + } + else if ((*val).is_string () == true) { // try a vector 4 first, then a vector3 and then a vector 2 - constant = new Effects::Constants::CShaderConstantVector4 (WallpaperEngine::Core::aToVector4 (*cur)); + constant = new Effects::Constants::CShaderConstantVector4 (WallpaperEngine::Core::aToVector4 (*val)); } else { diff --git a/src/WallpaperEngine/Core/Projects/CProperty.cpp b/src/WallpaperEngine/Core/Projects/CProperty.cpp index b381699..f296fd8 100644 --- a/src/WallpaperEngine/Core/Projects/CProperty.cpp +++ b/src/WallpaperEngine/Core/Projects/CProperty.cpp @@ -1,5 +1,6 @@ #include "CProperty.h" #include "CPropertyColor.h" +#include "CPropertySlider.h" #include "CPropertyBoolean.h" using namespace WallpaperEngine::Core::Projects; @@ -11,14 +12,13 @@ CProperty* CProperty::fromJSON (json data, const std::string& name) auto text = data.find ("text"); if (*type == CPropertyColor::Type) - { return CPropertyColor::fromJSON (data, name); - } if (*type == CPropertyBoolean::Type) - { return CPropertyBoolean::fromJSON (data, name); - } + + if (*type == CPropertySlider::Type) + return CPropertySlider::fromJSON (data, name); throw std::runtime_error ("Unexpected type for property"); } diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp b/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp new file mode 100644 index 0000000..5e89d92 --- /dev/null +++ b/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp @@ -0,0 +1,49 @@ +#include "CPropertySlider.h" + +using namespace WallpaperEngine::Core::Projects; + +CPropertySlider* CPropertySlider::fromJSON (json data, const std::string& name) +{ + auto value = data.find ("value"); + auto text = data.find ("type"); + auto min = jsonFindDefault(data, "min", 0.0); + auto max = jsonFindDefault (data, "max", 0.0); + auto step = jsonFindDefault (data, "step", 0.0); + + return new CPropertySlider ( + *value, + name, + *text, + min, + max, + step + ); +} + +const double& CPropertySlider::getValue () const +{ + return this->m_value; +} + +const double& CPropertySlider::getMinValue () const +{ + return this->m_min; +} + +const double& CPropertySlider::getMaxValue () const +{ + return this->m_max; +} + +const double& CPropertySlider::getStep () const +{ + return this->m_step; +} + +CPropertySlider::CPropertySlider (double value, const std::string& name, const std::string& text, double min, double max, double step) : + CProperty (name, Type, text), + m_value (value), m_min (min), m_max (max), m_step (step) +{ +} + +const std::string CPropertySlider::Type = "slider"; \ No newline at end of file diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.h b/src/WallpaperEngine/Core/Projects/CPropertySlider.h new file mode 100644 index 0000000..a5aa67a --- /dev/null +++ b/src/WallpaperEngine/Core/Projects/CPropertySlider.h @@ -0,0 +1,32 @@ +#pragma once + +#include "CProperty.h" + +#include "WallpaperEngine/Core/Core.h" + +namespace WallpaperEngine::Core::Projects +{ + using json = nlohmann::json; + using namespace WallpaperEngine::Core::Types; + + class CPropertySlider : public CProperty + { + public: + static CPropertySlider* fromJSON (json data, const std::string& name); + + const double& getValue () const; + const double& getMinValue () const; + const double& getMaxValue () const; + const double& getStep () const; + + static const std::string Type; + + private: + CPropertySlider (double value, const std::string& name, const std::string& text, double min, double max, double step); + + double m_value; + double m_min; + double m_max; + double m_step; + }; +};