From b4fbbed3794d6c6458beda323c781a78262f38f5 Mon Sep 17 00:00:00 2001 From: Angen Date: Sun, 17 Oct 2021 10:58:51 +0000 Subject: [PATCH] There have been quite a bit of number of questions how to change scale of the gui, because this option is hidden from the user. Use dropdown with values. Implement confirmation box with countdown to revert scale change because buttons can get unable to click. Differential revision: D3037 Comments by: @vladislavbelov, @Stan, @wraitii, @pieq, @sera Tested by: @Langbart This was SVN commit r25966. --- .../mods/mod/gui/common/functions_msgbox.js | 18 ++++++ .../data/mods/mod/gui/common/utilities.js | 35 ++++++++++++ binaries/data/mods/mod/gui/msgbox/msgbox.js | 34 +---------- .../mods/mod/gui/page_timedconfirmation.xml | 8 +++ .../timedconfirmation/timedconfirmation.js | 48 ++++++++++++++++ .../timedconfirmation/timedconfirmation.xml | 56 +++++++++++++++++++ .../data/mods/public/gui/options/options.js | 47 ++++++++++++++++ .../data/mods/public/gui/options/options.json | 18 ++++++ .../data/mods/public/gui/options/options.xml | 1 + source/gui/CGUI.cpp | 7 ++- source/gui/CGUIText.cpp | 11 ++-- source/gui/GUIMatrix.cpp | 9 ++- source/gui/ObjectTypes/CInput.cpp | 10 ++-- source/ps/CConsole.cpp | 5 +- source/ps/GameSetup/Config.cpp | 4 -- source/ps/GameSetup/Config.h | 1 - source/ps/VideoMode.cpp | 16 +++++- source/ps/VideoMode.h | 9 +++ source/ps/scripting/JSInterface_ConfigDB.cpp | 7 +++ 19 files changed, 287 insertions(+), 57 deletions(-) create mode 100644 binaries/data/mods/mod/gui/common/utilities.js create mode 100644 binaries/data/mods/mod/gui/page_timedconfirmation.xml create mode 100644 binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js create mode 100644 binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.xml diff --git a/binaries/data/mods/mod/gui/common/functions_msgbox.js b/binaries/data/mods/mod/gui/common/functions_msgbox.js index acf524bff3..9f2653b87b 100644 --- a/binaries/data/mods/mod/gui/common/functions_msgbox.js +++ b/binaries/data/mods/mod/gui/common/functions_msgbox.js @@ -15,6 +15,24 @@ function messageBox(mbWidth, mbHeight, mbMessage, mbTitle, mbButtonCaptions, mbB }); } +function timedConfirmation(width, height, message, timeout, title, buttonCaptions, btnCode, callbackArgs) +{ + Engine.PushGuiPage( + "page_timedconfirmation.xml", + { + "width": width, + "height": height, + "message": message, + "timeout": timeout, + "title": title, + "buttonCaptions": buttonCaptions + }, + button => { + if (btnCode !== undefined && btnCode[button]) + btnCode[button](callbackArgs ? callbackArgs[button] : undefined); + }); +} + function openURL(url) { Engine.OpenURL(url); diff --git a/binaries/data/mods/mod/gui/common/utilities.js b/binaries/data/mods/mod/gui/common/utilities.js new file mode 100644 index 0000000000..127c42239a --- /dev/null +++ b/binaries/data/mods/mod/gui/common/utilities.js @@ -0,0 +1,35 @@ +function distributeButtonsHorizontally(button, captions) +{ + const y1 = "100%-46"; + const y2 = "100%-18"; + switch (captions.length) + { + case 1: + button[0].size = "18 " + y1 + " 100%-18 " + y2; + break; + case 2: + button[0].size = "18 " + y1 + " 50%-5 " + y2; + button[1].size = "50%+5 " + y1 + " 100%-18 " + y2; + break; + case 3: + button[0].size = "18 " + y1 + " 33%-5 " + y2; + button[1].size = "33%+5 " + y1 + " 66%-5 " + y2; + button[2].size = "66%+5 " + y1 + " 100%-18 " + y2; + break; + } +} + +function setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, name) +{ + captions.forEach((caption, i) => { + button[i] = Engine.GetGUIObjectByName(name + (i + 1)); + button[i].caption = caption; + button[i].hidden = false; + button[i].onPress = () => { + Engine.PopGuiPage(i); + }; + + if (i == 0) + cancelHotkey.onPress = button[i].onPress; + }); +} diff --git a/binaries/data/mods/mod/gui/msgbox/msgbox.js b/binaries/data/mods/mod/gui/msgbox/msgbox.js index 9276a482c6..a52e34e0de 100644 --- a/binaries/data/mods/mod/gui/msgbox/msgbox.js +++ b/binaries/data/mods/mod/gui/msgbox/msgbox.js @@ -24,37 +24,7 @@ function init(data) 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; - } + setButtonCaptionsAndVisibitily(mbButton, captions, mbCancelHotkey, "mbButton"); + distributeButtonsHorizontally(mbButton, captions); } diff --git a/binaries/data/mods/mod/gui/page_timedconfirmation.xml b/binaries/data/mods/mod/gui/page_timedconfirmation.xml new file mode 100644 index 0000000000..f006c12219 --- /dev/null +++ b/binaries/data/mods/mod/gui/page_timedconfirmation.xml @@ -0,0 +1,8 @@ + + + common/modern/setup.xml + common/modern/styles.xml + common/modern/sprites.xml + + timedconfirmation/timedconfirmation.xml + diff --git a/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js b/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js new file mode 100644 index 0000000000..6a8a1f7b11 --- /dev/null +++ b/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.js @@ -0,0 +1,48 @@ +/** + * Currently limited to at most 3 buttons per message box. + * The convention is to have "cancel" appear first. + */ +function init(data) +{ + Engine.GetGUIObjectByName("tmcTitleBar").caption = data.title; + + const textObj = Engine.GetGUIObjectByName("tmcText"); + textObj.caption = data.message; + + updateDisplayedTimer(data.timeout); + + Engine.GetGUIObjectByName("tmcTimer").caption = data.timeout; + if (data.font) + textObj.font = data.font; + + const cancelHotkey = Engine.GetGUIObjectByName("tmcCancelHotkey"); + cancelHotkey.onPress = Engine.PopGuiPage; + + const lRDiff = data.width / 2; + const uDDiff = data.height / 2; + Engine.GetGUIObjectByName("tmcMain").size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff; + + const captions = data.buttonCaptions || [translate("OK")]; + + // Set button captions and visibility + const button = []; + setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, "tmcButton"); + distributeButtonsHorizontally(button, captions); +} + +function onTick() +{ + const timerObj = Engine.GetGUIObjectByName("tmcTimer"); + let time = +timerObj.caption; + --time; + if (time < 1) + Engine.GetGUIObjectByName("tmcButton1").onPress(); + + timerObj.caption = time; + updateDisplayedTimer(time); +} + +function updateDisplayedTimer(time) +{ + Engine.GetGUIObjectByName("tmcTimerDisplay").caption = Math.ceil(time / 100); +} diff --git a/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.xml b/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.xml new file mode 100644 index 0000000000..ba7a9c9811 --- /dev/null +++ b/binaries/data/mods/mod/gui/timedconfirmation/timedconfirmation.xml @@ -0,0 +1,56 @@ + + + + +