Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / UiNode.cs
@小李xl 小李xl on 22 Aug 2023 3 KB 完成保存与未保存状态
  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.PrevUi = prevUi;
  46. Instance.AddChild(uiBase);
  47. UiPanel.RecordNestedUi(uiBase, this, UiManager.RecordType.Open);
  48. uiBase.OnCreateUi();
  49. uiBase.OnInitNestedUi();
  50. if (UiPanel.IsOpen)
  51. {
  52. uiBase.ShowUi();
  53. }
  54. return uiBase;
  55. }
  56.  
  57. public T OpenNestedUi<T>(string uiName, UiBase prevUi = null) where T : UiBase
  58. {
  59. return (T)OpenNestedUi(uiName, prevUi);
  60. }
  61.  
  62. /// <summary>
  63. /// 克隆当前节点, 并放到同父节点下
  64. /// </summary>
  65. public TCloneType CloneAndPut()
  66. {
  67. var inst = Clone();
  68. Instance.GetParent().AddChild(inst.GetUiInstance());
  69. return inst;
  70. }
  71.  
  72. public UiBase GetUiPanel()
  73. {
  74. return UiPanel;
  75. }
  76.  
  77. public Node GetUiInstance()
  78. {
  79. return Instance;
  80. }
  81.  
  82. public IUiCellNode CloneUiCell()
  83. {
  84. return Clone();
  85. }
  86.  
  87. public void AddChild(IUiNode uiNode)
  88. {
  89. Instance.AddChild(uiNode.GetUiInstance());
  90. }
  91.  
  92. public void AddChild(Node node)
  93. {
  94. Instance.AddChild(node);
  95. }
  96. public void RemoveChild(IUiNode uiNode)
  97. {
  98. Instance.RemoveChild(uiNode.GetUiInstance());
  99. }
  100. public void RemoveChild(Node node)
  101. {
  102. Instance.RemoveChild(node);
  103. }
  104.  
  105. public void QueueFree()
  106. {
  107. Instance.QueueFree();
  108. }
  109.  
  110. public void Reparent(IUiNode uiNode)
  111. {
  112. Instance.Reparent(uiNode.GetUiInstance());
  113. }
  114. public void Reparent(Node node)
  115. {
  116. Instance.Reparent(node);
  117. }
  118.  
  119. public Node GetParent()
  120. {
  121. return Instance.GetParent();
  122. }
  123. }