From 300a2f1a8ac9a6ba36267ace941ab40591b589dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 29 Jun 2025 22:22:44 +0200 Subject: [PATCH] Refactored (unified) rendering of directory items --- .../src/EditWindows/ContentBrowserWindow.bf | 94 ++++++------------- 1 file changed, 27 insertions(+), 67 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf index 7fa7042..ae97738 100644 --- a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf +++ b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf @@ -465,7 +465,7 @@ namespace GlitchyEditor.EditWindows { ImGui.PushID("Back"); - DrawBackButton(currentDirectoryNode.Parent); + DrawDirectoryEntry(currentDirectoryNode.Parent, .ParentDirectory); // X-Coordinate of the right side of the current entry. float currentButtonRight = ImGui.GetItemRectMax().x; @@ -500,7 +500,7 @@ namespace GlitchyEditor.EditWindows ImGui.PushID(entry->Name); - DrawDirectoryItem(entry); + DrawDirectoryEntry(entry, .Default); // X-Coordinate of the right side of the current entry. float currentButtonRight = ImGui.GetItemRectMax().x; @@ -529,56 +529,6 @@ namespace GlitchyEditor.EditWindows float2 IconSize => IconBaseSize * _zoom; - /// Renders the button for the given directory item. - private void DrawBackButton(TreeNode entry) - { - ImGui.PushStyleVar(.WindowPadding, .(0, 0)); - ImGui.PushStyleVar(.FramePadding, .(0, 0)); - ImGui.PushStyleVar(.ItemSpacing, .(0, 0)); - - defer {ImGui.PopStyleVar(3); } - - float2 DirectoryItemSize = IconSize + (float2)ImGui.GetStyle().WindowPadding * 2 + float2(0, ImGui.GetFontSize()); - - ImGui.BeginChild("item", (.)DirectoryItemSize, .None, .NoScrollbar); - - if (entry->Path == _selectedFile) - { - var color = ImGui.GetStyleColorVec4(.ButtonHovered); - ImGui.PushStyleColor(.Button, *color); - } - else - { - ImGui.PushStyleColor(.Button, ImGui.Vec4(0, 0, 0, 0)); - } - - SubTexture2D image = s_FolderTexture; - - ImGui.ImageButton("BackFolder", image, (.)IconSize); - - ImGui.PopStyleColor(); - - FileDropTarget(entry, .Internal | .External); - - if (ImGui.IsItemHovered() && ImGui.IsMouseClicked(.Left)) - { - if (_selectedFile != entry->Path) - { - SelectFile(entry->Path); - _assetToRename.Clear(); - } - } - - if (ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(.Left)) - { - OpenEntry(entry); - } - - ImGui.TextUnformatted(".."); - - ImGui.EndChild(); - } - enum DropTargetMode { Internal = 1, @@ -650,8 +600,14 @@ namespace GlitchyEditor.EditWindows } } - /// Renders the button for the given directory item. - private void DrawDirectoryItem(TreeNode entry) + enum DirectoryItemType + { + Default, + ParentDirectory + } + + /// Renders the button for the given directory entry. + private void DrawDirectoryEntry(TreeNode entry, DirectoryItemType itemType) { ImGui.PushStyleVar(.WindowPadding, .(0, 0)); ImGui.PushStyleVar(.FramePadding, .(0, 0)); @@ -674,6 +630,7 @@ namespace GlitchyEditor.EditWindows } // TODO: preview images + // TODO: Special Icon for back-folder? SubTexture2D image = _thumbnailManager.GetThumbnail(entry.Value); ImGui.ImageButton("FileImage", image, (.)IconSize); @@ -746,14 +703,14 @@ namespace GlitchyEditor.EditWindows } else { - ImGui.TextUnformatted(entry->Name); + ImGui.TextUnformatted(itemType == .ParentDirectory ? ".." : entry->Name); } bool wantsDelete = false; if (ImGui.BeginPopupContextWindow()) { - ShowItemContextMenu(entry, ref wantsDelete); + ShowItemContextMenu(entry, itemType, ref wantsDelete); ImGui.EndPopup(); } @@ -888,7 +845,7 @@ namespace GlitchyEditor.EditWindows } /// Shows the context menu for the given file/folder. - private void ShowItemContextMenu(TreeNode fileOrFolder, ref bool wantsDelete) + private void ShowItemContextMenu(TreeNode fileOrFolder, DirectoryItemType itemType, ref bool wantsDelete) { bool isFile = !fileOrFolder->IsDirectory; @@ -934,19 +891,22 @@ namespace GlitchyEditor.EditWindows ImGui.EndMenu(); } - ImGui.Separator(); - - if (ImGui.MenuItem("Rename")) + if (itemType != .ParentDirectory) { - // Copy the path, just for the rare case that fileOrFolder gets deleted - _assetToRename.Set(fileOrFolder->Path); + ImGui.Separator(); - fileOrFolder->Name.CopyTo(_renameFileNameBuffer); - } + if (ImGui.MenuItem("Rename")) + { + // Copy the path, just for the rare case that fileOrFolder gets deleted + _assetToRename.Set(fileOrFolder->Path); - if (ImGui.MenuItem("Delete")) - { - wantsDelete = true; + fileOrFolder->Name.CopyTo(_renameFileNameBuffer); + } + + if (ImGui.MenuItem("Delete")) + { + wantsDelete = true; + } } ImGui.Separator();