using System; using System.Globalization; using Godot; using RustToDust.Core; namespace RustToDust.Client; /// /// Industrial Imperial main menu overlay over the live Sion 3D orbit backdrop, /// plus the in-match [ESC] pause overlay. /// public sealed partial class MainMenu : CanvasLayer { public enum OverlayKind { Hidden, MainMenu, PauseOverlay } private Control _mainRoot = null!; private ImperialChromePanel _menuPanel = null!; private ImperialChromePanel _hostPanel = null!; private LineEdit _callsignEdit = null!; private OptionButton _soloSquadOption = null!; private Label _statusLabel = null!; private LineEdit _serverNameEdit = null!; private LineEdit _portEdit = null!; private OptionButton _hostMapOption = null!; private OptionButton _hostSquadOption = null!; private CheckBox _listPublicCheck = null!; private Label _directoryHintLabel = null!; private Label _hostErrorLabel = null!; private Control _pauseRoot = null!; private Label _pauseContextLabel = null!; private OverlayKind _overlayKind = OverlayKind.Hidden; private string _directoryUrl = string.Empty; public MainMenu() { Layer = 20; } public event Action? PlaySoloRequested; public event Action? ServerBrowserRequested; public event Action? HostLobbyRequested; public event Action? CallsignChanged; public event Action? QuitRequested; public event Action? ResumeRequested; public event Action? ReturnToMainMenuRequested; public bool IsHostPanelOpen => _hostPanel is not null && _hostPanel.Visible; public bool IsTextInputFocused => Visible && _overlayKind == OverlayKind.MainMenu && (_callsignEdit.HasFocus() || _serverNameEdit.HasFocus() || _portEdit.HasFocus()); public string Callsign => SanitizeCallsign(_callsignEdit?.Text); public override void _Ready() { BuildMainRoot(); BuildPauseRoot(); SetOverlayKind(OverlayKind.Hidden); } public void SetOverlayKind(OverlayKind kind) { _overlayKind = kind; Visible = kind != OverlayKind.Hidden; if (_mainRoot is not null) { _mainRoot.Visible = kind == OverlayKind.MainMenu; } if (_pauseRoot is not null) { _pauseRoot.Visible = kind == OverlayKind.PauseOverlay; } if (kind != OverlayKind.MainMenu) { ReleaseTextFocus(); } } public void SetHostPanelOpen(bool open) { if (_hostPanel is null) return; _hostPanel.Visible = open; if (open) { _hostErrorLabel.Text = string.Empty; _hostErrorLabel.Visible = false; } else { _serverNameEdit.ReleaseFocus(); _portEdit.ReleaseFocus(); } } public void SetDefaults(string callsign, string directoryUrl, ArenaMapKind defaultMap = ArenaMapKind.Sion, int defaultPort = 3076, byte defaultSquadSize = 3) { _directoryUrl = directoryUrl ?? string.Empty; if (_callsignEdit is not null) { _callsignEdit.Text = SanitizeCallsign(callsign); } int sqIdx = Math.Clamp((int)defaultSquadSize, 1, 4) - 1; _soloSquadOption?.Select(sqIdx); _hostSquadOption?.Select(sqIdx); if (_portEdit is not null) { _portEdit.Text = defaultPort.ToString(CultureInfo.InvariantCulture); } if (_hostMapOption is not null) { _hostMapOption.Select(defaultMap == ArenaMapKind.Desert ? 1 : 0); } if (_directoryHintLabel is not null) { _directoryHintLabel.Text = string.IsNullOrWhiteSpace(_directoryUrl) ? "Public directory disabled (empty URL)" : $"Directory: {_directoryUrl}"; } } public byte SelectedSoloSquadSize => _soloSquadOption is not null ? (byte)Math.Clamp(_soloSquadOption.Selected + 1, 1, 4) : (byte)3; public byte SelectedHostSquadSize => _hostSquadOption is not null ? (byte)Math.Clamp(_hostSquadOption.Selected + 1, 1, 4) : (byte)3; public void SetStatus(string? message, bool isError = false) { if (_statusLabel is null) return; if (string.IsNullOrWhiteSpace(message)) { _statusLabel.Visible = false; _statusLabel.Text = string.Empty; return; } _statusLabel.Text = message.Trim(); _statusLabel.AddThemeColorOverride("font_color", isError ? ImperialChrome.RedCol : ImperialChrome.CyanCol); _statusLabel.Visible = true; } public void SetHostError(string? message) { if (_hostErrorLabel is null) return; if (string.IsNullOrWhiteSpace(message)) { _hostErrorLabel.Visible = false; _hostErrorLabel.Text = string.Empty; return; } _hostErrorLabel.Text = message.Trim(); _hostErrorLabel.Visible = true; } public void SetPauseContext(string contextLine) { if (_pauseContextLabel is not null) { _pauseContextLabel.Text = contextLine; } } private void ReleaseTextFocus() { _callsignEdit?.ReleaseFocus(); _serverNameEdit?.ReleaseFocus(); _portEdit?.ReleaseFocus(); } private void BuildMainRoot() { _mainRoot = new Control { Name = "MainMenuRoot", MouseFilter = Control.MouseFilterEnum.Ignore }; _mainRoot.SetAnchorsPreset(Control.LayoutPreset.FullRect); AddChild(_mainRoot); _menuPanel = ImperialChrome.CreatePanel(ImperialChrome.OrangeCol, cornerLen: 18f, borderPx: 2f, bgAlpha: 0.91f, padH: 20, padV: 18); _menuPanel.Name = "MainMenuPanel"; _menuPanel.AnchorLeft = 0f; _menuPanel.AnchorTop = 0f; _menuPanel.OffsetLeft = 48f; _menuPanel.OffsetTop = 52f; _menuPanel.OffsetRight = 458f; _mainRoot.AddChild(_menuPanel); var vbox = new VBoxContainer(); vbox.AddThemeConstantOverride("separation", 10); _menuPanel.AddChild(vbox); vbox.AddChild(ImperialChrome.CreateLabel("IMPERIAL ORBITAL INSERTION • SECTOR 01", 11, ImperialChrome.GoldCol)); var title = ImperialChrome.CreateLabel("RUST TO DUST", 32, ImperialChrome.TextLight); vbox.AddChild(title); vbox.AddChild(ImperialChrome.CreateLabel("SION 3KM ALPINE ARENA • BROKKR COMBAT CHASSIS", 11, ImperialChrome.TextMuted)); vbox.AddChild(ImperialChrome.MakeRule()); // Callsign row var callRow = new HBoxContainer(); callRow.AddThemeConstantOverride("separation", 10); vbox.AddChild(callRow); var callLbl = ImperialChrome.CreateLabel("CALLSIGN", 12, ImperialChrome.TextMuted); callLbl.CustomMinimumSize = new Vector2(82f, 0f); callRow.AddChild(callLbl); _callsignEdit = new LineEdit { Text = "Pilot", MaxLength = 24, PlaceholderText = "Pilot", SizeFlagsHorizontal = Control.SizeFlags.ExpandFill }; ImperialChrome.StyleLineEdit(_callsignEdit, ImperialChrome.CyanCol, 13); _callsignEdit.TextChanged += txt => CallsignChanged?.Invoke(SanitizeCallsign(txt)); callRow.AddChild(_callsignEdit); var squadRow = new HBoxContainer(); squadRow.AddThemeConstantOverride("separation", 10); vbox.AddChild(squadRow); var squadLbl = ImperialChrome.CreateLabel("SQUAD MODE", 12, ImperialChrome.TextMuted); squadLbl.CustomMinimumSize = new Vector2(82f, 0f); squadRow.AddChild(squadLbl); _soloSquadOption = CreateSquadOptionButton(); _soloSquadOption.ItemSelected += idx => _hostSquadOption?.Select((int)idx); squadRow.AddChild(_soloSquadOption); vbox.AddChild(ImperialChrome.MakeRule()); var playSionBtn = ImperialChrome.CreateButton("PLAY SOLO — SION", ImperialChrome.OliveCol, 14, 38f); playSionBtn.Pressed += () => PlaySoloRequested?.Invoke(ArenaMapKind.Sion, Callsign, SelectedSoloSquadSize); vbox.AddChild(playSionBtn); var playDesertBtn = ImperialChrome.CreateButton("PLAY SOLO — DESERT", ImperialChrome.OrangeCol, 14, 38f); playDesertBtn.Pressed += () => PlaySoloRequested?.Invoke(ArenaMapKind.Desert, Callsign, SelectedSoloSquadSize); vbox.AddChild(playDesertBtn); var browserBtn = ImperialChrome.CreateButton("SERVER BROWSER", ImperialChrome.CyanCol, 14, 38f); browserBtn.Pressed += () => ServerBrowserRequested?.Invoke(Callsign); vbox.AddChild(browserBtn); var hostBtn = ImperialChrome.CreateButton("HOST MATCH", ImperialChrome.GoldCol, 14, 38f); hostBtn.Pressed += () => SetHostPanelOpen(!IsHostPanelOpen); vbox.AddChild(hostBtn); vbox.AddChild(ImperialChrome.MakeRule()); var quitBtn = ImperialChrome.CreateButton("QUIT", ImperialChrome.RedCol, 13, 34f); quitBtn.Pressed += () => QuitRequested?.Invoke(); vbox.AddChild(quitBtn); _statusLabel = ImperialChrome.CreateLabel(string.Empty, 11, ImperialChrome.CyanCol); _statusLabel.AutowrapMode = TextServer.AutowrapMode.Word; _statusLabel.Visible = false; vbox.AddChild(_statusLabel); vbox.AddChild(ImperialChrome.CreateLabel("Data: swisstopo 3km Sion | CultNet UDP 3076", 10, ImperialChrome.TextMuted)); BuildHostPanel(); } private void BuildHostPanel() { _hostPanel = ImperialChrome.CreatePanel(ImperialChrome.GoldCol, cornerLen: 16f, borderPx: 1.5f, bgAlpha: 0.93f, padH: 18, padV: 16); _hostPanel.Name = "HostConfigPanel"; _hostPanel.AnchorLeft = 0f; _hostPanel.AnchorTop = 0f; _hostPanel.OffsetLeft = 480f; _hostPanel.OffsetTop = 132f; _hostPanel.OffsetRight = 900f; _hostPanel.Visible = false; _mainRoot.AddChild(_hostPanel); var vbox = new VBoxContainer(); vbox.AddThemeConstantOverride("separation", 10); _hostPanel.AddChild(vbox); vbox.AddChild(ImperialChrome.CreateLabel("HOST PRE-MATCH LOBBY", 14, ImperialChrome.GoldCol)); vbox.AddChild(ImperialChrome.MakeRule()); var nameRow = new HBoxContainer(); nameRow.AddThemeConstantOverride("separation", 8); vbox.AddChild(nameRow); var srvLbl = ImperialChrome.CreateLabel("SERVER NAME", 11, ImperialChrome.TextMuted); srvLbl.CustomMinimumSize = new Vector2(106f, 0f); nameRow.AddChild(srvLbl); _serverNameEdit = new LineEdit { Text = "Rust to Dust", MaxLength = 48, PlaceholderText = "Server Name", SizeFlagsHorizontal = Control.SizeFlags.ExpandFill }; ImperialChrome.StyleLineEdit(_serverNameEdit, ImperialChrome.GoldCol, 13); nameRow.AddChild(_serverNameEdit); var portRow = new HBoxContainer(); portRow.AddThemeConstantOverride("separation", 8); vbox.AddChild(portRow); var portLbl = ImperialChrome.CreateLabel("UDP PORT", 11, ImperialChrome.TextMuted); portLbl.CustomMinimumSize = new Vector2(106f, 0f); portRow.AddChild(portLbl); _portEdit = new LineEdit { Text = "3076", MaxLength = 5, PlaceholderText = "3076", SizeFlagsHorizontal = Control.SizeFlags.ExpandFill }; ImperialChrome.StyleLineEdit(_portEdit, ImperialChrome.GoldCol, 13); portRow.AddChild(_portEdit); var mapRow = new HBoxContainer(); mapRow.AddThemeConstantOverride("separation", 8); vbox.AddChild(mapRow); var mapLbl = ImperialChrome.CreateLabel("DROP ZONE", 11, ImperialChrome.TextMuted); mapLbl.CustomMinimumSize = new Vector2(106f, 0f); mapRow.AddChild(mapLbl); _hostMapOption = new OptionButton { SizeFlagsHorizontal = Control.SizeFlags.ExpandFill }; _hostMapOption.AddItem("SION (3KM ALPINE)", 0); _hostMapOption.AddItem("DESERT RING", 1); _hostMapOption.Select(0); ImperialChrome.StyleOptionButton(_hostMapOption, ImperialChrome.OliveCol, 13); mapRow.AddChild(_hostMapOption); var hostSqRow = new HBoxContainer(); hostSqRow.AddThemeConstantOverride("separation", 8); vbox.AddChild(hostSqRow); var hostSqLbl = ImperialChrome.CreateLabel("SQUAD MODE", 11, ImperialChrome.TextMuted); hostSqLbl.CustomMinimumSize = new Vector2(106f, 0f); hostSqRow.AddChild(hostSqLbl); _hostSquadOption = CreateSquadOptionButton(); hostSqRow.AddChild(_hostSquadOption); _listPublicCheck = new CheckBox { Text = "List on Public Directory", ButtonPressed = false }; ImperialChrome.StyleCheckBox(_listPublicCheck, ImperialChrome.CyanCol, 12); vbox.AddChild(_listPublicCheck); _directoryHintLabel = ImperialChrome.CreateLabel("Directory: https://eureka.gamecult.org/rtd-directory", 10, ImperialChrome.TextMuted); _directoryHintLabel.AutowrapMode = TextServer.AutowrapMode.Word; vbox.AddChild(_directoryHintLabel); _hostErrorLabel = ImperialChrome.CreateLabel(string.Empty, 11, ImperialChrome.RedCol); _hostErrorLabel.AutowrapMode = TextServer.AutowrapMode.Word; _hostErrorLabel.Visible = false; vbox.AddChild(_hostErrorLabel); var btnRow = new HBoxContainer(); btnRow.AddThemeConstantOverride("separation", 8); vbox.AddChild(btnRow); var launchBtn = ImperialChrome.CreateButton("LAUNCH LOBBY", ImperialChrome.OliveCol, 13, 34f); launchBtn.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill; launchBtn.Pressed += OnHostLaunchPressed; btnRow.AddChild(launchBtn); var cancelBtn = ImperialChrome.CreateButton("CANCEL", ImperialChrome.BorderCol, 13, 34f); cancelBtn.Pressed += () => SetHostPanelOpen(false); btnRow.AddChild(cancelBtn); } private void OnHostLaunchPressed() { if (!int.TryParse(_portEdit.Text.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int port) || port is < 1 or > 65535) { SetHostError("UDP port must be between 1 and 65535."); return; } string serverName = string.IsNullOrWhiteSpace(_serverNameEdit.Text) ? "Rust to Dust" : _serverNameEdit.Text.Trim(); var mapKind = _hostMapOption.Selected == 1 ? ArenaMapKind.Desert : ArenaMapKind.Sion; bool listPublic = _listPublicCheck.ButtonPressed; SetHostError(null); HostLobbyRequested?.Invoke(port, serverName, mapKind, listPublic, Callsign, SelectedHostSquadSize); } private static OptionButton CreateSquadOptionButton() { var opt = new OptionButton { SizeFlagsHorizontal = Control.SizeFlags.ExpandFill }; opt.AddItem("FFA (1)", 0); opt.AddItem("DUO (2)", 1); opt.AddItem("TRIO (3)", 2); opt.AddItem("QUAD (4)", 3); opt.Select(2); ImperialChrome.StyleOptionButton(opt, ImperialChrome.CyanCol, 13); return opt; } private void BuildPauseRoot() { _pauseRoot = new Control { Name = "PauseOverlayRoot", MouseFilter = Control.MouseFilterEnum.Stop }; _pauseRoot.SetAnchorsPreset(Control.LayoutPreset.FullRect); AddChild(_pauseRoot); var scrim = new ColorRect { Color = ImperialChrome.ScrimCol, MouseFilter = Control.MouseFilterEnum.Stop }; scrim.SetAnchorsPreset(Control.LayoutPreset.FullRect); _pauseRoot.AddChild(scrim); var panel = ImperialChrome.CreatePanel(ImperialChrome.OrangeCol, cornerLen: 16f, borderPx: 2f, bgAlpha: 0.94f, padH: 20, padV: 18); panel.Name = "PauseOverlayPanel"; panel.AnchorLeft = 0.5f; panel.AnchorRight = 0.5f; panel.AnchorTop = 0.5f; panel.AnchorBottom = 0.5f; panel.OffsetLeft = -190f; panel.OffsetRight = 190f; panel.OffsetTop = -132f; panel.OffsetBottom = 132f; _pauseRoot.AddChild(panel); var vbox = new VBoxContainer(); vbox.AddThemeConstantOverride("separation", 10); panel.AddChild(vbox); vbox.AddChild(ImperialChrome.CreateLabel("TACTICAL PAUSE • [ESC]", 16, ImperialChrome.OrangeCol, HorizontalAlignment.Center)); _pauseContextLabel = ImperialChrome.CreateLabel("COMBAT ACTIVE", 11, ImperialChrome.TextMuted, HorizontalAlignment.Center); vbox.AddChild(_pauseContextLabel); vbox.AddChild(ImperialChrome.MakeRule()); var resumeBtn = ImperialChrome.CreateButton("RESUME", ImperialChrome.OliveCol, 14, 36f); resumeBtn.Pressed += () => ResumeRequested?.Invoke(); vbox.AddChild(resumeBtn); var menuBtn = ImperialChrome.CreateButton("RETURN TO MAIN MENU", ImperialChrome.OrangeCol, 14, 36f); menuBtn.Pressed += () => ReturnToMainMenuRequested?.Invoke(); vbox.AddChild(menuBtn); var quitBtn = ImperialChrome.CreateButton("QUIT", ImperialChrome.RedCol, 13, 34f); quitBtn.Pressed += () => QuitRequested?.Invoke(); vbox.AddChild(quitBtn); } private static string SanitizeCallsign(string? raw) { string trimmed = (raw ?? string.Empty).Trim(); return string.IsNullOrEmpty(trimmed) ? "Pilot" : trimmed; } }