Search in Content Browser (and added ForEach in TreeNode)

This commit is contained in:
Simon Lübeß
2023-06-28 19:27:52 +02:00
parent bb520b43ec
commit 334c2c0366
2 changed files with 163 additions and 47 deletions
@@ -48,18 +48,38 @@ namespace GlitchyEditor.EditWindows
return;
}
ImGui.Columns(2);
ImGui.PushStyleVar(.CellPadding, .(0, 0));
ImGui.BeginChild("Sidebar");
bool alt_pressed = ImGui.GetIO().KeyAlt;
if (ImGui.BeginTable("table", 2, .BordersInnerV | .Resizable | .Reorderable | .NoPadOuterX))
{
if (alt_pressed)
{
// Header anzeigen, damit sie neu angeordnet werden können
ImGui.TableSetupColumn("Folder Hierarchy");
ImGui.TableSetupColumn("Files");
ImGui.TableHeadersRow();
}
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(0);
if (ImGui.BeginChild("Sidebar"))
{
DrawDirectorySideBar();
ImGui.EndChild();
}
ImGui.NextColumn();
ImGui.TableNextColumn();
ImGui.BeginChild("Files");
DrawSearchBar();
ImGui.Separator();
if (ImGui.BeginChild("Files"))
{
DrawCurrentDirectory();
// Context menu when clicking on the background.
@@ -70,12 +90,28 @@ namespace GlitchyEditor.EditWindows
}
ImGui.EndChild();
}
ImGui.Columns(1);
ImGui.EndTable();
}
ImGui.PopStyleVar(1);
ImGui.End();
}
char8[256] _filesFilter = .();
bool _searchEverywhere;
private void DrawSearchBar()
{
ImGui.TextUnformatted("Search:");
ImGui.SameLine();
ImGui.InputText("##search", &_filesFilter, _filesFilter.Count);
ImGui.SameLine();
ImGui.Checkbox("Search everywhere", &_searchEverywhere);
}
/// Renders the context menu that is shown when the user right clicks on the background of the file browser.
private void ShowCurrentFolderContextMenu()
{
@@ -139,13 +175,30 @@ namespace GlitchyEditor.EditWindows
/// Renders the contents of _currentDirectory.
private void DrawCurrentDirectory()
{
if (_currentDirectory.IsEmpty)
return;
List<TreeNode<AssetNode>> files = null;
ImGui.Style* style = ImGui.GetStyle();
float window_visible_x2 = ImGui.GetWindowPos().x + ImGui.GetWindowContentRegionMax().x;
StringView searchFilter = StringView(&_filesFilter);
if (_searchEverywhere && searchFilter.Length > 0)
{
files = scope:: List<TreeNode<AssetNode>>();
_manager.AssetHierarchy.[Friend]_assetHierarchy.ForEach(scope (node) =>
{
if (node->Name.Contains(searchFilter, true))
{
files.Add(node);
}
});
}
else
{
if (_currentDirectory.IsEmpty)
return;
// Get the node of the current directory.
var currentDirectoryNode = _manager.AssetHierarchy.GetNodeFromPath(_currentDirectory);
@@ -156,6 +209,9 @@ namespace GlitchyEditor.EditWindows
return;
}
files = currentDirectoryNode->Children;
// show back button (".."-File)
if (currentDirectoryNode->Parent != null)
{
ImGui.PushID("Back");
@@ -173,9 +229,13 @@ namespace GlitchyEditor.EditWindows
ImGui.PopID();
}
}
for (var entry in currentDirectoryNode->Children)
for (var entry in files)
{
if (!entry->Name.Contains(searchFilter, true))
continue;
ImGui.PushID(entry->Name);
DrawDirectoryItem(entry);
@@ -186,7 +246,7 @@ namespace GlitchyEditor.EditWindows
float expectedButtonRight = currentButtonRight + style.ItemSpacing.x + DirectoryItemSize.X;
// If we aren't the last entry and the next button won't fit on the same line we start a new line.
if (entry != currentDirectoryNode->Children.Back && expectedButtonRight < window_visible_x2)
if (entry != files.Back && expectedButtonRight < window_visible_x2)
ImGui.SameLine();
ImGui.PopID();
+56
View File
@@ -56,6 +56,62 @@ namespace GlitchyEngine.Collections
{
return ref node.Value;
}
public delegate void ElementFunc(TreeNode<T> currentNode);
public enum IterationMode
{
BreadthFirst,
DepthFirst
}
public void ForEach(ElementFunc func, IterationMode iterationMode = .DepthFirst)
{
if (iterationMode == .BreadthFirst)
{
ForEach_BreadthFirst(func);
}
else if (iterationMode == .DepthFirst)
{
ForEach_DepthFirst(func);
}
}
public void ForEach_BreadthFirst(ElementFunc func)
{
Queue<TreeNode<T>> nodes = scope .();
nodes.Add(this);
while (!nodes.IsEmpty)
{
TreeNode<T> node = nodes.PopFront();
func(node);
for (var child in node.Children)
{
nodes.Add(child);
}
}
}
public void ForEach_DepthFirst(ElementFunc func)
{
List<TreeNode<T>> nodes = scope .();
nodes.Add(this);
while (!nodes.IsEmpty)
{
TreeNode<T> node = nodes.PopBack();
func(node);
for (var child in node.Children)
{
nodes.Add(child);
}
}
}
}
static