From e22d7b31f99dfb1b9207b1fd8746746fc5806137 Mon Sep 17 00:00:00 2001 From: Gustas <37534529+PunkPun@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:52:10 +0300 Subject: [PATCH] Fix selected map in server creation panel not updating --- .../Widgets/Logic/ServerCreationLogic.cs | 72 +++++++------------ mods/cnc/chrome/multiplayer-createserver.yaml | 35 +-------- .../chrome/multiplayer-createserver.yaml | 35 +-------- 3 files changed, 30 insertions(+), 112 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs index 8038a517f5..2b6e7e89ac 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs @@ -11,7 +11,6 @@ using System; using System.Globalization; -using System.Linq; using OpenRA.Network; using OpenRA.Primitives; using OpenRA.Widgets; @@ -58,7 +57,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic readonly LabelWidget noticesLabelA, noticesLabelB, noticesLabelC; readonly Action onCreate; readonly Action onExit; - MapPreview preview = MapCache.UnknownMap; + MapPreview map = MapCache.UnknownMap; bool advertiseOnline; [ObjectCreator.UseCtor] @@ -70,7 +69,20 @@ namespace OpenRA.Mods.Common.Widgets.Logic this.onExit = onExit; var settings = Game.Settings; - preview = modData.MapCache[modData.MapCache.ChooseInitialMap(modData.MapCache.PickLastModifiedMap(MapVisibility.Lobby) ?? Game.Settings.Server.Map, Game.CosmeticRandom)]; + + map = modData.MapCache[modData.MapCache.ChooseInitialMap(modData.MapCache.PickLastModifiedMap(MapVisibility.Lobby) ?? Game.Settings.Server.Map, Game.CosmeticRandom)]; + + Ui.LoadWidget("MAP_PREVIEW", panel.Get("MAP_PREVIEW_ROOT"), new WidgetArgs + { + { "orderManager", null }, + { "getMap", (Func<(MapPreview, Session.MapStatus)>)(() => (map, Session.MapStatus.Playable)) }, + { "onMouseDown", null }, + { "getSpawnOccupants", null }, + { "getDisabledSpawnPoints", null }, + { "showUnoccupiedSpawnpoints", false }, + { "mapUpdatesEnabled", true }, + { "onMapUpdate", (Action)(uid => map = modData.MapCache[uid]) }, + }); panel.Get("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); }; panel.Get("CREATE_BUTTON").OnClick = CreateAndJoin; @@ -82,50 +94,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic { Ui.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs() { - { "initialMap", preview.Uid }, + { "initialMap", map.Uid }, { "initialTab", MapClassification.System }, - { "onExit", () => { } }, - { "onSelect", (Action)(uid => preview = modData.MapCache[uid]) }, + { "onExit", () => modData.MapCache.UpdateMaps() }, + { "onSelect", (Action)(uid => map = modData.MapCache[uid]) }, { "filter", MapVisibility.Lobby }, { "onStart", () => { } } }); }; - - panel.Get("MAP_PREVIEW").Preview = () => preview; - - var titleLabel = panel.GetOrNull("MAP_TITLE"); - if (titleLabel != null) - { - var font = Game.Renderer.Fonts[titleLabel.Font]; - var title = new CachedTransform(m => - { - var truncated = WidgetUtils.TruncateText(m.Title, titleLabel.Bounds.Width, font); - - if (m.Title != truncated) - titleLabel.GetTooltipText = () => m.Title; - else - titleLabel.GetTooltipText = null; - - return truncated; - }); - titleLabel.GetText = () => title.Update(preview); - } - - var typeLabel = panel.GetOrNull("MAP_TYPE"); - if (typeLabel != null) - { - var type = new CachedTransform(m => m.Categories.FirstOrDefault() ?? ""); - typeLabel.GetText = () => type.Update(preview); - } - - var authorLabel = panel.GetOrNull("MAP_AUTHOR"); - if (authorLabel != null) - { - var font = Game.Renderer.Fonts[authorLabel.Font]; - var author = new CachedTransform( - m => WidgetUtils.TruncateText($"Created by {m.Author}", authorLabel.Bounds.Width, font)); - authorLabel.GetText = () => author.Update(preview); - } } var serverName = panel.Get("SERVER_NAME"); @@ -221,6 +197,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic void CreateAndJoin() { + // Refresh MapCache. + if (modData.MapCache[map.Uid].Status != MapStatus.Available) + return; + var name = Game.Settings.SanitizedServerName(panel.Get("SERVER_NAME").Text); if (!int.TryParse(panel.Get("LISTEN_PORT").Text, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out var listenPort)) listenPort = 1234; @@ -228,18 +208,18 @@ namespace OpenRA.Mods.Common.Widgets.Logic var passwordField = panel.GetOrNull("PASSWORD"); var password = passwordField != null ? passwordField.Text : ""; - // Save new settings + // Save new settings. Game.Settings.Server.Name = name; Game.Settings.Server.ListenPort = listenPort; Game.Settings.Server.AdvertiseOnline = advertiseOnline; - Game.Settings.Server.Map = preview.Uid; + Game.Settings.Server.Map = map.Uid; Game.Settings.Server.Password = password; Game.Settings.Save(); - // Take a copy so that subsequent changes don't affect the server + // Take a copy so that subsequent changes don't affect the server. var settings = Game.Settings.Server.Clone(); - // Create and join the server + // Create and join the server. try { var endpoint = Game.CreateServer(settings); diff --git a/mods/cnc/chrome/multiplayer-createserver.yaml b/mods/cnc/chrome/multiplayer-createserver.yaml index 0c8a36988f..833d56f9d5 100644 --- a/mods/cnc/chrome/multiplayer-createserver.yaml +++ b/mods/cnc/chrome/multiplayer-createserver.yaml @@ -204,42 +204,11 @@ Container@MULTIPLAYER_CREATESERVER_PANEL: Font: Tiny Align: Left Text: - You can disable UPnP/NAT-PMP in the settings menu. - Background@MAP_BG: + Container@MAP_PREVIEW_ROOT: X: PARENT_RIGHT - 189 Y: 15 Width: 174 - Height: 174 - Background: panel-gray - Children: - MapPreview@MAP_PREVIEW: - X: 1 - Y: 1 - Width: PARENT_RIGHT - 2 - Height: PARENT_RIGHT - 2 - LabelWithTooltip@MAP_TITLE: - X: PARENT_RIGHT - 189 - Y: 187 - Width: 174 - Height: 25 - Font: Bold - Align: Center - TooltipContainer: TOOLTIP_CONTAINER - TooltipTemplate: SIMPLE_TOOLTIP - Label@MAP_TYPE: - X: PARENT_RIGHT - 189 - Y: 202 - Width: 174 - Height: 25 - Font: TinyBold - Align: Center - IgnoreMouseOver: true - Label@MAP_AUTHOR: - X: PARENT_RIGHT - 189 - Y: 215 - Width: 174 - Height: 25 - Font: Tiny - Align: Center + Height: 250 Button@BACK_BUTTON: Key: return Y: PARENT_BOTTOM - 1 diff --git a/mods/common/chrome/multiplayer-createserver.yaml b/mods/common/chrome/multiplayer-createserver.yaml index c7e5499970..8fca382c18 100644 --- a/mods/common/chrome/multiplayer-createserver.yaml +++ b/mods/common/chrome/multiplayer-createserver.yaml @@ -199,42 +199,11 @@ Background@MULTIPLAYER_CREATESERVER_PANEL: Font: Tiny Align: Left Text: - You can disable UPnP/NAT-PMP in the settings menu. - Background@MAP_BG: + Container@MAP_PREVIEW_ROOT: X: PARENT_RIGHT - 194 Y: 45 Width: 174 - Height: 174 - Background: dialog3 - Children: - MapPreview@MAP_PREVIEW: - X: 1 - Y: 1 - Width: PARENT_RIGHT - 2 - Height: PARENT_RIGHT - 2 - LabelWithTooltip@MAP_TITLE: - X: PARENT_RIGHT - 194 - Y: 218 - Width: 174 - Height: 25 - Font: Bold - Align: Center - TooltipContainer: TOOLTIP_CONTAINER - TooltipTemplate: SIMPLE_TOOLTIP - Label@MAP_TYPE: - X: PARENT_RIGHT - 194 - Y: 233 - Width: 174 - Height: 25 - Font: TinyBold - Align: Center - IgnoreMouseOver: true - Label@MAP_AUTHOR: - X: PARENT_RIGHT - 194 - Y: 246 - Width: 174 - Height: 25 - Font: Tiny - Align: Center + Height: 250 Button@MAP_BUTTON: X: 20 Y: PARENT_BOTTOM - 45