From ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 01:25:34 +0000 Subject: Support for loading objects, uses and model factories from an XML resource --- assetFactory/modelFactory.cpp | 17 +++++++++++++++++ assetFactory/modelFactory.h | 9 ++++++++- assetFactory/object.cpp | 22 ++++++++++++++++++++++ assetFactory/object.h | 13 ++++++++++++- assetFactory/use.cpp | 20 ++++++++++++++++++++ assetFactory/use.h | 12 +++++++++++- 6 files changed, 90 insertions(+), 3 deletions(-) (limited to 'assetFactory') 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 ModelFactory::ModelFactory() : shapes { @@ -12,3 +15,17 @@ ModelFactory::ModelFactory() : } { } + +std::shared_ptr +ModelFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +ModelFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + 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 -class ModelFactory { +class ModelFactory : public Persistence::Persistable { public: using Shapes = std::map>; ModelFactory(); + [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; + +private: + friend Persistence::SelectionPtrBase, 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 struct Appender : public Persistence::SelectionT> { + Appender(Container & c) : Persistence::SelectionT> {s}, container {c} { } + using Persistence::SelectionT>::SelectionT; + void + endObject(Persistence::Stack & stk) override + { + container.emplace_back(s); + stk.pop(); + } + +private: + std::shared_ptr s; + Container & container; +}; + +bool +Object::persist(Persistence::PersistenceStore & store) +{ + using UseAppend = Appender; + 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, public Shape { +class Object : public StdTypeDefs, 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, 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 { + using Persistence::SelectionV::SelectionV; + using Persistence::SelectionV::setValue; + void + setValue(std::string && str) override + { + if (auto mf = std::dynamic_pointer_cast(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, public Mutation { +class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { public: using FaceControllers = std::map; @@ -14,4 +15,13 @@ public: Shape::CPtr type; std::string colour; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; -- cgit v1.2.3