source: ps/trunk/binaries/data/mods/mod/gui/msgbox/msgbox.js@ 22676

Last change on this file since 22676 was 22676, checked in by elexis, 5 years ago

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 rP14496.

Supports stacked message boxes and removes the according workaround.
Change structree / civinfo switch-dialog code from rP21339 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

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 1.6 KB
Line 
1/**
2 * Currently limited to at most 3 buttons per message box.
3 * The convention is to have "cancel" appear first.
4 */
5function init(data)
6{
7 // Set title
8 Engine.GetGUIObjectByName("mbTitleBar").caption = data.title;
9
10 // Set subject
11 let mbTextObj = Engine.GetGUIObjectByName("mbText");
12 mbTextObj.caption = data.message;
13 if (data.font)
14 mbTextObj.font = data.font;
15
16 // Default behaviour
17 let mbCancelHotkey = Engine.GetGUIObjectByName("mbCancelHotkey");
18 mbCancelHotkey.onPress = Engine.PopGuiPage;
19
20 // Calculate size
21 let mbLRDiff = data.width / 2;
22 let mbUDDiff = data.height / 2;
23 Engine.GetGUIObjectByName("mbMain").size = "50%-" + mbLRDiff + " 50%-" + mbUDDiff + " 50%+" + mbLRDiff + " 50%+" + mbUDDiff;
24
25 let captions = data.buttonCaptions || [translate("OK")];
26
27 // Set button captions and visibility
28 let mbButton = [];
29 captions.forEach((caption, i) => {
30 mbButton[i] = Engine.GetGUIObjectByName("mbButton" + (i + 1));
31 mbButton[i].caption = caption;
32 mbButton[i].hidden = false;
33 mbButton[i].onPress = () => {
34 Engine.PopGuiPage(i);
35 };
36
37 // Convention: Cancel is the first button
38 if (i == 0)
39 mbCancelHotkey.onPress = mbButton[i].onPress;
40 });
41
42 // Distribute buttons horizontally
43 let y1 = "100%-46";
44 let y2 = "100%-18";
45 switch (captions.length)
46 {
47 case 1:
48 mbButton[0].size = "18 " + y1 + " 100%-18 " + y2;
49 break;
50 case 2:
51 mbButton[0].size = "18 " + y1 + " 50%-5 " + y2;
52 mbButton[1].size = "50%+5 " + y1 + " 100%-18 " + y2;
53 break;
54 case 3:
55 mbButton[0].size = "18 " + y1 + " 33%-5 " + y2;
56 mbButton[1].size = "33%+5 " + y1 + " 66%-5 " + y2;
57 mbButton[2].size = "66%+5 " + y1 + " 100%-18 " + y2;
58 break;
59 }
60}
Note: See TracBrowser for help on using the repository browser.