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

108 lines
2.9 KiB
C#

using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV.UI;
public partial class ResourceSelector : Control
{
public event ResourceSelectedEventHandler ResourceSelected;
public delegate void ResourceSelectedEventHandler(string campaign, string resource);
[Export]
public ResourceType ResType;
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
private ItemList _Resources;
private Button _LoadButton;
private Button _CancelButton;
public override void _Ready()
{
// TODO: Load initial folderpath from config and prefil everything
_FolderSelect = GetNode<FileDialog>("%FolderSelect");
_FolderPath = GetNode<LineEdit>("%FolderPath");
_BrowseButton = GetNode<Button>("%BrowseButton");
_Campaigns = GetNode<ItemList>("%Campaigns");
_Resources = GetNode<ItemList>("%Resources");
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
_Campaigns.ItemSelected += BuildResourceList;
_Resources.ItemSelected += (long _) => _LoadButton.Disabled = false;
_LoadButton.Pressed += EmitResourceSelected;
_CancelButton.Pressed += () => Visible = false;
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Escape)
{
Visible = !Visible;
}
}
}
private void SetInstallPath(string path)
{
_FolderPath.Text = path;
Context.Instance.PathManager.Init(path);
BuildCampaignList();
}
private void BuildCampaignList()
{
_Campaigns.Clear();
_Resources.Clear();
_LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager;
_Campaigns.AddItem("Original");
foreach (var campaign in pathManager.GetCampaignNames())
{
_Campaigns.AddItem(campaign);
}
}
private void BuildResourceList(long idx)
{
_Resources.Clear();
_LoadButton.Disabled = true;
var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx);
var pathManager = Context.Instance.PathManager;
var campaignResources = pathManager.GetCampaign(campaignName);
var resourceNames = campaignResources.GetResourceNames(ResType);
foreach (var resource in resourceNames)
{
_Resources.AddItem(resource);
}
}
private void EmitResourceSelected()
{
var campaignIdxs = _Campaigns.GetSelectedItems();
var resourceIdxs = _Resources.GetSelectedItems();
if (campaignIdxs.IsEmpty() || resourceIdxs.IsEmpty())
{
return;
}
var campaignIdx = campaignIdxs[0];
var resourceIdx = resourceIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
var resourceName = _Resources.GetItemText(resourceIdx);
ResourceSelected(campaignName, resourceName);
Visible = false;
}
}