Differential D4318 Diff 19618 ps/trunk/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js
/** | /** | ||||
* Currently limited to at most 3 buttons per message box. | * @class TimedConfirmation | ||||
* The convention is to have "cancel" appear first. | * This class displays confirmation box, which will be closed after defined time in miliseconds. | ||||
*/ | */ | ||||
function init(data) | class TimedConfirmation | ||||
{ | { | ||||
Engine.GetGUIObjectByName("tmcTitleBar").caption = data.title; | /** | ||||
* @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); | |||||
} | |||||
const textObj = Engine.GetGUIObjectByName("tmcText"); | setup(data) | ||||
textObj.caption = data.message; | { | ||||
Engine.GetGUIObjectByName("tmcTitleBar").caption = data.title; | |||||
updateDisplayedTimer(data.timeout); | this.timeout = +data.timeout + Date.now(); | ||||
this.message = data.message; | |||||
this.timeParameter = data.timeParameter; | |||||
Engine.GetGUIObjectByName("tmcTimer").caption = data.timeout; | |||||
if (data.font) | if (data.font) | ||||
textObj.font = data.font; | this.messageObject.font = data.font; | ||||
this.updateDisplayedTimer(data.timeout); | |||||
const cancelHotkey = Engine.GetGUIObjectByName("tmcCancelHotkey"); | const cancelHotkey = Engine.GetGUIObjectByName("tmcCancelHotkey"); | ||||
cancelHotkey.onPress = Engine.PopGuiPage; | cancelHotkey.onPress = Engine.PopGuiPage; | ||||
const lRDiff = data.width / 2; | const lRDiff = data.width / 2; | ||||
const uDDiff = data.height / 2; | const uDDiff = data.height / 2; | ||||
Engine.GetGUIObjectByName("tmcMain").size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff; | this.panel.size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff; | ||||
const captions = data.buttonCaptions || [translate("OK")]; | const captions = data.buttonCaptions || [translate("OK")]; | ||||
// Set button captions and visibility | |||||
const button = []; | const button = []; | ||||
setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, "tmcButton"); | setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, "tmcButton"); | ||||
distributeButtonsHorizontally(button, captions); | distributeButtonsHorizontally(button, captions); | ||||
} | } | ||||
function onTick() | onTick() | ||||
{ | { | ||||
const timerObj = Engine.GetGUIObjectByName("tmcTimer"); | const remaining = this.timeout - Date.now(); | ||||
let time = +timerObj.caption; | if (remaining < 1) | ||||
--time; | |||||
if (time < 1) | |||||
Engine.GetGUIObjectByName("tmcButton1").onPress(); | Engine.GetGUIObjectByName("tmcButton1").onPress(); | ||||
timerObj.caption = time; | this.updateDisplayedTimer(remaining); | ||||
updateDisplayedTimer(time); | |||||
} | } | ||||
function updateDisplayedTimer(time) | updateDisplayedTimer(time) | ||||
{ | |||||
this.messageObject.caption = sprintf( | |||||
this.message, | |||||
{ [this.timeParameter]: Math.ceil(time / 1000) } | |||||
); | |||||
} | |||||
} | |||||
function init(data) | |||||
{ | { | ||||
Engine.GetGUIObjectByName("tmcTimerDisplay").caption = Math.ceil(time / 100); | new TimedConfirmation(data); | ||||
} | } |
Wildfire Games · Phabricator