diff --git a/project/code/TMV/UI/TextureBrowser.cs b/project/code/TMV/UI/TextureBrowser.cs index bf655cb..a0fd3c1 100644 --- a/project/code/TMV/UI/TextureBrowser.cs +++ b/project/code/TMV/UI/TextureBrowser.cs @@ -24,6 +24,8 @@ public partial class TextureBrowser : Node private TextureRect _previewTexture; private LineEdit _texturePath; + private string _searchFilterPrefix = ""; + public override void _Ready() { // !HACK TEMP @@ -41,6 +43,7 @@ public partial class TextureBrowser : Node _searchBar.TextChanged += ApplySearchFilter; _sortMenu.GetPopup().IdPressed += ApplySortMode; + _folderTree.ItemSelected += SetActiveFolder; BuildFolderTree(); BuildTextureList(); // TODO: This should be triggered on folder change @@ -95,8 +98,10 @@ public partial class TextureBrowser : Node textureRect.StretchMode = TextureRect.StretchModeEnum.KeepAspectCentered; textureRect.SetAnchorsPreset(Control.LayoutPreset.FullRect); + // We use meta here rather than just Name because Name replaces / with _ so + // we can't reliably construct it (the path can have natural _) var slot = new Panel(); - slot.Name = name; + slot.SetMeta("TexPath", name); slot.CustomMinimumSize = new Vector2(128, 128); slot.AddChild(textureRect); slot.GuiInput += (input) => @@ -121,11 +126,12 @@ public partial class TextureBrowser : Node private void ApplySearchFilter(string filter) { - var regexString = "^.*" + Regex.Escape(filter).Replace("\\*", ".*") + ".*$"; + var regex = $"^{_searchFilterPrefix}.*{Regex.Escape(filter).Replace("\\*", ".*")}.*$"; foreach (var node in _textureList.GetChildren()) { var panel = (Panel)node; - panel.Visible = Regex.IsMatch(panel.Name.ToString(), regexString); + var name = panel.GetMeta("TexPath").ToString(); + panel.Visible = Regex.IsMatch(name, regex); } } @@ -139,4 +145,25 @@ public partial class TextureBrowser : Node // TODO: Actualy sort } + + private void SetActiveFolder() + { + var selected = _folderTree.GetSelected(); + var selectedFolder = selected.GetText(0) + "/"; + var parent = selected.GetParent(); + while (parent != null) + { + var text = parent.GetText(0); + if (text != "") + { + selectedFolder = text + "/" + selectedFolder; + } + parent = parent.GetParent(); + } + + // This prefix is used in a regex, so we escape it here. The additional + // replace is because C# Regex doesn't escape / + _searchFilterPrefix = Regex.Escape(selectedFolder).Replace("/", "\\/"); + ApplySearchFilter(_searchBar.Text); + } }