BaseWindow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ET.Log.Debug("zoya:_window:" + _window + " contentPane: " + _window.contentPane);
  68. _window.contentPane = value;
  69. ET.Log.Debug("zoya:modal0:" + _window);
  70. ET.Log.Debug("zoya:modal1:" + _window.modal);
  71. _window.modal = modal;
  72. }
  73. }
  74. public override void Dispose()
  75. {
  76. // if (packageName == ResPathUtil.GetUIPackagePath("CommonGame") || packageName == ResPathUtil.GetUIPackagePath("Common") || packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  77. base.Dispose();
  78. if (_window != null)
  79. {
  80. _window.RemoveFromParent();
  81. _window.Dispose();
  82. _window = null;
  83. }
  84. }
  85. /// <summary>
  86. /// 打开界面
  87. /// </summary>
  88. public override void Show()
  89. {
  90. if (_window != null)
  91. {
  92. _window.Show();
  93. }
  94. }
  95. protected override void OnInit()
  96. {
  97. base.OnInit();
  98. _clickBlankToClose = true;
  99. if (isfullScreen)
  100. {
  101. viewAnimationType = EnumViewAnimationType.None;
  102. }
  103. }
  104. protected override void OnShown()
  105. {
  106. base.OnShown();
  107. CheckCreateCloseButton();
  108. }
  109. protected override void OnHide()
  110. {
  111. base.OnHide();
  112. CheckRemoveCloseButton();
  113. }
  114. protected override void OnDoHideAnimationCompleted()
  115. {
  116. if (this._window != null)
  117. {
  118. this._window.Hide();
  119. }
  120. }
  121. virtual protected void closeEventHandler(EventContext context)
  122. {
  123. Hide();
  124. }
  125. private void CheckCreateCloseButton()
  126. {
  127. if (_clickBlankToClose)
  128. {
  129. _btnClose = CloseButtonPool.GetCloseButton();
  130. _window.AddChildAt(_btnClose, 0);
  131. _btnClose.onClick.Add(closeEventHandler);
  132. _btnClose.x = -(_btnClose.width - _window.width) / 2;
  133. _btnClose.y = -(_btnClose.height - _window.height) / 2;
  134. }
  135. }
  136. private void CheckRemoveCloseButton()
  137. {
  138. if (_btnClose != null)
  139. {
  140. _btnClose.onClick.Remove(closeEventHandler);
  141. _btnClose.RemoveFromParent();
  142. CloseButtonPool.PutCloseButton(_btnClose);
  143. _btnClose = null;
  144. }
  145. }
  146. private class CloseButtonPool
  147. {
  148. private static List<GGraph> _pool = new List<GGraph>();
  149. public static GGraph GetCloseButton()
  150. {
  151. GGraph btnClose = null;
  152. if (_pool.Count > 0)
  153. {
  154. int i = _pool.Count - 1;
  155. btnClose = _pool[i];
  156. _pool.RemoveAt(_pool.Count - 1);
  157. }
  158. else
  159. {
  160. btnClose = new GGraph();
  161. btnClose.DrawRect(GRoot.inst.width, GRoot.inst.height, 0, Color.red, UIConfig.modalLayerColor);
  162. btnClose.AddRelation(GRoot.inst, RelationType.Size);
  163. btnClose.alpha = 0;
  164. }
  165. return btnClose;
  166. }
  167. public static void PutCloseButton(GGraph gGraph)
  168. {
  169. if (!_pool.Contains(gGraph))
  170. {
  171. _pool.Add(gGraph);
  172. }
  173. }
  174. }
  175. }
  176. }