messageBoxDlg.tscript 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // --------------------------------------------------------------------
  23. // Message Sound
  24. // --------------------------------------------------------------------
  25. /*new SFXDescription(MessageBoxAudioDescription)
  26. {
  27. volume = 1.0;
  28. isLooping = false;
  29. is3D = false;
  30. channel = $GuiAudioType;
  31. };
  32. new SFXProfile(messageBoxBeep)
  33. {
  34. filename = "./messageBoxSound";
  35. description = MessageBoxAudioDescription;
  36. preload = true;
  37. };*/
  38. //---------------------------------------------------------------------------------------------
  39. // messageCallback
  40. // Calls a callback passed to a message box.
  41. //---------------------------------------------------------------------------------------------
  42. function messageCallback(%dlg, %callback)
  43. {
  44. Canvas.popDialog(%dlg);
  45. eval(%callback);
  46. }
  47. //---------------------------------------------------------------------------------------------
  48. // MBSetText
  49. // Sets the text of a message box and resizes it to accomodate the new string.
  50. //---------------------------------------------------------------------------------------------
  51. function MBSetText(%text, %frame, %msg)
  52. {
  53. // Get the extent of the text box.
  54. %ext = %text.getExtent();
  55. // Set the text in the center of the text box.
  56. %text.setText("<just:center>" @ %msg);
  57. // Force the textbox to resize itself vertically.
  58. %text.forceReflow();
  59. // Grab the new extent of the text box.
  60. %newExtent = %text.getExtent();
  61. // Get the vertical change in extent.
  62. %deltaY = getWord(%newExtent, 1) - getWord(%ext, 1);
  63. // Resize the window housing the text box.
  64. %windowPos = %frame.getPosition();
  65. %windowExt = %frame.getExtent();
  66. %frame.resize(getWord(%windowPos, 0), getWord(%windowPos, 1) - (%deltaY / 2),
  67. getWord(%windowExt, 0), getWord(%windowExt, 1) + %deltaY);
  68. %frame.canMove = "0";
  69. //%frame.canClose = "0";
  70. %frame.resizeWidth = "0";
  71. %frame.resizeHeight = "0";
  72. %frame.canMinimize = "0";
  73. %frame.canMaximize = "0";
  74. //sfxPlayOnce( messageBoxBeep );
  75. }
  76. function MessageBoxCtrl::onWake(%this)
  77. {
  78. %this.callback = "";
  79. %this.cancelCallback = "";
  80. }
  81. //---------------------------------------------------------------------------------------------
  82. // Various message box display functions. Each one takes a window title, a message, and a
  83. // callback for each button.
  84. //---------------------------------------------------------------------------------------------
  85. function MessageBoxCtrl::createButton(%this, %text, %command, %bitmap)
  86. {
  87. %btn = new GuiIconButtonCtrl() {
  88. BitmapAsset = %bitmap;
  89. sizeIconToButton = "1";
  90. makeIconSquare = "1";
  91. textLocation = "Center";
  92. iconLocation = "Left";
  93. text = %text;
  94. position = "251 0";
  95. extent = "140 40";
  96. profile = "GuiMenuButtonProfile";
  97. command = %command;
  98. tooltipProfile = "GuiToolTipProfile";
  99. };
  100. MessageBoxButtonHolder.add(%btn);
  101. //update positioning of the holder to be centered
  102. MessageBoxButtonHolder.position.x = MessageBoxCtrl.extent.x/2 - MessageBoxButtonHolder.extent.x/2;
  103. return %btn;
  104. }
  105. function MessageBoxDlg::onWake(%this)
  106. {
  107. }
  108. if(!isObject( MessageBoxActionMap ) )
  109. {
  110. new ActionMap(MessageBoxActionMap){};
  111. MessageBoxActionMap.bind( keyboard, Space, messageBoxYesClicked );
  112. MessageBoxActionMap.bind( gamepad, btn_a, messageBoxYesClicked );
  113. MessageBoxActionMap.bind( keyboard, Escape, messageBoxNoClicked );
  114. MessageBoxActionMap.bind( gamepad, btn_b, messageBoxNoClicked );
  115. }
  116. function MessageBoxCtrl::syncGui(%this)
  117. {
  118. }
  119. function messageBoxYesClicked(%val)
  120. {
  121. if(%val)
  122. MessageCallback(MessageBoxDlg, MessageBoxDlg.callback);
  123. }
  124. function messageBoxNoClicked(%val)
  125. {
  126. if(%val)
  127. MessageCallback(MessageBoxDlg,MessageBoxDlg.cancelCallback);
  128. }
  129. //MessageBoxOK("Test", "This is a test message box", "echo(\"Uhhhhhawhat?\"");
  130. function MessageBoxOK(%title, %message, %callback)
  131. {
  132. MessageBoxButtonHolder.clear();
  133. Canvas.pushDialog(MessageBoxDlg);
  134. MessageBoxTitleText.text = %title;
  135. %okButton = MessageBoxCtrl.createButton("OK", "messageBoxYesClicked(1);");
  136. %bitmapAssetId = MessageBoxActionMap.getCommandButtonBitmap(Canvas.getLastInputDevice(), "messageBoxYesClicked");
  137. %okButton.setBitmap(%bitmapAssetId);
  138. MBSetText(MessageBoxText, MessageBoxCtrl, %message);
  139. MessageBoxDlg.callback = %callback;
  140. }
  141. function MessageBoxOKCancel(%title, %message, %callback, %cancelCallback, %okLabelOverride, %cancelLabelOverride)
  142. {
  143. MessageBoxButtonHolder.clear();
  144. Canvas.pushDialog(MessageBoxDlg);
  145. MessageBoxTitleText.text = %title;
  146. if(%okLabelOverride $= "")
  147. %okLabel = "OK";
  148. else
  149. %okLabel = %okLabelOverride;
  150. if(%cancelLabelOverride $= "")
  151. %cancelLabel = "Cancel";
  152. else
  153. %cancelLabel = %cancelLabelOverride;
  154. %okButton = MessageBoxCtrl.createButton(%okLabel, "messageBoxYesClicked(1);");
  155. %bitmapAssetId = MessageBoxActionMap.getCommandButtonBitmap(Canvas.getLastInputDevice(), "messageBoxYesClicked");
  156. %okButton.setBitmap(%bitmapAssetId);
  157. %cancelButton = MessageBoxCtrl.createButton(%cancelLabel, "messageBoxNoClicked(1);");
  158. %bitmapAssetId = MessageBoxActionMap.getCommandButtonBitmap(Canvas.getLastInputDevice(), "messageBoxNoClicked");
  159. %cancelButton.setBitmap(%bitmapAssetId);
  160. MBSetText(MessageBoxText, MessageBoxCtrl, %message);
  161. MessageBoxDlg.callback = %callback;
  162. MessageBoxDlg.cancelCallback = %cancelCallback;
  163. }
  164. function MessageBoxYesNo(%title, %message, %yesCallback, %noCallback)
  165. {
  166. MessageBoxOKCancel(%title, %message, %yesCallback, %noCallback, "Yes", "No");
  167. }
  168. //---------------------------------------------------------------------------------------------
  169. // MessagePopup
  170. // Displays a message box with no buttons. Disappears after %delay milliseconds.
  171. //---------------------------------------------------------------------------------------------
  172. function MessagePopup(%title, %message, %delay)
  173. {
  174. Canvas.pushDialog(MessageBoxDlg);
  175. MessageBoxTitleText.text = %title;
  176. MBSetText(MessageBoxText, MessageBoxCtrl, %message);
  177. if (%delay !$= "")
  178. schedule(%delay, 0, CloseMessagePopup);
  179. }
  180. function CloseMessagePopup()
  181. {
  182. Canvas.popDialog(MessageBoxDlg);
  183. }