Bläddra i källkod

+ Added parsing for shader constants that have extra information
+ Added support for slider properties

Signed-off-by: Alexis Maiquez <almamu@almamu.com>

Alexis Maiquez 4 år sedan
förälder
incheckning
7db3618350

+ 2 - 0
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

+ 23 - 6
src/WallpaperEngine/Core/Objects/CEffect.cpp

@@ -1,6 +1,7 @@
 #include "CEffect.h"
 
 #include <utility>
+#include <iostream>
 
 #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)
+        {
+            val = (*cur).find ("value");
+
+            if (val == (*cur).end ())
+            {
+                std::cerr << "Found object for shader constant without \"value\" member" << std::endl;
+                continue;
+            }
+        }
+
+        if ((*val).is_number_float () == true)
         {
-            constant = new Effects::Constants::CShaderConstantFloat ((*cur).get <float> ());
+            constant = new Effects::Constants::CShaderConstantFloat ((*val).get <float> ());
         }
-        else if ((*cur).is_number_integer () == true)
+        else if ((*val).is_number_integer () == true)
         {
-            constant = new Effects::Constants::CShaderConstantInteger ((*cur).get <int> ());
+            constant = new Effects::Constants::CShaderConstantInteger ((*val).get <int> ());
         }
-        else if ((*cur).is_string () == true)
+        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
         {

+ 4 - 4
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");
 }

+ 49 - 0
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";

+ 32 - 0
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;
+    };
+};