Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / NodeExtend.cs
@小李xl 小李xl on 6 Aug 2023 5 KB 更新Ui生成器
  1. using System;
  2. using System.Collections;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 该类为 node 节点通用扩展函数类
  7. /// </summary>
  8. public static class NodeExtend
  9. {
  10. /// <summary>
  11. /// 尝试将一个 Node2d 节点转换成一个 ActivityObject 对象, 如果转换失败, 则返回 null
  12. /// </summary>
  13. public static ActivityObject AsActivityObject(this Node2D node2d)
  14. {
  15. if (node2d is ActivityObject p)
  16. {
  17. return p;
  18. }
  19. var parent = node2d.GetParent();
  20. if (parent != null && parent is ActivityObject p2)
  21. {
  22. return p2;
  23. }
  24. return null;
  25. }
  26. /// <summary>
  27. /// 尝试将一个 Node2d 节点转换成一个 ActivityObject 对象, 如果转换失败, 则返回 null
  28. /// </summary>
  29. public static T AsActivityObject<T>(this Node2D node2d) where T : ActivityObject
  30. {
  31. if (node2d is T p)
  32. {
  33. return p;
  34. }
  35. var parent = node2d.GetParent();
  36. if (parent != null && parent is T p2)
  37. {
  38. return p2;
  39. }
  40. return null;
  41. }
  42.  
  43. /// <summary>
  44. /// 将节点插入的房间物体根节点
  45. /// </summary>
  46. /// <param name="node">实例</param>
  47. /// <param name="layer">放入的层</param>
  48. public static void AddToActivityRoot(this Node2D node, RoomLayerEnum layer)
  49. {
  50. GameApplication.Instance.World.GetRoomLayer(layer).AddChild(node);
  51. }
  52.  
  53. /// <summary>
  54. /// 设置Ui布局方式是否横向扩展, 如果为 true, 则 GridContainer 的宽度会撑满父物体
  55. /// </summary>
  56. public static void SetHorizontalExpand(this Control control, bool flag)
  57. {
  58. if (flag)
  59. {
  60. control.SizeFlagsHorizontal |= Control.SizeFlags.Expand;
  61. }
  62. else if ((control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0)
  63. {
  64. control.SizeFlagsHorizontal ^= Control.SizeFlags.Expand;
  65. }
  66. }
  67.  
  68. /// <summary>
  69. /// 获取Ui布局方式是否横向扩展
  70. /// </summary>
  71. public static bool GetHorizontalExpand(this Control control)
  72. {
  73. return (control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0;
  74. }
  75. /// <summary>
  76. /// 延时指定时间调用一个回调函数
  77. /// </summary>
  78. public static void CallDelay(this ICoroutine coroutine, float delayTime, Action cb)
  79. {
  80. coroutine.StartCoroutine(_CallDelay(delayTime, cb));
  81. }
  82. /// <summary>
  83. /// 延时指定时间调用一个回调函数
  84. /// </summary>
  85. public static void CallDelay<T1>(this ICoroutine coroutine, float delayTime, Action<T1> cb, T1 arg1)
  86. {
  87. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1));
  88. }
  89. /// <summary>
  90. /// 延时指定时间调用一个回调函数
  91. /// </summary>
  92. public static void CallDelay<T1, T2>(this ICoroutine coroutine, float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  93. {
  94. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2));
  95. }
  96. /// <summary>
  97. /// 延时指定时间调用一个回调函数
  98. /// </summary>
  99. public static void CallDelay<T1, T2, T3>(this ICoroutine coroutine, float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  100. {
  101. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2, arg3));
  102. }
  103. //---------------------------
  104. /// <summary>
  105. /// 延时指定时间调用一个回调函数
  106. /// </summary>
  107. public static void CallDelayInNode(this Node node, float delayTime, Action cb)
  108. {
  109. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb));
  110. }
  111. /// <summary>
  112. /// 延时指定时间调用一个回调函数
  113. /// </summary>
  114. public static void CallDelayInNode<T1>(this Node node, float delayTime, Action<T1> cb, T1 arg1)
  115. {
  116. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1));
  117. }
  118. /// <summary>
  119. /// 延时指定时间调用一个回调函数
  120. /// </summary>
  121. public static void CallDelayInNode<T1, T2>(this Node node, float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  122. {
  123. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2));
  124. }
  125. /// <summary>
  126. /// 延时指定时间调用一个回调函数
  127. /// </summary>
  128. public static void CallDelayInNode<T1, T2, T3>(this Node node, float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  129. {
  130. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2, arg3));
  131. }
  132. private static IEnumerator _CallDelay(float delayTime, Action cb)
  133. {
  134. yield return new WaitForSeconds(delayTime);
  135. cb();
  136. }
  137. private static IEnumerator _CallDelay<T1>(float delayTime, Action<T1> cb, T1 arg1)
  138. {
  139. yield return new WaitForSeconds(delayTime);
  140. cb(arg1);
  141. }
  142. private static IEnumerator _CallDelay<T1, T2>(float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  143. {
  144. yield return new WaitForSeconds(delayTime);
  145. cb(arg1, arg2);
  146. }
  147. private static IEnumerator _CallDelay<T1, T2, T3>(float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  148. {
  149. yield return new WaitForSeconds(delayTime);
  150. cb(arg1,arg2, arg3);
  151. }
  152. }