diff options
author | rtk0c <[email protected]> | 2021-06-05 19:44:54 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-06-05 19:44:54 -0700 |
commit | a180e1b56025c6b7d81d2e587ad90531d29de44c (patch) | |
tree | 1c88361320371b508c81b52a28ae0579fcdd1fd7 /core/src/Model/Assets.cpp | |
parent | d3fa128d5408673b0ea8d3ba3435c38b258a5e7a (diff) |
Initial work on asset system (does not compile)
Diffstat (limited to 'core/src/Model/Assets.cpp')
-rw-r--r-- | core/src/Model/Assets.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/core/src/Model/Assets.cpp b/core/src/Model/Assets.cpp new file mode 100644 index 0000000..e4eee54 --- /dev/null +++ b/core/src/Model/Assets.cpp @@ -0,0 +1,126 @@ +#include "Assets.hpp" + +#include <imgui.h> +#include <fstream> +#include <utility> + +namespace fs = std::filesystem; + +Asset::Asset() +{ +} + +std::unique_ptr<Asset> AssetCategory::CreateEmptyUnique(const SavedAsset& diskForm) const +{ + return std::unique_ptr<Asset>(CreateEmpty(diskForm)); +} + +std::unique_ptr<Asset> AssetCategory::LoadUnique(const SavedAsset& diskForm) const +{ + return std::unique_ptr<Asset>(Load(diskForm)); +} + +void AssetCategory::DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, std::string_view extension) +{ + for (auto entry : fs::directory_iterator(containerDir)) { + if (!entry.is_regular_file()) continue; + + // If the caller provided an extension to match against, and it doesn't equal to current file extension, skip + if (!extension.empty() && + entry.path().extension() != extension) + { + continue; + } + + callback(SavedAsset{ + .Path = entry.path(), + .Name = entry.path().stem().string(), + }); + } +} + +void AssetCategory::DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const std::filesystem::__cxx11::path& containerDir, const std::function<bool(std::istream&)>& validater) +{ + // TODO +} + +AssetList::AssetList(const AssetCategory& loader) + : mLoader{ &loader } +{ +} + +void AssetList::Reload() +{ + mLoader->DiscoverFiles([this](SavedAsset asset) -> void { + Create(std::move(asset)); + }); +} + +const SavedAsset* AssetList::FindByName(std::string_view name) const +{ + auto iter = mAssets.find(name); + if (iter != mAssets.end()) { + return &iter.value(); + } else { + return nullptr; + } +} + +const SavedAsset& AssetList::Create(SavedAsset asset) +{ + auto [iter, DISCARD] = mAssets.insert(asset.Name, SavedAsset{}); + auto& savedAsset = iter.value(); + + mLoader->CreateEmpty(asset); + savedAsset = std::move(asset); + + return savedAsset; +} + +std::unique_ptr<Asset> AssetList::CreateAndLoad(SavedAsset assetIn) +{ + auto& savedAsset = Create(std::move(assetIn)); + auto asset = std::unique_ptr<Asset>(mLoader->CreateEmpty(savedAsset)); + return asset; +} + +std::unique_ptr<Asset> AssetList::LoadFromDisk(std::string_view name) const +{ + if (auto savedAsset = FindByName(name)) { + auto asset = mLoader->LoadUnique(*savedAsset); + return asset; + } +} + +bool AssetList::Rename(std::string_view oldName, std::string_view newName) +{ +} + +bool AssetList::Remove(std::string_view name) +{ +} + +int AssetList::GetCacheSizeLimit() const +{ + return mCacheSizeLimit; +} + +void AssetList::SetCacheSizeLimit(int limit) +{ + mCacheSizeLimit = limit; +} + +void AssetList::DrawBigIcons(DrawState& state) +{ + // TODO +} + +void AssetList::DrawDetails(DrawState& state) +{ + mLoader->SetupDetailsTable("AssetDetailsTable"); + for (auto& asset : mAssets) { + mLoader->DrawDetailsTableRow(asset); + ImGui::TableNextRow(); + } + ImGui::EndTable(); +} |