AlertWindow.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_tips = "";
  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. 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.SetTips(m_tips);
  50. this.SetLeftButton(m_isShowLeftButton, m_textLeft);
  51. this.SetRightButton(m_isShowRightButton, m_textRight);
  52. updateButtonPosition();
  53. _ui.m_btnLeft.onClick.Add(() =>
  54. {
  55. this.Hide();
  56. m_leftButtonCallback?.Invoke(m_data);
  57. });
  58. _ui.m_btnRight.onClick.Add(() =>
  59. {
  60. this.Hide();
  61. m_rightButtonCallback?.Invoke(m_data);
  62. });
  63. }
  64. protected override void OnShown()
  65. {
  66. base.OnShown();
  67. _ui.target.Center();
  68. Timers.inst.AddUpdate(CheckGuide);
  69. }
  70. protected override void OnHide()
  71. {
  72. base.OnHide();
  73. Timers.inst.Remove(CheckGuide);
  74. }
  75. //protected override void OnUpdate() {
  76. // base.OnUpdate();
  77. // if(this.root != null) {
  78. // int i = this.parent.GetChildIndex(this);
  79. // int iMax = this.parent.numChildren - 1;
  80. // if(i < iMax) {
  81. // this.parent.SetChildIndex(this, iMax);
  82. // }
  83. // }
  84. //}
  85. public IAlert SetLayer(string setlayer)
  86. {
  87. layer = setlayer;
  88. return this;
  89. }
  90. private void updateButtonPosition()
  91. {
  92. float xLeftButton = m_xLeftButton;
  93. float xRightButton = m_xRightButton;
  94. float xCenter = (m_xLeftButton + m_xRightButton) / 2;
  95. if (!m_isShowLeftButton && m_isShowRightButton)
  96. {
  97. xRightButton = xCenter;
  98. }
  99. else if (m_isShowLeftButton && !m_isShowRightButton)
  100. {
  101. xLeftButton = xCenter;
  102. }
  103. if (_ui != null)
  104. {
  105. _ui.m_btnLeft.x = xLeftButton;
  106. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  107. }
  108. if (_ui != null)
  109. {
  110. _ui.m_btnRight.x = xRightButton;
  111. this._ui.m_btnRight.visible = m_isShowRightButton;
  112. }
  113. }
  114. public IAlert Reset()
  115. {
  116. m_content = "";
  117. m_tips = "";
  118. m_textLeft = "取消";
  119. m_textRight = "确认";
  120. m_leftButtonCallback = null;
  121. m_rightButtonCallback = null;
  122. m_isShowLeftButton = false;
  123. m_isShowRightButton = false;
  124. // clickBlankToClose = false;
  125. m_data = null;
  126. if (this._ui != null)
  127. {
  128. this._ui.m_txtContent.text = m_content;
  129. }
  130. if (this._ui != null)
  131. {
  132. this._ui.m_txtTips.text = m_tips;
  133. }
  134. if (this._ui != null)
  135. {
  136. this._ui.m_btnLeft.text = m_textLeft;
  137. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  138. }
  139. if (this._ui != null)
  140. {
  141. this._ui.m_btnRight.text = m_textRight;
  142. this._ui.m_btnRight.visible = m_isShowRightButton;
  143. }
  144. updateButtonPosition();
  145. return this;
  146. }
  147. public IAlert SetMessage(string message)
  148. {
  149. this.m_content = message;
  150. if (this._ui != null)
  151. {
  152. this._ui.m_txtContent.text = m_content;
  153. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  154. if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2)
  155. {
  156. this._ui.m_txtContent.align = AlignType.Left;
  157. }
  158. else
  159. {
  160. this._ui.m_txtContent.align = AlignType.Center;
  161. }
  162. this._ui.target.Center();
  163. }
  164. return this;
  165. }
  166. public IAlert SetTips(string message)
  167. {
  168. this.m_tips = message;
  169. if (this._ui != null)
  170. {
  171. this._ui.m_txtTips.text = m_tips;
  172. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  173. if (this._ui.m_txtTips.textHeight > this._ui.m_txtTips.textFormat.size * 2)
  174. {
  175. this._ui.m_txtTips.align = AlignType.Left;
  176. }
  177. else
  178. {
  179. this._ui.m_txtTips.align = AlignType.Center;
  180. }
  181. this._ui.target.Center();
  182. }
  183. return this;
  184. }
  185. public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null)
  186. {
  187. this.m_isShowLeftButton = isShow;
  188. this.m_leftButtonCallback = callback;
  189. this.m_textLeft = textLeft;
  190. if (this._ui != null)
  191. {
  192. this._ui.m_btnLeft.text = m_textLeft;
  193. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  194. }
  195. updateButtonPosition();
  196. return this;
  197. }
  198. public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null)
  199. {
  200. this.m_isShowRightButton = isShow;
  201. this.m_rightButtonCallback = callback;
  202. this.m_textRight = textRight;
  203. if (this._ui != null)
  204. {
  205. this._ui.m_btnRight.text = m_textRight;
  206. this._ui.m_btnRight.visible = m_isShowRightButton;
  207. }
  208. updateButtonPosition();
  209. return this;
  210. }
  211. public IAlert SetData(object data)
  212. {
  213. this.m_data = data;
  214. return this;
  215. }
  216. public IAlert SetClickBlankToClose(bool value)
  217. {
  218. // this.clickBlankToClose = value;
  219. return this;
  220. }
  221. private void CheckGuide(object param)
  222. {
  223. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  224. {
  225. UpdateToCheckGuide(null);
  226. }
  227. else
  228. {
  229. Timers.inst.Remove(CheckGuide);
  230. }
  231. }
  232. protected override void UpdateToCheckGuide(object param)
  233. {
  234. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  235. if (GameGlobal.DataInited)
  236. {
  237. GuideController.TryGuide(_ui.m_btnRight, ConstGuideId.LUCKY_BOX, 4, "点击确定。");
  238. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  239. }
  240. }
  241. }
  242. }