DialogBox.cs 11 KB

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