diff options
Diffstat (limited to 'core/src/Model/Assets.hpp')
-rw-r--r-- | core/src/Model/Assets.hpp | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/core/src/Model/Assets.hpp b/core/src/Model/Assets.hpp index bc5219d..3401e42 100644 --- a/core/src/Model/Assets.hpp +++ b/core/src/Model/Assets.hpp @@ -1,18 +1,22 @@ #pragma once #include <tsl/array_map.h> +#include <uuid.h> #include <filesystem> #include <iosfwd> #include <memory> #include <string_view> /// A structure representing a ready-to-be-loaded asset, locating on the disk. +/// Each asset should be identified by a unique uuid within the asset category (i.e. a workflow and a template can share the same uuid), +/// generated on insertion to an asset list if not given by the caller. struct SavedAsset { - std::filesystem::path Path; std::string Name; - /// `Path`'s string form, encoded in UTF-8. - std::string PathString = Path.string(); + /// UUID of this asset. This field is generated as a random UUID v4 upon insertion into an AssetList, if not already provided by the caller (indicated by !is_nil()). + uuids::uuid Uuid; + /// Extra data to be used by the AssetList/Asset implementation. + uint64_t Payload; }; class Asset @@ -22,40 +26,15 @@ public: virtual ~Asset() = default; }; -class AssetCategory -{ -public: - virtual ~AssetCategory() = default; - - virtual void DiscoverFiles(const std::function<void(SavedAsset)>& callback) const = 0; - - virtual Asset* CreateEmpty(const SavedAsset& diskForm) const = 0; - std::unique_ptr<Asset> CreateEmptyUnique(const SavedAsset& diskForm) const; - virtual Asset* Load(const SavedAsset& diskForm) const = 0; - std::unique_ptr<Asset> LoadUnique(const SavedAsset& diskForm) const; - - /// This should call ImGui::BeginTable() along with other accessories such as setting up the header row. - virtual void SetupDetailsTable(const char* tableId) const = 0; - virtual void DrawBigIcon(const SavedAsset& asset) const = 0; - virtual void DrawDetailsTableRow(const SavedAsset& asset) const = 0; - -protected: - /* Helper loader functions */ - - static void DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, std::string_view extension); - static void DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, const std::function<bool(std::istream&)>& validater); -}; - class AssetList { private: - const AssetCategory* mLoader; tsl::array_map<char, SavedAsset> mAssets; tsl::array_map<char, std::unique_ptr<Asset>> mCache; int mCacheSizeLimit = 0; public: - AssetList(const AssetCategory& loader); + virtual ~AssetList() = default; // TODO support file watches void Reload(); @@ -63,42 +42,62 @@ public: const SavedAsset* FindByName(std::string_view name) const; const SavedAsset& Create(SavedAsset asset); std::unique_ptr<Asset> CreateAndLoad(SavedAsset asset); - std::unique_ptr<Asset> LoadFromDisk(std::string_view name) const; - bool Rename(std::string_view oldName, std::string_view newName); + /// Load the asset on disk by its name. + std::unique_ptr<Asset> Load(std::string_view name) const; + /// Load the asset on disk by a reference to its SavedAsset instance. This function assumes that the given SavedAsset + /// is stored in AssetList, otherwise the behavior is undefined. + std::unique_ptr<Asset> Load(const SavedAsset& asset) const; + const SavedAsset* Rename(std::string_view oldName, std::string_view newName); bool Remove(std::string_view name); int GetCacheSizeLimit() const; void SetCacheSizeLimit(int limit); - struct DrawState + struct ListState { const SavedAsset* SelectedAsset = nullptr; }; - void DrawBigIcons(DrawState& state); - void DrawDetails(DrawState& state); + void DrawBigIcons(ListState& state); + void DrawDetails(ListState& state); + +protected: + virtual void DiscoverFiles(const std::function<void(SavedAsset)>& callback) const = 0; + + // Helper + void DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, std::string_view extension) const; + void DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, const std::function<bool(std::istream&)>& validater) const; + + virtual void SaveEmptyInstance(const SavedAsset& asset) const = 0; + virtual Asset* CreateEmptyInstance(const SavedAsset& asset) const = 0; + + virtual Asset* LoadImpl(const SavedAsset& asset) const = 0; + + virtual std::string RetrieveNameFromFile(const std::filesystem::path& file) const = 0; + virtual uuids::uuid RetrieveUuidFromFile(const std::filesystem::path& file) const = 0; + virtual std::filesystem::path RetrievePathFromAsset(const SavedAsset& asset) const = 0; + + /// This should call ImGui::BeginTable() along with other accessories such as setting up the header row. + virtual void SetupDetailsTable(const char* tableId) const = 0; + virtual void DrawBigIcon(const SavedAsset& asset) const = 0; + virtual void DrawDetailsTableRow(const SavedAsset& asset) const = 0; }; -template <class TAsset> -class TypedAssetList : public AssetList +template <class T> +class AssetListTyped : public AssetList { public: - using Asset = TAsset; - using AssetType = typename TAsset::CategoryType; - -public: - // Import constructor - using AssetList::AssetList; - - Asset* FindByName(std::string_view name) const + std::unique_ptr<T> CreateAndLoad(SavedAsset asset) { - return static_cast<TAsset*>(AssetList::FindByName(name)); + return std::unique_ptr<T>(static_cast<T*>(AssetList::CreateAndLoad(asset).release())); } - std::unique_ptr<Asset> Create(std::string_view name) + + std::unique_ptr<T> Load(std::string_view name) const { - return std::unique_ptr<TAsset>(static_cast<TAsset*>(AssetList::Create(name).release())); + return std::unique_ptr<T>(static_cast<T*>(AssetList::Load(name).release())); } - std::unique_ptr<Asset> LoadFromDisk(std::string_view name) const + + std::unique_ptr<T> Load(const SavedAsset& asset) const { - return std::unique_ptr<TAsset>(static_cast<TAsset>(AssetList::LoadFromDisk(name))); + return std::unique_ptr<T>(static_cast<T*>(AssetList::Load(asset).release())); } }; |