0ad/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js
phosit 36b9fd7b42 Return a promise from setButtonCaptionsAndVisibility
Now It's not hardcoded what is done if a button is pressed. The caller
can decide that.
Previously one had to call this function and then overwrite the on Press
attribute of each button.

Rename "Visibitily" to "Visibility" in setButtonCaptionsAndVisibility.
2024-10-17 20:47:54 +02:00

77 lines
2.3 KiB
JavaScript

/**
* @class TimedConfirmation
* This class displays confirmation box, which will be closed after defined time in miliseconds.
*/
class TimedConfirmation
{
/**
* @param {Object} data
* @param {Number} data.width - The width of the confirmation box
* @param {Number} data.height - The height of the confirmation box
* @param {String} data.message - The message to be displayed with parameter for time which will be displayed in seconds
* @param {String} data.timeParameter - The string used in 'message' for time, e.g 'time' with %(time)s in message
* @param {Number} data.timeout - The time in miliseconds after which confirmation box closes itself
* @param {String} data.title - The string displayed in header
* @param {String|undefined} data.buttonCaptions - The captions used for buttons (if not defined, defaults to 'OK')
* @param {String|undefined} data.font - The used font
*/
constructor(data)
{
this.messageObject = Engine.GetGUIObjectByName("tmcText");
this.panel = Engine.GetGUIObjectByName("tmcMain");
this.panel.onTick = this.onTick.bind(this);
this.setup(data);
}
async setup(data)
{
Engine.GetGUIObjectByName("tmcTitleBar").caption = data.title;
this.timeout = +data.timeout + Date.now();
this.message = data.message;
this.timeParameter = data.timeParameter;
if (data.font)
this.messageObject.font = data.font;
this.updateDisplayedTimer(data.timeout);
const cancelHotkey = Engine.GetGUIObjectByName("tmcCancelHotkey");
cancelHotkey.onPress = Engine.PopGuiPage;
const lRDiff = data.width / 2;
const uDDiff = data.height / 2;
this.panel.size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff;
const captions = data.buttonCaptions || [translate("OK")];
const button = [];
const closePromise =
setButtonCaptionsAndVisibility(button, captions, cancelHotkey, "tmcButton");
distributeButtonsHorizontally(button, captions);
Engine.PopGuiPage(await closePromise);
}
onTick()
{
const remaining = this.timeout - Date.now();
if (remaining < 1)
Engine.GetGUIObjectByName("tmcButton1").onPress();
this.updateDisplayedTimer(remaining);
}
updateDisplayedTimer(time)
{
this.messageObject.caption = sprintf(
this.message,
{ [this.timeParameter]: Math.ceil(time / 1000) }
);
}
}
function init(data)
{
new TimedConfirmation(data);
}