BaseWindow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using FairyGUI;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace GFGGame
  5. {
  6. /// <summary>
  7. /// FairyGUI中Window的扩展类,功能界面基本都用这个
  8. /// </summary>
  9. public class BaseWindow : UIView, IUIView
  10. {
  11. private Window _window;
  12. private GGraph _btnClose;
  13. private bool _clickBlankToClose;
  14. private bool _modal;
  15. public bool modal
  16. {
  17. get => _modal;
  18. set
  19. {
  20. _modal = value;
  21. if (_window != null)
  22. {
  23. _window.modal = modal;
  24. }
  25. }
  26. }
  27. protected bool bringToFontOnClick
  28. {
  29. set
  30. {
  31. _window.bringToFontOnClick = value;
  32. }
  33. }
  34. /// <summary>
  35. /// 点击空白关闭,true关闭,false不关闭,默认值关闭
  36. /// </summary>
  37. protected bool clickBlankToClose
  38. {
  39. set
  40. {
  41. _clickBlankToClose = value;
  42. if (this.viewCom != null && this.viewCom.root != null)
  43. {
  44. if (_clickBlankToClose)
  45. {
  46. CheckCreateCloseButton();
  47. }
  48. else
  49. {
  50. CheckRemoveCloseButton();
  51. }
  52. }
  53. }
  54. }
  55. public override GComponent viewCom
  56. {
  57. get => this._window;
  58. set
  59. {
  60. contentPane = value;
  61. }
  62. }
  63. /// <summary>
  64. /// 设置视图组件
  65. /// </summary>
  66. private GComponent contentPane
  67. {
  68. set
  69. {
  70. if (_window == null)
  71. {
  72. _window = new Window();
  73. }
  74. _window.contentPane = value;
  75. _window.modal = modal;
  76. }
  77. }
  78. public override void Dispose()
  79. {
  80. // if (packageName == ResPathUtil.GetUIPackagePath("CommonGame") || packageName == ResPathUtil.GetUIPackagePath("Common") || packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  81. if (_window != null)
  82. {
  83. _window.RemoveFromParent();
  84. _window.Dispose();
  85. _window = null;
  86. }
  87. base.Dispose();
  88. }
  89. /// <summary>
  90. /// 打开界面
  91. /// </summary>
  92. public override void Show()
  93. {
  94. if (_window != null)
  95. {
  96. _window.Show();
  97. }
  98. }
  99. /// <summary>
  100. /// 刷新界面
  101. /// </summary>
  102. protected override void BringToFront()
  103. {
  104. if (_window != null)
  105. {
  106. this._window.BringToFront();
  107. }
  108. }
  109. protected override void OnInit()
  110. {
  111. base.OnInit();
  112. _clickBlankToClose = true;
  113. if (isfullScreen)
  114. {
  115. viewAnimationType = EnumViewAnimationType.None;
  116. }
  117. }
  118. protected override void OnShown()
  119. {
  120. base.OnShown();
  121. CheckCreateCloseButton();
  122. }
  123. protected override void OnHide()
  124. {
  125. base.OnHide();
  126. CheckRemoveCloseButton();
  127. }
  128. protected override void OnDoHideAnimationCompleted()
  129. {
  130. if (this._window != null)
  131. {
  132. this._window.Hide();
  133. }
  134. }
  135. virtual protected void closeEventHandler(EventContext context)
  136. {
  137. Hide();
  138. }
  139. private void CheckCreateCloseButton()
  140. {
  141. if (_clickBlankToClose)
  142. {
  143. _btnClose = CloseButtonPool.GetCloseButton();
  144. _window.AddChildAt(_btnClose, 0);
  145. _btnClose.onClick.Add(closeEventHandler);
  146. _btnClose.x = -(_btnClose.width - _window.width) / 2;
  147. _btnClose.y = -(_btnClose.height - _window.height) / 2;
  148. }
  149. }
  150. private void CheckRemoveCloseButton()
  151. {
  152. if (_btnClose != null)
  153. {
  154. _btnClose.onClick.Remove(closeEventHandler);
  155. _btnClose.RemoveFromParent();
  156. CloseButtonPool.PutCloseButton(_btnClose);
  157. _btnClose = null;
  158. }
  159. }
  160. private class CloseButtonPool
  161. {
  162. private static List<GGraph> _pool = new List<GGraph>();
  163. public static GGraph GetCloseButton()
  164. {
  165. GGraph btnClose = null;
  166. if (_pool.Count > 0)
  167. {
  168. int i = _pool.Count - 1;
  169. btnClose = _pool[i];
  170. _pool.RemoveAt(_pool.Count - 1);
  171. }
  172. else
  173. {
  174. btnClose = new GGraph();
  175. btnClose.DrawRect(ViewManager.ViewWidth, GRoot.inst.height, 0, Color.red, UIConfig.modalLayerColor);
  176. btnClose.AddRelation(GRoot.inst, RelationType.Size);
  177. btnClose.alpha = 0;
  178. }
  179. return btnClose;
  180. }
  181. public static void PutCloseButton(GGraph gGraph)
  182. {
  183. if (!_pool.Contains(gGraph))
  184. {
  185. _pool.Add(gGraph);
  186. }
  187. }
  188. }
  189. }
  190. }