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

111 lines
2.8 KiB
C#
Raw Normal View History

2024-08-17 19:53:16 +00:00
using System.IO;
using System.IO.Compression;
using System.Linq;
using Godot;
namespace KeepersCompound.TMV.UI;
public partial class ModelSelector : Control
{
public event ModelSelectedEventHandler ModelSelected;
public delegate void ModelSelectedEventHandler(string campaign, string mission);
2024-08-17 19:53:16 +00:00
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
2024-08-17 19:53:16 +00:00
private ItemList _Models;
private Button _LoadButton;
private Button _CancelButton;
private string _extractedObjectsPath = ProjectSettings.GlobalizePath($"user://objects/tmp");
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-08-17 19:53:16 +00:00
_Models = GetNode<ItemList>("%Models");
_LoadButton = GetNode<Button>("%LoadButton");
_CancelButton = GetNode<Button>("%CancelButton");
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
_FolderSelect.DirSelected += SetInstallPath;
_FolderPath.TextSubmitted += SetInstallPath;
_Campaigns.ItemSelected += BuildModelList;
2024-08-17 19:53:16 +00:00
_Models.ItemSelected += (long _) => _LoadButton.Disabled = false;
_LoadButton.Pressed += EmitLoadModel;
_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)
2024-08-17 19:53:16 +00:00
{
_FolderPath.Text = path;
if (Context.Instance.PathManager.Init(path))
{
BuildCampaignList();
}
}
2024-08-17 19:53:16 +00:00
private void BuildCampaignList()
{
_Campaigns.Clear();
2024-08-17 19:53:16 +00:00
_Models.Clear();
_LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager;
_Campaigns.AddItem("Original Missions");
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
}
}
private void BuildModelList(long idx)
2024-08-17 19:53:16 +00:00
{
_Models.Clear();
_LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager;
var campaignName = idx == 0 ? null : _Campaigns.GetItemText((int)idx);
var modelNames = pathManager.GetModelNames(campaignName);
foreach (var model in modelNames)
2024-08-17 19:53:16 +00:00
{
_Models.AddItem(model);
2024-08-17 19:53:16 +00:00
}
}
private void EmitLoadModel()
{
var campaignIdxs = _Campaigns.GetSelectedItems();
var modelIdxs = _Models.GetSelectedItems();
if (campaignIdxs.IsEmpty() || modelIdxs.IsEmpty())
2024-08-17 19:53:16 +00:00
{
return;
}
var campaignIdx = campaignIdxs[0];
var modelIdx = modelIdxs[0];
var campaignName = campaignIdx == 0 ? null : _Campaigns.GetItemText(campaignIdx);
var modelName = _Models.GetItemText(modelIdx);
ModelSelected(campaignName, modelName);
2024-08-17 19:53:16 +00:00
Visible = false;
}
}