diff --git a/src/WallpaperEngine/Core/CObject.cpp b/src/WallpaperEngine/Core/CObject.cpp index 3cdf961..2202d4e 100644 --- a/src/WallpaperEngine/Core/CObject.cpp +++ b/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 (data, "visible", true); 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"); diff --git a/src/WallpaperEngine/Core/CObject.h b/src/WallpaperEngine/Core/CObject.h index c3dd033..8526f3b 100644 --- a/src/WallpaperEngine/Core/CObject.h +++ b/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 { diff --git a/src/WallpaperEngine/Core/CScene.cpp b/src/WallpaperEngine/Core/CScene.cpp index 9513ffc..d289959 100644 --- a/src/WallpaperEngine/Core/CScene.cpp +++ b/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 (*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 (*general_it, "bloomstrength", 0.0); - auto bloomthreshold = jsonFindUserConfig (*general_it, "bloomthreshold", 0.0); + auto bloom = jsonFindUserConfig (*general_it, "bloom", false); + 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); @@ -80,7 +75,7 @@ CScene* CScene::fromFile (const std::string& filename, CContainer* container) auto camerashakeamplitude = jsonFindDefault (*general_it, "camerashakeamplitude", 0.0f); auto camerashakeroughness = jsonFindDefault (*general_it, "camerashakeroughness", 0.0f); auto camerashakespeed = jsonFindDefault (*general_it, "camerashakespeed", 0.0f); - auto clearcolor = jsonFindUserConfig (*general_it, "clearcolor", "1 1 1"); + auto clearcolor = jsonFindUserConfig (*general_it, "clearcolor", {1, 1, 1}); auto orthogonalprojection_it = jsonFindRequired (*general_it, "orthogonalprojection", "General section must have orthogonal projection info"); auto skylightcolor = jsonFindDefault (*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 diff --git a/src/WallpaperEngine/Core/CScene.h b/src/WallpaperEngine/Core/CScene.h index 7942e9e..71e97eb 100644 --- a/src/WallpaperEngine/Core/CScene.h +++ b/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; diff --git a/src/WallpaperEngine/Core/Core.cpp b/src/WallpaperEngine/Core/Core.cpp index 08d65fb..592e0ab 100644 --- a/src/WallpaperEngine/Core/Core.cpp +++ b/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 T Core::jsonFindUserConfig (nlohmann::json& data, const char *key, T defaultValue) +template T* Core::jsonFindUserConfig (nlohmann::json& data, const char *key, S defaultValue) { - auto value = data.find (key); + auto it = data.find (key); - if (value == data.end () || value->type () == nlohmann::detail::value_t::null) - return defaultValue; + if (it == data.end () || it->type () == nlohmann::detail::value_t::null) + return T::fromScalar (defaultValue); - if (value->is_object () == true) - { - auto internal = value->find ("value"); - - 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); \ No newline at end of file +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); \ No newline at end of file diff --git a/src/WallpaperEngine/Core/Core.h b/src/WallpaperEngine/Core/Core.h index b3ff648..1f9539d 100644 --- a/src/WallpaperEngine/Core/Core.h +++ b/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 T jsonFindDefault (nlohmann::json& data, const char *key, T defaultValue); - template T jsonFindUserConfig (nlohmann::json& data, const char *key, T defaultValue); + template T* jsonFindUserConfig (nlohmann::json& data, const char *key, S defaultValue); }; diff --git a/src/WallpaperEngine/Core/Objects/CImage.cpp b/src/WallpaperEngine/Core/Objects/CImage.cpp index 43fdea3..cf5aa75 100644 --- a/src/WallpaperEngine/Core/Objects/CImage.cpp +++ b/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 (data, "size", "0.0 0.0"); // this one might need some adjustment auto alignment = jsonFindDefault (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 (data, "alpha", 1.0); + auto color = jsonFindUserConfig (data, "color", {1, 1, 1}); auto brightness_val = jsonFindDefault (data, "brightness", 1.0); auto colorBlendMode_val = jsonFindDefault (data, "colorBlendMode", 0); auto parallaxDepth_val = jsonFindDefault (data, "parallaxDepth", "0 0");