diff options
author | rtk0c <[email protected]> | 2023-10-19 22:50:07 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2023-10-19 22:50:07 -0700 |
commit | 2c92e07f337e42cf58970443f9de678f85a9b2a4 (patch) | |
tree | 075d5407e1e12a9d35cbee6e4c20ad34e0765c42 /src/brussel.engine/Ires.hpp | |
parent | 615809c036f604bce4582cea8ad49c64693f4f45 (diff) |
The great renaming: switch to "module style"
Diffstat (limited to 'src/brussel.engine/Ires.hpp')
-rw-r--r-- | src/brussel.engine/Ires.hpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/brussel.engine/Ires.hpp b/src/brussel.engine/Ires.hpp new file mode 100644 index 0000000..e2e79bd --- /dev/null +++ b/src/brussel.engine/Ires.hpp @@ -0,0 +1,130 @@ +#pragma once + +#include "EditorAttachment.hpp" +#include "EditorCore.hpp" + +#include <MacrosCodegen.hpp> +#include <RcPtr.hpp> +#include <Uid.hpp> +#include <Utils.hpp> + +#include <rapidjson/fwd.h> +#include <robin_hood.h> +#include <filesystem> +#include <memory> +#include <string_view> + +// Forward declarations +class IresManager; +class IresWritingContext; +class IresLoadingContext; + +namespace Tags { +enum class IresObjectKind { + // clang-format off + BRUSSEL_ENUM(ToString, FromString, RemovePrefix KD_, AddPrefix Ires, ExcludeHeuristics) + // clang-format on + + KD_Texture, + KD_Shader, + KD_Material, + KD_SpriteFiles, + KD_Spritesheet, + KD_COUNT, +}; +} // namespace Tags + +class IresObject : public RefCounted { + friend class IresManager; + +public: + using Kind = Tags::IresObjectKind; + using enum Tags::IresObjectKind; + +private: + std::string mName; // Serialized as filename + Uid mUid; // Serialized in full mode + std::unique_ptr<EditorAttachment> mEditorAttachment; // Transient + IresManager* mMan = nullptr; // Transient + Kind mKind; // Serialized in full mode + +public: + IresObject(Kind kind); + virtual ~IresObject() = default; + + static std::unique_ptr<IresObject> Create(Kind kind); + Kind GetKind() const { return mKind; } + + IresManager* GetAssociatedManager() const { return mMan; } + bool IsAnnoymous() const; + const std::string& GetName() const { return mName; } + void SetName(std::string name); + const Uid& GetUid() const { return mUid; } + + static void ShowNameSafe(IresObject* ires); + static void ShowNameNull(); + void ShowName() const; + + static void ShowReferenceSafe(IEditor& editor, IresObject* ires); + static void ShowReferenceNull(IEditor& editor); + void ShowReference(IEditor& editor); + + virtual void ShowEditor(IEditor& editor); + + EditorAttachment* GetEditorAttachment() const { return mEditorAttachment.get(); } + void SetEditorAttachment(EditorAttachment* attachment) { mEditorAttachment.reset(attachment); } + + static void WriteFull(IresWritingContext& ctx, IresObject* ires, rapidjson::Value& value, rapidjson::Document& root); + + /// Sequentially call ReadBasic() and then ReadPartial() + static std::unique_ptr<IresObject> ReadFull(IresLoadingContext& ctx, const rapidjson::Value& value); + /// Reads the type and UID of the object from data, and return a newly constructed Ires object. + static std::unique_ptr<IresObject> ReadBasic(const rapidjson::Value& value); + /// Reads all object-speific data from the root-level data object into the given Ires object. + static bool ReadPartial(IresLoadingContext& ctx, IresObject* ires, const rapidjson::Value& value); + + virtual void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const; + virtual void Read(IresLoadingContext& ctx, const rapidjson::Value& value); + +protected: + rapidjson::Value WriteIresRef(IresObject* object); +}; + +class IresWritingContext { +public: + virtual ~IresWritingContext() = default; +}; + +class IresLoadingContext { +public: + virtual ~IresLoadingContext() = default; + virtual IresObject* FindIres(const Uid& uid) const = 0; +}; + +class IresManager final : public IresWritingContext, public IresLoadingContext { +public: + static inline IresManager* instance = nullptr; + +private: + robin_hood::unordered_map<Uid, RcPtr<IresObject>> mObjByUid; + +public: + void DiscoverFilesDesignatedLocation(); + void DiscoverFiles(const std::filesystem::path& dir); + + std::pair<IresObject*, bool> Add(IresObject* mat); + IresObject* Load(const std::filesystem::path& filePath); + void Delete(IresObject* ires); + bool Rename(IresObject* ires, std::string newName); + + void Reload(IresObject* ires); + void Save(IresObject* ires); + void Save(IresObject* ires, const std::filesystem::path& filePath); + + void OverwriteAllToDisk(); + + const auto& GetObjects() const { return mObjByUid; } + virtual IresObject* FindIres(const Uid& uid) const override; +}; + +#include <generated/Ires.gh.inl> |