From 094a7c22687fa9014d42765b8e1292371ffec227 Mon Sep 17 00:00:00 2001 From: phosit Date: Sat, 21 Sep 2024 20:24:36 +0200 Subject: [PATCH] Return a promise from most page-inits Adopt the new interface by all pages which close themself. (Not thous using `Engine.SwitchGuiPage`.) --- .../mods/mod/gui/colormixer/colormixer.js | 14 ++- .../incompatible_mods/incompatible_mods.js | 8 +- .../incompatible_mods/incompatible_mods.xml | 1 - binaries/data/mods/mod/gui/modio/modio.js | 42 ++++----- binaries/data/mods/mod/gui/modio/modio.xml | 8 +- .../data/mods/mod/gui/modmod/help/help.js | 3 + .../data/mods/mod/gui/modmod/help/help.xml | 3 +- .../mods/mod/gui/termsdialog/termsdialog.js | 19 ++-- .../mods/mod/gui/termsdialog/termsdialog.xml | 4 +- .../timedconfirmation/timedconfirmation.js | 24 +++-- .../campaigns/new_modal/NewCampaignModal.js | 8 +- .../data/mods/public/gui/credits/credits.js | 4 + .../data/mods/public/gui/credits/credits.xml | 5 +- .../public/gui/gamesetup_mp/gamesetup_mp.js | 87 ++++++++++++------- .../public/gui/gamesetup_mp/gamesetup_mp.xml | 4 +- .../mods/public/gui/hotkeys/HotkeysPage.js | 8 +- .../public/gui/loadgame/SavegameLoader.js | 7 +- .../mods/public/gui/loadgame/SavegamePage.js | 12 +-- .../public/gui/loadgame/SavegameWriter.js | 5 +- .../mods/public/gui/lobby/LobbyHandler.js | 4 +- .../gui/lobby/LobbyPage/Buttons/QuitButton.js | 5 +- .../public/gui/lobby/LobbyPage/LobbyPage.js | 5 +- binaries/data/mods/public/gui/lobby/lobby.js | 11 ++- .../data/mods/public/gui/locale/locale.js | 7 +- .../data/mods/public/gui/locale/locale.xml | 3 +- .../gui/locale_advanced/locale_advanced.js | 15 ++-- .../gui/locale_advanced/locale_advanced.xml | 4 +- .../data/mods/public/gui/manual/manual.js | 4 + .../data/mods/public/gui/manual/manual.xml | 3 +- .../gui/maps/mapbrowser/MapBrowserPage.js | 2 +- .../data/mods/public/gui/options/options.js | 35 ++++---- .../data/mods/public/gui/options/options.xml | 3 +- .../gui/prelobby/common/feedback/feedback.js | 4 +- .../gui/prelobby/common/feedback/feedback.xml | 1 - .../public/gui/prelobby/entrance/entrance.js | 9 +- .../public/gui/prelobby/entrance/entrance.xml | 1 - .../mods/public/gui/prelobby/login/login.js | 2 + .../public/gui/prelobby/register/register.js | 8 +- .../reference/catafalque/CatafalquePage.js | 6 +- .../gui/reference/catafalque/catafalque.js | 2 +- .../gui/reference/civinfo/CivInfoPage.js | 8 +- .../public/gui/reference/civinfo/civinfo.js | 8 +- .../reference/common/Buttons/CivInfoButton.js | 2 +- .../common/Buttons/StructreeButton.js | 2 +- .../gui/reference/common/ReferencePage.js | 3 +- .../gui/reference/structree/StructreePage.js | 6 +- .../gui/reference/structree/structree.js | 8 +- .../public/gui/reference/tips/TipsPage.js | 5 +- .../mods/public/gui/reference/tips/tips.js | 4 +- .../public/gui/reference/viewer/ViewerPage.js | 6 +- .../public/gui/reference/viewer/viewer.js | 3 +- .../gui/session/campaigns/CampaignSession.js | 8 +- .../data/mods/public/gui/session/session.js | 10 ++- .../public/gui/splashscreen/splashscreen.js | 10 +-- .../public/gui/splashscreen/splashscreen.xml | 1 - .../data/mods/public/gui/summary/summary.js | 36 +++++--- .../data/mods/public/gui/summary/summary.xml | 13 +-- 57 files changed, 287 insertions(+), 246 deletions(-) diff --git a/binaries/data/mods/mod/gui/colormixer/colormixer.js b/binaries/data/mods/mod/gui/colormixer/colormixer.js index 71abcdd8fe..a98d4f8b78 100644 --- a/binaries/data/mods/mod/gui/colormixer/colormixer.js +++ b/binaries/data/mods/mod/gui/colormixer/colormixer.js @@ -4,10 +4,7 @@ */ class ColorMixer { - /** - * @param {String} color - initial color as RGB string e.g. 100 0 200 - */ - constructor(color) + constructor() { this.panel = Engine.GetGUIObjectByName("main"); this.colorDisplay = Engine.GetGUIObjectByName("colorDisplay"); @@ -15,10 +12,11 @@ class ColorMixer this.color = [0, 0, 0]; this.sliders = []; this.valuesText = []; - - this.run(color); } + /** + * @param {String} color - initial color as RGB string e.g. 100 0 200 + */ async run(color) { Engine.GetGUIObjectByName("titleBar").caption = translate("Color"); @@ -64,7 +62,7 @@ class ColorMixer // Update return color on cancel to prevent malformed values from initial input. color = this.color.join(" "); - Engine.PopGuiPage(await closePromise === 0 ? color : this.color.join(" ")); + return await closePromise === 0 ? color : this.color.join(" "); } updateFromSlider(index) @@ -85,5 +83,5 @@ ColorMixer.prototype.captions = [translate("Cancel"), translate("Save")]; function init(color) { - new ColorMixer(color); + return new ColorMixer().run(color); } diff --git a/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.js b/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.js index 12c71f70f7..4df0f388d0 100644 --- a/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.js +++ b/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.js @@ -3,9 +3,7 @@ var g_IncompatibleModsFile = "gui/incompatible_mods/incompatible_mods.txt"; function init(data) { Engine.GetGUIObjectByName("mainText").caption = Engine.TranslateLines(Engine.ReadFile(g_IncompatibleModsFile)); -} - -function closePage() -{ - Engine.PopGuiPage(); + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("btnClose").onPress = closePageCallback; + }); } diff --git a/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.xml b/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.xml index 5c8c10abd1..bb017ddeee 100644 --- a/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.xml +++ b/binaries/data/mods/mod/gui/incompatible_mods/incompatible_mods.xml @@ -17,7 +17,6 @@ Ok - closePage(); diff --git a/binaries/data/mods/mod/gui/modio/modio.js b/binaries/data/mods/mod/gui/modio/modio.js index c0b6563136..43b3bdd898 100644 --- a/binaries/data/mods/mod/gui/modio/modio.js +++ b/binaries/data/mods/mod/gui/modio/modio.js @@ -27,10 +27,10 @@ var g_ModIOState = { /** * Finished status indicators */ - "ready": progressData => { + "ready": (progressData, closePageCallback) => { // GameID acquired, ready to fetch mod list if (!g_RequestCancelled) - updateModList(); + updateModList(closePageCallback); }, "listed": progressData => { // List of available mods acquired @@ -65,7 +65,7 @@ var g_ModIOState = { /** * Error/Failure status indicators. */ - "failed_gameid": async(progressData) => { + "failed_gameid": async(progressData, closePageCallback) => { // Game ID couldn't be retrieved const promise = showErrorMessageBox( sprintf(translateWithContext("mod.io error message", "Game ID could not be retrieved.\n\n%(technicalDetails)s"), { @@ -76,11 +76,11 @@ var g_ModIOState = { if (!promise) return; if (await promise === 0) - closePage(); + closePageCallback(); else init(); }, - "failed_listing": async(progressData) => { + "failed_listing": async(progressData, closePageCallback) => { // Mod list couldn't be retrieved const promise = showErrorMessageBox( sprintf(translateWithContext("mod.io error message", "Mod List could not be retrieved.\n\n%(technicalDetails)s"), { @@ -91,9 +91,9 @@ var g_ModIOState = { if (!promise) return; if (await promise === 0) - cancelModListUpdate(); + cancelModListUpdate(closePageCallback); else - updateModList(); + updateModList(closePageCallback); }, "failed_downloading": async(progressData) => { // File couldn't be retrieved @@ -131,7 +131,7 @@ var g_ModIOState = { } }; -async function init(data) +function init(data) { const promise = progressDialog( translate("Initializing mod.io interface."), @@ -142,11 +142,16 @@ async function init(data) g_Failure = false; Engine.ModIoStartGetGameId(); - await promise; - closePage(); + return Promise.race([ + promise, + new Promise(closePageCallback => { + Engine.GetGUIObjectByName("backButton").onPress = closePageCallback; + Engine.GetGUIObjectByName("modio").onTick = onTick.bind(null, closePageCallback); + }) + ]); } -function onTick() +function onTick(closePageCallback) { let progressData = Engine.ModIoGetDownloadProgress(); @@ -157,7 +162,7 @@ function onTick() return; } - handler(progressData); + handler(progressData, closePageCallback); if (!progressData.status.startsWith("failed")) Engine.ModIoAdvanceRequest(); } @@ -230,13 +235,13 @@ function showModDescription() Engine.GetGUIObjectByName("modError").caption = isSelected && isInvalid ? sprintf(translate("Invalid mod: %(error)s"), {"error": g_ModsAvailableOnline[selected].error }) : ""; } -function cancelModListUpdate() +function cancelModListUpdate(closePageCallback) { cancelRequest(); if (!g_ModsAvailableOnline.length) { - closePage(); + closePageCallback(); return; } @@ -244,7 +249,7 @@ function cancelModListUpdate() Engine.GetGUIObjectByName('refreshButton').enabled = true; } -async function updateModList() +async function updateModList(closePageCallback) { clearModList(); Engine.GetGUIObjectByName("refreshButton").enabled = false; @@ -260,7 +265,7 @@ async function updateModList() Engine.ModIoStartListMods(); await promise; - cancelModListUpdate(); + cancelModListUpdate(closePageCallback); } async function downloadMod() @@ -296,11 +301,6 @@ function cancelRequest() hideDialog(); } -function closePage() -{ - Engine.PopGuiPage(); -} - function showErrorMessageBox(caption, title, buttonCaptions) { if (g_Failure) diff --git a/binaries/data/mods/mod/gui/modio/modio.xml b/binaries/data/mods/mod/gui/modio/modio.xml index 16808e8bd9..f87d918f57 100644 --- a/binaries/data/mods/mod/gui/modio/modio.xml +++ b/binaries/data/mods/mod/gui/modio/modio.xml @@ -8,11 +8,6 @@ - - - onTick(); - - mod.io Mods @@ -84,9 +79,8 @@ - + Back - closePage(); diff --git a/binaries/data/mods/mod/gui/modmod/help/help.js b/binaries/data/mods/mod/gui/modmod/help/help.js index 00d97c89eb..513aee2136 100644 --- a/binaries/data/mods/mod/gui/modmod/help/help.js +++ b/binaries/data/mods/mod/gui/modmod/help/help.js @@ -1,4 +1,7 @@ function init(data) { Engine.GetGUIObjectByName("mainText").caption = Engine.TranslateLines(Engine.ReadFile("gui/modmod/help/help.txt")); + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("closeButton").onPress = closePageCallback; + }); } diff --git a/binaries/data/mods/mod/gui/modmod/help/help.xml b/binaries/data/mods/mod/gui/modmod/help/help.xml index 99566c19db..40d6458e06 100644 --- a/binaries/data/mods/mod/gui/modmod/help/help.xml +++ b/binaries/data/mods/mod/gui/modmod/help/help.xml @@ -17,9 +17,8 @@ - + Close - Engine.PopGuiPage(); Modding Guide diff --git a/binaries/data/mods/mod/gui/termsdialog/termsdialog.js b/binaries/data/mods/mod/gui/termsdialog/termsdialog.js index 9c5ed8d335..873e583835 100644 --- a/binaries/data/mods/mod/gui/termsdialog/termsdialog.js +++ b/binaries/data/mods/mod/gui/termsdialog/termsdialog.js @@ -11,7 +11,7 @@ var g_TermsPage; var g_TermsFile; var g_TermsSprintf; -function init(data) +async function init(data) { g_TermsPage = data.page; g_TermsFile = data.file; @@ -20,6 +20,15 @@ function init(data) Engine.GetGUIObjectByName("title").caption = data.title; initURLButtons(data.termsURL, data.urlButtons); initLanguageSelection(); + + const accepted = await new Promise(resolve => { + Engine.GetGUIObjectByName("cancelButton").onPress = resolve.bind(null, false); + Engine.GetGUIObjectByName("connectButton").onPress = resolve.bind(null, true); + }); + return { + "page": g_TermsPage, + "accepted": accepted + }; } function initURLButtons(termsURL, urlButtons) @@ -82,11 +91,3 @@ function initLanguageSelection() languageDropdown.selected = languageDropdown.list.length - 1; } - -function closeTerms(accepted) -{ - Engine.PopGuiPage({ - "page": g_TermsPage, - "accepted": accepted - }); -} diff --git a/binaries/data/mods/mod/gui/termsdialog/termsdialog.xml b/binaries/data/mods/mod/gui/termsdialog/termsdialog.xml index b291d270dd..02b9c14aa1 100644 --- a/binaries/data/mods/mod/gui/termsdialog/termsdialog.xml +++ b/binaries/data/mods/mod/gui/termsdialog/termsdialog.xml @@ -29,14 +29,12 @@ - + Close - - Engine.PopGuiPage(); - diff --git a/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.js b/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.js index 3a73993658..e92cc2f180 100644 --- a/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.js +++ b/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.js @@ -24,7 +24,7 @@ var g_IsRejoining = false; var g_PlayerAssignments; // used when rejoining var g_UserRating; -function init(attribs) +async function init(attribs) { g_UserRating = attribs.rating; @@ -45,6 +45,8 @@ function init(attribs) } else if (startJoinFromLobby(attribs.name, attribs.hostJID, "")) switchSetupPage("pageConnecting"); + else if (cancelSetup()) + return; break; } case "host": @@ -66,8 +68,26 @@ function init(attribs) break; } - Engine.GetGUIObjectByName("multiplayerPages").onTick = onTick.bind(null, attribs.loadSavedGame); - Engine.GetGUIObjectByName("continueButton").onPress = confirmSetup.bind(null, attribs.loadSavedGame); + while (true) + { + await new Promise(resolve => { + Engine.GetGUIObjectByName("cancelButton").onPress = resolve; + Engine.GetGUIObjectByName("multiplayerPages").onTick = async() => { + if (await onTick(attribs.loadSavedGame)) + resolve(); + }; + Engine.GetGUIObjectByName("continueButton").onPress = () => { + if (confirmSetup(attribs.loadSavedGame)) + resolve(); + }; + Engine.GetGUIObjectByName("confirmPasswordButton").onPress = () => { + if (confirmPassword()) + resolve(); + }; + }); + if (cancelSetup()) + return; + } } function cancelSetup() @@ -80,10 +100,7 @@ function cancelSetup() // Keep the page open if an attempt to join/host by ip failed if (!g_IsConnecting || (Engine.HasXmppClient() && g_GameType == "client")) - { - Engine.PopGuiPage(); - return; - } + return true; g_IsConnecting = false; Engine.GetGUIObjectByName("hostFeedback").caption = ""; @@ -94,14 +111,19 @@ function cancelSetup() switchSetupPage("pageHost"); else error("cancelSetup: Unrecognised multiplayer game type: " + g_GameType); + return false; } function confirmPassword() { if (Engine.GetGUIObjectByName("pagePassword").hidden) - return; + return false; if (startJoinFromLobby(g_ServerName, g_ServerId, Engine.GetGUIObjectByName("clientPassword").caption)) + { switchSetupPage("pageConnecting"); + return false; + } + return true; } function confirmSetup(loadSavedGame) @@ -113,15 +135,20 @@ function confirmSetup(loadSavedGame) let joinPort = Engine.GetGUIObjectByName("joinPort").caption; if (startJoin(joinPlayerName, joinServer, getValidPort(joinPort))) + { switchSetupPage("pageConnecting"); + return false; + } + return true; } - else if (!Engine.GetGUIObjectByName("pageHost").hidden) + + if (!Engine.GetGUIObjectByName("pageHost").hidden) { let hostServerName = Engine.GetGUIObjectByName("hostServerName").caption; if (!hostServerName) { Engine.GetGUIObjectByName("hostFeedback").caption = translate("Please enter a valid server name."); - return; + return false; } let hostPort = Engine.GetGUIObjectByName("hostPort").caption; @@ -132,7 +159,7 @@ function confirmSetup(loadSavedGame) "min": g_ValidPorts.min, "max": g_ValidPorts.max }); - return; + return false; } let hostPlayerName = Engine.GetGUIObjectByName("hostPlayerName").caption; @@ -141,8 +168,12 @@ function confirmSetup(loadSavedGame) loadSavedGame)) { switchSetupPage("pageConnecting"); + return false; } + return true; } + + return false; } function startConnectionStatus(type) @@ -156,9 +187,9 @@ function startConnectionStatus(type) function onTick(loadSavedGame) { if (!g_IsConnecting) - return; + return false; - pollAndHandleNetworkClient(loadSavedGame); + return pollAndHandleNetworkClient(loadSavedGame); } function getConnectionFailReason(reason) @@ -191,7 +222,7 @@ function pollAndHandleNetworkClient(loadSavedGame) { var message = Engine.PollNetworkClient(); if (!message) - break; + return false; log(sprintf("Net message: %(message)s", { "message": uneval(message) })); // If we're rejoining an active game, we don't want to actually display @@ -205,9 +236,8 @@ function pollAndHandleNetworkClient(loadSavedGame) switch (message.status) { case "failed": - cancelSetup(); reportConnectionFail(message.reason, false); - return; + return true; default: error("Unrecognised netstatus type: " + message.status); @@ -219,12 +249,11 @@ function pollAndHandleNetworkClient(loadSavedGame) switch (message.status) { case "disconnected": - cancelSetup(); if (message.reason === 16) reportHandshakeDisconnect(message.mismatch_type, message.client_mismatch, message.server_mismatch); else reportDisconnect(message.reason, false); - return; + return true; default: error("Unrecognised netstatus type: " + message.status); @@ -244,7 +273,7 @@ function pollAndHandleNetworkClient(loadSavedGame) }); // Process further pending netmessages in the session page - return; + return false; case "chat": break; @@ -265,9 +294,8 @@ function pollAndHandleNetworkClient(loadSavedGame) switch (message.status) { case "failed": - cancelSetup(); reportConnectionFail(message.reason, false); - return; + return true; default: error("Unrecognised netstatus type: " + message.status); @@ -283,16 +311,14 @@ function pollAndHandleNetworkClient(loadSavedGame) break; case "authenticated": - handleAuthenticated(message, loadSavedGame); - return; + return handleAuthenticated(message, loadSavedGame); case "disconnected": - cancelSetup(); if (message.reason === 16) reportHandshakeDisconnect(message.mismatch_type, message.client_mismatch_component, message.server_mismatch_component); else reportDisconnect(message.reason, false); - return; + return true; default: error("Unrecognised netstatus type: " + message.status); @@ -318,7 +344,7 @@ async function handleAuthenticated(message, loadSavedGame) Engine.GetGUIObjectByName("connectionStatus").caption = translate("Game has already started, rejoining..."); g_IsRejoining = true; - return; // we'll process the game setup messages in the next tick + return false; // we'll process the game setup messages in the next tick } g_IsConnecting = false; @@ -328,7 +354,7 @@ async function handleAuthenticated(message, loadSavedGame) { Engine.DisconnectNetworkGame(); cancelSetup(); - return; + return true; } Engine.SwitchGuiPage("page_gamesetup.xml", { @@ -336,7 +362,7 @@ async function handleAuthenticated(message, loadSavedGame) "serverName": g_ServerName, "hasPassword": g_ServerHasPassword }); - return; // don't process any more messages - leave them for the game GUI loop + return false; // don't process any more messages - leave them for the game GUI loop } function switchSetupPage(newPage) @@ -385,7 +411,6 @@ function startHost(playername, servername, port, password, loadSavedGame) if (Engine.HasXmppClient() && Engine.GetGameList().some(game => game.name == servername)) { - cancelSetup(); hostFeedback.caption = translate("Game name already in use."); return false; } @@ -397,7 +422,6 @@ function startHost(playername, servername, port, password, loadSavedGame) } catch (e) { - cancelSetup(); messageBox( 400, 200, sprintf(translate("Cannot host game: %(message)s."), { "message": e.message }), @@ -426,7 +450,6 @@ function startJoin(playername, ip, port) } catch (e) { - cancelSetup(); messageBox( 400, 200, sprintf(translate("Cannot join game: %(message)s."), { "message": e.message }), @@ -456,7 +479,6 @@ function startJoinFromLobby(playername, hostJID, password) { if (!Engine.HasXmppClient()) { - cancelSetup(); messageBox( 400, 200, sprintf("You cannot join a lobby game without logging in to the lobby."), @@ -471,7 +493,6 @@ function startJoinFromLobby(playername, hostJID, password) } catch (e) { - cancelSetup(); messageBox( 400, 200, sprintf(translate("Cannot join game: %(message)s."), { "message": e.message }), diff --git a/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.xml b/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.xml index 201491646d..699d124663 100644 --- a/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.xml +++ b/binaries/data/mods/public/gui/gamesetup_mp/gamesetup_mp.xml @@ -123,9 +123,8 @@ Continue - + Cancel - cancelSetup(); Confirm - confirmPassword(); diff --git a/binaries/data/mods/public/gui/hotkeys/HotkeysPage.js b/binaries/data/mods/public/gui/hotkeys/HotkeysPage.js index ed53e728d8..03270cab26 100644 --- a/binaries/data/mods/public/gui/hotkeys/HotkeysPage.js +++ b/binaries/data/mods/public/gui/hotkeys/HotkeysPage.js @@ -1,6 +1,6 @@ class HotkeysPage { - constructor(metadata) + constructor(metadata, closePageCallback) { this.metadata = metadata; @@ -23,7 +23,7 @@ class HotkeysPage this.saveButton = Engine.GetGUIObjectByName("hotkeySave"); this.saveButton.enabled = false; - Engine.GetGUIObjectByName("hotkeyClose").onPress = () => Engine.PopGuiPage(); + Engine.GetGUIObjectByName("hotkeyClose").onPress = closePageCallback; Engine.GetGUIObjectByName("hotkeyReset").onPress = () => this.resetUserHotkeys(); this.saveButton.onPress = () => { this.saveUserHotkeys(); @@ -181,7 +181,9 @@ class HotkeysPage function init() { - let hotkeyPage = new HotkeysPage(new HotkeyMetadata()); + return new Promise(closePageCallback => { + new HotkeysPage(new HotkeyMetadata(), closePageCallback); + }); } HotkeysPage.prototype.UnavailableTooltipString = markForTranslation("No tooltip available."); diff --git a/binaries/data/mods/public/gui/loadgame/SavegameLoader.js b/binaries/data/mods/public/gui/loadgame/SavegameLoader.js index 4fc9ab4734..30f3617e75 100644 --- a/binaries/data/mods/public/gui/loadgame/SavegameLoader.js +++ b/binaries/data/mods/public/gui/loadgame/SavegameLoader.js @@ -3,8 +3,9 @@ */ class SavegameLoader { - constructor() + constructor(closePageCallback) { + this.closePageCallback = closePageCallback; this.confirmButton = Engine.GetGUIObjectByName("confirmButton"); this.confirmButton.caption = translate("Load"); this.confirmButton.enabled = false; @@ -27,7 +28,7 @@ class SavegameLoader if (sameEngineVersion && sameMods) { - Engine.PopGuiPage(gameId); + this.closePageCallback(gameId); return; } @@ -60,6 +61,6 @@ class SavegameLoader translate("Warning"), [translate("No"), translate("Yes")]); if (buttonIndex === 1) - Engine.PopGuiPage(gameId); + this.closePageCallback(gameId); } } diff --git a/binaries/data/mods/public/gui/loadgame/SavegamePage.js b/binaries/data/mods/public/gui/loadgame/SavegamePage.js index bae0bfecef..74d260be93 100644 --- a/binaries/data/mods/public/gui/loadgame/SavegamePage.js +++ b/binaries/data/mods/public/gui/loadgame/SavegamePage.js @@ -4,7 +4,7 @@ */ class SavegamePage { - constructor(data) + constructor(data, closePageCallback) { this.savegameList = new SavegameList(data && data.campaignRun || null); @@ -18,7 +18,7 @@ class SavegamePage const savePage = !!data?.savedGameData; if (savePage) { - this.savegameWriter = new SavegameWriter(data.savedGameData); + this.savegameWriter = new SavegameWriter(closePageCallback, data.savedGameData); this.savegameList.registerSelectionChangeHandler(this.savegameWriter); let size = this.savegameList.gameSelection.size; size.bottom -= 24; @@ -26,13 +26,13 @@ class SavegamePage } else { - this.savegameLoader = new SavegameLoader(); + this.savegameLoader = new SavegameLoader(closePageCallback); this.savegameList.registerSelectionChangeHandler(this.savegameLoader); this.savegameList.selectFirst(); } Engine.GetGUIObjectByName("title").caption = savePage ? translate("Save Game") : translate("Load Game"); - Engine.GetGUIObjectByName("cancel").onPress = () => { Engine.PopGuiPage(); }; + Engine.GetGUIObjectByName("cancel").onPress = closePageCallback; } } @@ -40,5 +40,7 @@ var g_SavegamePage; function init(data) { - g_SavegamePage = new SavegamePage(data); + return new Promise(closePageCallback => { + g_SavegamePage = new SavegamePage(data, closePageCallback); + }); } diff --git a/binaries/data/mods/public/gui/loadgame/SavegameWriter.js b/binaries/data/mods/public/gui/loadgame/SavegameWriter.js index a2b358f526..6e0242b270 100644 --- a/binaries/data/mods/public/gui/loadgame/SavegameWriter.js +++ b/binaries/data/mods/public/gui/loadgame/SavegameWriter.js @@ -4,8 +4,9 @@ */ class SavegameWriter { - constructor(savedGameData) + constructor(closePageCallback, savedGameData) { + this.closePageCallback = closePageCallback; this.savedGameData = savedGameData; let saveNew = () => { @@ -58,6 +59,6 @@ class SavegameWriter Engine[gameID ? "SaveGame" : "SaveGamePrefix"](name, desc, this.savedGameData); - Engine.PopGuiPage(); + this.closePageCallback(); } } diff --git a/binaries/data/mods/public/gui/lobby/LobbyHandler.js b/binaries/data/mods/public/gui/lobby/LobbyHandler.js index d99dd19d3a..acb1eb5a44 100644 --- a/binaries/data/mods/public/gui/lobby/LobbyHandler.js +++ b/binaries/data/mods/public/gui/lobby/LobbyHandler.js @@ -3,13 +3,13 @@ */ class LobbyHandler { - constructor(dialog) + constructor(closePageCallback, dialog) { this.xmppMessages = new XmppMessages(); this.profilePage = new ProfilePage(this.xmppMessages); this.leaderboardPage = new LeaderboardPage(this.xmppMessages); - this.lobbyPage = new LobbyPage(dialog, this.xmppMessages, this.leaderboardPage, this.profilePage); + this.lobbyPage = new LobbyPage(closePageCallback, dialog, this.xmppMessages, this.leaderboardPage, this.profilePage); this.xmppMessages.processHistoricMessages(); diff --git a/binaries/data/mods/public/gui/lobby/LobbyPage/Buttons/QuitButton.js b/binaries/data/mods/public/gui/lobby/LobbyPage/Buttons/QuitButton.js index b4e2be3331..097e794eee 100644 --- a/binaries/data/mods/public/gui/lobby/LobbyPage/Buttons/QuitButton.js +++ b/binaries/data/mods/public/gui/lobby/LobbyPage/Buttons/QuitButton.js @@ -3,8 +3,9 @@ */ class QuitButton { - constructor(dialog, leaderboardPage, profilePage) + constructor(closePageCallback, dialog, leaderboardPage, profilePage) { + this.closePageCallback = closePageCallback; let closeDialog = this.closeDialog.bind(this); let returnToMainMenu = this.returnToMainMenu.bind(this); let onPress = dialog ? closeDialog : returnToMainMenu; @@ -29,7 +30,7 @@ class QuitButton closeDialog() { Engine.LobbySetPlayerPresence("playing"); - Engine.PopGuiPage(); + this.closePageCallback(); } returnToMainMenu() diff --git a/binaries/data/mods/public/gui/lobby/LobbyPage/LobbyPage.js b/binaries/data/mods/public/gui/lobby/LobbyPage/LobbyPage.js index 88518f3f63..3d98b3b305 100644 --- a/binaries/data/mods/public/gui/lobby/LobbyPage/LobbyPage.js +++ b/binaries/data/mods/public/gui/lobby/LobbyPage/LobbyPage.js @@ -4,7 +4,7 @@ */ class LobbyPage { - constructor(dialog, xmppMessages, leaderboardPage, profilePage) + constructor(closePageCallback, dialog, xmppMessages, leaderboardPage, profilePage) { Engine.ProfileStart("Create LobbyPage"); let mapCache = new MapCache(); @@ -25,7 +25,8 @@ class LobbyPage Engine.GetGUIObjectByName("hostSavedGameButton"), true), "leaderboardButton": new LeaderboardButton(xmppMessages, leaderboardPage), "profileButton": new ProfileButton(xmppMessages, profilePage), - "quitButton": new QuitButton(dialog, leaderboardPage, profilePage) + "quitButton": new QuitButton(closePageCallback, dialog, leaderboardPage, + profilePage) }, "panels": { "chatPanel": new ChatPanel(xmppMessages), diff --git a/binaries/data/mods/public/gui/lobby/lobby.js b/binaries/data/mods/public/gui/lobby/lobby.js index 9d94a89329..472b07521c 100644 --- a/binaries/data/mods/public/gui/lobby/lobby.js +++ b/binaries/data/mods/public/gui/lobby/lobby.js @@ -31,10 +31,13 @@ var g_LobbyHandler; /** * Called after the XmppConnection succeeded and when returning from a game. */ -function init(attribs) +async function init(attribs) { if (g_Settings) - g_LobbyHandler = new LobbyHandler(attribs && attribs.dialog); - else - error("Could not load settings"); + return new Promise(closePageCallback => { + g_LobbyHandler = new LobbyHandler(closePageCallback, attribs && attribs.dialog); + }); + + error("Could not load settings"); + return undefined; } diff --git a/binaries/data/mods/public/gui/locale/locale.js b/binaries/data/mods/public/gui/locale/locale.js index 7e4ba9bbb8..c74206f6d2 100644 --- a/binaries/data/mods/public/gui/locale/locale.js +++ b/binaries/data/mods/public/gui/locale/locale.js @@ -13,11 +13,10 @@ function init() var localeText = Engine.GetGUIObjectByName("localeText"); localeText.caption = currentLocale; -} -function cancelSetup() -{ - Engine.PopGuiPage(); + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("cancelButton").onPress = closePageCallback; + }); } function applySelectedLocale() diff --git a/binaries/data/mods/public/gui/locale/locale.xml b/binaries/data/mods/public/gui/locale/locale.xml index 7846eac65c..0b0083edc4 100644 --- a/binaries/data/mods/public/gui/locale/locale.xml +++ b/binaries/data/mods/public/gui/locale/locale.xml @@ -32,9 +32,8 @@ - + Cancel - cancelSetup(); diff --git a/binaries/data/mods/public/gui/locale_advanced/locale_advanced.js b/binaries/data/mods/public/gui/locale_advanced/locale_advanced.js index 45a8b20115..1e926916b2 100644 --- a/binaries/data/mods/public/gui/locale_advanced/locale_advanced.js +++ b/binaries/data/mods/public/gui/locale_advanced/locale_advanced.js @@ -46,6 +46,13 @@ function init(initData) // fill the script scriptInput.caption = Engine.GetLocaleScript(initData.locale); + + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("cancelButton").onPress = closePageCallback; + Engine.GetGUIObjectByName("acceptButton").onPress = () => { + closePageCallback(applySelectedLocale()); + }; + }); } // TODO: an onChanged event for input boxes would be useful and would allow us to avoid a tick event here. @@ -54,11 +61,6 @@ function onTick() updateResultingLocale(); } -function cancelSetup() -{ - Engine.PopGuiPage(); -} - function updateResultingLocale() { var languageList = Engine.GetGUIObjectByName("languageList"); @@ -113,6 +115,5 @@ function autoDetectLocale() function applySelectedLocale() { - var resultingLocaleText = Engine.GetGUIObjectByName("resultingLocale"); - Engine.PopGuiPage(resultingLocaleText.caption); + return Engine.GetGUIObjectByName("resultingLocale").caption; } diff --git a/binaries/data/mods/public/gui/locale_advanced/locale_advanced.xml b/binaries/data/mods/public/gui/locale_advanced/locale_advanced.xml index 6cc46741d1..707c8ddf75 100644 --- a/binaries/data/mods/public/gui/locale_advanced/locale_advanced.xml +++ b/binaries/data/mods/public/gui/locale_advanced/locale_advanced.xml @@ -61,9 +61,8 @@ - + Cancel - cancelSetup(); @@ -73,7 +72,6 @@ Accept - applySelectedLocale(); diff --git a/binaries/data/mods/public/gui/manual/manual.js b/binaries/data/mods/public/gui/manual/manual.js index 42566d40f1..1b042af7c2 100644 --- a/binaries/data/mods/public/gui/manual/manual.js +++ b/binaries/data/mods/public/gui/manual/manual.js @@ -7,4 +7,8 @@ function init() // Replace anything starting with 'hotkey.' with its hotkey. mainText.caption = text.replace(/hotkey\.([a-z0-9_\.]+)/g, (_, k) => formatHotkeyCombinations(hotkeys[k])); + + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("closeButton").onPress = closePageCallback; + }); } diff --git a/binaries/data/mods/public/gui/manual/manual.xml b/binaries/data/mods/public/gui/manual/manual.xml index bef162eabd..c696b4c033 100644 --- a/binaries/data/mods/public/gui/manual/manual.xml +++ b/binaries/data/mods/public/gui/manual/manual.xml @@ -17,9 +17,8 @@ - + Close - Engine.PopGuiPage(); diff --git a/binaries/data/mods/public/gui/maps/mapbrowser/MapBrowserPage.js b/binaries/data/mods/public/gui/maps/mapbrowser/MapBrowserPage.js index 4265d4e3f6..e08e3046bf 100644 --- a/binaries/data/mods/public/gui/maps/mapbrowser/MapBrowserPage.js +++ b/binaries/data/mods/public/gui/maps/mapbrowser/MapBrowserPage.js @@ -8,7 +8,7 @@ function init() let cache = new MapCache(); let filters = new MapFilters(cache); let browser = new MapBrowser(cache, filters); - browser.registerClosePageHandler(() => Engine.PopGuiPage()); browser.openPage(false); browser.controls.MapFiltering.select("default", "skirmish"); + return new Promise(closePageCallback => { browser.registerClosePageHandler(closePageCallback); }); } diff --git a/binaries/data/mods/public/gui/options/options.js b/binaries/data/mods/public/gui/options/options.js index 5754c717be..93aa71ae3b 100644 --- a/binaries/data/mods/public/gui/options/options.js +++ b/binaries/data/mods/public/gui/options/options.js @@ -209,7 +209,7 @@ var g_OptionType = { } }; -function init(data, hotloadData) +async function init(data, hotloadData) { g_ChangedKeys = hotloadData ? hotloadData.changedKeys : new Set(); g_TabCategorySelected = hotloadData ? hotloadData.tabCategorySelected : 0; @@ -225,6 +225,13 @@ function init(data, hotloadData) g_TabButtonDist, selectPanel, displayOptions); + + while (true) + { + await new Promise(resolve => { Engine.GetGUIObjectByName("closeButton").onPress = resolve; }); + if (await shouldClosePage()) + return g_ChangedKeys; + } } function getHotloadData() @@ -436,20 +443,18 @@ async function saveChanges() } /** - * Close GUI page and inform the parent GUI page which options changed. - **/ -async function closePage() + * Asks the user if this page really should be closed. + */ +async function shouldClosePage() { - if (Engine.ConfigDB_HasChanges("user")) - { - const buttonIndex = await messageBox( - 500, 200, - translate("You have unsaved changes, do you want to close this window?"), - translate("Warning"), - [translate("No"), translate("Yes")]); - if (buttonIndex === 0) - return; - } + if (!Engine.ConfigDB_HasChanges("user")) + return true - Engine.PopGuiPage(g_ChangedKeys); + const buttonIndex = await messageBox( + 500, 200, + translate("You have unsaved changes, do you want to close this window?"), + translate("Warning"), + [translate("No"), translate("Yes")]); + + return buttonIndex === 1; } diff --git a/binaries/data/mods/public/gui/options/options.xml b/binaries/data/mods/public/gui/options/options.xml index 25f96b4990..83a8dbe4ba 100644 --- a/binaries/data/mods/public/gui/options/options.xml +++ b/binaries/data/mods/public/gui/options/options.xml @@ -58,10 +58,9 @@ saveChanges(); - + Close Unsaved changes affect this session only - closePage(); diff --git a/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.js b/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.js index 6028cff9c6..79af54ef87 100644 --- a/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.js +++ b/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.js @@ -37,9 +37,9 @@ function setFeedback(feedbackText) Engine.GetGUIObjectByName("continue").enabled = !feedbackText; } -function cancelButton() +async function cancelButton() { + await new Promise(resolve => { Engine.GetGUIObjectByName("cancel").onPress = resolve; }); if (Engine.HasXmppClient()) Engine.StopXmppClient(); - Engine.PopGuiPage(); } diff --git a/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.xml b/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.xml index 91e2b5fd37..449dee9497 100644 --- a/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.xml +++ b/binaries/data/mods/public/gui/prelobby/common/feedback/feedback.xml @@ -8,7 +8,6 @@ Cancel - cancelButton(); diff --git a/binaries/data/mods/public/gui/prelobby/entrance/entrance.js b/binaries/data/mods/public/gui/prelobby/entrance/entrance.js index 6c702d7fa5..be34802a1c 100644 --- a/binaries/data/mods/public/gui/prelobby/entrance/entrance.js +++ b/binaries/data/mods/public/gui/prelobby/entrance/entrance.js @@ -2,6 +2,10 @@ function init() { if (Engine.ConfigDB_GetValue("user", "lobby.login")) loginButton(); + + return new Promise(closePageCallback => { + Engine.GetGUIObjectByName("cancel").onPress = closePageCallback; + }); } function loginButton() @@ -13,8 +17,3 @@ function registerButton() { Engine.PushGuiPage("page_prelobby_register.xml"); } - -function cancelButton() -{ - Engine.PopGuiPage(); -} diff --git a/binaries/data/mods/public/gui/prelobby/entrance/entrance.xml b/binaries/data/mods/public/gui/prelobby/entrance/entrance.xml index abe103c30c..94c5641497 100644 --- a/binaries/data/mods/public/gui/prelobby/entrance/entrance.xml +++ b/binaries/data/mods/public/gui/prelobby/entrance/entrance.xml @@ -28,7 +28,6 @@ Cancel - cancelButton(); diff --git a/binaries/data/mods/public/gui/prelobby/login/login.js b/binaries/data/mods/public/gui/prelobby/login/login.js index 60f5ceaaf4..32c94b9315 100644 --- a/binaries/data/mods/public/gui/prelobby/login/login.js +++ b/binaries/data/mods/public/gui/prelobby/login/login.js @@ -13,6 +13,8 @@ function init() initRememberPassword(); updateFeedback(); + + return cancelButton(); } function updateFeedback() diff --git a/binaries/data/mods/public/gui/prelobby/register/register.js b/binaries/data/mods/public/gui/prelobby/register/register.js index f4b9facdd5..d56a4ac060 100644 --- a/binaries/data/mods/public/gui/prelobby/register/register.js +++ b/binaries/data/mods/public/gui/prelobby/register/register.js @@ -1,7 +1,5 @@ function init() { - g_LobbyMessages.registered = onRegistered; - Engine.GetGUIObjectByName("continue").caption = translate("Register"); initLobbyTerms(); @@ -9,6 +7,8 @@ function init() initRememberPassword(); updateFeedback(); + + return Promise.race([ onRegistered(), cancelButton() ]); } function updateFeedback() @@ -32,14 +32,14 @@ function continueButton() Engine.ConnectXmppClient(); } -function onRegistered() +async function onRegistered() { + await new Promise(resolve => { g_LobbyMessages.registered = resolve; }); saveCredentials(); setFeedback(translate("Registered")); Engine.StopXmppClient(); - Engine.PopGuiPage(); Engine.PushGuiPage("page_prelobby_login.xml"); } diff --git a/binaries/data/mods/public/gui/reference/catafalque/CatafalquePage.js b/binaries/data/mods/public/gui/reference/catafalque/CatafalquePage.js index e385a37e80..cf6e5701ca 100644 --- a/binaries/data/mods/public/gui/reference/catafalque/CatafalquePage.js +++ b/binaries/data/mods/public/gui/reference/catafalque/CatafalquePage.js @@ -1,8 +1,8 @@ class CatafalquePage extends ReferencePage { - constructor(data) + constructor(closePageCallback) { - super(); + super(closePageCallback); this.Canvas = Engine.GetGUIObjectByName("canvas"); this.Emblems = []; @@ -31,7 +31,7 @@ class CatafalquePage extends ReferencePage closePage() { - Engine.PopGuiPage({ "page": "page_catafalque.xml" }); + this.closePageCallback({ "page": "page_catafalque.xml" }); } } diff --git a/binaries/data/mods/public/gui/reference/catafalque/catafalque.js b/binaries/data/mods/public/gui/reference/catafalque/catafalque.js index 621e70bcb1..d8f823836e 100644 --- a/binaries/data/mods/public/gui/reference/catafalque/catafalque.js +++ b/binaries/data/mods/public/gui/reference/catafalque/catafalque.js @@ -1,4 +1,4 @@ function init(data = {}) { - g_Page = new CatafalquePage(data); + return new Promise(closePageCallback => { g_Page = new CatafalquePage(closePageCallback); }); } diff --git a/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js b/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js index 1ee7f099ba..09fc7e6c1d 100644 --- a/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js +++ b/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js @@ -1,8 +1,8 @@ class CivInfoPage extends ReferencePage { - constructor(data) + constructor(closePageCallback) { - super(); + super(closePageCallback); this.civSelection = new CivSelectDropdown(this.civData); if (!this.civSelection.hasCivs()) @@ -25,7 +25,7 @@ class CivInfoPage extends ReferencePage switchToStructreePage() { - Engine.PopGuiPage({ + this.closePageCallback({ "nextPage": "page_structree.xml", "args": { "civ": this.activeCiv @@ -35,7 +35,7 @@ class CivInfoPage extends ReferencePage closePage() { - Engine.PopGuiPage({ + this.closePageCallback({ "page": "page_civinfo.xml", "args": { "civ": this.activeCiv diff --git a/binaries/data/mods/public/gui/reference/civinfo/civinfo.js b/binaries/data/mods/public/gui/reference/civinfo/civinfo.js index 61c518b065..a4877212bb 100644 --- a/binaries/data/mods/public/gui/reference/civinfo/civinfo.js +++ b/binaries/data/mods/public/gui/reference/civinfo/civinfo.js @@ -1,12 +1,14 @@ /** * Initialize the dropdown containing all the available civs. */ -function init(data = {}) +function init(data) { - g_Page = new CivInfoPage(data); + const promise = new Promise(closePageCallback => { g_Page = new CivInfoPage(closePageCallback); }); - if (data.civ) + if (data?.civ) g_Page.civSelection.selectCiv(data.civ); else g_Page.civSelection.selectFirstCiv(); + + return promise; } diff --git a/binaries/data/mods/public/gui/reference/common/Buttons/CivInfoButton.js b/binaries/data/mods/public/gui/reference/common/Buttons/CivInfoButton.js index e4ce45743f..ec934f62ab 100644 --- a/binaries/data/mods/public/gui/reference/common/Buttons/CivInfoButton.js +++ b/binaries/data/mods/public/gui/reference/common/Buttons/CivInfoButton.js @@ -12,7 +12,7 @@ class CivInfoButton onPress() { - Engine.PopGuiPage({ + this.parentPage.closePageCallback({ "nextPage": "page_civinfo.xml", "args": { "civ": this.parentPage.activeCiv diff --git a/binaries/data/mods/public/gui/reference/common/Buttons/StructreeButton.js b/binaries/data/mods/public/gui/reference/common/Buttons/StructreeButton.js index d8353a0b8b..f195bb7533 100644 --- a/binaries/data/mods/public/gui/reference/common/Buttons/StructreeButton.js +++ b/binaries/data/mods/public/gui/reference/common/Buttons/StructreeButton.js @@ -12,7 +12,7 @@ class StructreeButton onPress() { - Engine.PopGuiPage({ + this.parentPage.closePageCallback({ "nextPage": "page_structree.xml", "args": { "civ": this.parentPage.activeCiv diff --git a/binaries/data/mods/public/gui/reference/common/ReferencePage.js b/binaries/data/mods/public/gui/reference/common/ReferencePage.js index 87843063a0..1ee2b625bd 100644 --- a/binaries/data/mods/public/gui/reference/common/ReferencePage.js +++ b/binaries/data/mods/public/gui/reference/common/ReferencePage.js @@ -3,8 +3,9 @@ */ class ReferencePage { - constructor() + constructor(closePageCallback) { + this.closePageCallback = closePageCallback; this.civData = loadCivData(true, false); this.TemplateLoader = new TemplateLoader(); diff --git a/binaries/data/mods/public/gui/reference/structree/StructreePage.js b/binaries/data/mods/public/gui/reference/structree/StructreePage.js index 88c97dc8b5..9b3f499f99 100644 --- a/binaries/data/mods/public/gui/reference/structree/StructreePage.js +++ b/binaries/data/mods/public/gui/reference/structree/StructreePage.js @@ -5,9 +5,9 @@ */ class StructreePage extends ReferencePage { - constructor(data) + constructor(closePageCallback) { - super(); + super(closePageCallback); this.structureBoxes = []; this.trainerBoxes = []; @@ -36,7 +36,7 @@ class StructreePage extends ReferencePage closePage() { - Engine.PopGuiPage({ + this.closePageCallback({ "page": "page_structree.xml", "args": { "civ": this.activeCiv diff --git a/binaries/data/mods/public/gui/reference/structree/structree.js b/binaries/data/mods/public/gui/reference/structree/structree.js index bf829975f0..a738f7751e 100644 --- a/binaries/data/mods/public/gui/reference/structree/structree.js +++ b/binaries/data/mods/public/gui/reference/structree/structree.js @@ -3,12 +3,14 @@ * * @param {Object} data - Parameters passed from the code that calls this page into existence. */ -function init(data = {}) +function init(data) { - g_Page = new StructreePage(data); + const promise = new Promise(closePageCallback => { g_Page = new StructreePage(closePageCallback); }); - if (data.civ) + if (data?.civ) g_Page.civSelection.selectCiv(data.civ); else g_Page.civSelection.selectFirstCiv(); + + return promise; } diff --git a/binaries/data/mods/public/gui/reference/tips/TipsPage.js b/binaries/data/mods/public/gui/reference/tips/TipsPage.js index 119f9f4c12..ad2185a30e 100644 --- a/binaries/data/mods/public/gui/reference/tips/TipsPage.js +++ b/binaries/data/mods/public/gui/reference/tips/TipsPage.js @@ -1,14 +1,15 @@ class TipsPage { - constructor(initData, hotloadData) + constructor(initData, hotloadData, closePageCallback) { + this.closePageCallback = closePageCallback; this.tipDisplay = new TipDisplay(initData, hotloadData); this.closeButton = new CloseButton(this); } closePage() { - Engine.PopGuiPage("page_tips.xml"); + this.closePageCallback("page_tips.xml"); } } diff --git a/binaries/data/mods/public/gui/reference/tips/tips.js b/binaries/data/mods/public/gui/reference/tips/tips.js index 9b8d5fe832..acc8cb0f60 100644 --- a/binaries/data/mods/public/gui/reference/tips/tips.js +++ b/binaries/data/mods/public/gui/reference/tips/tips.js @@ -2,7 +2,9 @@ var g_TipsPage; function init(initData, hotloadData) { - g_TipsPage = new TipsPage(initData, hotloadData); + return new Promise(closePageCallback => { + g_TipsPage = new TipsPage(initData, hotloadData, closePageCallback); + }); } function getHotloadData() diff --git a/binaries/data/mods/public/gui/reference/viewer/ViewerPage.js b/binaries/data/mods/public/gui/reference/viewer/ViewerPage.js index 26fbc81e26..832e85f8b1 100644 --- a/binaries/data/mods/public/gui/reference/viewer/ViewerPage.js +++ b/binaries/data/mods/public/gui/reference/viewer/ViewerPage.js @@ -3,9 +3,9 @@ */ class ViewerPage extends ReferencePage { - constructor(data) + constructor(closePageCallback) { - super(); + super(closePageCallback); this.currentTemplate = undefined; @@ -140,7 +140,7 @@ class ViewerPage extends ReferencePage closePage() { - Engine.PopGuiPage({ "civ": this.activeCiv, "page": "page_viewer.xml" }); + this.closePageCallback({ "civ": this.activeCiv, "page": "page_viewer.xml" }); } } diff --git a/binaries/data/mods/public/gui/reference/viewer/viewer.js b/binaries/data/mods/public/gui/reference/viewer/viewer.js index ffffbf7cd3..76eb4ad161 100644 --- a/binaries/data/mods/public/gui/reference/viewer/viewer.js +++ b/binaries/data/mods/public/gui/reference/viewer/viewer.js @@ -14,6 +14,7 @@ g_TooltipTextFormats.nameSecondary.font = "sans-bold-16"; */ function init(data) { - g_Page = new ViewerPage(); + const promise = new Promise(closePageCallback => { g_Page = new ViewerPage(closePageCallback); }); g_Page.selectTemplate(data); + return promise; } diff --git a/binaries/data/mods/public/gui/session/campaigns/CampaignSession.js b/binaries/data/mods/public/gui/session/campaigns/CampaignSession.js index c496f273f6..6aba40bc74 100644 --- a/binaries/data/mods/public/gui/session/campaigns/CampaignSession.js +++ b/binaries/data/mods/public/gui/session/campaigns/CampaignSession.js @@ -1,9 +1,9 @@ class CampaignSession { - constructor(data) + constructor(data, closePageCallback) { this.run = new CampaignRun(data.run).load(); - registerPlayersFinishedHandler(this.onFinish.bind(this)); + registerPlayersFinishedHandler(this.onFinish.bind(this, closePageCallback)); this.endGameData = { "won": false, "initData": data, @@ -11,7 +11,7 @@ class CampaignSession }; } - onFinish(players, won) + onFinish(closePageCallback, players, won) { let playerID = Engine.GetPlayerID(); if (players.indexOf(playerID) === -1) @@ -24,7 +24,7 @@ class CampaignSession // Run the endgame script. Engine.PushGuiPage(this.getEndGame(), this.endGameData); - Engine.PopGuiPage(); + closePageCallback(); } getMenu() diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index 6bda6fa19c..9590caeed5 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -250,7 +250,7 @@ function init(initData, hotloadData) { Engine.EndGame(); Engine.SwitchGuiPage("page_pregame.xml"); - return; + return undefined; } // Fallback used by atlas @@ -269,8 +269,10 @@ function init(initData, hotloadData) restoreSavedGameData(initData.savedGUIData); } - if (g_InitAttributes.campaignData) - g_CampaignSession = new CampaignSession(g_InitAttributes.campaignData); + const promise = new Promise(closePageCallback => { + if (g_InitAttributes.campaignData) + g_CampaignSession = new CampaignSession(g_InitAttributes.campaignData, closePageCallback); + }); let mapCache = new MapCache(); g_Cheats = new Cheats(); @@ -331,6 +333,8 @@ function init(initData, hotloadData) onSimulationUpdate(); setTimeout(displayGamestateNotifications, 1000); + + return promise; } function registerPlayersInitHandler(handler) diff --git a/binaries/data/mods/public/gui/splashscreen/splashscreen.js b/binaries/data/mods/public/gui/splashscreen/splashscreen.js index ae56034756..a961474d50 100644 --- a/binaries/data/mods/public/gui/splashscreen/splashscreen.js +++ b/binaries/data/mods/public/gui/splashscreen/splashscreen.js @@ -1,4 +1,4 @@ -function init(data) +async function init(data) { const paragraphObjects = Engine.GetGUIObjectByName("paragraphs").children; @@ -25,12 +25,12 @@ function init(data) } Engine.GetGUIObjectByName("displaySplashScreen").checked = Engine.ConfigDB_GetValue("user", "gui.splashscreen.enable") === "true"; -} -function closePage() -{ + await new Promise(resolve => { + Engine.GetGUIObjectByName("btnOK").onPress = resolve; + }); + Engine.ConfigDB_CreateValue("user", "gui.splashscreen.enable", String(Engine.GetGUIObjectByName("displaySplashScreen").checked)); Engine.ConfigDB_CreateValue("user", "gui.splashscreen.version", Engine.CalculateMD5(Engine.ReadFile("gui/splashscreen/splashscreen.xml"))); Engine.ConfigDB_SaveChanges("user"); - Engine.PopGuiPage(); } diff --git a/binaries/data/mods/public/gui/splashscreen/splashscreen.xml b/binaries/data/mods/public/gui/splashscreen/splashscreen.xml index 213280d88b..8d4c702e70 100644 --- a/binaries/data/mods/public/gui/splashscreen/splashscreen.xml +++ b/binaries/data/mods/public/gui/splashscreen/splashscreen.xml @@ -54,7 +54,6 @@ OK - closePage(); diff --git a/binaries/data/mods/public/gui/summary/summary.js b/binaries/data/mods/public/gui/summary/summary.js index 159aba27ae..02a53839b5 100644 --- a/binaries/data/mods/public/gui/summary/summary.js +++ b/binaries/data/mods/public/gui/summary/summary.js @@ -135,10 +135,22 @@ var g_SelectedChart = { "type": [0, 0] }; -function init(data) +async function init(data) { initSummaryData(data); initGUISummary(); + + while (true) + { + const branchless = await new Promise(resolve => { + Engine.GetGUIObjectByName("continueButton").onPress = resolve.bind(null, true); + Engine.GetGUIObjectByName("summaryHotkey").onPress = resolve.bind(null, true); + Engine.GetGUIObjectByName("cancelHotkey").onPress = resolve.bind(null, false); + }); + + if (branchless || data.gui.isInGame) + return continueButton(data); + } } function initSummaryData(data) @@ -454,30 +466,32 @@ function updatePanelData(panelInfo) updateCountersTeam(teamCounterFn, panelInfo.counters, panelInfo.headings, index); } -function continueButton() +function continueButton(gameData) { let summarySelection = { "panel": g_TabCategorySelected, "charts": g_SelectedChart, "teamCharts": Engine.GetGUIObjectByName("toggleTeamBox").checked }; - if (g_GameData.gui.isInGame) - Engine.PopGuiPage({ + if (gameData.gui.isInGame) + return { "summarySelection": summarySelection - }); - else if (g_GameData.gui.dialog) - Engine.PopGuiPage(); + }; + if (gameData.gui.dialog) + return undefined; else if (Engine.HasXmppClient()) Engine.SwitchGuiPage("page_lobby.xml", { "dialog": false }); - else if (g_GameData.gui.isReplay) + else if (gameData.gui.isReplay) Engine.SwitchGuiPage("page_replaymenu.xml", { - "replaySelectionData": g_GameData.gui.replaySelectionData, + "replaySelectionData": gameData.gui.replaySelectionData, "summarySelection": summarySelection }); - else if (g_GameData.campaignData) - Engine.SwitchGuiPage(g_GameData.nextPage, g_GameData.campaignData); + else if (gameData.campaignData) + Engine.SwitchGuiPage(gameData.nextPage, gameData.campaignData); else Engine.SwitchGuiPage("page_pregame.xml"); + + return undefined; } function startReplay() diff --git a/binaries/data/mods/public/gui/summary/summary.xml b/binaries/data/mods/public/gui/summary/summary.xml index 7609b114a9..adaeca9df6 100644 --- a/binaries/data/mods/public/gui/summary/summary.xml +++ b/binaries/data/mods/public/gui/summary/summary.xml @@ -5,16 +5,8 @@