AlertWindow.cs 7.3 KB

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