0ad/binaries/data/mods/mod/gui/msgbox/msgbox.js
elexis 86c151ebaa PushGuiPage support for passing a function instead of a function name.
Allows coding the GUI without global functions which break
prototype-oriented coding, refs #5322, fixing the concern in 4b1297b328.

Supports stacked message boxes and removes the according workaround.
Change structree / civinfo switch-dialog code from 760a47335d to perform
the callback for page that actually registered the callback.
Ensure the parent that the callbackhandler is always called if the page
is closed.
Merge PopGuiPage and PopGuiPageCB following that choice, incidentally
leaving cleaner code.

Differential Revision: https://code.wildfiregames.com/D1684
Comments by: Yves, Vladislav, wraitii, leper
This was SVN commit r22676.
2019-08-16 18:46:04 +00:00

60 lines
1.6 KiB
JavaScript

/**
* Currently limited to at most 3 buttons per message box.
* The convention is to have "cancel" appear first.
*/
function init(data)
{
// Set title
Engine.GetGUIObjectByName("mbTitleBar").caption = data.title;
// Set subject
let mbTextObj = Engine.GetGUIObjectByName("mbText");
mbTextObj.caption = data.message;
if (data.font)
mbTextObj.font = data.font;
// Default behaviour
let mbCancelHotkey = Engine.GetGUIObjectByName("mbCancelHotkey");
mbCancelHotkey.onPress = Engine.PopGuiPage;
// Calculate size
let mbLRDiff = data.width / 2;
let mbUDDiff = data.height / 2;
Engine.GetGUIObjectByName("mbMain").size = "50%-" + mbLRDiff + " 50%-" + mbUDDiff + " 50%+" + mbLRDiff + " 50%+" + mbUDDiff;
let captions = data.buttonCaptions || [translate("OK")];
// Set button captions and visibility
let mbButton = [];
captions.forEach((caption, i) => {
mbButton[i] = Engine.GetGUIObjectByName("mbButton" + (i + 1));
mbButton[i].caption = caption;
mbButton[i].hidden = false;
mbButton[i].onPress = () => {
Engine.PopGuiPage(i);
};
// Convention: Cancel is the first button
if (i == 0)
mbCancelHotkey.onPress = mbButton[i].onPress;
});
// Distribute buttons horizontally
let y1 = "100%-46";
let y2 = "100%-18";
switch (captions.length)
{
case 1:
mbButton[0].size = "18 " + y1 + " 100%-18 " + y2;
break;
case 2:
mbButton[0].size = "18 " + y1 + " 50%-5 " + y2;
mbButton[1].size = "50%+5 " + y1 + " 100%-18 " + y2;
break;
case 3:
mbButton[0].size = "18 " + y1 + " 33%-5 " + y2;
mbButton[1].size = "33%+5 " + y1 + " 66%-5 " + y2;
mbButton[2].size = "66%+5 " + y1 + " 100%-18 " + y2;
break;
}
}