ソースを参照

Added small helper to get user-customizable settings with defaults

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez 3 年 前
親
コミット
691ac42e85

+ 5 - 32
src/WallpaperEngine/Core/CObject.cpp

@@ -33,7 +33,7 @@ CObject* CObject::fromJSON (json data, const CContainer* container)
     std::string json = data.dump ();
 
     auto id_it = jsonFindRequired (data, "id", "Objects must have id");
-    auto visible_it = data.find ("visible");
+    auto visible = jsonFindUserConfig (data, "visible", false);
     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");
@@ -41,23 +41,6 @@ CObject* CObject::fromJSON (json data, const CContainer* container)
     auto effects_it = data.find ("effects");
     auto dependencies_it = data.find ("dependencies");
 
-    bool visible = true;
-
-    // visibility is optional
-    if (visible_it != data.end ())
-    {
-        if (visible_it->is_boolean () == false)
-        {
-            // TODO: SUPPORT CONFIGURATION VALUES ON ATTRIBUTES LIKE VISIBLE
-            // TODO: FOR NOW JUST DEFAULT TO FALSE
-            visible = false;
-        }
-        else
-        {
-            visible = *visible_it;
-        }
-    }
-
     auto image_it = data.find ("image");
     auto sound_it = data.find ("sound");
     auto particle_it = data.find ("particle");
@@ -137,20 +120,10 @@ CObject* CObject::fromJSON (json data, const CContainer* container)
         for (; cur != end; cur ++)
         {
             // check if the effect is visible or not
-            auto effectVisible_it = (*cur).find ("visible");
-
-            if (effectVisible_it != (*cur).end ())
-            {
-                if ((*effectVisible_it).is_boolean () && (*effectVisible_it) == false)
-                    continue;
-                if ((*effectVisible_it).is_object () == true)
-                {
-                    auto effectVisibleValue_it = (*effectVisible_it).find ("value");
-
-                    if (effectVisibleValue_it != (*effectVisible_it).end () && (*effectVisibleValue_it) == false)
-                        continue;
-                }
-            }
+            auto effectVisible = jsonFindUserConfig (*cur, "visible", true);
+
+            if (effectVisible == false)
+                continue;
 
             object->insertEffect (
                 Objects::CEffect::fromJSON (*cur, object, container)

+ 4 - 38
src/WallpaperEngine/Core/CScene.cpp

@@ -62,8 +62,8 @@ 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 = jsonFindDefault <bool> (*general_it, "bloom", false);
-    auto bloomstrength = (*general_it).find ("bloomstrength");
-    auto bloomthreshold = (*general_it).find ("bloomthreshold");
+    auto bloomstrength = jsonFindUserConfig <float> (*general_it, "bloomstrength", 0.0);
+    auto bloomthreshold = jsonFindUserConfig <float> (*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);
@@ -77,48 +77,14 @@ CScene* CScene::fromFile (const std::string& filename, CContainer* container)
     auto clearcolor_it = jsonFindRequired (*general_it, "clearcolor", "General section must have clear color");
     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");
-    double bloomstrength_val = 0.0f;
-    double bloomthreshold_val = 0.0f;
-
-    if (bloomstrength != (*general_it).end ())
-    {
-        if ((*bloomstrength).is_object ())
-        {
-            auto value = (*bloomstrength).find ("value");
-
-            if (value != (*bloomstrength).end ())
-            {
-                bloomstrength = value;
-            }
-        }
-
-        if ((*bloomstrength).is_number ())
-            bloomstrength_val = (*bloomstrength);
-    }
-
-    if (bloomthreshold != (*general_it).end ())
-    {
-        if ((*bloomthreshold).is_object ())
-        {
-            auto value = (*bloomthreshold).find ("value");
-
-            if (value != (*bloomthreshold).end ())
-            {
-                bloomthreshold = value;
-            }
-        }
-
-        if ((*bloomthreshold).is_number ())
-            bloomthreshold_val = (*bloomthreshold);
-    }
 
     CScene* scene = new CScene (
             container,
             Scenes::CCamera::fromJSON (*camera_it),
             WallpaperEngine::Core::aToColorf(ambientcolor),
             bloom,
-            bloomstrength_val,
-            bloomthreshold_val,
+            bloomstrength,
+            bloomthreshold,
             camerafade,
             cameraparallax,
             cameraparallaxamount,

+ 32 - 1
src/WallpaperEngine/Core/Core.cpp

@@ -135,4 +135,35 @@ template uint32_t Core::jsonFindDefault (nlohmann::json& data, const char *key,
 template int64_t Core::jsonFindDefault (nlohmann::json& data, const char *key, int64_t defaultValue);
 template uint64_t Core::jsonFindDefault (nlohmann::json& data, const char *key, uint64_t defaultValue);
 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 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)
+{
+    auto value = data.find (key);
+
+    if (value == data.end () || value->type () == nlohmann::detail::value_t::null)
+        return defaultValue;
+
+    if (value->is_object () == true)
+    {
+        auto internal = value->find ("value");
+
+        if (internal == value->end ())
+            return defaultValue;
+
+        value = internal;
+    }
+
+    return *value;
+}
+
+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);

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

@@ -30,4 +30,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);
 };