AlertWindow.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using FairyGUI;
  2. using UI.Common;
  3. namespace GFGGame
  4. {
  5. //不要直接创建此类,通过AlertSystem调用
  6. public class AlertWindow : BaseView, 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. private bool _setAlertLayer = false;
  21. public AlertWindow() : base()
  22. {
  23. }
  24. protected override void OnInit()
  25. {
  26. base.OnInit();
  27. _ui = UI_AlertUI.Create();
  28. this.viewCom = _ui.target;
  29. isfullScreen = true;
  30. layer = ConstViewLayer.TOP;
  31. // viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  32. this.viewCom.Center();
  33. m_xLeftButton = _ui.m_btnLeft.x;
  34. m_xRightButton = _ui.m_btnRight.x;
  35. // this.sortingOrder = ConstSortingOrder.FLOATING_PROMPT;
  36. this.SetMessage(m_content);
  37. this.SetLeftButton(m_isShowLeftButton, m_textLeft);
  38. this.SetRightButton(m_isShowRightButton, m_textRight);
  39. updateButtonPosition();
  40. _ui.m_btnLeft.onClick.Add(() =>
  41. {
  42. this.Hide();
  43. m_leftButtonCallback?.Invoke(m_data);
  44. });
  45. _ui.m_btnRight.onClick.Add(() =>
  46. {
  47. this.Hide();
  48. m_rightButtonCallback?.Invoke(m_data);
  49. });
  50. }
  51. protected override void OnShown()
  52. {
  53. base.OnShown();
  54. _ui.target.Center();
  55. Timers.inst.AddUpdate(CheckGuide);
  56. }
  57. protected override void OnHide()
  58. {
  59. base.OnHide();
  60. Timers.inst.Remove(CheckGuide);
  61. }
  62. //protected override void OnUpdate() {
  63. // base.OnUpdate();
  64. // if(this.root != null) {
  65. // int i = this.parent.GetChildIndex(this);
  66. // int iMax = this.parent.numChildren - 1;
  67. // if(i < iMax) {
  68. // this.parent.SetChildIndex(this, iMax);
  69. // }
  70. // }
  71. //}
  72. public IAlert SetLayer(string setlayer)
  73. {
  74. layer = setlayer;
  75. return this;
  76. }
  77. private void updateButtonPosition()
  78. {
  79. float xLeftButton = m_xLeftButton;
  80. float xRightButton = m_xRightButton;
  81. float xCenter = (m_xLeftButton + m_xRightButton) / 2;
  82. if (!m_isShowLeftButton && m_isShowRightButton)
  83. {
  84. xRightButton = xCenter;
  85. }
  86. else if (m_isShowLeftButton && !m_isShowRightButton)
  87. {
  88. xLeftButton = xCenter;
  89. }
  90. if (_ui != null)
  91. {
  92. _ui.m_btnLeft.x = xLeftButton;
  93. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  94. }
  95. if (_ui != null)
  96. {
  97. _ui.m_btnRight.x = xRightButton;
  98. this._ui.m_btnRight.visible = m_isShowRightButton;
  99. }
  100. }
  101. public IAlert Reset()
  102. {
  103. m_content = "";
  104. m_textLeft = "取消";
  105. m_textRight = "确认";
  106. m_leftButtonCallback = null;
  107. m_rightButtonCallback = null;
  108. m_isShowLeftButton = false;
  109. m_isShowRightButton = false;
  110. // clickBlankToClose = false;
  111. m_data = null;
  112. if (this._ui != null)
  113. {
  114. this._ui.m_txtContent.text = m_content;
  115. }
  116. if (this._ui != null)
  117. {
  118. this._ui.m_btnLeft.text = m_textLeft;
  119. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  120. }
  121. if (this._ui != null)
  122. {
  123. this._ui.m_btnRight.text = m_textRight;
  124. this._ui.m_btnRight.visible = m_isShowRightButton;
  125. }
  126. updateButtonPosition();
  127. return this;
  128. }
  129. public IAlert SetMessage(string message)
  130. {
  131. this.m_content = message;
  132. if (this._ui != null)
  133. {
  134. this._ui.m_txtContent.text = m_content;
  135. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  136. if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2)
  137. {
  138. this._ui.m_txtContent.align = AlignType.Left;
  139. }
  140. else
  141. {
  142. this._ui.m_txtContent.align = AlignType.Center;
  143. }
  144. this._ui.target.Center();
  145. }
  146. return this;
  147. }
  148. public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null)
  149. {
  150. this.m_isShowLeftButton = isShow;
  151. this.m_leftButtonCallback = callback;
  152. this.m_textLeft = textLeft;
  153. if (this._ui != null)
  154. {
  155. this._ui.m_btnLeft.text = m_textLeft;
  156. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  157. }
  158. updateButtonPosition();
  159. return this;
  160. }
  161. public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null)
  162. {
  163. this.m_isShowRightButton = isShow;
  164. this.m_rightButtonCallback = callback;
  165. this.m_textRight = textRight;
  166. if (this._ui != null)
  167. {
  168. this._ui.m_btnRight.text = m_textRight;
  169. this._ui.m_btnRight.visible = m_isShowRightButton;
  170. }
  171. updateButtonPosition();
  172. return this;
  173. }
  174. public IAlert SetData(object data)
  175. {
  176. this.m_data = data;
  177. return this;
  178. }
  179. public IAlert SetClickBlankToClose(bool value)
  180. {
  181. // this.clickBlankToClose = value;
  182. return this;
  183. }
  184. private void CheckGuide(object param)
  185. {
  186. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  187. {
  188. UpdateToCheckGuide(null);
  189. }
  190. else
  191. {
  192. Timers.inst.Remove(CheckGuide);
  193. }
  194. }
  195. protected override void UpdateToCheckGuide(object param)
  196. {
  197. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  198. GuideController.TryGuide(_ui.m_btnRight, ConstGuideId.LUCKY_BOX, 4, "点击确定");
  199. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  200. }
  201. }
  202. }