AlertWindow.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using FairyGUI;
  2. using System.Collections;
  3. using UI.Launcher;
  4. namespace GFGGame.Launcher
  5. {
  6. public delegate void AlertCallback(object data);
  7. //不要直接创建此类,通过AlertSystem调用
  8. public class AlertWindow : Window, IAlert
  9. {
  10. private UI_LauncherAlertUI _ui;
  11. private float m_xLeftButton = 0;
  12. private float m_xRightButton = 0;
  13. private string m_content = "";
  14. private string m_textLeft = "取消";
  15. private string m_textRight = "确认";
  16. private bool m_isShowLeftButton = false;
  17. private bool m_isShowRightButton = false;
  18. private object m_data = null;
  19. private AlertCallback m_leftButtonCallback = null;
  20. private AlertCallback m_rightButtonCallback = null;
  21. public AlertWindow() : base()
  22. {
  23. }
  24. protected override void OnInit()
  25. {
  26. base.OnInit();
  27. _ui = UI_LauncherAlertUI.Create();
  28. this.contentPane = _ui.target;
  29. this.Center();
  30. this.modal = true;
  31. //this.clickBlankToClose = false;
  32. m_xLeftButton = _ui.m_btnLeft.x;
  33. m_xRightButton = _ui.m_btnRight.x;
  34. // this.sortingOrder = ConstSortingOrder.FLOATING_PROMPT;
  35. this.SetMessage(m_content);
  36. this.SetLeftButton(m_isShowLeftButton, m_textLeft);
  37. this.SetRightButton(m_isShowRightButton, m_textRight);
  38. updateButtonPosition();
  39. _ui.m_btnLeft.onClick.Add(() =>
  40. {
  41. this.Hide();
  42. m_leftButtonCallback?.Invoke(m_data);
  43. });
  44. _ui.m_btnRight.onClick.Add(() =>
  45. {
  46. this.Hide();
  47. m_rightButtonCallback?.Invoke(m_data);
  48. });
  49. }
  50. protected override void OnShown()
  51. {
  52. base.OnShown();
  53. }
  54. protected override void OnHide()
  55. {
  56. base.OnHide();
  57. }
  58. protected override void OnUpdate()
  59. {
  60. base.OnUpdate();
  61. if (this.root != null)
  62. {
  63. int i = this.parent.GetChildIndex(this);
  64. int iMax = this.parent.numChildren - 1;
  65. if (i < iMax)
  66. {
  67. this.parent.SetChildIndex(this, iMax);
  68. }
  69. }
  70. }
  71. private void updateButtonPosition()
  72. {
  73. float xLeftButton = m_xLeftButton;
  74. float xRightButton = m_xRightButton;
  75. float xCenter = (m_xLeftButton + m_xRightButton) / 2;
  76. if (!m_isShowLeftButton && m_isShowRightButton)
  77. {
  78. xRightButton = xCenter;
  79. }
  80. else if (m_isShowLeftButton && !m_isShowRightButton)
  81. {
  82. xLeftButton = xCenter;
  83. }
  84. if (_ui != null)
  85. {
  86. _ui.m_btnLeft.x = xLeftButton;
  87. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  88. }
  89. if (_ui != null)
  90. {
  91. _ui.m_btnRight.x = xRightButton;
  92. this._ui.m_btnRight.visible = m_isShowRightButton;
  93. }
  94. }
  95. public IAlert Reset()
  96. {
  97. m_content = "";
  98. m_textLeft = "取消";
  99. m_textRight = "确认";
  100. m_leftButtonCallback = null;
  101. m_rightButtonCallback = null;
  102. m_isShowLeftButton = false;
  103. m_isShowRightButton = false;
  104. //clickBlankToClose = false;
  105. m_data = null;
  106. if (this._ui != null)
  107. {
  108. this._ui.m_txtContent.text = m_content;
  109. }
  110. if (this._ui != null)
  111. {
  112. this._ui.m_btnLeft.text = m_textLeft;
  113. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  114. }
  115. if (this._ui != null)
  116. {
  117. this._ui.m_btnRight.text = m_textRight;
  118. this._ui.m_btnRight.visible = m_isShowRightButton;
  119. }
  120. updateButtonPosition();
  121. return this;
  122. }
  123. public IAlert SetMessage(string message)
  124. {
  125. this.m_content = message;
  126. if (this._ui != null)
  127. {
  128. this._ui.m_txtContent.text = m_content;
  129. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  130. if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2)
  131. {
  132. this._ui.m_txtContent.align = AlignType.Left;
  133. }
  134. else
  135. {
  136. this._ui.m_txtContent.align = AlignType.Center;
  137. }
  138. this._ui.target.Center();
  139. }
  140. return this;
  141. }
  142. public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null)
  143. {
  144. this.m_isShowLeftButton = isShow;
  145. this.m_leftButtonCallback = callback;
  146. this.m_textLeft = textLeft;
  147. if (this._ui != null)
  148. {
  149. this._ui.m_btnLeft.text = m_textLeft;
  150. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  151. }
  152. updateButtonPosition();
  153. return this;
  154. }
  155. public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null)
  156. {
  157. this.m_isShowRightButton = isShow;
  158. this.m_rightButtonCallback = callback;
  159. this.m_textRight = textRight;
  160. if (this._ui != null)
  161. {
  162. this._ui.m_btnRight.text = m_textRight;
  163. this._ui.m_btnRight.visible = m_isShowRightButton;
  164. }
  165. updateButtonPosition();
  166. return this;
  167. }
  168. public IAlert SetData(object data)
  169. {
  170. this.m_data = data;
  171. return this;
  172. }
  173. public IAlert SetClickBlankToClose(bool value)
  174. {
  175. //this.clickBlankToClose = value;
  176. return this;
  177. }
  178. public object Current => null;
  179. public bool MoveNext()
  180. {
  181. return this.isShowing;
  182. }
  183. void IEnumerator.Reset()
  184. {
  185. }
  186. }
  187. }