Mission selector allows better browsing of campaigns

This commit is contained in:
Jarrod Doyle 2024-08-13 18:24:00 +01:00
parent 27dbc47419
commit e773635677
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
4 changed files with 117 additions and 8 deletions

View File

@ -0,0 +1,50 @@
using System.IO;
using Godot;
namespace KeepersCompound.TMV;
// TODO: Error handling lol
public class InstallPaths
{
public string rootPath;
public string famPath;
public string omsPath;
public string fmsPath;
public InstallPaths(string root)
{
var searchOptions = new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true
};
rootPath = root;
famPath = Directory.GetFiles(rootPath, "fam.crf", searchOptions)[0];
var installCfgPath = Directory.GetFiles(rootPath, "install.cfg", searchOptions)[0];
GD.Print($"Install.cfg: {installCfgPath}");
foreach (var line in File.ReadLines(installCfgPath))
{
if (line.StartsWith("load_path"))
{
var path = line.Split(" ")[1].Replace("\\", "/");
omsPath = Path.GetFullPath(rootPath + path);
break;
}
}
var camModPath = Directory.GetFiles(rootPath, "cam_mod.ini", searchOptions)[0];
fmsPath = rootPath + "/FMs";
foreach (var line in File.ReadLines(camModPath))
{
if (line.StartsWith("fm_path"))
{
var path = line.Split(" ")[1].Replace("\\", "/");
fmsPath = Path.GetFullPath(rootPath + path);
break;
}
}
GD.Print($"OMs Path: {omsPath}");
}
}

View File

@ -60,6 +60,7 @@ public partial class TextureLoader
public bool Load(int id, string path)
{
// TODO: Handle lowercase .pcx :)
var userTexturesPath = ProjectSettings.GlobalizePath($"user://textures{path}.PCX");
var loaded = false;

View File

@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using Godot;
namespace KeepersCompound.TMV.UI;
@ -8,26 +9,33 @@ public partial class MissionSelector : Control
[Signal]
public delegate void LoadMissionEventHandler(string path);
private InstallPaths _installPaths;
private FileDialog _FolderSelect;
private LineEdit _FolderPath;
private Button _BrowseButton;
private ItemList _Campaigns;
private ItemList _Missions;
private Button _LoadButton;
private Button _CancelButton;
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");
_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;
_FolderSelect.DirSelected += (string dir) => { _FolderPath.Text = dir; BuildCampaignList(dir); };
_FolderPath.TextSubmitted += BuildCampaignList;
_Missions.ItemSelected += (long _) => _LoadButton.Disabled = false;
_Campaigns.ItemSelected += BuildMissionList;
_LoadButton.Pressed += EmitLoadMission;
_CancelButton.Pressed += () => Visible = false;
}
@ -43,6 +51,21 @@ public partial class MissionSelector : Control
}
}
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(string path)
{
_Missions.Clear();
@ -56,17 +79,43 @@ public partial class MissionSelector : Control
}
}
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));
}
}
}
private void EmitLoadMission()
{
var selectedCampaign = _Campaigns.GetSelectedItems()[0];
var selected = _Missions.GetSelectedItems();
if (selected.IsEmpty())
{
return;
}
var item = _Missions.GetItemText(selected[0]);
var basePath = _FolderPath.Text;
var path = basePath + item;
var campaignPath = selectedCampaign == 0 ? _installPaths.omsPath : _installPaths.fmsPath + _Campaigns.GetItemText(selectedCampaign);
var path = campaignPath + _Missions.GetItemText(selected[0]);
EmitSignal(SignalName.LoadMission, path);
Visible = false;

View File

@ -6,7 +6,6 @@
font_size = 20
[node name="MissionSelector" type="Control"]
visible = false
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5
@ -28,7 +27,7 @@ access = 2
use_native_dialog = true
[node name="PanelContainer" type="PanelContainer" parent="."]
custom_minimum_size = Vector2(460, 0)
custom_minimum_size = Vector2(640, 0)
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
@ -75,10 +74,20 @@ unique_name_in_owner = true
layout_mode = 2
text = "Browse"
[node name="Missions" type="ItemList" parent="PanelContainer/MarginContainer/VBoxContainer"]
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="Campaigns" type="ItemList" parent="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 480)
layout_mode = 2
size_flags_horizontal = 3
[node name="Missions" type="ItemList" parent="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 480)
layout_mode = 2
size_flags_horizontal = 3
[node name="Buttons" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2