BaseWindow.cs 4.8 KB

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