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.
This commit is contained in:
phosit 2024-05-10 13:16:52 +00:00
parent f48e480f7f
commit 660fdfac44
8 changed files with 66 additions and 54 deletions

View File

@ -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 });
}

View File

@ -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);
}
}

View File

@ -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"),

View File

@ -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
}
});
}
/**

View File

@ -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
}
});
}
}

View File

@ -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
}
});
}
}

View File

@ -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)

View File

@ -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()