From 398475b4ef19b47cf8cef1fa1ef8f45513bf6c5a Mon Sep 17 00:00:00 2001 From: elexis Date: Sun, 1 Sep 2019 15:35:10 +0000 Subject: [PATCH] Gamesetup OOP cleanup, refs #5322. Move procedural updateGameDescription and getMapPreview to OOP g_MiscControls from b4e5858f6d/D322. Delete unused variable childSize following ac7b5ce861. Remove host check in launchGame from 1c0536bf08 needless since d1d7afe46c. This was SVN commit r22826. --- .../mods/public/gui/common/gamedescription.js | 6 +-- .../mods/public/gui/gamesetup/gamesetup.js | 47 +++++++------------ .../data/mods/public/gui/loadgame/load.js | 5 +- binaries/data/mods/public/gui/lobby/lobby.js | 3 +- .../mods/public/gui/replaymenu/replay_menu.js | 6 +-- 5 files changed, 23 insertions(+), 44 deletions(-) diff --git a/binaries/data/mods/public/gui/common/gamedescription.js b/binaries/data/mods/public/gui/common/gamedescription.js index ee7d8b8f47..8c3c4518a2 100644 --- a/binaries/data/mods/public/gui/common/gamedescription.js +++ b/binaries/data/mods/public/gui/common/gamedescription.js @@ -75,13 +75,11 @@ function getMapDescriptionAndPreview(mapType, mapName, gameAttributes = undefine * Sets the mappreview image correctly. * It needs to be cropped as the engine only allows loading square textures. * - * @param {string} guiObject * @param {string} filename */ -function setMapPreviewImage(guiObject, filename) +function getMapPreviewImage(filename) { - Engine.GetGUIObjectByName(guiObject).sprite = - "cropped:" + 400 / 512 + "," + 300 / 512 + ":" + + return "cropped:" + 400 / 512 + "," + 300 / 512 + ":" + g_MapPreviewPath + filename; } diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index a3953cd6ec..cdfe0c5d87 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -1002,6 +1002,22 @@ var g_MiscControls = { "chatInput": { "tooltip": () => colorizeAutocompleteHotkey(translate("Press %(hotkey)s to autocomplete player names or settings.")), }, + "mapInfoName": { + "caption": () => translateMapTitle(getMapDisplayName(g_GameAttributes.map)) + }, + "mapInfoDescription": { + "caption": getGameDescription + }, + "mapPreview": { + "sprite": () => { + let biomePreview = g_GameAttributes.settings.Biome && getBiomePreview(g_GameAttributes.map, g_GameAttributes.settings.Biome); + if (biomePreview) + return getMapPreviewImage(biomePreview); + + let mapData = loadMapData(g_GameAttributes.map); + return getMapPreviewImage(mapData && mapData.settings && mapData.settings.Preview || "nopreview.png"); + } + }, "cheatWarningText": { "hidden": () => !g_IsNetworked || !g_GameAttributes.settings.CheatsEnabled, }, @@ -1484,7 +1500,6 @@ function distributeSettings() thisColumn = 0; } - let childSize = child.size; child.size = new GUISize( column * columnWidth, yPos, @@ -1676,19 +1691,6 @@ function getMapDisplayName(map) return mapData.settings.Name; } -function getMapPreview(map) -{ - let biomePreview = g_GameAttributes.settings.Biome && getBiomePreview(map, g_GameAttributes.settings.Biome); - if (biomePreview) - return biomePreview; - - let mapData = loadMapData(map); - if (!mapData || !mapData.settings || !mapData.settings.Preview) - return "nopreview.png"; - - return mapData.settings.Preview; -} - /** * Filter maps with filterFunc and by chosen map type. * @@ -2193,12 +2195,6 @@ function updateGUIMiscControl(name, playerIdx) function launchGame() { - if (!g_IsController) - { - error("Only host can start game"); - return; - } - if (!g_GameAttributes.map || g_GameStarted) return; @@ -2351,7 +2347,6 @@ function updateGUIObjects() for (let name in g_MiscControls) updateGUIMiscControl(name); - updateGameDescription(); distributeSettings(); rightAlignCancelButton(); updateAutocompleteEntries(); @@ -2390,16 +2385,6 @@ function rightAlignCancelButton() cancelGame.size = cancelGameSize; } -function updateGameDescription() -{ - setMapPreviewImage("mapPreview", getMapPreview(g_GameAttributes.map)); - - Engine.GetGUIObjectByName("mapInfoName").caption = - translateMapTitle(getMapDisplayName(g_GameAttributes.map)); - - Engine.GetGUIObjectByName("mapInfoDescription").caption = getGameDescription(); -} - /** * Broadcast the changed settings to all clients and the lobbybot. */ diff --git a/binaries/data/mods/public/gui/loadgame/load.js b/binaries/data/mods/public/gui/loadgame/load.js index caa183693d..e617705d53 100644 --- a/binaries/data/mods/public/gui/loadgame/load.js +++ b/binaries/data/mods/public/gui/loadgame/load.js @@ -106,9 +106,8 @@ function selectionChanged() return; Engine.GetGUIObjectByName("savedMapName").caption = translate(metadata.initAttributes.settings.Name); - let mapData = getMapDescriptionAndPreview(metadata.initAttributes.mapType, metadata.initAttributes.map, metadata.initAttributes); - setMapPreviewImage("savedInfoPreview", mapData.preview); - + Engine.GetGUIObjectByName("savedInfoPreview").sprite = getMapPreviewImage( + getMapDescriptionAndPreview(metadata.initAttributes.mapType, metadata.initAttributes.map, metadata.initAttributes).preview); Engine.GetGUIObjectByName("savedPlayers").caption = metadata.initAttributes.settings.PlayerData.length - 1; Engine.GetGUIObjectByName("savedPlayedTime").caption = timeToString(metadata.gui.timeElapsed ? metadata.gui.timeElapsed : 0); Engine.GetGUIObjectByName("savedMapType").caption = translateMapType(metadata.initAttributes.mapType); diff --git a/binaries/data/mods/public/gui/lobby/lobby.js b/binaries/data/mods/public/gui/lobby/lobby.js index b10ab6fe5f..ccec2fcdbe 100644 --- a/binaries/data/mods/public/gui/lobby/lobby.js +++ b/binaries/data/mods/public/gui/lobby/lobby.js @@ -1157,9 +1157,8 @@ function updateGameSelection() Engine.GetGUIObjectByName("sgMapType").caption = g_MapTypes.Title[mapTypeIdx] || ""; let mapData = getMapDescriptionAndPreview(game.mapType, game.mapName); + Engine.GetGUIObjectByName("sgMapPreview").sprite = getMapPreviewImage(mapData.preview); Engine.GetGUIObjectByName("sgMapDescription").caption = mapData.description; - - setMapPreviewImage("sgMapPreview", mapData.preview); } function selectedGame() diff --git a/binaries/data/mods/public/gui/replaymenu/replay_menu.js b/binaries/data/mods/public/gui/replaymenu/replay_menu.js index fe891b2615..7c9086838c 100644 --- a/binaries/data/mods/public/gui/replaymenu/replay_menu.js +++ b/binaries/data/mods/public/gui/replaymenu/replay_menu.js @@ -281,15 +281,13 @@ function displayReplayDetails() Engine.GetGUIObjectByName("showSpoiler").checked && metadata && metadata.playerStates && - metadata.playerStates.map(pState => pState.state) - ); + metadata.playerStates.map(pState => pState.state)); let mapData = getMapDescriptionAndPreview(replay.attribs.settings.mapType, replay.attribs.map, replay.attribs); + Engine.GetGUIObjectByName("sgMapPreview").sprite = getMapPreviewImage(mapData.preview); Engine.GetGUIObjectByName("sgMapDescription").caption = mapData.description; Engine.GetGUIObjectByName("summaryButton").hidden = !Engine.HasReplayMetadata(replay.directory); - - setMapPreviewImage("sgMapPreview", mapData.preview); } /**