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

127 lines
3.4 KiB
C#

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;
public override void _Ready()
{
// !HACK TEMP
Context.Instance.PathManager.Init("/stuff/Games/thief/drive_c/GOG Games/TG ND 1.27 (MAPPING)/");
Context.Instance.CampaignName = "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");
_searchBar.TextChanged += ApplySearchFilter;
_sortMenu.GetPopup().IdPressed += ApplySortMode;
BuildFolderTree();
BuildTextureList(); // TODO: This should be triggered on folder change
}
private void BuildFolderTree()
{
_folderTree.Clear();
_folderTree.SetColumnTitle(0, "Name");
_folderTree.SetColumnTitle(1, "Count");
// TODO: We need some way to get family names
// Maybe PathManager and CampaignResource should be expanded so that we
// can get the resource itself and query things
var pathManager = Context.Instance.PathManager;
}
private void BuildTextureList()
{
foreach (var child in _textureList.GetChildren())
{
child.QueueFree();
}
var pathManager = Context.Instance.PathManager;
var campaign = Context.Instance.CampaignName;
var resType = LGS.ResourceType.Texture;
var textureNames = pathManager.GetResourceNames(resType, campaign);
foreach (var name in textureNames)
{
var (_, path) = pathManager.GetResourcePath(resType, campaign, 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);
var slot = new Panel();
slot.Name = 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);
}
};
_textureList.AddChild(slot);
}
}
private void SetPreviewTexture(string name, Texture2D texture)
{
_previewTexture.Texture = texture;
_texturePath.Text = name;
}
private void ApplySearchFilter(string filter)
{
var regexString = "^.*" + Regex.Escape(filter).Replace("\\*", ".*") + ".*$";
foreach (var node in _textureList.GetChildren())
{
var panel = (Panel)node;
panel.Visible = Regex.IsMatch(panel.Name.ToString(), regexString);
}
}
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
}
}