Sfoglia il codice sorgente

Removed old jsonFindUserConfig and updated to a new version using the CUserSetting classes

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez 3 anni fa
parent
commit
fdd5bcb0b5

+ 1 - 8
src/WallpaperEngine/Core/CObject.cpp

@@ -39,14 +39,7 @@ CObject* CObject::fromJSON (json data, CScene* scene, const CContainer* containe
     std::string json = data.dump ();
 
     auto id_it = jsonFindRequired (data, "id", "Objects must have id");
-    auto visible_it = data.find ("visible");
-    CUserSettingBoolean* visible;
-
-    if (visible_it == data.end ())
-        visible = CUserSettingBoolean::fromScalar (true);
-    else
-        visible = CUserSettingBoolean::fromJSON (*visible_it);
-
+    auto visible = jsonFindUserConfig <CUserSettingBoolean, bool> (data, "visible", true);
     auto origin_val = jsonFindDefault <std::string> (data, "origin", "0.0 0.0 0.0");
     auto scale_val = jsonFindDefault <std::string> (data, "scale", "0.0 0.0 0.0");
     auto angles_val = jsonFindDefault <std::string> (data, "angles", "0.0 0.0 0.0");

+ 2 - 0
src/WallpaperEngine/Core/CObject.h

@@ -5,6 +5,8 @@
 #include "WallpaperEngine/Core/Objects/CEffect.h"
 #include "WallpaperEngine/Assets/CContainer.h"
 #include "WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingFloat.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingColor.h"
 
 namespace WallpaperEngine::Core
 {

+ 16 - 21
src/WallpaperEngine/Core/CScene.cpp

@@ -3,6 +3,8 @@
 
 #include "WallpaperEngine/FileSystem/FileSystem.h"
 #include "WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingFloat.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingColor.h"
 
 using namespace WallpaperEngine::Core;
 
@@ -11,8 +13,8 @@ CScene::CScene (
         Scenes::CCamera* camera,
         glm::vec3 ambientColor,
         CUserSettingBoolean* bloom,
-        double bloomStrength,
-        double bloomThreshold,
+        CUserSettingFloat* bloomStrength,
+        CUserSettingFloat* bloomThreshold,
         bool cameraFade,
         bool cameraParallax,
         double cameraParallaxAmount,
@@ -23,7 +25,7 @@ CScene::CScene (
         double cameraShakeAmplitude,
         double cameraShakeRoughness,
         double cameraShakeSpeed,
-        glm::vec3 clearColor,
+        CUserSettingColor* clearColor,
         Scenes::CProjection* orthogonalProjection,
         glm::vec3 skylightColor) :
     CWallpaper (Type),
@@ -60,16 +62,9 @@ CScene* CScene::fromFile (const std::string& filename, CContainer* container)
 
     // TODO: FIND IF THESE DEFAULTS ARE SENSIBLE OR NOT AND PERFORM PROPER VALIDATION WHEN CAMERA PREVIEW AND CAMERA PARALLAX ARE PRESENT
     auto ambientcolor = jsonFindDefault <std::string> (*general_it, "ambientcolor", "0 0 0");
-    auto bloom_it = (*general_it).find ("bloom");
-    CUserSettingBoolean* bloom;
-
-    if (bloom_it == (*general_it).end ())
-        bloom = CUserSettingBoolean::fromScalar (false);
-    else
-        bloom = CUserSettingBoolean::fromJSON (*bloom_it);
-
-    auto bloomstrength = jsonFindUserConfig <float> (*general_it, "bloomstrength", 0.0);
-    auto bloomthreshold = jsonFindUserConfig <float> (*general_it, "bloomthreshold", 0.0);
+    auto bloom = jsonFindUserConfig <CUserSettingBoolean, bool> (*general_it, "bloom", false);
+    auto bloomstrength = jsonFindUserConfig <CUserSettingFloat, double> (*general_it, "bloomstrength", 0.0);
+    auto bloomthreshold = jsonFindUserConfig <CUserSettingFloat, double> (*general_it, "bloomthreshold", 0.0);
     auto camerafade = jsonFindDefault <bool> (*general_it, "camerafade", false);
     auto cameraparallax = jsonFindDefault <bool> (*general_it, "cameraparallax", true);
     auto cameraparallaxamount = jsonFindDefault <double> (*general_it, "cameraparallaxamount", 1.0f);
@@ -80,7 +75,7 @@ CScene* CScene::fromFile (const std::string& filename, CContainer* container)
     auto camerashakeamplitude = jsonFindDefault <double> (*general_it, "camerashakeamplitude", 0.0f);
     auto camerashakeroughness = jsonFindDefault <double> (*general_it, "camerashakeroughness", 0.0f);
     auto camerashakespeed = jsonFindDefault <double> (*general_it, "camerashakespeed", 0.0f);
-    auto clearcolor = jsonFindUserConfig <std::string> (*general_it, "clearcolor", "1 1 1");
+    auto clearcolor = jsonFindUserConfig <CUserSettingColor, glm::vec3> (*general_it, "clearcolor", {1, 1, 1});
     auto orthogonalprojection_it = jsonFindRequired (*general_it, "orthogonalprojection", "General section must have orthogonal projection info");
     auto skylightcolor = jsonFindDefault <std::string> (*general_it, "skylightcolor", "0 0 0");
 
@@ -101,7 +96,7 @@ CScene* CScene::fromFile (const std::string& filename, CContainer* container)
             camerashakeamplitude,
             camerashakeroughness,
             camerashakespeed,
-            WallpaperEngine::Core::aToColorf(clearcolor),
+            clearcolor,
             Scenes::CProjection::fromJSON (*orthogonalprojection_it),
             WallpaperEngine::Core::aToColorf(skylightcolor)
     );
@@ -158,14 +153,14 @@ const bool CScene::isBloom () const
     return this->m_bloom->processValue (this->getProject ()->getProperties ());
 }
 
-const double CScene::getBloomStrength () const
+double CScene::getBloomStrength () const
 {
-    return this->m_bloomStrength;
+    return this->m_bloomStrength->processValue (this->getProject ()->getProperties ());
 }
 
-const double CScene::getBloomThreshold () const
+double CScene::getBloomThreshold () const
 {
-    return this->m_bloomThreshold;
+    return this->m_bloomThreshold->processValue (this->getProject ()->getProperties ());
 }
 
 const bool CScene::isCameraFade () const
@@ -218,9 +213,9 @@ const double CScene::getCameraShakeSpeed () const
     return this->m_cameraShakeSpeed;
 }
 
-const glm::vec3& CScene::getClearColor () const
+glm::vec3 CScene::getClearColor () const
 {
-    return this->m_clearColor;
+    return this->m_clearColor->processValue (this->getProject ()->getProperties ());
 }
 
 Scenes::CProjection* CScene::getOrthogonalProjection () const

+ 9 - 9
src/WallpaperEngine/Core/CScene.h

@@ -24,8 +24,8 @@ namespace WallpaperEngine::Core
 
         const glm::vec3& getAmbientColor() const;
         const bool isBloom() const;
-        const double getBloomStrength() const;
-        const double getBloomThreshold() const;
+        double getBloomStrength() const;
+        double getBloomThreshold() const;
         const bool isCameraFade() const;
         const bool isCameraParallax() const;
         const double getCameraParallaxAmount() const;
@@ -36,7 +36,7 @@ namespace WallpaperEngine::Core
         const double getCameraShakeAmplitude() const;
         const double getCameraShakeRoughness() const;
         const double getCameraShakeSpeed() const;
-        const glm::vec3& getClearColor() const;
+        glm::vec3 getClearColor() const;
         Scenes::CProjection* getOrthogonalProjection() const;
         const glm::vec3& getSkylightColor() const;
         const Scenes::CCamera* getCamera () const;
@@ -49,8 +49,8 @@ namespace WallpaperEngine::Core
                 Scenes::CCamera* camera,
                 glm::vec3 ambientColor,
                 CUserSettingBoolean* bloom,
-                double bloomStrength,
-                double bloomThreshold,
+                CUserSettingFloat* bloomStrength,
+                CUserSettingFloat* bloomThreshold,
                 bool cameraFade,
                 bool cameraParallax,
                 double cameraParallaxAmount,
@@ -61,7 +61,7 @@ namespace WallpaperEngine::Core
                 double cameraShakeAmplitude,
                 double cameraShakeRoughness,
                 double cameraShakeSpeed,
-                glm::vec3 clearColor,
+                CUserSettingColor* clearColor,
                 Scenes::CProjection* orthogonalProjection,
                 glm::vec3 skylightColor
         );
@@ -78,8 +78,8 @@ namespace WallpaperEngine::Core
         // data from general section on the json
         glm::vec3 m_ambientColor;
         CUserSettingBoolean* m_bloom;
-        double m_bloomStrength;
-        double m_bloomThreshold;
+        CUserSettingFloat* m_bloomStrength;
+        CUserSettingFloat* m_bloomThreshold;
         bool m_cameraFade;
         bool m_cameraParallax;
         double m_cameraParallaxAmount;
@@ -90,7 +90,7 @@ namespace WallpaperEngine::Core
         double m_cameraShakeAmplitude;
         double m_cameraShakeRoughness;
         double m_cameraShakeSpeed;
-        glm::vec3 m_clearColor;
+        CUserSettingColor* m_clearColor;
         Scenes::CProjection* m_orthogonalProjection;
         glm::vec3 m_skylightColor;
 

+ 13 - 25
src/WallpaperEngine/Core/Core.cpp

@@ -1,6 +1,11 @@
 #include "Core.h"
 
+#include "WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingColor.h"
+#include "WallpaperEngine/Core/UserSettings/CUserSettingFloat.h"
+
 using namespace WallpaperEngine;
+using namespace WallpaperEngine::Core::UserSettings;
 
 glm::vec4 Core::aToVector4 (const char* str)
 {
@@ -141,33 +146,16 @@ template uint64_t Core::jsonFindDefault (nlohmann::json& data, const char *key,
 template float Core::jsonFindDefault (nlohmann::json& data, const char *key, float defaultValue);
 template double Core::jsonFindDefault (nlohmann::json& data, const char *key, double defaultValue);
 
-template <typename T> T Core::jsonFindUserConfig (nlohmann::json& data, const char *key, T defaultValue)
+template <typename T, typename S> T* Core::jsonFindUserConfig (nlohmann::json& data, const char *key, S defaultValue)
 {
-    auto value = data.find (key);
-
-    if (value == data.end () || value->type () == nlohmann::detail::value_t::null)
-        return defaultValue;
+    auto it = data.find (key);
 
-    if (value->is_object () == true)
-    {
-        auto internal = value->find ("value");
+    if (it == data.end () || it->type () == nlohmann::detail::value_t::null)
+        return T::fromScalar (defaultValue);
 
-        if (internal == value->end ())
-            return defaultValue;
-
-        value = internal;
-    }
-
-    return *value;
+    return T::fromJSON (*it);
 }
 
-template bool Core::jsonFindUserConfig (nlohmann::json& data, const char *key, bool defaultValue);
-template std::string Core::jsonFindUserConfig (nlohmann::json& data, const char *key, std::string defaultValue);
-template int16_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, int16_t defaultValue);
-template uint16_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, uint16_t defaultValue);
-template int32_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, int32_t defaultValue);
-template uint32_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, uint32_t defaultValue);
-template int64_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, int64_t defaultValue);
-template uint64_t Core::jsonFindUserConfig (nlohmann::json& data, const char *key, uint64_t defaultValue);
-template float Core::jsonFindUserConfig (nlohmann::json& data, const char *key, float defaultValue);
-template double Core::jsonFindUserConfig (nlohmann::json& data, const char *key, double defaultValue);
+template CUserSettingBoolean* Core::jsonFindUserConfig (nlohmann::json& data, const char *key, bool defaultValue);
+template CUserSettingColor* Core::jsonFindUserConfig (nlohmann::json& data, const char *key, glm::vec3 defaultValue);
+template CUserSettingFloat* Core::jsonFindUserConfig (nlohmann::json& data, const char *key, double defaultValue);

+ 1 - 1
src/WallpaperEngine/Core/Core.h

@@ -25,5 +25,5 @@ namespace WallpaperEngine::Core
     nlohmann::json::iterator jsonFindRequired (nlohmann::json& data, const char *key, const char *notFoundMsg);
     nlohmann::json::iterator jsonFindRequired (nlohmann::json::iterator& data, const char *key, const char *notFoundMsg);
     template <typename T> T jsonFindDefault (nlohmann::json& data, const char *key, T defaultValue);
-    template <typename T> T jsonFindUserConfig (nlohmann::json& data, const char *key, T defaultValue);
+    template <typename T, typename S> T* jsonFindUserConfig (nlohmann::json& data, const char *key, S defaultValue);
 };

+ 2 - 16
src/WallpaperEngine/Core/Objects/CImage.cpp

@@ -55,22 +55,8 @@ WallpaperEngine::Core::CObject* CImage::fromJSON (
     auto image_it = data.find ("image");
     auto size_val = jsonFindDefault <std::string> (data, "size", "0.0 0.0"); // this one might need some adjustment
     auto alignment = jsonFindDefault <std::string> (data, "alignment", "center");
-    auto alpha_it = data.find ("alpha");
-    CUserSettingFloat* alpha;
-
-    if (alpha_it == data.end ())
-        alpha = CUserSettingFloat::fromScalar (1.0);
-    else
-        alpha = CUserSettingFloat::fromJSON (*alpha_it);
-
-    auto color_it = data.find ("color");
-    CUserSettingColor* color;
-
-    if (color_it == data.end ())
-        color = CUserSettingColor::fromScalar ({1, 1, 1});
-    else
-        color = CUserSettingColor::fromJSON (*color_it);
-
+    auto alpha = jsonFindUserConfig <CUserSettingFloat, double> (data, "alpha", 1.0);
+    auto color = jsonFindUserConfig <CUserSettingColor, glm::vec3> (data, "color", {1, 1, 1});
     auto brightness_val = jsonFindDefault <float> (data, "brightness", 1.0);
     auto colorBlendMode_val = jsonFindDefault <uint32_t> (data, "colorBlendMode", 0);
     auto parallaxDepth_val = jsonFindDefault <std::string> (data, "parallaxDepth", "0 0");