DialogBox.cs 10 KB

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