AceField/Scripts/UI/StartScreen.cs

81 lines
1.7 KiB
C#
Raw Normal View History

2024-08-05 08:30:50 +00:00
using Godot;
2024-08-07 15:01:57 +00:00
namespace AceField.Scripts.UI;
2024-08-05 08:30:50 +00:00
2024-08-05 15:54:22 +00:00
public partial class StartScreen : Control
2024-08-05 08:30:50 +00:00
{
[Export] public int DefaultServerPort = 4343;
[Export] public string DefaultServerAddr = "127.0.0.1";
2024-08-05 15:54:22 +00:00
[Export] public Launcher Launcher;
2024-08-05 08:30:50 +00:00
[Export] public LineEdit ServerPortInput;
[Export] public LineEdit ServerAddrInput;
2024-08-05 15:54:22 +00:00
[Export] public Button StartAsSingleButton;
2024-08-05 08:30:50 +00:00
[Export] public Button StartAsServerButton;
[Export] public Button StartAsClientButton;
private void ApplySize()
{
var screenSize = GetViewportRect().Size;
Position = new Vector2(0, 0);
Size = screenSize;
GetNode<CenterContainer>("CenterContainer").Size = screenSize;
}
private bool DoValidation()
{
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
if (string.IsNullOrEmpty(addr)) return false;
if (string.IsNullOrEmpty(port) || !int.TryParse(port, out _)) return false;
return true;
}
public override void _Ready()
{
ApplySize();
ServerPortInput.Text = DefaultServerPort.ToString();
ServerAddrInput.Text = DefaultServerAddr;
2024-08-05 15:54:22 +00:00
StartAsSingleButton.Pressed += () =>
{
Launcher.StartAsSingle();
Hide();
};
2024-08-05 08:30:50 +00:00
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var port = ServerPortInput.Text;
2024-08-05 15:54:22 +00:00
var result = Launcher.StartAsServer(int.Parse(port));
2024-08-05 08:30:50 +00:00
if (result)
Hide();
};
2024-08-05 11:08:28 +00:00
StartAsClientButton.Pressed += () =>
2024-08-05 08:30:50 +00:00
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
2024-08-05 15:54:22 +00:00
var result = Launcher.StartAsClient(addr, int.Parse(port));
2024-08-05 08:30:50 +00:00
if (result)
Hide();
};
}
}