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

108 lines
2.7 KiB
C#

using Godot;
using KeepersCompound.LGS;
namespace KeepersCompound.TMV.UI;
public partial class MissionSelector : Control
{
[Signal]
public delegate void LoadMissionEventHandler(string campaign, string mission);
public ResourcePathManager pathManager;
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
private ItemList _Missions;
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");
_Missions = GetNode<ItemList>("%Missions");
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
_Campaigns.ItemSelected += BuildMissionList;
_LoadButton.Pressed += EmitLoadMission;
_CancelButton.Pressed += () => Visible = false;
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Escape)
{
Visible = !Visible;
}
}
}
private void SetInstallPath(string path)
{
_FolderPath.Text = path;
if (pathManager.Init(path))
{
BuildCampaignList();
}
}
private void BuildCampaignList()
{
_Campaigns.Clear();
_Missions.Clear();
_LoadButton.Disabled = true;
_Campaigns.AddItem("Original Missions");
foreach (var campaign in pathManager.GetCampaignNames())
{
_Campaigns.AddItem(campaign);
}
}
private void BuildMissionList(long idx)
{
_Missions.Clear();
_LoadButton.Disabled = true;
var campaignName = _Campaigns.GetItemText((int)idx);
var missionNames = (idx == 0) ?
pathManager.GetMissionNames() :
pathManager.GetMissionNames(campaignName);
foreach (var mission in missionNames)
{
_Missions.AddItem(mission);
}
}
private void EmitLoadMission()
{
var campaignIdxs = _Campaigns.GetSelectedItems();
var missionIdxs = _Missions.GetSelectedItems();
if (campaignIdxs.IsEmpty() || missionIdxs.IsEmpty())
{
return;
}
var campaignIdx = campaignIdxs[0];
var missionIdx = missionIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
var missionName = _Missions.GetItemText(missionIdx);
EmitSignal(SignalName.LoadMission, campaignName, missionName);
Visible = false;
}
}