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

111 lines
2.8 KiB
C#

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);
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
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");
_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;
_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)
{
_FolderPath.Text = path;
if (Context.Instance.PathManager.Init(path))
{
BuildCampaignList();
}
}
private void BuildCampaignList()
{
_Campaigns.Clear();
_Models.Clear();
_LoadButton.Disabled = true;
var pathManager = Context.Instance.PathManager;
_Campaigns.AddItem("Original Missions");
foreach (var campaign in pathManager.GetCampaignNames())
{
_Campaigns.AddItem(campaign);
}
}
private void BuildModelList(long idx)
{
_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)
{
_Models.AddItem(model);
}
}
private void EmitLoadModel()
{
var campaignIdxs = _Campaigns.GetSelectedItems();
var modelIdxs = _Models.GetSelectedItems();
if (campaignIdxs.IsEmpty() || modelIdxs.IsEmpty())
{
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);
Visible = false;
}
}