AlertWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. }
  56. protected override void OnHide()
  57. {
  58. base.OnHide();
  59. }
  60. //protected override void OnUpdate() {
  61. // base.OnUpdate();
  62. // if(this.root != null) {
  63. // int i = this.parent.GetChildIndex(this);
  64. // int iMax = this.parent.numChildren - 1;
  65. // if(i < iMax) {
  66. // this.parent.SetChildIndex(this, iMax);
  67. // }
  68. // }
  69. //}
  70. public IAlert SetLayer(string setlayer)
  71. {
  72. layer = setlayer;
  73. return this;
  74. }
  75. private void updateButtonPosition()
  76. {
  77. float xLeftButton = m_xLeftButton;
  78. float xRightButton = m_xRightButton;
  79. float xCenter = (m_xLeftButton + m_xRightButton) / 2;
  80. if (!m_isShowLeftButton && m_isShowRightButton)
  81. {
  82. xRightButton = xCenter;
  83. }
  84. else if (m_isShowLeftButton && !m_isShowRightButton)
  85. {
  86. xLeftButton = xCenter;
  87. }
  88. if (_ui != null)
  89. {
  90. _ui.m_btnLeft.x = xLeftButton;
  91. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  92. }
  93. if (_ui != null)
  94. {
  95. _ui.m_btnRight.x = xRightButton;
  96. this._ui.m_btnRight.visible = m_isShowRightButton;
  97. }
  98. }
  99. public IAlert Reset()
  100. {
  101. m_content = "";
  102. m_textLeft = "取消";
  103. m_textRight = "确认";
  104. m_leftButtonCallback = null;
  105. m_rightButtonCallback = null;
  106. m_isShowLeftButton = false;
  107. m_isShowRightButton = false;
  108. // clickBlankToClose = false;
  109. m_data = null;
  110. if (this._ui != null)
  111. {
  112. this._ui.m_txtContent.text = m_content;
  113. }
  114. if (this._ui != null)
  115. {
  116. this._ui.m_btnLeft.text = m_textLeft;
  117. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  118. }
  119. if (this._ui != null)
  120. {
  121. this._ui.m_btnRight.text = m_textRight;
  122. this._ui.m_btnRight.visible = m_isShowRightButton;
  123. }
  124. updateButtonPosition();
  125. return this;
  126. }
  127. public IAlert SetMessage(string message)
  128. {
  129. this.m_content = message;
  130. if (this._ui != null)
  131. {
  132. this._ui.m_txtContent.text = m_content;
  133. // Debug.Log("alert height=" + this._ui.m_txtContent.textHeight + " size=" + this._ui.m_txtContent.textFormat.size);
  134. if (this._ui.m_txtContent.textHeight > this._ui.m_txtContent.textFormat.size * 2)
  135. {
  136. this._ui.m_txtContent.align = AlignType.Left;
  137. }
  138. else
  139. {
  140. this._ui.m_txtContent.align = AlignType.Center;
  141. }
  142. this._ui.target.Center();
  143. }
  144. return this;
  145. }
  146. public IAlert SetLeftButton(bool isShow, string textLeft = "取消", AlertCallback callback = null)
  147. {
  148. this.m_isShowLeftButton = isShow;
  149. this.m_leftButtonCallback = callback;
  150. this.m_textLeft = textLeft;
  151. if (this._ui != null)
  152. {
  153. this._ui.m_btnLeft.text = m_textLeft;
  154. this._ui.m_btnLeft.visible = m_isShowLeftButton;
  155. }
  156. updateButtonPosition();
  157. return this;
  158. }
  159. public IAlert SetRightButton(bool isShow, string textRight = "确认", AlertCallback callback = null)
  160. {
  161. this.m_isShowRightButton = isShow;
  162. this.m_rightButtonCallback = callback;
  163. this.m_textRight = textRight;
  164. if (this._ui != null)
  165. {
  166. this._ui.m_btnRight.text = m_textRight;
  167. this._ui.m_btnRight.visible = m_isShowRightButton;
  168. }
  169. updateButtonPosition();
  170. return this;
  171. }
  172. public IAlert SetData(object data)
  173. {
  174. this.m_data = data;
  175. return this;
  176. }
  177. public IAlert SetClickBlankToClose(bool value)
  178. {
  179. // this.clickBlankToClose = value;
  180. return this;
  181. }
  182. protected override void UpdateToCheckGuide(object param)
  183. {
  184. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  185. GuideController.TryGuide(_ui.m_btnRight, ConstGuideId.LUCKY_BOX, 4, "点击确定");
  186. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  187. }
  188. }
  189. }