From 2f1bf9531b1fd517b3dc952828671a2c80c63e4d Mon Sep 17 00:00:00 2001 From: phosit Date: Wed, 23 Apr 2025 19:17:34 +0200 Subject: [PATCH] Remove Engine.SwitchGuiPage from replay menu --- .../public/gui/replaymenu/replay_actions.js | 108 +++++++++++------- .../mods/public/gui/replaymenu/replay_menu.js | 22 +++- .../public/gui/replaymenu/replay_menu.xml | 6 +- 3 files changed, 87 insertions(+), 49 deletions(-) diff --git a/binaries/data/mods/public/gui/replaymenu/replay_actions.js b/binaries/data/mods/public/gui/replaymenu/replay_actions.js index b81dec4d3d..ec39bce146 100644 --- a/binaries/data/mods/public/gui/replaymenu/replay_actions.js +++ b/binaries/data/mods/public/gui/replaymenu/replay_actions.js @@ -37,17 +37,30 @@ function createReplaySelectionData(selectedDirectory) /** * Starts the selected visual replay, or shows an error message in case of incompatibility. */ -function startReplay() +function startReplayHandler() { - var selected = Engine.GetGUIObjectByName("replaySelection").selected; - if (selected == -1) - return; + return new Promise(resolve => + { + const startReplay = () => + { + const selected = Engine.GetGUIObjectByName("replaySelection").selected; + if (selected == -1) + return; - var replay = g_ReplaysFiltered[selected]; - if (isReplayCompatible(replay)) - reallyStartVisualReplay(replay.directory); - else - displayReplayCompatibilityError(replay); + const replay = g_ReplaysFiltered[selected]; + if (!isReplayCompatible(replay)) + { + displayReplayCompatibilityError(replay); + return; + } + + const ret = reallyStartVisualReplay(replay.directory); + if (ret !== undefined) + resolve(ret); + }; + Engine.GetGUIObjectByName("startReplayButton").onPress = startReplay; + Engine.GetGUIObjectByName("replaySelection").onMouseLeftDoubleClickItem = startReplay; + }); } /** @@ -60,20 +73,23 @@ function reallyStartVisualReplay(replayDirectory) if (!Engine.StartVisualReplay(replayDirectory)) { warn('Replay "' + escapeText(Engine.GetReplayDirectoryName(replayDirectory)) + '" not found! Please click on reload cache.'); - return; + return undefined; } - Engine.SwitchGuiPage("page_loading.xml", { - "attribs": Engine.GetReplayAttributes(replayDirectory), - "playerAssignments": { - "local": { - "name": singleplayerName(), - "player": -1 - } - }, - "savedGUIData": "", - "replaySelectionData": createReplaySelectionData(replayDirectory) - }); + return { + "page": "page_loading.xml", + "argument": { + "attribs": Engine.GetReplayAttributes(replayDirectory), + "playerAssignments": { + "local": { + "name": singleplayerName(), + "player": -1 + } + }, + "savedGUIData": "", + "replaySelectionData": createReplaySelectionData(replayDirectory) + } + }; } /** @@ -106,15 +122,26 @@ function displayReplayCompatibilityError(replay) */ function showReplaySummary() { - const selected = Engine.GetGUIObjectByName("replaySelection").selected; - if (selected == -1) - return; + return new Promise(closePageCallback => + { + Engine.GetGUIObjectByName("summaryButton").onPress = () => + { + const selected = Engine.GetGUIObjectByName("replaySelection").selected; + if (selected == -1) + return; - const replay = g_ReplaysFiltered[selected]; - if (isReplayCompatible(replay)) - reallyShowReplaySummary(replay.directory); - else - displayReplayCompatibilityError(replay); + const replay = g_ReplaysFiltered[selected]; + if (!isReplayCompatible(replay)) + { + displayReplayCompatibilityError(replay); + return; + } + + const ret = reallyShowReplaySummary(replay.directory); + if (ret !== undefined) + closePageCallback(ret); + }; + }); } function reallyShowReplaySummary(directory) @@ -125,19 +152,22 @@ function reallyShowReplaySummary(directory) if (!simData) { messageBox(500, 200, translate("No summary data available."), translate("Error")); - return; + return undefined; } - Engine.SwitchGuiPage("page_summary.xml", { - "sim": simData, - "gui": { - "dialog": false, - "isReplay": true, - "replayDirectory": directory, - "replaySelectionData": createReplaySelectionData(directory), - "summarySelection": g_SummarySelection + return { + "page": "page_summary.xml", + "argument": { + "sim": simData, + "gui": { + "dialog": false, + "isReplay": true, + "replayDirectory": directory, + "replaySelectionData": createReplaySelectionData(directory), + "summarySelection": g_SummarySelection + } } - }); + }; } function reloadCache() diff --git a/binaries/data/mods/public/gui/replaymenu/replay_menu.js b/binaries/data/mods/public/gui/replaymenu/replay_menu.js index 62bf1010a3..189f6f0b7d 100644 --- a/binaries/data/mods/public/gui/replaymenu/replay_menu.js +++ b/binaries/data/mods/public/gui/replaymenu/replay_menu.js @@ -58,12 +58,13 @@ var g_MapCache = new MapCache(); /** * Initializes globals, loads replays and displays the list. */ -function init(data) +async function init(data) { if (!g_Settings) { - Engine.SwitchGuiPage("page_pregame.xml"); - return; + return { [Engine.openRequest]: { + "page": "page_pregame.xml" + } }; } g_SummarySelection = data && data.summarySelection; @@ -72,12 +73,23 @@ function init(data) if (!g_Replays) { - Engine.SwitchGuiPage("page_pregame.xml"); - return; + return { [Engine.openRequest]: { + "page": "page_pregame.xml" + } }; } initHotkeyTooltips(); displayReplayList(); + + const closePromise = new Promise(closePageCallback => + { + Engine.GetGUIObjectByName("mainMenu").onPress = closePageCallback.bind(undefined, { + "page": "page_pregame.xml" + }); + }); + + const ret = await Promise.race([ closePromise, startReplayHandler(), showReplaySummary() ]); + return { [Engine.openRequest]: ret }; } /** diff --git a/binaries/data/mods/public/gui/replaymenu/replay_menu.xml b/binaries/data/mods/public/gui/replaymenu/replay_menu.xml index 4b59a6b7fd..14180ecc95 100644 --- a/binaries/data/mods/public/gui/replaymenu/replay_menu.xml +++ b/binaries/data/mods/public/gui/replaymenu/replay_menu.xml @@ -64,7 +64,6 @@ displayReplayDetails(); displayReplayList(); - startReplay(); @@ -224,9 +223,8 @@ - + Main Menu - Engine.SwitchGuiPage("page_pregame.xml"); @@ -245,13 +243,11 @@ Summary - showReplaySummary(); Start Replay - startReplay();