From 64a6dbcfdb89a5f57d93d47a2be0c741dda4662d Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 15 Aug 2021 09:17:02 -0700 Subject: Fix issues and cleanup - Fix compile error on MSVC where is not included properly - Fix creating a workflow actually saves to the templates/ directory - Fix renaming assets are not saved to disk - TODO ser/deser still not working - Cleanup AssetList::* pure virtual setup (fewer splits) --- core/src/Model/Assets.cpp | 25 +++++---- core/src/Model/Assets.hpp | 26 +++++---- core/src/Model/Template/TableTemplate.hpp | 11 ++-- core/src/Model/Template/Template.hpp | 25 ++++----- core/src/Model/Template/Template_Main.cpp | 90 ++++++++++++++++++++----------- core/src/Model/Workflow/Value.hpp | 3 +- core/src/Model/Workflow/Workflow.hpp | 31 +++++------ core/src/Model/Workflow/Workflow_Main.cpp | 84 +++++++++++++++++++---------- core/src/UI/UI_Templates.cpp | 34 +++++++----- core/src/UI/UI_Workflows.cpp | 14 +++-- 10 files changed, 211 insertions(+), 132 deletions(-) (limited to 'core/src') diff --git a/core/src/Model/Assets.cpp b/core/src/Model/Assets.cpp index 374995d..dc1ebbd 100644 --- a/core/src/Model/Assets.cpp +++ b/core/src/Model/Assets.cpp @@ -3,10 +3,10 @@ #include "UI/UI.hpp" #include "Utils/I18n.hpp" +#include #include #include #include -#include #include #include #include @@ -94,6 +94,11 @@ int AssetList::GetCount() const return mPrivate->Assets.size(); } +const tsl::array_map& AssetList::GetAssets() const +{ + return mPrivate->Assets; +} + const SavedAsset* AssetList::FindByName(std::string_view name) const { auto iter = mPrivate->Assets.find(name); @@ -114,7 +119,7 @@ const SavedAsset& AssetList::Create(SavedAsset asset) savedAsset.Uuid = uuids::uuid_random_generator{}(); } - SaveEmptyInstance(savedAsset); + SaveInstance(savedAsset, nullptr); return savedAsset; } @@ -122,7 +127,7 @@ const SavedAsset& AssetList::Create(SavedAsset asset) std::unique_ptr AssetList::CreateAndLoad(SavedAsset assetIn) { auto& savedAsset = Create(std::move(assetIn)); - auto asset = std::unique_ptr(CreateEmptyInstance(savedAsset)); + auto asset = std::unique_ptr(CreateInstance(savedAsset)); return asset; } @@ -138,7 +143,7 @@ std::unique_ptr AssetList::Load(std::string_view name) const std::unique_ptr AssetList::Load(const SavedAsset& asset) const { - return std::unique_ptr(LoadImpl(asset)); + return std::unique_ptr(LoadInstance(asset)); } const SavedAsset* AssetList::Rename(std::string_view oldName, std::string_view newName) @@ -149,8 +154,10 @@ const SavedAsset* AssetList::Rename(std::string_view oldName, std::string_view n auto info = std::move(iter.value()); info.Name = newName; - auto [newIter, DISCARD] = mPrivate->Assets.insert(newName, std::move(info)); + RenameInstanceOnDisk(info, oldName); + mPrivate->Assets.erase(iter); + auto [newIter, DISCARD] = mPrivate->Assets.insert(newName, std::move(info)); return &newIter.value(); } @@ -186,12 +193,8 @@ void AssetList::DisplayIconsList(ListState& state) void AssetList::DisplayDetailsList(ListState& state) { - SetupDetailsTable("AssetDetailsTable"); - for (auto& asset : mPrivate->Assets) { - ImGui::TableNextRow(); - DrawDetailsTableRow(state, asset); - } - ImGui::EndTable(); + // Note: stub function remained here in case any state processing needs to be done before issuing to implementers + DisplayDetailsTable(state); } void AssetList::DisplayControls(ListState& state) diff --git a/core/src/Model/Assets.hpp b/core/src/Model/Assets.hpp index 9fd781f..0a050d6 100644 --- a/core/src/Model/Assets.hpp +++ b/core/src/Model/Assets.hpp @@ -3,6 +3,7 @@ #include "Utils/UUID.hpp" #include "cplt_fwd.hpp" +#include #include #include #include @@ -50,6 +51,9 @@ public: void Reload(); int GetCount() const; + // TODO convert to custom iterable + const tsl::array_map& GetAssets() const; + const SavedAsset* FindByName(std::string_view name) const; const SavedAsset& Create(SavedAsset asset); std::unique_ptr CreateAndLoad(SavedAsset asset); @@ -79,21 +83,22 @@ protected: void DiscoverFilesByExtension(const std::function& callback, const std::filesystem::path& containerDir, std::string_view extension) const; void DiscoverFilesByHeader(const std::function& callback, const std::filesystem::path& containerDir, const std::function& 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; + /// Create an empty/default instance of this asset type on disk, potentially qualified by SavedAsset::Payload. + /// Return `true` on success and `false` on failure. + virtual bool SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const = 0; + /// The returned pointer indicate ownership to the object. + virtual Asset* LoadInstance(const SavedAsset& assetInfo) const = 0; + /// Create an empty/default instance of this asset type, potentially qualified by SavedAsset::Payload. + /// The returned pointer indicate ownership to the object. + virtual Asset* CreateInstance(const SavedAsset& assetInfo) const = 0; + virtual bool RenameInstanceOnDisk(const SavedAsset& assetInfo, std::string_view oldName) 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; virtual void DisplayAssetCreator(ListState& state) = 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(ListState& state, const SavedAsset& asset) const = 0; - virtual void DrawDetailsTableRow(ListState& state, const SavedAsset& asset) const = 0; + virtual void DisplayDetailsTable(ListState& state) const = 0; }; template @@ -102,6 +107,8 @@ class AssetListTyped : public AssetList public: using AssetList::AssetList; +#pragma clang diagnostic push +#pragma ide diagnostic ignored "HidingNonVirtualFunction" std::unique_ptr CreateAndLoad(SavedAsset asset) { return std::unique_ptr(static_cast(AssetList::CreateAndLoad(asset).release())); @@ -116,4 +123,5 @@ public: { return std::unique_ptr(static_cast(AssetList::Load(asset).release())); } +#pragma clang diagnostic pop }; diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index 141e952..c651ce1 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -43,7 +43,7 @@ public: TextAlignment HorizontalAlignment = AlignCenter; TextAlignment VerticalAlignment = AlignCenter; CellType Type = ConstantCell; - /// The id of the group description object, if this cell isn't a constant or singluar parameter cell. Otherwise, this value is -1. + /// The id of the group description object, if this cell isn't a constant or singular parameter cell. Otherwise, this value is -1. int DataId = -1; public: @@ -57,11 +57,12 @@ public: // TODO support reverse (bottom to top) filling order // TODO support horizontal filling order -/// Parameter group information for a grouped array of cells. When instanciated, an array of 0 or more + +/// Parameter group information for a grouped array of cells. When instantiated, an array of 0 or more /// elements shall be provided by the user, which will replace the group of templated cells with a list /// of rows, each instantiated with the n-th element in the provided array. /// \code -/// [["foo", "bar", "foobar"], +/// [["foo", "bar", "foobar"], /// ["a", "b", c"], /// ["1", "2", "3"], /// ["x", "y", "z"]] @@ -210,6 +211,6 @@ public: lxw_workbook* InstantiateToExcelWorkbook(const TableInstantiationParameters& params) const; lxw_worksheet* InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstantiationParameters& params) const; - virtual ReadResult ReadFrom(std::istream& stream) override; - virtual void WriteTo(std::ostream& stream) const override; + ReadResult ReadFrom(std::istream& stream) override; + void WriteTo(std::ostream& stream) const override; }; diff --git a/core/src/Model/Template/Template.hpp b/core/src/Model/Template/Template.hpp index 7d43130..30fdc75 100644 --- a/core/src/Model/Template/Template.hpp +++ b/core/src/Model/Template/Template.hpp @@ -31,7 +31,7 @@ public: static bool IsInstance(const Template* tmpl); Template(Kind kind); - virtual ~Template() = default; + ~Template() override = default; Kind GetKind() const; @@ -57,20 +57,17 @@ public: using AssetListTyped::AssetListTyped; protected: - virtual void DiscoverFiles(const std::function& callback) const override; + void DiscoverFiles(const std::function& callback) const override; - virtual std::string RetrieveNameFromFile(const std::filesystem::path& file) const override; - virtual uuids::uuid RetrieveUuidFromFile(const std::filesystem::path& file) const override; - virtual std::filesystem::path RetrievePathFromAsset(const SavedAsset& asset) const override; + std::string RetrieveNameFromFile(const std::filesystem::path& file) const override; + uuids::uuid RetrieveUuidFromFile(const std::filesystem::path& file) const override; + std::filesystem::path RetrievePathFromAsset(const SavedAsset& asset) const override; - virtual void SaveEmptyInstance(const SavedAsset& asset) const override; - virtual Template* CreateEmptyInstance(const SavedAsset& diskForm) const override; + bool SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const override; + Template* LoadInstance(const SavedAsset& assetInfo) const override; + Template* CreateInstance(const SavedAsset& assetInfo) const override; + bool RenameInstanceOnDisk(const SavedAsset& assetInfo, std::string_view oldName) const override; - virtual Template* LoadImpl(const SavedAsset& diskForm) const override; - - virtual void DisplayAssetCreator(ListState& state) override; - - virtual void SetupDetailsTable(const char* tableId) const override; - virtual void DrawBigIcon(ListState& state, const SavedAsset& asset) const override; - virtual void DrawDetailsTableRow(ListState& state, const SavedAsset& asset) const override; + void DisplayAssetCreator(ListState& state) override; + void DisplayDetailsTable(ListState& state) const override; }; diff --git a/core/src/Model/Template/Template_Main.cpp b/core/src/Model/Template/Template_Main.cpp index a681c4a..19803aa 100644 --- a/core/src/Model/Template/Template_Main.cpp +++ b/core/src/Model/Template/Template_Main.cpp @@ -2,8 +2,8 @@ #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" -#include "Utils/I18n.hpp" #include "UI/UI.hpp" +#include "Utils/I18n.hpp" #include "Utils/UUID.hpp" #include @@ -57,28 +57,33 @@ fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const return GetConnectedProject().GetTemplatePath(fileName); } -void TemplateAssetList::SaveEmptyInstance(const SavedAsset& asset) const +bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const { - auto path = RetrievePathFromAsset(asset); + auto path = RetrievePathFromAsset(assetInfo); - std::ofstream ofs(path); - if (!ofs) return; - ofs << (uint64_t)asset.Name.size(); - ofs << asset.Name; - ofs << static_cast(asset.Payload); -} + std::ofstream ofs(path, std::ios::binary); + if (!ofs) return false; + ofs << (uint64_t)assetInfo.Name.size(); + ofs << assetInfo.Name; + ofs << static_cast(assetInfo.Payload); -Template* TemplateAssetList::CreateEmptyInstance(const SavedAsset& asset) const -{ - auto kind = static_cast(asset.Payload); - return Template::CreateByKind(kind).release(); + if (auto tmpl = static_cast(asset)) { + tmpl->WriteTo(ofs); + } + + return true; } -Template* TemplateAssetList::LoadImpl(const SavedAsset& asset) const +static std::unique_ptr