AceField/Scripts/UI/LaunchScreen.cs

75 lines
1.6 KiB
C#
Raw Permalink 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-08 07:20:09 +00:00
public partial class LaunchScreen : 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-09 06:14:51 +00:00
2024-08-05 08:30:50 +00:00
[Export] public LineEdit ServerPortInput;
[Export] public LineEdit ServerAddrInput;
[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;
StartAsServerButton.Pressed += () =>
{
if (!DoValidation())
{
OS.Alert("Invalid multiplayer configuration.");
return;
}
var port = ServerPortInput.Text;
2024-08-09 06:14:51 +00:00
var result = Launcher.StartAsServer(int.Parse(port));
2024-08-08 07:20:09 +00:00
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-09 06:14:51 +00:00
var result = Launcher.StartAsClient(addr, int.Parse(port));
2024-08-05 08:30:50 +00:00
if (result)
Hide();
};
}
}