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

108 lines
2.9 KiB
C#
Raw Normal View History

2024-08-17 19:53:16 +00:00
using Godot;
using KeepersCompound.LGS;
2024-08-17 19:53:16 +00:00
namespace KeepersCompound.TMV.UI;
2024-09-05 18:44:19 +00:00
public partial class ResourceSelector : Control
2024-08-17 19:53:16 +00:00
{
2024-09-05 18:44:19 +00:00
public event ResourceSelectedEventHandler ResourceSelected;
public delegate void ResourceSelectedEventHandler(string campaign, string resource);
[Export]
public ResourceType ResType;
2024-08-17 19:53:16 +00:00
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
2024-09-05 18:44:19 +00:00
private ItemList _Resources;
2024-08-17 19:53:16 +00:00
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");
2024-09-05 18:44:19 +00:00
_Resources = GetNode<ItemList>("%Resources");
2024-08-17 19:53:16 +00:00
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
2024-09-05 18:44:19 +00:00
_Campaigns.ItemSelected += BuildResourceList;
_Resources.ItemSelected += (long _) => _LoadButton.Disabled = false;
_LoadButton.Pressed += EmitResourceSelected;
2024-08-17 19:53:16 +00:00
_CancelButton.Pressed += () => Visible = false;
}
public override void _UnhandledInput(InputEvent @event)
2024-08-17 19:53:16 +00:00
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Escape)
{
Visible = !Visible;
}
}
}
private void SetInstallPath(string path)
2024-08-17 19:53:16 +00:00
{
_FolderPath.Text = path;
2024-09-19 20:16:07 +00:00
Context.Instance.PathManager.Init(path);
BuildCampaignList();
}
2024-08-17 19:53:16 +00:00
private void BuildCampaignList()
{
_Campaigns.Clear();
2024-09-05 18:44:19 +00:00
_Resources.Clear();
2024-08-17 19:53:16 +00:00
_LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager;
2024-09-05 18:44:19 +00:00
_Campaigns.AddItem("Original");
foreach (var campaign in pathManager.GetCampaignNames())
2024-08-17 19:53:16 +00:00
{
_Campaigns.AddItem(campaign);
2024-08-17 19:53:16 +00:00
}
}
2024-09-05 18:44:19 +00:00
private void BuildResourceList(long idx)
2024-08-17 19:53:16 +00:00
{
2024-09-05 18:44:19 +00:00
_Resources.Clear();
_LoadButton.Disabled = true;
var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx);
2024-09-19 20:16:07 +00:00
var pathManager = Context.Instance.PathManager;
var campaignResources = pathManager.GetCampaign(campaignName);
var resourceNames = campaignResources.GetResourceNames(ResType);
foreach (var resource in resourceNames)
2024-08-17 19:53:16 +00:00
{
2024-09-05 18:44:19 +00:00
_Resources.AddItem(resource);
2024-08-17 19:53:16 +00:00
}
}
2024-09-05 18:44:19 +00:00
private void EmitResourceSelected()
2024-08-17 19:53:16 +00:00
{
var campaignIdxs = _Campaigns.GetSelectedItems();
2024-09-05 18:44:19 +00:00
var resourceIdxs = _Resources.GetSelectedItems();
if (campaignIdxs.IsEmpty() || resourceIdxs.IsEmpty())
2024-08-17 19:53:16 +00:00
{
return;
}
var campaignIdx = campaignIdxs[0];
2024-09-05 18:44:19 +00:00
var resourceIdx = resourceIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
2024-09-05 18:44:19 +00:00
var resourceName = _Resources.GetItemText(resourceIdx);
ResourceSelected(campaignName, resourceName);
2024-08-17 19:53:16 +00:00
Visible = false;
}
}