From 4a84ca0bcd4f5a4ed1ff454407b888ad99773a61 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Fri, 24 Jun 2022 20:15:48 -0400 Subject: [PATCH] adds trash folder and functionality to the project, content_manager and EditorAsset classes to use the trash folder. When assets get removed they are moved to trash instead of deleted. --- docs/tasks/editor.todo | 1 + .../editor/contents/content_manager.cpp | 15 ++++++++++++ .../editor/contents/editor_asset.cpp | 8 +++++-- src/run_modes/editor/contents/editor_asset.h | 2 ++ src/run_modes/editor/panels/asset_browser.cpp | 10 ++++---- src/run_modes/editor/panels/asset_browser.h | 1 - src/run_modes/editor/project.cpp | 7 ++++++ src/run_modes/editor/project.h | 1 + test2/contents/content_meta.xml | 24 ------------------- test2/test2.lproj | 6 ----- 10 files changed, 36 insertions(+), 39 deletions(-) delete mode 100644 test2/contents/content_meta.xml delete mode 100644 test2/test2.lproj diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 9cee506..a61ef69 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -7,6 +7,7 @@ Editor: ✔ Implement Run Mode interface class @high @done (2/8/2022, 4:05:17 PM) ✔ Reference raw asset files in a "content" folder@high @done (3/3/2022, 3:15:32 PM) ✔ Platform independant file browsing @done (2/8/2022, 4:05:29 PM) + ☐ Test the Asset Trashing system @high ☐ Scan script files to make sure they don't overwrite globals ☐ Figure out how to make game asset types integrate with editor asset types @critical diff --git a/src/run_modes/editor/contents/content_manager.cpp b/src/run_modes/editor/contents/content_manager.cpp index b171360..98100bf 100644 --- a/src/run_modes/editor/contents/content_manager.cpp +++ b/src/run_modes/editor/contents/content_manager.cpp @@ -105,6 +105,13 @@ namespace lunarium { namespace editor return OpRes::Fail("Invalid asset node in content_meta.json"); } + // Check if asset is trashed + if (asset["Trashed"].get()) + { + // Skip trashed assets + continue; + } + // Load Type AssetType type = (AssetType)asset["Type"].get(); EditorAsset* pAsset = CreateAsset(type); @@ -115,6 +122,9 @@ namespace lunarium { namespace editor } pAsset->mAssetDir = mpProject->GetAssetDirectory(); + // If we got to this point the asset must not be trashed + pAsset->mIsTrashed = false; + // Load ID pAsset->mID = asset["ID"].get(); @@ -168,6 +178,7 @@ namespace lunarium { namespace editor asset["Type"] = (i32)pAsset->GetType(); asset["ID"] = pAsset->GetID(); + asset["Trashed"] = pAsset->GetIsTrashed(); // TODO: This needs to be a relative path! (Relative to the project root) asset["Location"] = pAsset->GetFileLocation().string().c_str(); @@ -305,6 +316,10 @@ namespace lunarium { namespace editor return; } + // Move the asset to a trash directory to prevent data loss + iter->second->mIsTrashed = true; + MoveAsset(iter->second, mpProject->GetTrashDirectory()); + delete iter->second; mAssets.erase(iter); Save().LogIfFailed(Editor::LogCat, "Asset was removed"); diff --git a/src/run_modes/editor/contents/editor_asset.cpp b/src/run_modes/editor/contents/editor_asset.cpp index fb5c08f..e6d8c9b 100644 --- a/src/run_modes/editor/contents/editor_asset.cpp +++ b/src/run_modes/editor/contents/editor_asset.cpp @@ -11,12 +11,11 @@ namespace lunarium { namespace editor { EditorAsset::EditorAsset(AssetType type) - : mType(type) + : mType(type), mIsTrashed(false) { } - AssetType EditorAsset::GetType() { return mType; @@ -31,5 +30,10 @@ namespace lunarium { namespace editor { { return mLocation; } + + bool EditorAsset::GetIsTrashed() const + { + return mIsTrashed; + } }} \ No newline at end of file diff --git a/src/run_modes/editor/contents/editor_asset.h b/src/run_modes/editor/contents/editor_asset.h index 010ef59..9fe328f 100644 --- a/src/run_modes/editor/contents/editor_asset.h +++ b/src/run_modes/editor/contents/editor_asset.h @@ -26,6 +26,7 @@ namespace lunarium { namespace editor AssetType GetType(); uint64_t GetID(); std::filesystem::path GetFileLocation(); + bool GetIsTrashed() const; [[nodiscard]] virtual OpRes LoadRawFile() = 0; [[nodiscard]] virtual OpRes LoadFromJSON(nlohmann::json& node) = 0; @@ -37,6 +38,7 @@ namespace lunarium { namespace editor AssetType mType; uint64_t mID; std::filesystem::path mLocation; + bool mIsTrashed; void SetLocation(std::filesystem::path path) { mLocation = path; } diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index 9ec3a1d..1533867 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -24,7 +24,7 @@ namespace editor { AssetBrowser::AssetBrowser(std::filesystem::path dir, Editor* pEditor) : Panel("Asset Browser", PanelDockZone::DDZ_BOTTOM, true, (ImGuiWindowFlags)ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), - mAssetDirectory(dir), mpEditor(pEditor), mNewFolder(false), mSyncTree(false) + mAssetDirectory(dir), mpEditor(pEditor), mSyncTree(false) { DefinePopups(); } @@ -184,6 +184,8 @@ namespace editor } } + + // List Assets std::filesystem::path selected_final = mpEditor->GetProject()->MakeRelativeToAssets(mSelectedDir); std::vector assets; ContentManager::GetInstance().GetAllAssetsInDirectory(assets, selected_final); @@ -243,11 +245,7 @@ namespace editor { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("EditorAsset")) { - EditorAsset* pAsset = *((EditorAsset**) payload->Data); - // EditorAsset* pAsset = ((EditorAsset*) payload->Data); - // Logger::Info(Editor::LogCat, "Dropping asset: %s - To: %s", - // pAsset->GetFileLocation().stem().string().c_str(), dir.string().c_str()); - + EditorAsset* pAsset = *((EditorAsset**) payload->Data); ContentManager::GetInstance().MoveAsset(pAsset, dir / pAsset->GetFileLocation().filename()); } ImGui::EndDragDropTarget(); diff --git a/src/run_modes/editor/panels/asset_browser.h b/src/run_modes/editor/panels/asset_browser.h index 01816cc..e2ab111 100644 --- a/src/run_modes/editor/panels/asset_browser.h +++ b/src/run_modes/editor/panels/asset_browser.h @@ -33,7 +33,6 @@ namespace editor std::filesystem::path mAssetDirectory; std::filesystem::path mSelectedDir; Editor* mpEditor; - bool mNewFolder; bool mSyncTree; private: diff --git a/src/run_modes/editor/project.cpp b/src/run_modes/editor/project.cpp index f25441b..c2c2402 100644 --- a/src/run_modes/editor/project.cpp +++ b/src/run_modes/editor/project.cpp @@ -61,6 +61,7 @@ namespace lunarium { namespace editor std::filesystem::create_directory(mLocation / std::filesystem::path("engine")); std::filesystem::create_directory(mLocation / std::filesystem::path("contents")); std::filesystem::create_directory(mLocation / std::filesystem::path("contents/assets")); + std::filesystem::create_directory(mLocation / std::filesystem::path("contents/trash")); mpContentManager = &ContentManager::GetInstance(); if (Failed(mpContentManager->Load(this).LogIfFailed(Editor::LogCat))) @@ -129,6 +130,12 @@ namespace lunarium { namespace editor return GetContentDirectory() / "assets"; } + + std::filesystem::path Project::GetTrashDirectory() const + { + return GetContentDirectory() / "trash"; + } + std::filesystem::path Project::MakeRelativeToRoot(std::filesystem::path abs_path, bool include_root) const { return MakeRelativeTo(abs_path, mLocation, include_root); diff --git a/src/run_modes/editor/project.h b/src/run_modes/editor/project.h index 3e41366..30e41d4 100644 --- a/src/run_modes/editor/project.h +++ b/src/run_modes/editor/project.h @@ -38,6 +38,7 @@ namespace lunarium { namespace editor std::filesystem::path GetRootDirectory() const; std::filesystem::path GetContentDirectory() const; std::filesystem::path GetAssetDirectory() const; + std::filesystem::path GetTrashDirectory() const; // Convert an absolute path to a path relative to the project root std::filesystem::path MakeRelativeToRoot(std::filesystem::path, bool include_root = false) const; diff --git a/test2/contents/content_meta.xml b/test2/contents/content_meta.xml deleted file mode 100644 index 8522427..0000000 --- a/test2/contents/content_meta.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - -{ - "Project" : { - "Name" = "test2.lproj", - "NextID" = 1, - "Contents" : [ - { - "Type" = 1, - "ID" = 0, - "Location" = "D:\Projects\lunarium\build\Debug\test2\contents\assets\LinkToThePast1_sized.png", - "TileSizeWidth" = 16, - "TileSizeHeight" = 16, - "NumTilesCol" = 64, - "NumTilesRow" = 64 - }, - ] - } -} \ No newline at end of file diff --git a/test2/test2.lproj b/test2/test2.lproj deleted file mode 100644 index 2cf4c7e..0000000 --- a/test2/test2.lproj +++ /dev/null @@ -1,6 +0,0 @@ -{ - "Project" : { - "Name" : "test2" - } -} -