From 660fdfac4444d5f69b0a979216728b770d92d2c1 Mon Sep 17 00:00:00 2001 From: phosit Date: Fri, 10 May 2024 13:16:52 +0000 Subject: [PATCH] Use pageLoop for structree and civinfo Deduplicate code in four places. Comments by: @elexis Differential Revision: https://code.wildfiregames.com/D5261 This was SVN commit r28085. --- .../public/gui/common/functions_utility.js | 22 ++++++++++++++ .../Panels/Buttons/CivInfoButton.js | 17 +---------- .../mods/public/gui/pregame/MainMenuItems.js | 16 ++-------- .../gui/reference/civinfo/CivInfoPage.js | 14 +++++++-- .../reference/common/Buttons/CivInfoButton.js | 7 ++++- .../common/Buttons/StructreeButton.js | 7 ++++- .../gui/reference/structree/StructreePage.js | 7 ++++- .../public/gui/session/top_panel/CivIcon.js | 30 +++++++------------ 8 files changed, 66 insertions(+), 54 deletions(-) diff --git a/binaries/data/mods/public/gui/common/functions_utility.js b/binaries/data/mods/public/gui/common/functions_utility.js index 60e39210c0..f3c42fb211 100644 --- a/binaries/data/mods/public/gui/common/functions_utility.js +++ b/binaries/data/mods/public/gui/common/functions_utility.js @@ -282,3 +282,25 @@ function getBuildString() "revision": Engine.GetBuildRevision() }); } + +/** + * Opens a page. If that page completes with an object with a @a nextPage + * property that page is opened with the @a args property of that object. + * That continues untill there is no @a nextPage property in the completion + * value. If there is no @a nextPage in the completion value the + * @a continuation is called with the completion value. + * @param {String} page - The page first opened. + * @param args - passed to the first page opened. + * @param continuation {function | undefined} - Completion callback, called when + * there is no @a nextPage property in the completion value. + */ +function pageLoop(page, args, continuation) +{ + (function recursiveFunction(completionValue) + { + if (completionValue?.nextPage != null) + Engine.PushGuiPage(completionValue.nextPage, completionValue.args, recursiveFunction); + else + continuation?.(completionValue); + })({ "nextPage": page, "args": args }); +} diff --git a/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/Panels/Buttons/CivInfoButton.js b/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/Panels/Buttons/CivInfoButton.js index 002d15a785..fda58c1983 100644 --- a/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/Panels/Buttons/CivInfoButton.js +++ b/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/Panels/Buttons/CivInfoButton.js @@ -3,7 +3,6 @@ class CivInfoButton constructor() { this.civInfo = { - "civ": "", "page": "page_civinfo.xml" }; @@ -26,21 +25,7 @@ class CivInfoButton openPage(page) { - Engine.PushGuiPage( - page, - { "civ": this.civInfo.civ }, - this.storeCivInfoPage.bind(this)); - } - - storeCivInfoPage(data) - { - if (data.nextPage) - Engine.PushGuiPage( - data.nextPage, - { "civ": data.civ }, - this.storeCivInfoPage.bind(this)); - else - this.civInfo = data; + pageLoop(page, this.civInfo.args, data => this.civInfo = data); } } diff --git a/binaries/data/mods/public/gui/pregame/MainMenuItems.js b/binaries/data/mods/public/gui/pregame/MainMenuItems.js index c0f33e41ef..8102bbdd57 100644 --- a/binaries/data/mods/public/gui/pregame/MainMenuItems.js +++ b/binaries/data/mods/public/gui/pregame/MainMenuItems.js @@ -36,25 +36,13 @@ var g_MainMenuItems = [ "caption": translate("Structure Tree"), "tooltip": colorizeHotkey(translate("%(hotkey)s: View the structure tree of civilizations featured in 0 A.D."), "structree"), "hotkey": "structree", - "onPress": () => { - let callback = data => { - if (data.nextPage) - Engine.PushGuiPage(data.nextPage, { "civ": data.civ }, callback); - }; - Engine.PushGuiPage("page_structree.xml", {}, callback); - }, + "onPress": pageLoop.bind(null, "page_structree.xml") }, { "caption": translate("Civilization Overview"), "tooltip": colorizeHotkey(translate("%(hotkey)s: Learn about the civilizations featured in 0 A.D."), "civinfo"), "hotkey": "civinfo", - "onPress": () => { - let callback = data => { - if (data.nextPage) - Engine.PushGuiPage(data.nextPage, { "civ": data.civ }, callback); - }; - Engine.PushGuiPage("page_civinfo.xml", {}, callback); - } + "onPress": pageLoop.bind(null, "page_civinfo.xml") }, { "caption": translate("Catafalque Overview"), diff --git a/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js b/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js index 1705cc2d7e..890b1935f8 100644 --- a/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js +++ b/binaries/data/mods/public/gui/reference/civinfo/CivInfoPage.js @@ -17,12 +17,22 @@ class CivInfoPage extends ReferencePage switchToStructreePage() { - Engine.PopGuiPage({ "civ": this.activeCiv, "nextPage": "page_structree.xml" }); + Engine.PopGuiPage({ + "nextPage": "page_structree.xml", + "args": { + "civ": this.activeCiv + } + }); } closePage() { - Engine.PopGuiPage({ "civ": this.activeCiv, "page": "page_civinfo.xml" }); + Engine.PopGuiPage({ + "page": "page_civinfo.xml", + "args": { + "civ": this.activeCiv + } + }); } /** 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 956afd5d24..e4ce45743f 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,12 @@ class CivInfoButton onPress() { - Engine.PopGuiPage({ "civ": this.parentPage.activeCiv, "nextPage": "page_civinfo.xml" }); + Engine.PopGuiPage({ + "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 d7b28b23b0..d8353a0b8b 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,12 @@ class StructreeButton onPress() { - Engine.PopGuiPage({ "civ": this.parentPage.activeCiv, "nextPage": "page_structree.xml" }); + Engine.PopGuiPage({ + "nextPage": "page_structree.xml", + "args": { + "civ": this.parentPage.activeCiv + } + }); } } diff --git a/binaries/data/mods/public/gui/reference/structree/StructreePage.js b/binaries/data/mods/public/gui/reference/structree/StructreePage.js index e19edcbfb2..33ad01d9ea 100644 --- a/binaries/data/mods/public/gui/reference/structree/StructreePage.js +++ b/binaries/data/mods/public/gui/reference/structree/StructreePage.js @@ -39,7 +39,12 @@ class StructreePage extends ReferencePage closePage() { - Engine.PopGuiPage({ "civ": this.activeCiv, "page": "page_structree.xml" }); + Engine.PopGuiPage({ + "page": "page_structree.xml", + "args": { + "civ": this.activeCiv + } + }); } selectCiv(civCode) diff --git a/binaries/data/mods/public/gui/session/top_panel/CivIcon.js b/binaries/data/mods/public/gui/session/top_panel/CivIcon.js index a58734de0b..f207d50a6c 100644 --- a/binaries/data/mods/public/gui/session/top_panel/CivIcon.js +++ b/binaries/data/mods/public/gui/session/top_panel/CivIcon.js @@ -7,8 +7,10 @@ class CivIcon constructor(playerViewControl) { this.dialogSelection = { - "civ": "", - "page": "page_structree.xml" + "page": "page_structree.xml", + "args": { + "civ": undefined + } }; this.civIcon = Engine.GetGUIObjectByName("civIcon"); @@ -32,30 +34,20 @@ class CivIcon closeOpenDialogs(); g_PauseControl.implicitPause(); - Engine.PushGuiPage( + pageLoop( page, { // If an Observer triggers `openPage()` via hotkey, g_ViewedPlayer could be -1 or 0 // (depending on whether they're "viewing" no-one or gaia respectively) - "civ": this.dialogSelection.civ || g_Players[Math.max(g_ViewedPlayer, 1)].civ, + "civ": this.dialogSelection.args.civ ?? g_Players[Math.max(g_ViewedPlayer, 1)].civ // TODO add info about researched techs and unlocked entities }, - this.storePageSelection.bind(this)); - } - - storePageSelection(data) - { - if (data.nextPage) - Engine.PushGuiPage( - data.nextPage, - { "civ": data.civ }, - this.storePageSelection.bind(this)); - else - { - this.dialogSelection = data; - resumeGame(); - } + data => + { + this.dialogSelection = data; + resumeGame(); + }); } rebuild()