2024-08-03 09:24:45 +00:00
|
|
|
using System.IO;
|
2024-08-13 17:24:00 +00:00
|
|
|
using System.Linq;
|
2024-08-03 09:24:45 +00:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
namespace KeepersCompound.TMV.UI;
|
|
|
|
|
|
|
|
public partial class MissionSelector : Control
|
|
|
|
{
|
|
|
|
[Signal]
|
2024-08-13 18:11:03 +00:00
|
|
|
public delegate void LoadMissionEventHandler(string rootPath, string missionPath);
|
2024-08-03 09:24:45 +00:00
|
|
|
|
2024-08-13 17:24:00 +00:00
|
|
|
private InstallPaths _installPaths;
|
|
|
|
|
2024-08-03 09:24:45 +00:00
|
|
|
private FileDialog _FolderSelect;
|
|
|
|
private LineEdit _FolderPath;
|
|
|
|
private Button _BrowseButton;
|
2024-08-13 17:24:00 +00:00
|
|
|
private ItemList _Campaigns;
|
2024-08-03 09:24:45 +00:00
|
|
|
private ItemList _Missions;
|
|
|
|
private Button _LoadButton;
|
|
|
|
private Button _CancelButton;
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2024-08-13 17:24:00 +00:00
|
|
|
// 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");
|
2024-08-13 17:24:00 +00:00
|
|
|
_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-13 17:24:00 +00:00
|
|
|
_FolderSelect.DirSelected += (string dir) => { _FolderPath.Text = dir; BuildCampaignList(dir); };
|
|
|
|
_FolderPath.TextSubmitted += BuildCampaignList;
|
2024-08-03 09:24:45 +00:00
|
|
|
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
|
2024-08-13 17:24:00 +00:00
|
|
|
_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-13 17:24:00 +00:00
|
|
|
private void BuildCampaignList(string path)
|
|
|
|
{
|
|
|
|
_installPaths = new InstallPaths(path);
|
|
|
|
|
|
|
|
_Campaigns.Clear();
|
|
|
|
_Missions.Clear();
|
|
|
|
_LoadButton.Disabled = true;
|
|
|
|
|
|
|
|
_Campaigns.AddItem("Original Missions");
|
|
|
|
foreach (var c in Directory.GetDirectories(_installPaths.fmsPath))
|
|
|
|
{
|
|
|
|
_Campaigns.AddItem(c.TrimPrefix(_installPaths.fmsPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void BuildMissionList(long idx)
|
|
|
|
{
|
|
|
|
_Missions.Clear();
|
|
|
|
_LoadButton.Disabled = true;
|
|
|
|
|
|
|
|
var campaignPath = "";
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
|
|
|
campaignPath = _installPaths.omsPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
campaignPath = _installPaths.fmsPath + _Campaigns.GetItemText((int)idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
string[] extensions = { "mis", "cow" };
|
|
|
|
campaignPath = idx == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText((int)idx);
|
|
|
|
foreach (var f in Directory.GetFiles(campaignPath))
|
|
|
|
{
|
|
|
|
if (extensions.Contains(f.GetExtension().ToLower()))
|
|
|
|
{
|
|
|
|
_Missions.AddItem(f.TrimPrefix(campaignPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:24:45 +00:00
|
|
|
private void EmitLoadMission()
|
|
|
|
{
|
2024-08-13 17:24:00 +00:00
|
|
|
var selectedCampaign = _Campaigns.GetSelectedItems()[0];
|
2024-08-03 09:24:45 +00:00
|
|
|
var selected = _Missions.GetSelectedItems();
|
|
|
|
if (selected.IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-13 17:24:00 +00:00
|
|
|
var campaignPath = selectedCampaign == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText(selectedCampaign);
|
|
|
|
var path = campaignPath + _Missions.GetItemText(selected[0]);
|
2024-08-13 18:11:03 +00:00
|
|
|
EmitSignal(SignalName.LoadMission, _installPaths.rootPath, path);
|
2024-08-03 09:24:45 +00:00
|
|
|
|
|
|
|
Visible = false;
|
|
|
|
}
|
|
|
|
}
|