DialogBox.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. public class DialogBox : ModalWindow
  6. {
  7. public enum Type
  8. {
  9. OK,
  10. OKCancel,
  11. YesNo,
  12. YesNoCancel,
  13. RetryCancel,
  14. RetryAbortIgnore,
  15. TryCancelContinue
  16. }
  17. public enum ResultType
  18. {
  19. Yes,
  20. No,
  21. OK,
  22. Cancel,
  23. Retry,
  24. Abort,
  25. Ignore,
  26. Try,
  27. Continue,
  28. None
  29. }
  30. private Action<ResultType> resultCallback;
  31. private Type type;
  32. private GUILabel messageLabel;
  33. private ResultType result = ResultType.None;
  34. public ResultType Result
  35. {
  36. get { return result; }
  37. }
  38. public static DialogBox Open(LocString title, LocString message, Type type, Action<ResultType> resultCallback = null)
  39. {
  40. DialogBox instance = new DialogBox();
  41. instance.Width = 250;
  42. instance.Height = 150;
  43. instance.Title = title;
  44. instance.type = type;
  45. instance.resultCallback = resultCallback;
  46. return instance;
  47. }
  48. private void Initialize(LocString title, LocString message, Type type, Action<ResultType> resultCallback)
  49. {
  50. Width = 250;
  51. Height = 150;
  52. Title = title;
  53. messageLabel.SetContent(message);
  54. this.resultCallback = resultCallback;
  55. this.type = type;
  56. }
  57. protected DialogBox()
  58. : base(false)
  59. { }
  60. private void OnInitialize()
  61. {
  62. messageLabel = new GUILabel("");
  63. GUILayoutY layoutY = GUI.AddLayoutY();
  64. layoutY.AddFlexibleSpace();
  65. GUILayoutX messageLayout = layoutY.AddLayoutX();
  66. messageLayout.AddFlexibleSpace();
  67. messageLayout.AddElement(messageLabel);
  68. messageLayout.AddFlexibleSpace();
  69. layoutY.AddSpace(10);
  70. GUILayoutX btnLayout = layoutY.AddLayoutX();
  71. btnLayout.AddFlexibleSpace();
  72. switch (type)
  73. {
  74. case Type.OK:
  75. {
  76. GUIButton okBtn = new GUIButton("OK");
  77. okBtn.OnClick += () => ButtonClicked(ResultType.OK);
  78. btnLayout.AddElement(okBtn);
  79. }
  80. break;
  81. case Type.OKCancel:
  82. {
  83. GUIButton okBtn = new GUIButton("OK");
  84. okBtn.OnClick += () => ButtonClicked(ResultType.OK);
  85. GUIButton cancelBtn = new GUIButton("Cancel");
  86. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  87. btnLayout.AddElement(okBtn);
  88. btnLayout.AddSpace(20);
  89. btnLayout.AddElement(cancelBtn);
  90. }
  91. break;
  92. case Type.RetryAbortIgnore:
  93. {
  94. GUIButton retryBtn = new GUIButton("Retry");
  95. retryBtn.OnClick += () => ButtonClicked(ResultType.Retry);
  96. GUIButton abortBtn = new GUIButton("Abort");
  97. abortBtn.OnClick += () => ButtonClicked(ResultType.Abort);
  98. GUIButton ignoreBtn = new GUIButton("Ignore");
  99. ignoreBtn.OnClick += () => ButtonClicked(ResultType.Ignore);
  100. btnLayout.AddElement(retryBtn);
  101. btnLayout.AddSpace(20);
  102. btnLayout.AddElement(abortBtn);
  103. btnLayout.AddSpace(20);
  104. btnLayout.AddElement(ignoreBtn);
  105. }
  106. break;
  107. case Type.RetryCancel:
  108. {
  109. GUIButton retryBtn = new GUIButton("Retry");
  110. retryBtn.OnClick += () => ButtonClicked(ResultType.Retry);
  111. GUIButton cancelBtn = new GUIButton("Cancel");
  112. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  113. btnLayout.AddElement(retryBtn);
  114. btnLayout.AddSpace(20);
  115. btnLayout.AddElement(cancelBtn);
  116. }
  117. break;
  118. case Type.TryCancelContinue:
  119. {
  120. GUIButton tryBtn = new GUIButton("Try");
  121. tryBtn.OnClick += () => ButtonClicked(ResultType.Try);
  122. GUIButton cancelBtn = new GUIButton("Cancel");
  123. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  124. GUIButton continueBtn = new GUIButton("Continue");
  125. continueBtn.OnClick += () => ButtonClicked(ResultType.Continue);
  126. btnLayout.AddElement(tryBtn);
  127. btnLayout.AddSpace(20);
  128. btnLayout.AddElement(cancelBtn);
  129. btnLayout.AddSpace(20);
  130. btnLayout.AddElement(continueBtn);
  131. }
  132. break;
  133. case Type.YesNo:
  134. {
  135. GUIButton yesBtn = new GUIButton("Yes");
  136. yesBtn.OnClick += () => ButtonClicked(ResultType.Yes);
  137. GUIButton noBtn = new GUIButton("No");
  138. noBtn.OnClick += () => ButtonClicked(ResultType.No);
  139. btnLayout.AddElement(yesBtn);
  140. btnLayout.AddSpace(20);
  141. btnLayout.AddElement(noBtn);
  142. }
  143. break;
  144. case Type.YesNoCancel:
  145. {
  146. GUIButton yesBtn = new GUIButton("Yes");
  147. yesBtn.OnClick += () => ButtonClicked(ResultType.Yes);
  148. GUIButton noBtn = new GUIButton("No");
  149. noBtn.OnClick += () => ButtonClicked(ResultType.No);
  150. GUIButton cancelBtn = new GUIButton("Cancel");
  151. cancelBtn.OnClick += () => ButtonClicked(ResultType.Cancel);
  152. btnLayout.AddElement(yesBtn);
  153. btnLayout.AddSpace(20);
  154. btnLayout.AddElement(noBtn);
  155. btnLayout.AddSpace(20);
  156. btnLayout.AddElement(cancelBtn);
  157. }
  158. break;
  159. }
  160. btnLayout.AddFlexibleSpace();
  161. layoutY.AddFlexibleSpace();
  162. }
  163. private void ButtonClicked(ResultType result)
  164. {
  165. this.result = result;
  166. if (resultCallback != null)
  167. resultCallback(result);
  168. }
  169. }
  170. }