Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selected map in server creation panel not updating #19990

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions OpenRA.Mods.Common/Widgets/Logic/Lobby/MapPreviewLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ Widget SetupMapPreview(Widget parent)
{
var preview = parent.Get<MapPreviewWidget>("MAP_PREVIEW");
preview.Preview = () => getMap().Map;
preview.OnMouseDown = mi => onMouseDown(preview, getMap().Map, mi);
preview.SpawnOccupants = getSpawnOccupants;
preview.DisabledSpawnPoints = getDisabledSpawnPoints;
if (onMouseDown != null)
preview.OnMouseDown = mi => onMouseDown(preview, getMap().Map, mi);

if (getSpawnOccupants != null)
preview.SpawnOccupants = getSpawnOccupants;

if (getDisabledSpawnPoints != null)
preview.DisabledSpawnPoints = getDisabledSpawnPoints;

preview.ShowUnoccupiedSpawnpoints = showUnoccupiedSpawnpoints;

var titleLabel = parent.Get<LabelWithTooltipWidget>("MAP_TITLE");
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/ReplayBrowserLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public ReplayBrowserLogic(Widget widget, ModData modData, Action onExit, Action
{
{ "orderManager", null },
{ "getMap", (Func<(MapPreview, Session.MapStatus)>)(() => (map, Session.MapStatus.Playable)) },
{ "onMouseDown", (Action<MapPreviewWidget, MapPreview, MouseInput>)((preview, mapPreview, mi) => { }) },
{ "onMouseDown", null },
{ "getSpawnOccupants", (Func<Dictionary<int, SpawnOccupant>>)(() => spawnOccupants.Update(selectedReplay)) },
{ "getDisabledSpawnPoints", (Func<HashSet<int>>)(() => disabledSpawnPoints.Update(selectedReplay)) },
{ "showUnoccupiedSpawnpoints", false },
Expand Down
72 changes: 26 additions & 46 deletions OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using System;
using System.Globalization;
using System.Linq;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Widgets;
Expand Down Expand Up @@ -58,7 +57,7 @@ public class ServerCreationLogic : ChromeLogic
readonly LabelWidget noticesLabelA, noticesLabelB, noticesLabelC;
readonly Action onCreate;
readonly Action onExit;
MapPreview preview = MapCache.UnknownMap;
MapPreview map = MapCache.UnknownMap;
bool advertiseOnline;

[ObjectCreator.UseCtor]
Expand All @@ -70,7 +69,20 @@ public ServerCreationLogic(Widget widget, ModData modData, Action onExit, Action
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<string>)(uid => map = modData.MapCache[uid]) },
});

panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = CreateAndJoin;
Expand All @@ -82,50 +94,14 @@ public ServerCreationLogic(Widget widget, ModData modData, Action onExit, Action
{
Ui.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{
{ "initialMap", preview.Uid },
{ "initialMap", map.Uid },
{ "initialTab", MapClassification.System },
{ "onExit", () => { } },
{ "onSelect", (Action<string>)(uid => preview = modData.MapCache[uid]) },
{ "onExit", () => modData.MapCache.UpdateMaps() },
{ "onSelect", (Action<string>)(uid => map = modData.MapCache[uid]) },
{ "filter", MapVisibility.Lobby },
{ "onStart", () => { } }
});
};

panel.Get<MapPreviewWidget>("MAP_PREVIEW").Preview = () => preview;

var titleLabel = panel.GetOrNull<LabelWithTooltipWidget>("MAP_TITLE");
if (titleLabel != null)
{
var font = Game.Renderer.Fonts[titleLabel.Font];
var title = new CachedTransform<MapPreview, string>(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<LabelWidget>("MAP_TYPE");
if (typeLabel != null)
{
var type = new CachedTransform<MapPreview, string>(m => m.Categories.FirstOrDefault() ?? "");
typeLabel.GetText = () => type.Update(preview);
}

var authorLabel = panel.GetOrNull<LabelWidget>("MAP_AUTHOR");
if (authorLabel != null)
{
var font = Game.Renderer.Fonts[authorLabel.Font];
var author = new CachedTransform<MapPreview, string>(
m => WidgetUtils.TruncateText($"Created by {m.Author}", authorLabel.Bounds.Width, font));
authorLabel.GetText = () => author.Update(preview);
}
}

var serverName = panel.Get<TextFieldWidget>("SERVER_NAME");
Expand Down Expand Up @@ -221,25 +197,29 @@ void BuildNotices()

void CreateAndJoin()
{
// Refresh MapCache.
if (modData.MapCache[map.Uid].Status != MapStatus.Available)
return;
PunkPun marked this conversation as resolved.
Show resolved Hide resolved

var name = Game.Settings.SanitizedServerName(panel.Get<TextFieldWidget>("SERVER_NAME").Text);
if (!int.TryParse(panel.Get<TextFieldWidget>("LISTEN_PORT").Text, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out var listenPort))
listenPort = 1234;

var passwordField = panel.GetOrNull<PasswordFieldWidget>("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);
Expand Down
35 changes: 2 additions & 33 deletions mods/cnc/chrome/multiplayer-createserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 2 additions & 33 deletions mods/common/chrome/multiplayer-createserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down