BaseWindow.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /// <summary>
  28. /// 点击空白关闭,true关闭,false不关闭,默认值关闭
  29. /// </summary>
  30. protected bool clickBlankToClose
  31. {
  32. set
  33. {
  34. _clickBlankToClose = value;
  35. if (this.viewCom != null && this.viewCom.root != null)
  36. {
  37. if (_clickBlankToClose)
  38. {
  39. CheckCreateCloseButton();
  40. }
  41. else
  42. {
  43. CheckRemoveCloseButton();
  44. }
  45. }
  46. }
  47. }
  48. public override GComponent viewCom
  49. {
  50. get => this._window;
  51. set
  52. {
  53. contentPane = value;
  54. }
  55. }
  56. /// <summary>
  57. /// 设置视图组件
  58. /// </summary>
  59. private GComponent contentPane
  60. {
  61. set
  62. {
  63. if (_window == null)
  64. {
  65. _window = new Window();
  66. }
  67. _window.contentPane = value;
  68. _window.modal = modal;
  69. }
  70. }
  71. public override void Dispose()
  72. {
  73. // if (packageName == ResPathUtil.GetUIPackagePath("CommonGame") || packageName == ResPathUtil.GetUIPackagePath("Common") || packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  74. base.Dispose();
  75. if (_window != null)
  76. {
  77. _window.RemoveFromParent();
  78. _window.Dispose();
  79. _window = null;
  80. }
  81. }
  82. /// <summary>
  83. /// 打开界面
  84. /// </summary>
  85. public override void Show()
  86. {
  87. if (_window != null)
  88. {
  89. _window.Show();
  90. }
  91. }
  92. protected override void OnInit()
  93. {
  94. base.OnInit();
  95. _clickBlankToClose = true;
  96. if (isfullScreen)
  97. {
  98. viewAnimationType = EnumViewAnimationType.None;
  99. }
  100. }
  101. protected override void OnShown()
  102. {
  103. base.OnShown();
  104. CheckCreateCloseButton();
  105. }
  106. protected override void OnHide()
  107. {
  108. base.OnHide();
  109. CheckRemoveCloseButton();
  110. }
  111. protected override void OnDoHideAnimationCompleted()
  112. {
  113. if (this._window != null)
  114. {
  115. this._window.Hide();
  116. }
  117. }
  118. virtual protected void closeEventHandler(EventContext context)
  119. {
  120. Hide();
  121. }
  122. private void CheckCreateCloseButton()
  123. {
  124. if (_clickBlankToClose)
  125. {
  126. _btnClose = CloseButtonPool.GetCloseButton();
  127. _window.AddChildAt(_btnClose, 0);
  128. _btnClose.onClick.Add(closeEventHandler);
  129. _btnClose.x = -(_btnClose.width - _window.width) / 2;
  130. _btnClose.y = -(_btnClose.height - _window.height) / 2;
  131. }
  132. }
  133. private void CheckRemoveCloseButton()
  134. {
  135. if (_btnClose != null)
  136. {
  137. _btnClose.onClick.Remove(closeEventHandler);
  138. _btnClose.RemoveFromParent();
  139. CloseButtonPool.PutCloseButton(_btnClose);
  140. _btnClose = null;
  141. }
  142. }
  143. private class CloseButtonPool
  144. {
  145. private static List<GGraph> _pool = new List<GGraph>();
  146. public static GGraph GetCloseButton()
  147. {
  148. GGraph btnClose = null;
  149. if (_pool.Count > 0)
  150. {
  151. int i = _pool.Count - 1;
  152. btnClose = _pool[i];
  153. _pool.RemoveAt(_pool.Count - 1);
  154. }
  155. else
  156. {
  157. btnClose = new GGraph();
  158. btnClose.DrawRect(GRoot.inst.width, GRoot.inst.height, 0, Color.red, UIConfig.modalLayerColor);
  159. btnClose.AddRelation(GRoot.inst, RelationType.Size);
  160. btnClose.alpha = 0;
  161. }
  162. return btnClose;
  163. }
  164. public static void PutCloseButton(GGraph gGraph)
  165. {
  166. if (!_pool.Contains(gGraph))
  167. {
  168. _pool.Add(gGraph);
  169. }
  170. }
  171. }
  172. }
  173. }