From b1f89a345fac7db0d5c2e7523a8449b0e3b6b033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Tue, 1 Aug 2023 16:45:33 +0200 Subject: [PATCH] Deletion of Assets and Directories --- GlitchyEditor/src/Assets/AssetHierarchy.bf | 30 +++++++++++++++++++ .../src/EditWindows/ContentBrowserWindow.bf | 13 ++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/GlitchyEditor/src/Assets/AssetHierarchy.bf b/GlitchyEditor/src/Assets/AssetHierarchy.bf index 1b8e48b..6d1faff 100644 --- a/GlitchyEditor/src/Assets/AssetHierarchy.bf +++ b/GlitchyEditor/src/Assets/AssetHierarchy.bf @@ -137,6 +137,36 @@ class AssetHierarchy Log.EngineLogger.Trace($"Created root node"); } + /// Deletes the file that belongs to the given assetNode + public void DeleteFile(AssetNode assetNode) + { + if (assetNode.IsDirectory) + { + if (Directory.DelTree(assetNode.Path) case .Err(let error)) + { + Log.EngineLogger.Error($"Couldn't delete Directory ({error})."); + } + } + else + { + if (File.Delete(assetNode.Path) case .Err(let error)) + { + Log.EngineLogger.Error($"Couldn't delete File \"{assetNode.Path}\" ({error})."); + } + } + + StringView assetDescriptorPath = assetNode.AssetFile?.FilePath ?? ""; + + // If it exists, also try to delete the .ass file + if (File.Exists(assetDescriptorPath)) + { + if (File.Delete(assetDescriptorPath) case .Err(let error)) + { + Log.EngineLogger.Error($"Couldn't delete Asset descriptor file \"{assetDescriptorPath}\" ({error})."); + } + } + } + public void SetResourcesDirectory(StringView fileName) { ResourcesDirectory = fileName; diff --git a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf index d86985f..56c72cb 100644 --- a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf +++ b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf @@ -469,12 +469,17 @@ namespace GlitchyEditor.EditWindows ImGui.TextUnformatted(entry->Name); + bool wantsDelete = false; + if (ImGui.BeginPopupContextWindow()) { - ShowItemContextMenu(entry); + ShowItemContextMenu(entry, ref wantsDelete); ImGui.EndPopup(); } + if (wantsDelete) + ImGui.OpenPopup("Delete?"); + // TODO: Sub assets are probably borked now... I don't know if they ever worked, didn't test them if (entry->SubAssets?.Count > 0) { @@ -532,6 +537,8 @@ namespace GlitchyEditor.EditWindows if (ImGui.Button("Yes", ImGui.Vec2(120, 0))) { + _manager.AssetHierarchy.DeleteFile(fileOrFolder.Value); + ImGui.CloseCurrentPopup(); } @@ -548,7 +555,7 @@ namespace GlitchyEditor.EditWindows } /// Shows the context menu for the given file/folder. - private void ShowItemContextMenu(TreeNode fileOrFolder) + private void ShowItemContextMenu(TreeNode fileOrFolder, ref bool wantsDelete) { bool isFile = !fileOrFolder->IsDirectory; @@ -570,7 +577,7 @@ namespace GlitchyEditor.EditWindows if (ImGui.MenuItem("Delete")) { - ImGui.OpenPopup("Delete?"); + wantsDelete = true; } }