From 06ecbcdd65ff1579febca660421235ca2ffec0ff Mon Sep 17 00:00:00 2001 From: Alexis Maiquez Date: Tue, 3 Sep 2019 15:05:45 +0200 Subject: [PATCH] + Added property parsing (color parsing for now only) Signed-off-by: Alexis Maiquez --- CMakeLists.txt | 5 ++ WallpaperEngine/Core/CProject.cpp | 41 +++++++++++--- WallpaperEngine/Core/CProject.h | 7 +++ WallpaperEngine/Core/Projects/CProperty.cpp | 53 +++++++++++++++++++ WallpaperEngine/Core/Projects/CProperty.h | 31 +++++++++++ .../Core/Projects/CPropertyColor.cpp | 30 +++++++++++ .../Core/Projects/CPropertyColor.h | 25 +++++++++ 7 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 WallpaperEngine/Core/Projects/CProperty.cpp create mode 100644 WallpaperEngine/Core/Projects/CProperty.h create mode 100644 WallpaperEngine/Core/Projects/CPropertyColor.cpp create mode 100644 WallpaperEngine/Core/Projects/CPropertyColor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e600a85..a9cb44e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,11 @@ add_executable( WallpaperEngine/Core/CObject.cpp WallpaperEngine/Core/CObject.h + WallpaperEngine/Core/Projects/CProperty.h + WallpaperEngine/Core/Projects/CProperty.cpp + WallpaperEngine/Core/Projects/CPropertyColor.h + WallpaperEngine/Core/Projects/CPropertyColor.cpp + WallpaperEngine/Core/Scenes/CCamera.cpp WallpaperEngine/Core/Scenes/CCamera.h WallpaperEngine/Core/Scenes/CProjection.cpp diff --git a/WallpaperEngine/Core/CProject.cpp b/WallpaperEngine/Core/CProject.cpp index 89e548e..fcac071 100644 --- a/WallpaperEngine/Core/CProject.cpp +++ b/WallpaperEngine/Core/CProject.cpp @@ -2,8 +2,6 @@ #include "CProject.h" -#include "../FileSystem/utils.h" - using namespace WallpaperEngine::Core; CProject::CProject (std::string title, std::string type, CScene *scene) : @@ -21,6 +19,7 @@ CProject* CProject::fromFile (const irr::io::path& filename) json::const_iterator title = content.find ("title"); json::const_iterator type = content.find ("type"); json::const_iterator file = content.find ("file"); + json::const_iterator general = content.find ("general"); if (title == content.end ()) { @@ -37,11 +36,31 @@ CProject* CProject::fromFile (const irr::io::path& filename) throw std::runtime_error ("Project's main file missing"); } - return new CProject ( - *title, - *type, - CScene::fromFile ((*file).get ().c_str ()) + CProject* project = new CProject ( + *title, + *type, + CScene::fromFile ((*file).get ().c_str ()) ); + + if (general != content.end ()) + { + json::const_iterator properties = (*general).find ("properties"); + + if (properties != (*general).end ()) + { + json::const_iterator cur = (*properties).begin (); + json::const_iterator end = (*properties).end (); + + for (; cur != end; cur ++) + { + project->insertProperty ( + Projects::CProperty::fromJSON (*cur, cur.key ()) + ); + } + } + } + + return project; } CScene* CProject::getScene () @@ -57,4 +76,14 @@ std::string CProject::getTitle () std::string CProject::getType () { return this->m_type; +} + +std::vector* CProject::getProperties () +{ + return &this->m_properties; +} + +void CProject::insertProperty (Projects::CProperty* property) +{ + this->m_properties.push_back (property); } \ No newline at end of file diff --git a/WallpaperEngine/Core/CProject.h b/WallpaperEngine/Core/CProject.h index 990b7f3..369290d 100644 --- a/WallpaperEngine/Core/CProject.h +++ b/WallpaperEngine/Core/CProject.h @@ -4,6 +4,7 @@ #include #include "CScene.h" +#include "Projects/CProperty.h" namespace WallpaperEngine::Core { @@ -20,9 +21,15 @@ namespace WallpaperEngine::Core std::string getTitle (); std::string getType (); + std::vector* getProperties (); + protected: CProject (std::string title, std::string type, CScene* scene); + + void insertProperty (Projects::CProperty* property); private: + std::vector m_properties; + std::string m_title; std::string m_type; CScene* m_scene; diff --git a/WallpaperEngine/Core/Projects/CProperty.cpp b/WallpaperEngine/Core/Projects/CProperty.cpp new file mode 100644 index 0000000..964120b --- /dev/null +++ b/WallpaperEngine/Core/Projects/CProperty.cpp @@ -0,0 +1,53 @@ +#include "CProperty.h" + +#include +#include "CPropertyColor.h" + +namespace WallpaperEngine::Core::Projects +{ + CProperty* CProperty::fromJSON (json data, const std::string& name) + { + json::const_iterator type = data.find ("type"); + json::const_iterator value = data.find ("value"); + json::const_iterator text = data.find ("text"); + + if (value == data.end ()) + { + throw std::runtime_error ("Project properties must have the value field"); + } + + if (type == data.end ()) + { + throw std::runtime_error ("Project properties must have the type field"); + } + + if (*type == CPropertyColor::Type) + { + return CPropertyColor::fromJSON (data, name); + } + + throw std::runtime_error ("Unexpected type for property"); + } + + CProperty::CProperty (std::string name, std::string type, std::string text) : + m_name (std::move(name)), + m_type (std::move(type)), + m_text (std::move(text)) + { + } + + std::string& CProperty::getName () + { + return this->m_name; + } + + std::string& CProperty::getType () + { + return this->m_type; + } + + std::string& CProperty::getText () + { + return this->m_text; + } +}; diff --git a/WallpaperEngine/Core/Projects/CProperty.h b/WallpaperEngine/Core/Projects/CProperty.h new file mode 100644 index 0000000..40c18f3 --- /dev/null +++ b/WallpaperEngine/Core/Projects/CProperty.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +namespace WallpaperEngine::Core::Projects +{ + using json = nlohmann::json; + + class CPropertyColor; + + class CProperty + { + public: + static CProperty* fromJSON (json data, const std::string& name); + + template const T As () const { assert (Is ()); return (const T*) this; } + template T As () { assert (Is ()); return (T*) this; } + + template bool Is () { return this->m_type == T::Type; } + + std::string& getName (); + std::string& getType (); + std::string& getText (); + protected: + CProperty (std::string name, std::string type, std::string text); + + std::string m_type; + std::string m_name; + std::string m_text; + }; +} diff --git a/WallpaperEngine/Core/Projects/CPropertyColor.cpp b/WallpaperEngine/Core/Projects/CPropertyColor.cpp new file mode 100644 index 0000000..9d1dc47 --- /dev/null +++ b/WallpaperEngine/Core/Projects/CPropertyColor.cpp @@ -0,0 +1,30 @@ +#include "CPropertyColor.h" +#include "../Core.h" + +namespace WallpaperEngine::Core::Projects +{ + CPropertyColor* CPropertyColor::fromJSON (json data, const std::string& name) + { + json::const_iterator value = data.find ("value"); + json::const_iterator text = data.find ("type"); + + return new CPropertyColor ( + WallpaperEngine::Core::atoSColor (*value), + name, + *text + ); + } + + irr::video::SColor* CPropertyColor::getValue () + { + return &this->m_color; + } + + CPropertyColor::CPropertyColor (irr::video::SColor color, const std::string& name, const std::string& text) : + CProperty (name, Type, text), + m_color (color) + { + } + + const std::string CPropertyColor::Type = "color"; +}; \ No newline at end of file diff --git a/WallpaperEngine/Core/Projects/CPropertyColor.h b/WallpaperEngine/Core/Projects/CPropertyColor.h new file mode 100644 index 0000000..3464d53 --- /dev/null +++ b/WallpaperEngine/Core/Projects/CPropertyColor.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include "CProperty.h" + +namespace WallpaperEngine::Core::Projects +{ + using json = nlohmann::json; + + class CPropertyColor : public CProperty + { + public: + static CPropertyColor* fromJSON (json data, const std::string& name); + + irr::video::SColor* getValue (); + + static const std::string Type; + + private: + CPropertyColor (irr::video::SColor color, const std::string& name, const std::string& text); + + irr::video::SColor m_color; + }; +};