DialogBox.cs 11 KB

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