AlertWindow.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using FairyGUI;
  2. using UI.Common;
  3. namespace GFGGame
  4. {
  5. //不要直接创建此类,通过AlertSystem调用
  6. public class AlertWindow : BaseWindow, IAlert
  7. {
  8. public delegate void AlertCallback(object data);
  9. private UI_AlertUI _ui;
  10. private float m_xLeftButton = 0;
  11. private float m_xRightButton = 0;
  12. private string m_content = "";
  13. private string m_textLeft = "取消";
  14. private string m_textRight = "确认";
  15. private bool m_isShowLeftButton = false;
  16. private bool m_isShowRightButton = false;
  17. private object m_data = null;
  18. private AlertCallback m_leftButtonCallback = null;
  19. private AlertCallback m_rightButtonCallback = null;
  20. public AlertWindow() : base()
  21. {
  22. }
  23. protected override void OnInit()
  24. {
  25. base.OnInit();
  26. _ui = UI_AlertUI.Create();
  27. this.viewCom = _ui.target;
  28. this.viewCom.Center();
  29. this.modal = true;
  30. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  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. // base.OnUpdate();
  60. // if(this.root != null) {
  61. // int i = this.parent.GetChildIndex(this);
  62. // int iMax = this.parent.numChildren - 1;
  63. // if(i < iMax) {
  64. // this.parent.SetChildIndex(this, iMax);
  65. // }
  66. // }
  67. //}
  68. private void updateButtonPosition()
  69. {
  70. float xLeftButton = m_xLeftButton;
  71. float xRightButton = m_xRightButton;
  72. float xCenter = (m_xLeftButton + m_xRightButton) / 2;
  73. if (!m_isShowLeftButton && m_isShowRightButton)
  74. {
  75. xRightButton = xCenter;
  76. }
  77. else if (m_isShowLeftButton && !m_isShowRightButton)
  78. {
  79. xLeftButton = xCenter;
  80. }
  81. if (_ui != null)
  82. {
  83. _ui.m_btnLeft.x = xLeftButton;
  84. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  85. }
  86. if (_ui != null)
  87. {
  88. _ui.m_btnRight.x = xRightButton;
  89. this._ui.m_btnRight.visible = m_isShowRightButton;
  90. }
  91. }
  92. public IAlert Reset()
  93. {
  94. m_content = "";
  95. m_textLeft = "取消";
  96. m_textRight = "确认";
  97. m_leftButtonCallback = null;
  98. m_rightButtonCallback = null;
  99. m_isShowLeftButton = false;
  100. m_isShowRightButton = false;
  101. clickBlankToClose = false;
  102. m_data = null;
  103. if (this._ui != null)
  104. {
  105. this._ui.m_txtContent.text = m_content;
  106. }
  107. if (this._ui != null)
  108. {
  109. this._ui.m_btnLeft.text = m_textLeft;
  110. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  111. }
  112. if (this._ui != null)
  113. {
  114. this._ui.m_btnRight.text = m_textRight;
  115. this._ui.m_btnRight.visible = m_isShowRightButton;
  116. }
  117. updateButtonPosition();
  118. return this;
  119. }
  120. public IAlert SetMessage(string message)
  121. {
  122. this.m_content = message;
  123. if (this._ui != null)
  124. {
  125. this._ui.m_txtContent.text = m_content;
  126. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  127. if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2)
  128. {
  129. this._ui.m_txtContent.align = AlignType.Left;
  130. }
  131. else
  132. {
  133. this._ui.m_txtContent.align = AlignType.Center;
  134. }
  135. this._ui.target.Center();
  136. }
  137. return this;
  138. }
  139. public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null)
  140. {
  141. this.m_isShowLeftButton = isShow;
  142. this.m_leftButtonCallback = callback;
  143. this.m_textLeft = textLeft;
  144. if (this._ui != null)
  145. {
  146. this._ui.m_btnLeft.text = m_textLeft;
  147. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  148. }
  149. updateButtonPosition();
  150. return this;
  151. }
  152. public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null)
  153. {
  154. this.m_isShowRightButton = isShow;
  155. this.m_rightButtonCallback = callback;
  156. this.m_textRight = textRight;
  157. if (this._ui != null)
  158. {
  159. this._ui.m_btnRight.text = m_textRight;
  160. this._ui.m_btnRight.visible = m_isShowRightButton;
  161. }
  162. updateButtonPosition();
  163. return this;
  164. }
  165. public IAlert SetData(object data)
  166. {
  167. this.m_data = data;
  168. return this;
  169. }
  170. public IAlert SetClickBlankToClose(bool value)
  171. {
  172. this.clickBlankToClose = value;
  173. return this;
  174. }
  175. protected override void UpdateToCheckGuide(object param)
  176. {
  177. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  178. GuideController.TryGuide(_ui.m_btnRight, ConstGuideId.LUCKY_BOX, 4, "点击确定");
  179. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  180. }
  181. }
  182. }