AlertWindow.cs 8.5 KB

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