Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / UiBase.cs
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// Ui 基类
  8. /// </summary>
  9. [Tool]
  10. public abstract partial class UiBase : Control
  11. {
  12. /// <summary>
  13. /// 当前 UI 所属层级
  14. /// </summary>
  15. [Export]
  16. public UiLayer Layer = UiLayer.Middle;
  17.  
  18. /// <summary>
  19. /// Ui 模式, 单例/正常模式
  20. /// </summary>
  21. [Export]
  22. public UiMode Mode = UiMode.Normal;
  23.  
  24. /// <summary>
  25. /// 阻止下层 Ui 点击
  26. /// </summary>
  27. [Export]
  28. public bool KeepOut = false;
  29.  
  30. /// <summary>
  31. /// ui名称
  32. /// </summary>
  33. public string UiName { get; }
  34. /// <summary>
  35. /// 是否已经打开ui
  36. /// </summary>
  37. public bool IsOpen { get; private set; } = false;
  38. /// <summary>
  39. /// 是否已经销毁
  40. /// </summary>
  41. public bool IsDisposed { get; private set; } = false;
  42.  
  43. //开启的协程
  44. private List<CoroutineData> _coroutineList;
  45. public UiBase(string uiName)
  46. {
  47. UiName = uiName;
  48. //记录ui打开
  49. UiManager.RecordUi(this, UiManager.RecordType.Open);
  50. }
  51. /// <summary>
  52. /// 创建当前ui时调用
  53. /// </summary>
  54. public virtual void OnCreateUi()
  55. {
  56. }
  57. /// <summary>
  58. /// 当前ui显示时调用
  59. /// </summary>
  60. public abstract void OnShowUi();
  61.  
  62. /// <summary>
  63. /// 当前ui隐藏时调用
  64. /// </summary>
  65. public abstract void OnHideUi();
  66.  
  67. /// <summary>
  68. /// 销毁当前ui时调用
  69. /// </summary>
  70. public virtual void OnDisposeUi()
  71. {
  72. }
  73.  
  74. /// <summary>
  75. /// 每帧调用一次
  76. /// </summary>
  77. public virtual void Process(float delta)
  78. {
  79. }
  80.  
  81. /// <summary>
  82. /// 显示ui
  83. /// </summary>
  84. public void ShowUi()
  85. {
  86. if (IsOpen)
  87. {
  88. return;
  89. }
  90.  
  91. IsOpen = true;
  92. Visible = true;
  93. OnShowUi();
  94. }
  95. /// <summary>
  96. /// 隐藏ui, 不会执行销毁
  97. /// </summary>
  98. public void HideUi()
  99. {
  100. if (!IsOpen)
  101. {
  102. return;
  103. }
  104.  
  105. IsOpen = false;
  106. Visible = false;
  107. OnHideUi();
  108. }
  109.  
  110. /// <summary>
  111. /// 关闭并销毁ui
  112. /// </summary>
  113. public void DisposeUi()
  114. {
  115. if (IsDisposed)
  116. {
  117. return;
  118. }
  119. //记录ui关闭
  120. UiManager.RecordUi(this, UiManager.RecordType.Close);
  121. HideUi();
  122. IsDisposed = true;
  123. OnDisposeUi();
  124. QueueFree();
  125. }
  126.  
  127. public sealed override void _Process(double delta)
  128. {
  129. var newDelta = (float)delta;
  130. Process(newDelta);
  131. //协程更新
  132. if (_coroutineList != null)
  133. {
  134. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// 开启一个协程, 返回协程 id, 协程是在普通帧执行的, 支持: 协程嵌套, WaitForSeconds, WaitForFixedProcess, Task, SignalAwaiter
  140. /// </summary>
  141. public long StartCoroutine(IEnumerator able)
  142. {
  143. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  144. }
  145.  
  146. /// <summary>
  147. /// 根据协程 id 停止协程
  148. /// </summary>
  149. public void StopCoroutine(long coroutineId)
  150. {
  151. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  152. }
  153. /// <summary>
  154. /// 停止所有协程
  155. /// </summary>
  156. public void StopAllCoroutine()
  157. {
  158. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  159. }
  160. /// <summary>
  161. /// 延时指定时间调用一个回调函数
  162. /// </summary>
  163. public void CallDelay(float delayTime, Action cb)
  164. {
  165. StartCoroutine(_CallDelay(delayTime, cb));
  166. }
  167. /// <summary>
  168. /// 延时指定时间调用一个回调函数
  169. /// </summary>
  170. public void CallDelay<T1>(float delayTime, Action<T1> cb, T1 arg1)
  171. {
  172. StartCoroutine(_CallDelay(delayTime, cb, arg1));
  173. }
  174. /// <summary>
  175. /// 延时指定时间调用一个回调函数
  176. /// </summary>
  177. public void CallDelay<T1, T2>(float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  178. {
  179. StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2));
  180. }
  181. /// <summary>
  182. /// 延时指定时间调用一个回调函数
  183. /// </summary>
  184. public void CallDelay<T1, T2, T3>(float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  185. {
  186. StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2, arg3));
  187. }
  188.  
  189. private IEnumerator _CallDelay(float delayTime, Action cb)
  190. {
  191. yield return new WaitForSeconds(delayTime);
  192. cb();
  193. }
  194. private IEnumerator _CallDelay<T1>(float delayTime, Action<T1> cb, T1 arg1)
  195. {
  196. yield return new WaitForSeconds(delayTime);
  197. cb(arg1);
  198. }
  199. private IEnumerator _CallDelay<T1, T2>(float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  200. {
  201. yield return new WaitForSeconds(delayTime);
  202. cb(arg1, arg2);
  203. }
  204. private IEnumerator _CallDelay<T1, T2, T3>(float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  205. {
  206. yield return new WaitForSeconds(delayTime);
  207. cb(arg1,arg2, arg3);
  208. }
  209. }