diff options
author | rtk0c <[email protected]> | 2021-06-11 22:19:23 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-06-11 22:19:23 -0700 |
commit | bdee9dd0c92865e0cec2f4bbf170959df282a930 (patch) | |
tree | af9d40cb4378ee2166574faed9cc16e283110f31 /core/src/Model | |
parent | 8f7daa9bd100345d7e23639604c9a3a50ce6448b (diff) |
More UI polishing and fix asset saving/reloading
Diffstat (limited to 'core/src/Model')
-rw-r--r-- | core/src/Model/Assets.cpp | 9 | ||||
-rw-r--r-- | core/src/Model/Assets.hpp | 9 | ||||
-rw-r--r-- | core/src/Model/Project.cpp | 24 | ||||
-rw-r--r-- | core/src/Model/Project.hpp | 14 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.cpp | 10 | ||||
-rw-r--r-- | core/src/Model/Template/Template.hpp | 4 | ||||
-rw-r--r-- | core/src/Model/Template/Template_Main.cpp | 14 | ||||
-rw-r--r-- | core/src/Model/Workflow/Workflow.hpp | 4 | ||||
-rw-r--r-- | core/src/Model/Workflow/Workflow_Main.cpp | 14 |
9 files changed, 64 insertions, 38 deletions
diff --git a/core/src/Model/Assets.cpp b/core/src/Model/Assets.cpp index 64e1f22..374995d 100644 --- a/core/src/Model/Assets.cpp +++ b/core/src/Model/Assets.cpp @@ -21,6 +21,7 @@ Asset::Asset() class AssetList::Private { public: + Project* ConnectedProject; tsl::array_map<char, SavedAsset> Assets; tsl::array_map<char, std::unique_ptr<Asset>> Cache; int CacheSizeLimit = 0; @@ -65,9 +66,10 @@ public: } PopupPrivateState; }; -AssetList::AssetList() +AssetList::AssetList(Project& project) : mPrivate{ std::make_unique<Private>() } { + mPrivate->ConnectedProject = &project; } // Write an empty destructor here so std::unique_ptr's destructor can see AssetList::Private's implementation @@ -75,6 +77,11 @@ AssetList::~AssetList() { } +Project& AssetList::GetConnectedProject() const +{ + return *mPrivate->ConnectedProject; +} + void AssetList::Reload() { DiscoverFiles([this](SavedAsset asset) -> void { diff --git a/core/src/Model/Assets.hpp b/core/src/Model/Assets.hpp index adde97b..9fd781f 100644 --- a/core/src/Model/Assets.hpp +++ b/core/src/Model/Assets.hpp @@ -1,8 +1,7 @@ #pragma once #include "Utils/UUID.hpp" - -#include "Assets.hpp" +#include "cplt_fwd.hpp" #include <filesystem> #include <iosfwd> @@ -42,9 +41,11 @@ private: std::unique_ptr<Private> mPrivate; public: - AssetList(); + AssetList(Project& project); virtual ~AssetList(); + Project& GetConnectedProject() const; + // TODO support file watches void Reload(); @@ -99,6 +100,8 @@ template <class T> class AssetListTyped : public AssetList { public: + using AssetList::AssetList; + std::unique_ptr<T> CreateAndLoad(SavedAsset asset) { return std::unique_ptr<T>(static_cast<T*>(AssetList::CreateAndLoad(asset).release())); diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp index 9f41d3a..523ee9b 100644 --- a/core/src/Model/Project.cpp +++ b/core/src/Model/Project.cpp @@ -14,7 +14,7 @@ namespace fs = std::filesystem; template <class T> -void ReadItemList(ItemList<T>& list, const fs::path& filePath) +static void ReadItemList(ItemList<T>& list, const fs::path& filePath) { std::ifstream ifs(filePath); if (ifs) { @@ -25,9 +25,19 @@ void ReadItemList(ItemList<T>& list, const fs::path& filePath) } } +static void CreateProjectSubfolders(const Project& project) +{ + fs::create_directory(project.GetDatabasesDirectory()); + fs::create_directory(project.GetItemsDirectory()); + fs::create_directory(project.GetWorkflowsDirectory()); + fs::create_directory(project.GetTemplatesDirectory()); +} + Project::Project(fs::path rootPath) : mRootPath{ std::move(rootPath) } , mRootPathString{ mRootPath.string() } + , Workflows(*this) + , Templates(*this) , Database(*this) { // TODO better diagnostic @@ -58,18 +68,26 @@ Project::Project(fs::path rootPath) } } + CreateProjectSubfolders(*this); + auto itemsDir = mRootPath / "items"; ReadItemList(Products, itemsDir / "products.json"); ReadItemList(Factories, itemsDir / "factories.json"); ReadItemList(Customers, itemsDir / "customers.json"); + + Workflows.Reload(); + Templates.Reload(); } Project::Project(fs::path rootPath, std::string name) : mRootPath{ std::move(rootPath) } , mRootPathString{ mRootPath.string() } , mName{ std::move(name) } + , Workflows(*this) + , Templates(*this) , Database(*this) { + CreateProjectSubfolders(*this); } const fs::path& Project::GetPath() const @@ -143,9 +161,7 @@ void Project::WriteToDisk() std::ofstream ofs(mRootPath / "cplt_project.json"); ofs << this->Serialize(); - auto itemsDir = mRootPath / "items"; - fs::create_directories(itemsDir); - + auto itemsDir = GetItemsDirectory(); WriteItemList(Products, itemsDir / "products.json"); WriteItemList(Factories, itemsDir / "factories.json"); WriteItemList(Customers, itemsDir / "customers.json"); diff --git a/core/src/Model/Project.hpp b/core/src/Model/Project.hpp index 8cf3483..17d9acb 100644 --- a/core/src/Model/Project.hpp +++ b/core/src/Model/Project.hpp @@ -14,24 +14,24 @@ class Project { -public: - WorkflowAssetList Workflows; - TemplateAssetList Templates; - ItemList<ProductItem> Products; - ItemList<FactoryItem> Factories; - ItemList<CustomerItem> Customers; - private: std::filesystem::path mRootPath; std::string mRootPathString; std::string mName; + // (Exception to style guidelines) // This is put after the private fields, so that when XxxDatabase's constructor runs, all of them will be initialized public: + WorkflowAssetList Workflows; + TemplateAssetList Templates; + ItemList<ProductItem> Products; + ItemList<FactoryItem> Factories; + ItemList<CustomerItem> Customers; MainDatabase Database; public: /// Load the project from a directory containing the cplt_project.json file. + /// This only loads the main project file, the caller needs to Project(std::filesystem::path rootPath); /// Create a project with the given name in the given path. Note that the path should be a directory that will contain the project files once created. diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index 28a4d6e..f2524a0 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -88,17 +88,15 @@ void TableTemplate::Resize(int newWidth, int newHeight) int tableWidth = GetTableWidth(); int tableHeight = GetTableHeight(); - int yEnd = std::min(tableHeight, newHeight); - int xEnd = std::min(tableWidth, newWidth); - for (int y = 0; y < yEnd; ++y) { + for (int y = 0; y < newHeight; ++y) { if (y >= tableHeight) { - for (int x = 0; x < xEnd; ++x) { + for (int x = 0; x < newWidth; ++x) { cells.push_back(TableCell{}); } continue; } - for (int x = 0; x < xEnd; ++x) { + for (int x = 0; x < newWidth; ++x) { if (x >= tableWidth) { cells.push_back(TableCell{}); } else { @@ -109,6 +107,8 @@ void TableTemplate::Resize(int newWidth, int newHeight) } mCells = std::move(cells); + mColumnWidths.resize(newWidth); + mRowHeights.resize(newHeight); } int TableTemplate::GetRowHeight(int row) const diff --git a/core/src/Model/Template/Template.hpp b/core/src/Model/Template/Template.hpp index 7cfbb8b..7d43130 100644 --- a/core/src/Model/Template/Template.hpp +++ b/core/src/Model/Template/Template.hpp @@ -52,6 +52,10 @@ private: NameSelectionError mACNewNameError = NameSelectionError::Empty; Template::Kind mACNewKind = Template::InvalidKind; +public: + // Inherit constructors + using AssetListTyped::AssetListTyped; + protected: virtual void DiscoverFiles(const std::function<void(SavedAsset)>& callback) const override; diff --git a/core/src/Model/Template/Template_Main.cpp b/core/src/Model/Template/Template_Main.cpp index 7dd5f87..8b659cf 100644 --- a/core/src/Model/Template/Template_Main.cpp +++ b/core/src/Model/Template/Template_Main.cpp @@ -25,8 +25,8 @@ Template::Kind Template::GetKind() const void TemplateAssetList::DiscoverFiles(const std::function<void(SavedAsset)>& callback) const { - auto& gs = GlobalStates::GetInstance(); - DiscoverFilesByExtension(callback, gs.GetCurrentProject()->GetTemplatesDirectory(), ".cplt-template"sv); + auto dir = GetConnectedProject().GetTemplatesDirectory(); + DiscoverFilesByExtension(callback, dir, ".cplt-template"sv); } std::string TemplateAssetList::RetrieveNameFromFile(const fs::path& file) const @@ -46,12 +46,8 @@ uuids::uuid TemplateAssetList::RetrieveUuidFromFile(const fs::path& file) const fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const { - auto uuid = uuids::uuid_random_generator{}(); - auto fileName = uuids::to_string(uuid); - fileName.append(".cplt-template"); - - auto& gs = GlobalStates::GetInstance(); - return gs.GetCurrentProject()->GetTemplatePath(fileName); + auto fileName = uuids::to_string(asset.Uuid); + return GetConnectedProject().GetTemplatePath(fileName); } void TemplateAssetList::SaveEmptyInstance(const SavedAsset& asset) const @@ -183,7 +179,7 @@ void TemplateAssetList::DrawBigIcon(ListState& state, const SavedAsset& asset) c void TemplateAssetList::DrawDetailsTableRow(ListState& state, const SavedAsset& asset) const { ImGui::TableNextColumn(); - if (ImGui::Selectable(asset.Name.c_str(), state.SelectedAsset == &asset, ImGuiSelectableFlags_SpanAllColumns)) { + if (ImGui::Selectable(asset.Name.c_str(), state.SelectedAsset == &asset, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups)) { state.SelectedAsset = &asset; } diff --git a/core/src/Model/Workflow/Workflow.hpp b/core/src/Model/Workflow/Workflow.hpp index 3dc6f38..9c809bf 100644 --- a/core/src/Model/Workflow/Workflow.hpp +++ b/core/src/Model/Workflow/Workflow.hpp @@ -283,6 +283,10 @@ private: NameSelectionError mACNewNameError = NameSelectionError::Empty; public: + // Inherit constructors + using AssetListTyped::AssetListTyped; + +public: virtual void DiscoverFiles(const std::function<void(SavedAsset)>& callback) const override; virtual std::string RetrieveNameFromFile(const std::filesystem::path& file) const override; diff --git a/core/src/Model/Workflow/Workflow_Main.cpp b/core/src/Model/Workflow/Workflow_Main.cpp index adf944e..77b64d3 100644 --- a/core/src/Model/Workflow/Workflow_Main.cpp +++ b/core/src/Model/Workflow/Workflow_Main.cpp @@ -746,8 +746,8 @@ std::pair<std::unique_ptr<WorkflowNode>&, uint32_t> Workflow::AllocWorkflowStep( void WorkflowAssetList::DiscoverFiles(const std::function<void(SavedAsset)>& callback) const { - auto& gs = GlobalStates::GetInstance(); - DiscoverFilesByExtension(callback, gs.GetCurrentProject()->GetWorkflowsDirectory(), ".cplt-workflow"sv); + auto dir = GetConnectedProject().GetWorkflowsDirectory(); + DiscoverFilesByExtension(callback, dir, ".cplt-workflow"sv); } std::string WorkflowAssetList::RetrieveNameFromFile(const fs::path& file) const @@ -767,12 +767,8 @@ uuids::uuid WorkflowAssetList::RetrieveUuidFromFile(const fs::path& file) const fs::path WorkflowAssetList::RetrievePathFromAsset(const SavedAsset& asset) const { - auto uuid = uuids::uuid_random_generator{}(); - auto fileName = uuids::to_string(uuid); - fileName.append(".cplt-workflow"); - - auto& gs = GlobalStates::GetInstance(); - return gs.GetCurrentProject()->GetTemplatePath(fileName); + auto fileName = uuids::to_string(asset.Uuid); + return GetConnectedProject().GetTemplatePath(fileName); } void WorkflowAssetList::SaveEmptyInstance(const SavedAsset& asset) const @@ -875,7 +871,7 @@ void WorkflowAssetList::DrawBigIcon(ListState& state, const SavedAsset& asset) c void WorkflowAssetList::DrawDetailsTableRow(ListState& state, const SavedAsset& asset) const { ImGui::TableNextColumn(); - if (ImGui::Selectable(asset.Name.c_str(), state.SelectedAsset == &asset, ImGuiSelectableFlags_SpanAllColumns)) { + if (ImGui::Selectable(asset.Name.c_str(), state.SelectedAsset == &asset, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups)) { state.SelectedAsset = &asset; } } |