thief-mission-viewer/project/code/TMV/UI/TextureBrowser.cs

206 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Godot;
namespace KeepersCompound.TMV.UI;
public partial class TextureBrowser : Node
{
enum SortMode
{
Name,
Family,
Index,
Count,
}
private Tree _folderTree;
private LineEdit _searchBar;
private MenuButton _sortMenu;
private Button _openFolderButton;
private MenuButton _browserMenu;
private HFlowContainer _textureList;
private TextureRect _previewTexture;
private LineEdit _texturePath;
private Button _texturePathCopyButton;
private OptionButton _filterOptions;
private LineEdit _fileType;
private LineEdit _resolutionBox;
private LineEdit _fileSizeBox;
private string _searchFilterPrefix = "";
public override void _Ready()
{
// !HACK TEMP
Context.Instance.PathManager.Init("/stuff/Games/thief/drive_c/GOG Games/TG ND 1.27 (MAPPING)/");
Context.Instance.SetCampaign("TheBlackParade_1_0");
_folderTree = GetNode<Tree>("%FolderTree");
_searchBar = GetNode<LineEdit>("%SearchBar");
_sortMenu = GetNode<MenuButton>("%SortMenu");
_openFolderButton = GetNode<Button>("%OpenTextureFolderButton");
_browserMenu = GetNode<MenuButton>("%BrowserOverflowMenu");
_textureList = GetNode<HFlowContainer>("%TextureList");
_previewTexture = GetNode<TextureRect>("%PreviewTexture");
_texturePath = GetNode<LineEdit>("%PathBox");
_texturePathCopyButton = GetNode<Button>("%PathCopyButton");
_filterOptions = GetNode<OptionButton>("%FilterOptions");
_fileType = GetNode<LineEdit>("%FileTypeBox");
_resolutionBox = GetNode<LineEdit>("%ResolutionBox");
_fileSizeBox = GetNode<LineEdit>("%FileSizeBox");
_searchBar.TextChanged += ApplySearchFilter;
_sortMenu.GetPopup().IdPressed += ApplySortMode;
_folderTree.ItemSelected += SetActiveFolder;
_filterOptions.ItemSelected += SetPreviewTextureFilter;
_texturePathCopyButton.Pressed += CopyTexturePath;
BuildFolderTree();
BuildTextureList(); // TODO: This should be triggered on folder change
}
private void CopyTexturePath()
{
DisplayServer.ClipboardSet(_texturePath.Text);
}
private void BuildFolderTree()
{
_folderTree.Clear();
var campaignResources = Context.Instance.CampaignResources;
var textureNames = campaignResources.GetResourceNames(LGS.ResourceType.Texture);
var treeItems = new Dictionary<string, TreeItem>();
treeItems.Add("", _folderTree.CreateItem());
foreach (var name in textureNames)
{
var tokens = name.Split('/');
var fams = new string[tokens.Length + 1];
fams[0] = "";
for (var i = 1; i < tokens.Length; i++)
{
fams[i] = tokens[..i].Join("/");
if (!treeItems.ContainsKey(fams[i]))
{
var item = _folderTree.CreateItem(treeItems[fams[i - 1]]);
item.SetText(0, tokens[i - 1]);
treeItems.Add(fams[i], item);
}
}
}
}
private void BuildTextureList()
{
foreach (var child in _textureList.GetChildren())
{
child.QueueFree();
}
var campaignResources = Context.Instance.CampaignResources;
var resType = LGS.ResourceType.Texture;
var textureNames = campaignResources.GetResourceNames(resType);
foreach (var name in textureNames)
{
var path = campaignResources.GetResourcePath(resType, name);
var texture = TextureLoader.LoadTexture(path);
var textureRect = new TextureRect();
textureRect.Texture = texture;
textureRect.ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize;
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.SetMeta("TexPath", name);
slot.CustomMinimumSize = new Vector2(128, 128);
slot.AddChild(textureRect);
slot.GuiInput += (input) =>
{
if (input is InputEventMouseButton mouseEvent &&
mouseEvent.Pressed &&
mouseEvent.ButtonIndex == MouseButton.Left)
{
SetPreviewTexture(name, texture, path);
}
};
_textureList.AddChild(slot);
}
}
private void SetPreviewTexture(string name, Texture2D texture, string path)
{
_previewTexture.Texture = texture;
_texturePath.Text = name;
_fileType.Text = Path.GetExtension(path).ToUpper();
var resolution = texture.GetSize();
_resolutionBox.Text = $"{resolution.X}x{resolution.Y}";
var fileInfo = new FileInfo(path);
_fileSizeBox.Text = $"{(fileInfo.Length / 1000.0f).ToString("0.0")} kb";
}
private void ApplySearchFilter(string filter)
{
var regex = $"^{_searchFilterPrefix}.*{Regex.Escape(filter).Replace("\\*", ".*")}.*$";
foreach (var node in _textureList.GetChildren())
{
var panel = (Panel)node;
var name = panel.GetMeta("TexPath").ToString();
panel.Visible = Regex.IsMatch(name, regex);
}
}
private void ApplySortMode(long id)
{
var popup = _sortMenu.GetPopup();
for (var i = 0; i < popup.ItemCount; i++)
{
popup.SetItemChecked(i, popup.GetItemId(i) == id);
}
// 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);
}
private void SetPreviewTextureFilter(long idx)
{
var filterMode = idx switch
{
0 => CanvasItem.TextureFilterEnum.LinearWithMipmaps,
1 => CanvasItem.TextureFilterEnum.NearestWithMipmaps,
_ => throw new InvalidOperationException(),
};
_previewTexture.TextureFilter = filterMode;
}
}