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

105 lines
2.8 KiB
C#
Raw Normal View History

2024-08-03 09:24:45 +00:00
using Godot;
using KeepersCompound.LGS;
2024-08-03 09:24:45 +00:00
namespace KeepersCompound.TMV.UI;
public partial class MissionSelector : Control
{
2024-08-25 09:01:43 +00:00
public event MissionSelectedEventHandler MissionSelected;
public delegate void MissionSelectedEventHandler(string campaign, string mission);
2024-08-03 09:24:45 +00:00
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
2024-08-03 09:24:45 +00:00
private ItemList _Missions;
private Button _LoadButton;
private Button _CancelButton;
public override void _Ready()
{
// TODO: Load initial folderpath from config and prefil everything
2024-08-03 09:24:45 +00:00
_FolderSelect = GetNode<FileDialog>("%FolderSelect");
_FolderPath = GetNode<LineEdit>("%FolderPath");
_BrowseButton = GetNode<Button>("%BrowseButton");
_Campaigns = GetNode<ItemList>("%Campaigns");
2024-08-03 09:24:45 +00:00
_Missions = GetNode<ItemList>("%Missions");
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
2024-08-25 08:15:42 +00:00
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
2024-08-03 09:24:45 +00:00
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
_Campaigns.ItemSelected += BuildMissionList;
2024-08-03 09:24:45 +00:00
_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;
}
}
}
2024-08-25 08:15:42 +00:00
private void SetInstallPath(string path)
{
2024-08-25 08:15:42 +00:00
_FolderPath.Text = path;
2024-09-05 16:39:54 +00:00
if (Context.Instance.PathManager.Init(path))
2024-08-25 08:15:42 +00:00
{
BuildCampaignList();
}
}
2024-08-25 08:15:42 +00:00
private void BuildCampaignList()
{
_Campaigns.Clear();
_Missions.Clear();
_LoadButton.Disabled = true;
2024-09-05 16:39:54 +00:00
var pathManager = Context.Instance.PathManager;
_Campaigns.AddItem("Original Missions");
2024-08-25 08:15:42 +00:00
foreach (var campaign in pathManager.GetCampaignNames())
{
2024-08-25 08:15:42 +00:00
_Campaigns.AddItem(campaign);
}
}
private void BuildMissionList(long idx)
{
_Missions.Clear();
_LoadButton.Disabled = true;
2024-09-05 16:39:54 +00:00
var pathManager = Context.Instance.PathManager;
var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx);
var missionNames = pathManager.GetResourceNames(ResourceType.Mission, campaignName);
2024-08-25 08:15:42 +00:00
foreach (var mission in missionNames)
{
2024-08-25 08:15:42 +00:00
_Missions.AddItem(mission);
}
}
2024-08-03 09:24:45 +00:00
private void EmitLoadMission()
{
2024-08-25 08:15:42 +00:00
var campaignIdxs = _Campaigns.GetSelectedItems();
var missionIdxs = _Missions.GetSelectedItems();
if (campaignIdxs.IsEmpty() || missionIdxs.IsEmpty())
2024-08-03 09:24:45 +00:00
{
return;
}
2024-08-25 08:15:42 +00:00
var campaignIdx = campaignIdxs[0];
var missionIdx = missionIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
var missionName = _Missions.GetItemText(missionIdx);
2024-08-25 09:01:43 +00:00
MissionSelected(campaignName, missionName);
2024-08-03 09:24:45 +00:00
Visible = false;
}
}