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