Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / ui / UiNode.cs
@小李xl 小李xl on 9 Aug 2023 2 KB 抽出IUiNode接口
  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. }
  35. public UiBase OpenNestedUi(string uiName, UiBase prevUi = null)
  36. {
  37. var packedScene = ResourceManager.Load<PackedScene>("res://" + GameConfig.UiPrefabDir + uiName + ".tscn");
  38. var uiBase = packedScene.Instantiate<UiBase>();
  39. uiBase.PrevUi = prevUi;
  40. Instance.AddChild(uiBase);
  41. UiPanel.RecordNestedUi(uiBase, this, UiManager.RecordType.Open);
  42. uiBase.OnCreateUi();
  43. uiBase.OnInitNestedUi();
  44. if (UiPanel.IsOpen)
  45. {
  46. uiBase.ShowUi();
  47. }
  48. return uiBase;
  49. }
  50.  
  51. public T OpenNestedUi<T>(string uiName, UiBase prevUi = null) where T : UiBase
  52. {
  53. return (T)OpenNestedUi(uiName, prevUi);
  54. }
  55.  
  56. /// <summary>
  57. /// 克隆当前节点, 并放到同父节点下
  58. /// </summary>
  59. public TCloneType CloneAndPut()
  60. {
  61. var inst = Clone();
  62. Instance.GetParent().AddChild(inst.GetUiInstance());
  63. return inst;
  64. }
  65.  
  66. public UiBase GetUiPanel()
  67. {
  68. return UiPanel;
  69. }
  70.  
  71. public Node GetUiInstance()
  72. {
  73. return Instance;
  74. }
  75.  
  76. public IUiCellNode CloneUiCell()
  77. {
  78. return Clone();
  79. }
  80. }