+ Added property parsing (color parsing for now only)

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
This commit is contained in:
Alexis Maiquez 2019-09-03 15:05:45 +02:00
parent 5c56e0860b
commit 06ecbcdd65
7 changed files with 186 additions and 6 deletions

View File

@ -66,6 +66,11 @@ add_executable(
WallpaperEngine/Core/CObject.cpp WallpaperEngine/Core/CObject.cpp
WallpaperEngine/Core/CObject.h 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.cpp
WallpaperEngine/Core/Scenes/CCamera.h WallpaperEngine/Core/Scenes/CCamera.h
WallpaperEngine/Core/Scenes/CProjection.cpp WallpaperEngine/Core/Scenes/CProjection.cpp

View File

@ -2,8 +2,6 @@
#include "CProject.h" #include "CProject.h"
#include "../FileSystem/utils.h"
using namespace WallpaperEngine::Core; using namespace WallpaperEngine::Core;
CProject::CProject (std::string title, std::string type, CScene *scene) : 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 title = content.find ("title");
json::const_iterator type = content.find ("type"); json::const_iterator type = content.find ("type");
json::const_iterator file = content.find ("file"); json::const_iterator file = content.find ("file");
json::const_iterator general = content.find ("general");
if (title == content.end ()) 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"); throw std::runtime_error ("Project's main file missing");
} }
return new CProject ( CProject* project = new CProject (
*title, *title,
*type, *type,
CScene::fromFile ((*file).get <std::string> ().c_str ()) CScene::fromFile ((*file).get <std::string> ().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 () CScene* CProject::getScene ()
@ -58,3 +77,13 @@ std::string CProject::getType ()
{ {
return this->m_type; return this->m_type;
} }
std::vector<Projects::CProperty*>* CProject::getProperties ()
{
return &this->m_properties;
}
void CProject::insertProperty (Projects::CProperty* property)
{
this->m_properties.push_back (property);
}

View File

@ -4,6 +4,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "CScene.h" #include "CScene.h"
#include "Projects/CProperty.h"
namespace WallpaperEngine::Core namespace WallpaperEngine::Core
{ {
@ -20,9 +21,15 @@ namespace WallpaperEngine::Core
std::string getTitle (); std::string getTitle ();
std::string getType (); std::string getType ();
std::vector<Projects::CProperty*>* getProperties ();
protected: protected:
CProject (std::string title, std::string type, CScene* scene); CProject (std::string title, std::string type, CScene* scene);
void insertProperty (Projects::CProperty* property);
private: private:
std::vector<Projects::CProperty*> m_properties;
std::string m_title; std::string m_title;
std::string m_type; std::string m_type;
CScene* m_scene; CScene* m_scene;

View File

@ -0,0 +1,53 @@
#include "CProperty.h"
#include <utility>
#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;
}
};

View File

@ -0,0 +1,31 @@
#pragma once
#include <nlohmann/json.hpp>
namespace WallpaperEngine::Core::Projects
{
using json = nlohmann::json;
class CPropertyColor;
class CProperty
{
public:
static CProperty* fromJSON (json data, const std::string& name);
template<class T> const T As () const { assert (Is<T> ()); return (const T*) this; }
template<class T> T As () { assert (Is<T> ()); return (T*) this; }
template<class T> 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;
};
}

View File

@ -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";
};

View File

@ -0,0 +1,25 @@
#pragma once
#include <irrlicht/irrlicht.h>
#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;
};
};