diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-21 01:25:34 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-21 01:25:34 +0000 |
commit | ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585 (patch) | |
tree | 810350a653292d54621b7c4e72275bcd48dc6785 /assetFactory | |
parent | Add missing override (diff) | |
download | ilt-ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585.tar.bz2 ilt-ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585.tar.xz ilt-ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585.zip |
Support for loading objects, uses and model factories from an XML resource
Diffstat (limited to 'assetFactory')
-rw-r--r-- | assetFactory/modelFactory.cpp | 17 | ||||
-rw-r--r-- | assetFactory/modelFactory.h | 9 | ||||
-rw-r--r-- | assetFactory/object.cpp | 22 | ||||
-rw-r--r-- | assetFactory/object.h | 13 | ||||
-rw-r--r-- | assetFactory/use.cpp | 20 | ||||
-rw-r--r-- | assetFactory/use.h | 12 |
6 files changed, 90 insertions, 3 deletions
diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp index 4c25e48..2642900 100644 --- a/assetFactory/modelFactory.cpp +++ b/assetFactory/modelFactory.cpp @@ -2,7 +2,10 @@ #include "cuboid.h" #include "cylinder.h" #include "modelFactoryMesh_fwd.h" +#include "object.h" #include "plane.h" +#include "saxParse-persistence.h" +#include <filesystem.h> ModelFactory::ModelFactory() : shapes { @@ -12,3 +15,17 @@ ModelFactory::ModelFactory() : } { } + +std::shared_ptr<ModelFactory> +ModelFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState<std::shared_ptr<ModelFactory>>(file); +} + +bool +ModelFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember<Shapes, Object>; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h index f2b1b48..94db055 100644 --- a/assetFactory/modelFactory.h +++ b/assetFactory/modelFactory.h @@ -1,12 +1,19 @@ #pragma once +#include "persistence.h" #include "shape.h" +#include <filesystem> -class ModelFactory { +class ModelFactory : public Persistence::Persistable { public: using Shapes = std::map<std::string, Shape::CPtr, std::less<>>; ModelFactory(); + [[nodiscard]] static std::shared_ptr<ModelFactory> loadXML(const std::filesystem::path &); Shapes shapes; + +private: + friend Persistence::SelectionPtrBase<std::shared_ptr<ModelFactory>, true>; + bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp index 8b70676..faa9a17 100644 --- a/assetFactory/object.cpp +++ b/assetFactory/object.cpp @@ -15,3 +15,25 @@ Object::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) c } return faces; } + +template<typename Container, typename Type> struct Appender : public Persistence::SelectionT<std::shared_ptr<Type>> { + Appender(Container & c) : Persistence::SelectionT<std::shared_ptr<Type>> {s}, container {c} { } + using Persistence::SelectionT<std::shared_ptr<Type>>::SelectionT; + void + endObject(Persistence::Stack & stk) override + { + container.emplace_back(s); + stk.pop(); + } + +private: + std::shared_ptr<Type> s; + Container & container; +}; + +bool +Object::persist(Persistence::PersistenceStore & store) +{ + using UseAppend = Appender<Use::Collection, Use>; + return STORE_TYPE && STORE_MEMBER(id) && STORE_NAME_HELPER("use", uses, UseAppend); +} diff --git a/assetFactory/object.h b/assetFactory/object.h index da28c1f..1069f66 100644 --- a/assetFactory/object.h +++ b/assetFactory/object.h @@ -1,15 +1,26 @@ #pragma once +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" #include "use.h" -class Object : public StdTypeDefs<Object>, public Shape { +class Object : public StdTypeDefs<Object>, public Shape, public Persistence::Persistable { public: + Object() = default; Object(std::string i); CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; Use::Collection uses; std::string id; + +private: + friend Persistence::SelectionPtrBase<std::shared_ptr<Object>, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return id; + }; }; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index d191329..21e26f3 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -1,4 +1,5 @@ #include "use.h" +#include "modelFactory.h" Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const @@ -9,3 +10,22 @@ Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) cons } return faces; } + +struct Lookup : public Persistence::SelectionV<Shape::CPtr> { + using Persistence::SelectionV<Shape::CPtr>::SelectionV; + using Persistence::SelectionV<Shape::CPtr>::setValue; + void + setValue(std::string && str) override + { + if (auto mf = std::dynamic_pointer_cast<const ModelFactory>(Persistence::sharedObjects.at("modelFactory"))) { + v = mf->shapes.at(str); + } + } +}; + +bool +Use::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) + && STORE_MEMBER(rotation); +} diff --git a/assetFactory/use.h b/assetFactory/use.h index 28f5459..96f07f6 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -2,10 +2,11 @@ #include "faceController.h" #include "modelFactoryMesh_fwd.h" +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" -class Use : public StdTypeDefs<Use>, public Mutation { +class Use : public StdTypeDefs<Use>, public Mutation, public Persistence::Persistable { public: using FaceControllers = std::map<std::string, FaceController>; @@ -14,4 +15,13 @@ public: Shape::CPtr type; std::string colour; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase<std::shared_ptr<Use>, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; |