AlertWindow.cs 7.1 KB

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