Procházet zdrojové kódy

+ Added property parsing (color parsing for now only)

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez před 7 roky
rodič
revize
06ecbcdd65

+ 5 - 0
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

+ 35 - 6
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 <std::string> ().c_str ())
+    CProject* project = new CProject (
+        *title,
+        *type,
+        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 ()
@@ -57,4 +76,14 @@ std::string CProject::getTitle ()
 std::string CProject::getType ()
 {
     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);
 }

+ 7 - 0
WallpaperEngine/Core/CProject.h

@@ -4,6 +4,7 @@
 #include <nlohmann/json.hpp>
 
 #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<Projects::CProperty*>* getProperties ();
+
     protected:
         CProject (std::string title, std::string type, CScene* scene);
+
+        void insertProperty (Projects::CProperty* property);
     private:
+        std::vector<Projects::CProperty*> m_properties;
+
         std::string m_title;
         std::string m_type;
         CScene* m_scene;

+ 53 - 0
WallpaperEngine/Core/Projects/CProperty.cpp

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

+ 31 - 0
WallpaperEngine/Core/Projects/CProperty.h

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

+ 30 - 0
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";
+};

+ 25 - 0
WallpaperEngine/Core/Projects/CPropertyColor.h

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