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

75 lines
1.9 KiB
C#
Raw Normal View History

2024-08-03 09:24:45 +00:00
using System.IO;
using Godot;
namespace KeepersCompound.TMV.UI;
public partial class MissionSelector : Control
{
[Signal]
public delegate void LoadMissionEventHandler(string path);
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Missions;
private Button _LoadButton;
private Button _CancelButton;
public override void _Ready()
{
_FolderSelect = GetNode<FileDialog>("%FolderSelect");
_FolderPath = GetNode<LineEdit>("%FolderPath");
_BrowseButton = GetNode<Button>("%BrowseButton");
_Missions = GetNode<ItemList>("%Missions");
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += (string dir) => { _FolderPath.Text = dir; BuildMissionList(dir); };
_FolderPath.TextSubmitted += BuildMissionList;
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
_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 BuildMissionList(string path)
{
_Missions.Clear();
_LoadButton.Disabled = true;
// TODO: Use install config to select paths better?
var mis = Directory.GetFiles(path, "*.mis", SearchOption.AllDirectories);
foreach (var m in mis)
{
_Missions.AddItem(m.TrimPrefix(path));
}
}
private void EmitLoadMission()
{
var selected = _Missions.GetSelectedItems();
if (selected.IsEmpty())
{
return;
}
var item = _Missions.GetItemText(selected[0]);
var basePath = _FolderPath.Text;
var path = basePath + item;
EmitSignal(SignalName.LoadMission, path);
Visible = false;
}
}