internal MapChooserLogic([ObjectCreator.Param] Widget widget, [ObjectCreator.Param] string initialMap, [ObjectCreator.Param] Action onExit, [ObjectCreator.Param] Action<Map> onSelect) { map = Game.modData.AvailableMaps[WidgetUtils.ChooseInitialMap(initialMap)]; widget.GetWidget<ButtonWidget>("BUTTON_OK").OnClick = () => { Widget.CloseWindow(); onSelect(map); }; widget.GetWidget<ButtonWidget>("BUTTON_CANCEL").OnClick = () => { Widget.CloseWindow(); onExit(); }; scrollpanel = widget.GetWidget<ScrollPanelWidget>("MAP_LIST"); itemTemplate = scrollpanel.GetWidget<ScrollItemWidget>("MAP_TEMPLATE"); var gameModeDropdown = widget.GetWidget<DropDownButtonWidget>("GAMEMODE_FILTER"); if (gameModeDropdown != null) { var selectableMaps = Game.modData.AvailableMaps.Where(m => m.Value.Selectable); var gameModes = selectableMaps .GroupBy(m => m.Value.Type) .Select(g => Pair.New(g.Key, g.Count())).ToList(); // 'all game types' extra item gameModes.Insert( 0, Pair.New( null as string, selectableMaps.Count() ) ); Func<Pair<string,int>, string> showItem = x => "{0} ({1})".F( x.First ?? "All Game Types", x.Second ); Func<Pair<string,int>, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, template) => { var item = ScrollItemWidget.Setup(template, () => gameMode == ii.First, () => { gameMode = ii.First; EnumerateMaps(); }); item.GetWidget<LabelWidget>("LABEL").GetText = () => showItem(ii); return item; }; gameModeDropdown.OnClick = () => gameModeDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, gameModes, setupItem); gameModeDropdown.GetText = () => showItem(gameModes.First(m => m.First == gameMode)); } EnumerateMaps(); }
internal LobbyLogic(Widget widget, World world, OrderManager orderManager, Action onExit, Action onStart, bool addBots) { var lobby = widget; this.orderManager = orderManager; this.OnGameStart = () => { CloseWindow(); onStart(); }; this.onExit = onExit; Game.LobbyInfoChanged += UpdateCurrentMap; Game.LobbyInfoChanged += UpdatePlayerList; Game.BeforeGameStart += OnGameStart; Game.AddChatLine += AddChatLine; Game.ConnectionStateChanged += ConnectionStateChanged; UpdateCurrentMap(); PlayerPalettePreview = world.WorldActor.Trait<ColorPickerPaletteModifier>(); PlayerPalettePreview.Ramp = Game.Settings.Player.ColorRamp; Players = lobby.GetWidget<ScrollPanelWidget>("PLAYERS"); EditablePlayerTemplate = Players.GetWidget("TEMPLATE_EDITABLE_PLAYER"); NonEditablePlayerTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_PLAYER"); EmptySlotTemplate = Players.GetWidget("TEMPLATE_EMPTY"); EditableSpectatorTemplate = Players.GetWidget("TEMPLATE_EDITABLE_SPECTATOR"); NonEditableSpectatorTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_SPECTATOR"); NewSpectatorTemplate = Players.GetWidget("TEMPLATE_NEW_SPECTATOR"); var mapPreview = lobby.GetWidget<MapPreviewWidget>("MAP_PREVIEW"); mapPreview.IsVisible = () => Map != null; mapPreview.Map = () => Map; mapPreview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint( orderManager, mapPreview, Map, mi ); mapPreview.SpawnColors = () => LobbyUtils.GetSpawnColors( orderManager, Map ); var mapTitle = lobby.GetWidget<LabelWidget>("MAP_TITLE"); if (mapTitle != null) { mapTitle.IsVisible = () => Map != null; mapTitle.GetText = () => Map.Title; } CountryNames = Rules.Info["world"].Traits.WithInterface<CountryInfo>() .Where(c => c.Selectable) .ToDictionary(a => a.Race, a => a.Name); CountryNames.Add("random", "Any"); var mapButton = lobby.GetWidget<ButtonWidget>("CHANGEMAP_BUTTON"); mapButton.OnClick = () => { var onSelect = new Action<Map>(m => { orderManager.IssueOrder(Order.Command("map " + m.Uid)); Game.Settings.Server.Map = m.Uid; Game.Settings.Save(); }); Ui.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs() { { "initialMap", Map.Uid }, { "onExit", () => {} }, { "onSelect", onSelect } }); }; mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON"); disconnectButton.OnClick = () => { CloseWindow(); onExit(); }; var gameStarting = false; var allowCheats = lobby.GetWidget<CheckboxWidget>("ALLOWCHEATS_CHECKBOX"); allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats; allowCheats.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null || orderManager.LocalClient.IsReady; allowCheats.OnClick = () => orderManager.IssueOrder(Order.Command( "allowcheats {0}".F(!orderManager.LobbyInfo.GlobalSettings.AllowCheats))); var startGameButton = lobby.GetWidget<ButtonWidget>("START_GAME_BUTTON"); startGameButton.IsVisible = () => Game.IsHost; startGameButton.IsDisabled = () => gameStarting; startGameButton.OnClick = () => { gameStarting = true; orderManager.IssueOrder(Order.Command("startgame")); }; bool teamChat = false; var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE"); var chatTextField = lobby.GetWidget<TextFieldWidget>("CHAT_TEXTFIELD"); chatTextField.OnEnterKey = () => { if (chatTextField.Text.Length == 0) return true; orderManager.IssueOrder(Order.Chat(teamChat, chatTextField.Text)); chatTextField.Text = ""; return true; }; chatTextField.OnTabKey = () => { teamChat ^= true; chatLabel.Text = (teamChat) ? "Team:" : "Chat:"; return true; }; chatPanel = lobby.GetWidget<ScrollPanelWidget>("CHAT_DISPLAY"); chatTemplate = chatPanel.GetWidget("CHAT_TEMPLATE"); chatPanel.RemoveChildren(); var musicButton = lobby.GetWidget<ButtonWidget>("MUSIC_BUTTON"); if (musicButton != null) musicButton.OnClick = () => Ui.OpenWindow("MUSIC_PANEL", new WidgetArgs { { "onExit", () => {} } }); // Add a bot on the first lobbyinfo update if (addBots) Game.LobbyInfoChanged += WidgetUtils.Once(() => { var slot = orderManager.LobbyInfo.FirstEmptySlot(); var bot = Rules.Info["player"].Traits.WithInterface<IBotInfo>().Select(t => t.Name).FirstOrDefault(); if (slot != null && bot != null) orderManager.IssueOrder(Order.Command("slot_bot {0} {1}".F(slot, bot))); }); }
internal LobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby, [ObjectCreator.Param] OrderManager orderManager, [ObjectCreator.Param] WorldRenderer worldRenderer) { this.orderManager = orderManager; this.worldRenderer = worldRenderer; this.lobby = lobby; Game.BeforeGameStart += CloseWindow; Game.LobbyInfoChanged += UpdateCurrentMap; Game.LobbyInfoChanged += UpdatePlayerList; UpdateCurrentMap(); CurrentColorPreview = Game.Settings.Player.ColorRamp; Players = lobby.GetWidget<ScrollPanelWidget>("PLAYERS"); LocalPlayerTemplate = Players.GetWidget("TEMPLATE_LOCAL"); RemotePlayerTemplate = Players.GetWidget("TEMPLATE_REMOTE"); EmptySlotTemplate = Players.GetWidget("TEMPLATE_EMPTY"); EmptySlotTemplateHost = Players.GetWidget("TEMPLATE_EMPTY_HOST"); LocalSpectatorTemplate = Players.GetWidget("TEMPLATE_LOCAL_SPECTATOR"); RemoteSpectatorTemplate = Players.GetWidget("TEMPLATE_REMOTE_SPECTATOR"); NewSpectatorTemplate = Players.GetWidget("TEMPLATE_NEW_SPECTATOR"); var mapPreview = lobby.GetWidget<MapPreviewWidget>("LOBBY_MAP_PREVIEW"); mapPreview.Map = () => Map; mapPreview.OnMouseDown = mi => { var map = mapPreview.Map(); if (map == null || mi.Button != MouseButton.Left || orderManager.LocalClient.State == Session.ClientState.Ready) return; var p = map.SpawnPoints .Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(map, sp), i)) .Where(a => (a.First - mi.Location).LengthSquared < 64) .Select(a => a.Second + 1) .FirstOrDefault(); var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == p); if (p == 0 || !owned) orderManager.IssueOrder(Order.Command("spawn {0} {1}".F(orderManager.LocalClient.Index, p))); }; mapPreview.SpawnColors = () => { var spawns = Map.SpawnPoints; var sc = new Dictionary<int2, Color>(); for (int i = 1; i <= spawns.Count(); i++) { var client = orderManager.LobbyInfo.Clients.FirstOrDefault(c => c.SpawnPoint == i); if (client == null) continue; sc.Add(spawns.ElementAt(i - 1), client.ColorRamp.GetColor(0)); } return sc; }; CountryNames = Rules.Info["world"].Traits.WithInterface<OpenRA.Traits.CountryInfo>() .ToDictionary(a => a.Race, a => a.Name); CountryNames.Add("random", "Random"); var mapButton = lobby.GetWidget<ButtonWidget>("CHANGEMAP_BUTTON"); mapButton.OnClick = () => { Widget.OpenWindow("MAP_CHOOSER", new WidgetArgs() { { "orderManager", orderManager }, { "mapName", MapUid } }); }; mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON"); disconnectButton.OnClick = () => { CloseWindow(); Game.Disconnect(); Game.LoadShellMap(); Widget.OpenWindow("MAINMENU_BG"); }; var allowCheats = lobby.GetWidget<CheckboxWidget>("ALLOWCHEATS_CHECKBOX"); allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats; allowCheats.OnClick = () => { if (Game.IsHost) orderManager.IssueOrder(Order.Command( "allowcheats {0}".F(!orderManager.LobbyInfo.GlobalSettings.AllowCheats))); }; var startGameButton = lobby.GetWidget<ButtonWidget>("START_GAME_BUTTON"); startGameButton.OnClick = () => { mapButton.Visible = false; disconnectButton.Visible = false; orderManager.IssueOrder(Order.Command("startgame")); }; // Todo: Only show if the map requirements are met for player slots startGameButton.IsVisible = () => Game.IsHost; bool teamChat = false; var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE"); var chatTextField = lobby.GetWidget<TextFieldWidget>("CHAT_TEXTFIELD"); chatTextField.OnEnterKey = () => { if (chatTextField.Text.Length == 0) return true; var order = (teamChat) ? Order.TeamChat(chatTextField.Text) : Order.Chat(chatTextField.Text); orderManager.IssueOrder(order); chatTextField.Text = ""; return true; }; chatTextField.OnTabKey = () => { teamChat ^= true; chatLabel.Text = (teamChat) ? "Team:" : "Chat:"; return true; }; Game.AddChatLine += AddChatLine; }
internal CncLobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby, [ObjectCreator.Param] World world, // Shellmap world [ObjectCreator.Param] OrderManager orderManager, [ObjectCreator.Param] Action onExit, [ObjectCreator.Param] Action onStart, [ObjectCreator.Param] bool addBots) { this.orderManager = orderManager; this.OnGameStart = () => { CloseWindow(); onStart(); }; this.onExit = onExit; Game.LobbyInfoChanged += UpdateCurrentMap; Game.LobbyInfoChanged += UpdatePlayerList; Game.BeforeGameStart += OnGameStart; Game.AddChatLine += AddChatLine; Game.ConnectionStateChanged += ConnectionStateChanged; UpdateCurrentMap(); PlayerPalettePreview = world.WorldActor.Trait<CncColorPickerPaletteModifier>(); PlayerPalettePreview.Ramp = Game.Settings.Player.ColorRamp; Players = lobby.GetWidget<ScrollPanelWidget>("PLAYERS"); EditablePlayerTemplate = Players.GetWidget("TEMPLATE_EDITABLE_PLAYER"); NonEditablePlayerTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_PLAYER"); EmptySlotTemplate = Players.GetWidget("TEMPLATE_EMPTY"); EditableSpectatorTemplate = Players.GetWidget("TEMPLATE_EDITABLE_SPECTATOR"); NonEditableSpectatorTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_SPECTATOR"); NewSpectatorTemplate = Players.GetWidget("TEMPLATE_NEW_SPECTATOR"); var mapPreview = lobby.GetWidget<MapPreviewWidget>("MAP_PREVIEW"); mapPreview.IsVisible = () => Map != null; mapPreview.Map = () => Map; mapPreview.OnMouseDown = mi => { var map = mapPreview.Map(); if (map == null || mi.Button != MouseButton.Left || orderManager.LocalClient.State == Session.ClientState.Ready) return; var p = map.SpawnPoints .Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(map, sp), i)) .Where(a => (a.First - mi.Location).LengthSquared < 64) .Select(a => a.Second + 1) .FirstOrDefault(); var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == p); if (p == 0 || !owned) orderManager.IssueOrder(Order.Command("spawn {0} {1}".F(orderManager.LocalClient.Index, p))); }; var mapTitle = lobby.GetWidget<LabelWidget>("MAP_TITLE"); mapTitle.IsVisible = () => Map != null; mapTitle.GetText = () => Map.Title; mapPreview.SpawnColors = () => { var spawns = Map.SpawnPoints; var sc = new Dictionary<int2, Color>(); for (int i = 1; i <= spawns.Count(); i++) { var client = orderManager.LobbyInfo.Clients.FirstOrDefault(c => c.SpawnPoint == i); if (client == null) continue; sc.Add(spawns.ElementAt(i - 1), client.ColorRamp.GetColor(0)); } return sc; }; CountryNames = Rules.Info["world"].Traits.WithInterface<CountryInfo>() .ToDictionary(a => a.Race, a => a.Name); CountryNames.Add("random", "Any"); var mapButton = lobby.GetWidget<ButtonWidget>("CHANGEMAP_BUTTON"); mapButton.OnClick = () => { var onSelect = new Action<Map>(m => { orderManager.IssueOrder(Order.Command("map " + m.Uid)); Game.Settings.Server.Map = m.Uid; Game.Settings.Save(); }); Widget.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs() { { "initialMap", Map.Uid }, { "onExit", () => {} }, { "onSelect", onSelect } }); }; mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON"); disconnectButton.OnClick = () => { CloseWindow(); onExit(); }; var gameStarting = false; var allowCheats = lobby.GetWidget<CheckboxWidget>("ALLOWCHEATS_CHECKBOX"); allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats; allowCheats.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null || orderManager.LocalClient.State == Session.ClientState.Ready; allowCheats.OnClick = () => orderManager.IssueOrder(Order.Command( "allowcheats {0}".F(!orderManager.LobbyInfo.GlobalSettings.AllowCheats))); var startGameButton = lobby.GetWidget<ButtonWidget>("START_GAME_BUTTON"); startGameButton.IsVisible = () => Game.IsHost; startGameButton.IsDisabled = () => gameStarting; startGameButton.OnClick = () => { gameStarting = true; orderManager.IssueOrder(Order.Command("startgame")); }; bool teamChat = false; var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE"); var chatTextField = lobby.GetWidget<TextFieldWidget>("CHAT_TEXTFIELD"); chatTextField.OnEnterKey = () => { if (chatTextField.Text.Length == 0) return true; orderManager.IssueOrder(Order.Chat(teamChat, chatTextField.Text)); chatTextField.Text = ""; return true; }; chatTextField.OnTabKey = () => { teamChat ^= true; chatLabel.Text = (teamChat) ? "Team:" : "Chat:"; return true; }; chatPanel = lobby.GetWidget<ScrollPanelWidget>("CHAT_DISPLAY"); chatTemplate = chatPanel.GetWidget("CHAT_TEMPLATE"); chatPanel.RemoveChildren(); lobby.GetWidget<ButtonWidget>("MUSIC_BUTTON").OnClick = () => { Widget.OpenWindow("MUSIC_PANEL", new WidgetArgs() { { "onExit", () => {} }, }); }; // Add a bot on the first lobbyinfo update if (addBots) Game.LobbyInfoChanged += WidgetUtils.Once(() => { var slot = orderManager.LobbyInfo.FirstEmptySlot(); var bot = Rules.Info["player"].Traits.WithInterface<IBotInfo>().Select(t => t.Name).FirstOrDefault(); if (slot != null && bot != null) orderManager.IssueOrder(Order.Command("slot_bot {0} {1}".F(slot, bot))); }); }