2
0

DialogBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Windows
  8. * @{
  9. */
  10. /// <summary>
  11. /// Displays a modal window containing a title, a message and a set of buttons. Allows an easy way to query and inform
  12. /// the user.
  13. /// </summary>
  14. public class DialogBox : ModalWindow
  15. {
  16. /// <summary>
  17. /// Type of the dialog box, determines what buttons to show.
  18. /// </summary>
  19. public enum Type
  20. {
  21. OK,
  22. OKCancel,
  23. YesNo,
  24. YesNoCancel,
  25. RetryCancel,
  26. RetryAbortIgnore,
  27. TryCancelContinue
  28. }
  29. /// <summary>
  30. /// Type of button that was pressed when a dialog box was closed.
  31. /// </summary>
  32. public enum ResultType
  33. {
  34. Yes,
  35. No,
  36. OK,
  37. Cancel,
  38. Retry,
  39. Abort,
  40. Ignore,
  41. Try,
  42. Continue,
  43. None
  44. }
  45. private Action<ResultType> resultCallback;
  46. private Type type;
  47. private GUILabel messageLabel;
  48. private ResultType result = ResultType.None;
  49. /// <summary>
  50. /// Button that was pressed when the dialog box was closed. Only valid after the user closes the dialog box.
  51. /// </summary>
  52. public ResultType Result
  53. {
  54. get { return result; }
  55. }
  56. /// <summary>
  57. /// Opens a new dialog box.
  58. /// </summary>
  59. /// <param name="title">Text to display in the title bar.</param>
  60. /// <param name="message">Message to display in the dialog box.</param>
  61. /// <param name="type">Type of dialog box that determines what buttons to display.</param>
  62. /// <param name="resultCallback">Callback to trigger when the user clicks on a dialog box button.</param>
  63. /// <returns>Instance of the dialog box window.</returns>
  64. public static DialogBox Open(LocString title, LocString message, Type type, Action<ResultType> resultCallback = null)
  65. {
  66. return new DialogBox(title, message, type, resultCallback);
  67. }
  68. /// <summary>
  69. /// Constructs the dialog box.
  70. /// </summary>
  71. /// <param name="title">Text to display in the title bar.</param>
  72. /// <param name="message">Message to display in the dialog box.</param>
  73. /// <param name="type">Type of dialog box that determines what buttons to display.</param>
  74. /// <param name="resultCallback">Callback to trigger when the user clicks on a dialog box button.</param>
  75. protected DialogBox(LocString title, LocString message, Type type, Action<ResultType> resultCallback)
  76. : base(false)
  77. {
  78. this.resultCallback = resultCallback;
  79. this.type = type;
  80. SetupGUI();
  81. Title = title;
  82. messageLabel.SetContent(message);
  83. Width = 280;
  84. Height = messageLabel.Bounds.height + 60;
  85. }
  86. private void OnEditorUpdate()
  87. {
  88. if (Input.IsButtonDown(ButtonCode.Return))
  89. {
  90. switch (type)
  91. {
  92. case Type.OK:
  93. case Type.OKCancel:
  94. ButtonClicked(ResultType.OK);
  95. break;
  96. case Type.RetryAbortIgnore:
  97. case Type.RetryCancel:
  98. ButtonClicked(ResultType.Retry);
  99. break;
  100. case Type.TryCancelContinue:
  101. ButtonClicked(ResultType.Try);
  102. break;
  103. case Type.YesNo:
  104. case Type.YesNoCancel:
  105. ButtonClicked(ResultType.Yes);
  106. break;
  107. }
  108. }
  109. if (Input.IsButtonDown(ButtonCode.Escape))
  110. {
  111. switch (type)
  112. {
  113. case Type.OK:
  114. ButtonClicked(ResultType.OK);
  115. break;
  116. case Type.RetryAbortIgnore:
  117. ButtonClicked(ResultType.Ignore);
  118. break;
  119. case Type.OKCancel:
  120. case Type.RetryCancel:
  121. case Type.YesNoCancel:
  122. case Type.TryCancelContinue:
  123. ButtonClicked(ResultType.Cancel);
  124. break;
  125. case Type.YesNo:
  126. ButtonClicked(ResultType.No);
  127. break;
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Creates all of the GUI elements required for the specified type of dialog box.
  133. /// </summary>
  134. private void SetupGUI()
  135. {
  136. messageLabel = new GUILabel("", EditorStyles.MultiLineLabel,
  137. GUIOption.FixedWidth(260), GUIOption.FlexibleHeight(0, 600));
  138. GUILayoutY layoutY = GUI.AddLayoutY();
  139. layoutY.AddSpace(10);
  140. GUILayoutX messageLayout = layoutY.AddLayoutX();
  141. messageLayout.AddFlexibleSpace();
  142. messageLayout.AddElement(messageLabel);
  143. messageLayout.AddFlexibleSpace();
  144. layoutY.AddSpace(10);
  145. GUILayoutX btnLayout = layoutY.AddLayoutX();
  146. btnLayout.AddFlexibleSpace();
  147. switch (type)
  148. {
  149. case Type.OK:
  150. {
  151. GUIButton okBtn = new GUIButton(new LocEdString("OK"));
  152. okBtn.OnClick += () => ButtonClicked(ResultType.OK);
  153. btnLayout.AddElement(okBtn);
  154. }
  155. break;
  156. case Type.OKCancel:
  157. {
  158. GUIButton okBtn = new GUIButton(new LocEdString("OK"));
  159. okBtn.OnClick += () => ButtonClicked(ResultType.OK);
  160. GUIButton cancelBtn = new GUIButton(new LocEdString("Cancel"));
  161. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  162. btnLayout.AddElement(okBtn);
  163. btnLayout.AddSpace(20);
  164. btnLayout.AddElement(cancelBtn);
  165. }
  166. break;
  167. case Type.RetryAbortIgnore:
  168. {
  169. GUIButton retryBtn = new GUIButton(new LocEdString("Retry"));
  170. retryBtn.OnClick += () => ButtonClicked(ResultType.Retry);
  171. GUIButton abortBtn = new GUIButton(new LocEdString("Abort"));
  172. abortBtn.OnClick += () => ButtonClicked(ResultType.Abort);
  173. GUIButton ignoreBtn = new GUIButton(new LocEdString("Ignore"));
  174. ignoreBtn.OnClick += () => ButtonClicked(ResultType.Ignore);
  175. btnLayout.AddElement(retryBtn);
  176. btnLayout.AddSpace(20);
  177. btnLayout.AddElement(abortBtn);
  178. btnLayout.AddSpace(20);
  179. btnLayout.AddElement(ignoreBtn);
  180. }
  181. break;
  182. case Type.RetryCancel:
  183. {
  184. GUIButton retryBtn = new GUIButton(new LocEdString("Retry"));
  185. retryBtn.OnClick += () => ButtonClicked(ResultType.Retry);
  186. GUIButton cancelBtn = new GUIButton(new LocEdString("Cancel"));
  187. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  188. btnLayout.AddElement(retryBtn);
  189. btnLayout.AddSpace(20);
  190. btnLayout.AddElement(cancelBtn);
  191. }
  192. break;
  193. case Type.TryCancelContinue:
  194. {
  195. GUIButton tryBtn = new GUIButton(new LocEdString("Try"));
  196. tryBtn.OnClick += () => ButtonClicked(ResultType.Try);
  197. GUIButton cancelBtn = new GUIButton(new LocEdString("Cancel"));
  198. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  199. GUIButton continueBtn = new GUIButton(new LocEdString("Continue"));
  200. continueBtn.OnClick += () => ButtonClicked(ResultType.Continue);
  201. btnLayout.AddElement(tryBtn);
  202. btnLayout.AddSpace(20);
  203. btnLayout.AddElement(cancelBtn);
  204. btnLayout.AddSpace(20);
  205. btnLayout.AddElement(continueBtn);
  206. }
  207. break;
  208. case Type.YesNo:
  209. {
  210. GUIButton yesBtn = new GUIButton(new LocEdString("Yes"));
  211. yesBtn.OnClick += () => ButtonClicked(ResultType.Yes);
  212. GUIButton noBtn = new GUIButton(new LocEdString("No"));
  213. noBtn.OnClick += () => ButtonClicked(ResultType.No);
  214. btnLayout.AddElement(yesBtn);
  215. btnLayout.AddSpace(20);
  216. btnLayout.AddElement(noBtn);
  217. }
  218. break;
  219. case Type.YesNoCancel:
  220. {
  221. GUIButton yesBtn = new GUIButton(new LocEdString("Yes"));
  222. yesBtn.OnClick += () => ButtonClicked(ResultType.Yes);
  223. GUIButton noBtn = new GUIButton(new LocEdString("No"));
  224. noBtn.OnClick += () => ButtonClicked(ResultType.No);
  225. GUIButton cancelBtn = new GUIButton(new LocEdString("Cancel"));
  226. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  227. btnLayout.AddElement(yesBtn);
  228. btnLayout.AddSpace(20);
  229. btnLayout.AddElement(noBtn);
  230. btnLayout.AddSpace(20);
  231. btnLayout.AddElement(cancelBtn);
  232. }
  233. break;
  234. }
  235. btnLayout.AddFlexibleSpace();
  236. layoutY.AddFlexibleSpace();
  237. }
  238. /// <summary>
  239. /// Triggered when one of the dialog box buttons was clicked.
  240. /// </summary>
  241. /// <param name="result">Type of the button that was clicked.</param>
  242. private void ButtonClicked(ResultType result)
  243. {
  244. this.result = result;
  245. if (resultCallback != null)
  246. resultCallback(result);
  247. Close();
  248. }
  249. }
  250. /** @} */
  251. }