diff --git a/src/WallpaperEngine/Core/CObject.cpp b/src/WallpaperEngine/Core/CObject.cpp index 47bd293..b8daff5 100644 --- a/src/WallpaperEngine/Core/CObject.cpp +++ b/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 (data, "origin", "0.0 0.0 0.0"); auto scale_val = jsonFindDefault (data, "scale", "0.0 0.0 0.0"); auto angles_val = jsonFindDefault (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"); + auto effectVisible = jsonFindUserConfig (*cur, "visible", true); - 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; - } - } + if (effectVisible == false) + continue; object->insertEffect ( Objects::CEffect::fromJSON (*cur, object, container) diff --git a/src/WallpaperEngine/Core/CScene.cpp b/src/WallpaperEngine/Core/CScene.cpp index cf5f7b3..8a528a1 100644 --- a/src/WallpaperEngine/Core/CScene.cpp +++ b/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 (*general_it, "ambientcolor", "0 0 0"); auto bloom = jsonFindDefault (*general_it, "bloom", false); - auto bloomstrength = (*general_it).find ("bloomstrength"); - auto bloomthreshold = (*general_it).find ("bloomthreshold"); + auto bloomstrength = jsonFindUserConfig (*general_it, "bloomstrength", 0.0); + auto bloomthreshold = jsonFindUserConfig (*general_it, "bloomthreshold", 0.0); auto camerafade = jsonFindDefault (*general_it, "camerafade", false); auto cameraparallax = jsonFindDefault (*general_it, "cameraparallax", true); auto cameraparallaxamount = jsonFindDefault (*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 (*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, diff --git a/src/WallpaperEngine/Core/Core.cpp b/src/WallpaperEngine/Core/Core.cpp index 5c6c3e0..eb0919d 100644 --- a/src/WallpaperEngine/Core/Core.cpp +++ b/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); \ No newline at end of file +template double Core::jsonFindDefault (nlohmann::json& data, const char *key, double defaultValue); + +template 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); \ No newline at end of file diff --git a/src/WallpaperEngine/Core/Core.h b/src/WallpaperEngine/Core/Core.h index 663e9df..b71f482 100644 --- a/src/WallpaperEngine/Core/Core.h +++ b/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 T jsonFindDefault (nlohmann::json& data, const char *key, T defaultValue); + template T jsonFindUserConfig (nlohmann::json& data, const char *key, T defaultValue); };