AceField/Scripts/UI/StartScreen.cs
2024-08-07 23:01:57 +08:00

81 lines
1.7 KiB
C#

using Godot;
namespace AceField.Scripts.UI;
public partial class StartScreen : Control
{
[Export] public int DefaultServerPort = 4343;
[Export] public string DefaultServerAddr = "127.0.0.1";
[Export] public Launcher Launcher;
[Export] public LineEdit ServerPortInput;
[Export] public LineEdit ServerAddrInput;
[Export] public Button StartAsSingleButton;
[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;
StartAsSingleButton.Pressed += () =>
{
Launcher.StartAsSingle();
Hide();
};
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var port = ServerPortInput.Text;
var result = Launcher.StartAsServer(int.Parse(port));
if (result)
Hide();
};
StartAsClientButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text;
var result = Launcher.StartAsClient(addr, int.Parse(port));
if (result)
Hide();
};
}
}