Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / UiNode.cs
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// Ui节点父类
  6. /// </summary>
  7. /// <typeparam name="TUi">所属Ui面板类型</typeparam>
  8. /// <typeparam name="TNodeType">Godot中的节点类型</typeparam>
  9. /// <typeparam name="TCloneType">克隆该对象返回的类型</typeparam>
  10. public abstract class UiNode<TUi, TNodeType, TCloneType>
  11. : IUiNode, IUiCellNode, IClone<TCloneType>
  12. where TUi : UiBase
  13. where TNodeType : Node
  14. where TCloneType : IUiCellNode
  15. {
  16. /// <summary>
  17. /// 当前Ui节点所属的Ui面板对象
  18. /// </summary>
  19. public TUi UiPanel { get; }
  20. /// <summary>
  21. /// Godot节点实例
  22. /// </summary>
  23. public TNodeType Instance { get; }
  24. /// <summary>
  25. /// 克隆当前对象, 并返回新的对象,
  26. /// 注意: 如果子节点改名或者移动层级, 那么有可能对导致属性中的子节点无法访问
  27. /// </summary>
  28. public abstract TCloneType Clone();
  29.  
  30. public UiNode(TUi uiPanel, TNodeType node)
  31. {
  32. UiPanel = uiPanel;
  33. Instance = node;
  34. if (node is IUiNodeScript uiNodeScript)
  35. {
  36. uiPanel.RecordUiNodeScript(uiNodeScript);
  37. uiNodeScript.SetUiNode(this);
  38. }
  39. }
  40. //已知问题: 通过 OpenNestedUi() 打开子Ui, 然后再克隆当前节点, 被克隆出来的节点的子Ui不会被调用生命周期函数, 也就是没有被记录
  41. public UiBase OpenNestedUi(string uiName, UiBase prevUi = null)
  42. {
  43. var packedScene = ResourceManager.Load<PackedScene>("res://" + GameConfig.UiPrefabDir + uiName + ".tscn");
  44. var uiBase = packedScene.Instantiate<UiBase>();
  45. uiBase.Visible = false;
  46. uiBase.PrevUi = prevUi;
  47. Instance.AddChild(uiBase);
  48. UiPanel.RecordNestedUi(uiBase, this, UiManager.RecordType.Open);
  49. uiBase.OnCreateUi();
  50. uiBase.OnInitNestedUi();
  51. if (UiPanel.IsOpen)
  52. {
  53. uiBase.ShowUi();
  54. }
  55. return uiBase;
  56. }
  57.  
  58. public T OpenNestedUi<T>(string uiName, UiBase prevUi = null) where T : UiBase
  59. {
  60. return (T)OpenNestedUi(uiName, prevUi);
  61. }
  62.  
  63. /// <summary>
  64. /// 克隆当前节点, 并放到同父节点下
  65. /// </summary>
  66. public TCloneType CloneAndPut()
  67. {
  68. var inst = Clone();
  69. Instance.GetParent().AddChild(inst.GetUiInstance());
  70. return inst;
  71. }
  72.  
  73. public UiBase GetUiPanel()
  74. {
  75. return UiPanel;
  76. }
  77.  
  78. public Node GetUiInstance()
  79. {
  80. return Instance;
  81. }
  82.  
  83. public IUiCellNode CloneUiCell()
  84. {
  85. return Clone();
  86. }
  87.  
  88. public void AddChild(IUiNode uiNode)
  89. {
  90. Instance.AddChild(uiNode.GetUiInstance());
  91. }
  92.  
  93. public void AddChild(Node node)
  94. {
  95. Instance.AddChild(node);
  96. }
  97. public void RemoveChild(IUiNode uiNode)
  98. {
  99. Instance.RemoveChild(uiNode.GetUiInstance());
  100. }
  101. public void RemoveChild(Node node)
  102. {
  103. Instance.RemoveChild(node);
  104. }
  105.  
  106. public void QueueFree()
  107. {
  108. Instance.QueueFree();
  109. }
  110.  
  111. public void Reparent(IUiNode uiNode)
  112. {
  113. Instance.Reparent(uiNode.GetUiInstance());
  114. }
  115. public void Reparent(Node node)
  116. {
  117. Instance.Reparent(node);
  118. }
  119.  
  120. public Node GetParent()
  121. {
  122. return Instance.GetParent();
  123. }
  124. }