diff --git a/DungeonShooting_Godot/src/framework/generator/UiGenerator.cs b/DungeonShooting_Godot/src/framework/generator/UiGenerator.cs index e9970d7..fd72026 100644 --- a/DungeonShooting_Godot/src/framework/generator/UiGenerator.cs +++ b/DungeonShooting_Godot/src/framework/generator/UiGenerator.cs @@ -8,17 +8,94 @@ public static class UiGenerator { private static int _nodeIndex = 0; - + public static void GenerateUi(Control control) { _nodeIndex = 0; var uiNode = EachNode(control); + var code = GenerateClassCode(uiNode); + GD.Print("code: \n" + code); } + private static string GenerateClassCode(UiNode uiNode) + { + return $"namespace UI;\n\n" + + $"public abstract partial class {uiNode.Name} : UiBase\n" + + $"{{\n" + + GeneratePropertyListClassCode("", uiNode.Name + ".", uiNode, " ") + + $"\n\n" + + GenerateAllChildrenClassCode(uiNode.Name + ".", uiNode, " ") + + $"}}\n"; + } + + private static string GenerateAllChildrenClassCode(string parent, UiNode uiNode, string retraction) + { + var str = ""; + if (uiNode.Children != null) + { + for (var i = 0; i < uiNode.Children.Count; i++) + { + var item = uiNode.Children[i]; + str += GenerateAllChildrenClassCode(parent + item.Name + ".", item, retraction); + str += GenerateChildrenClassCode(parent, item, retraction); + } + } + + return str; + } + + private static string GenerateChildrenClassCode(string parent, UiNode uiNode, string retraction) + { + return retraction + $"/// \n" + + retraction + $"/// 类型: , 路径: {parent}{uiNode.Name}\n" + + retraction + $"/// \n" + + retraction + $"public class {uiNode.ClassName}\n" + + retraction + $"{{\n" + + retraction + $" /// \n" + + retraction + $" /// Ui节点实例, 节点类型: , 节点路径: {parent}{uiNode.Name}\n" + + retraction + $" /// \n" + + retraction + $" public {uiNode.TypeName} Instance {{ get; }}\n\n" + + GeneratePropertyListClassCode("Instance.", parent, uiNode, retraction + " ") + + retraction + $" public {uiNode.ClassName}({uiNode.TypeName} node) => Instance = node;\n" + + retraction + $" public {uiNode.ClassName} Clone() => new (({uiNode.TypeName})Instance.Duplicate());\n" + + retraction + $"}}\n\n"; + } + + private static string GeneratePropertyListClassCode(string target, string parent, UiNode uiNode, string retraction) + { + var str = ""; + if (uiNode.Children != null) + { + for (var i = 0; i < uiNode.Children.Count; i++) + { + var item = uiNode.Children[i]; + str += GeneratePropertyCode(target, parent, item, retraction); + } + } + + return str; + } + + private static string GeneratePropertyCode(string target, string parent, UiNode uiNode, string retraction) + { + return retraction + $"/// \n" + + retraction + $"/// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: {parent}{uiNode.Name}\n" + + retraction + $"/// \n" + + retraction + $"public {uiNode.ClassName} {uiNode.Name}\n" + + retraction + $"{{\n" + + retraction + $" get\n" + + retraction + $" {{\n" + + retraction + $" if (_{uiNode.Name} == null) _{uiNode.Name} = new {uiNode.ClassName}({target}GetNode<{uiNode.TypeName}>(\"{uiNode.Name}\"));\n" + + retraction + $" return _{uiNode.Name};\n" + + retraction + $" }}\n" + + retraction + $"}}\n" + + retraction + $"private {uiNode.ClassName} _{uiNode.Name};\n\n"; + } + private static UiNode EachNode(Control control) { var name = Regex.Replace(control.Name, "[^\\w_]", ""); - var uiNode = new UiNode(name, "UiNode" + (++_nodeIndex) + "_" + name, control.GetType().FullName); + var uiNode = new UiNode(name, "UiNode" + (_nodeIndex++) + "_" + name, control.GetType().FullName); var childCount = control.GetChildCount(); if (childCount > 0) diff --git a/DungeonShooting_Godot/src/game/ui/RoomUI.cs b/DungeonShooting_Godot/src/game/ui/RoomUI.cs index 2f25757..2e69718 100644 --- a/DungeonShooting_Godot/src/game/ui/RoomUI.cs +++ b/DungeonShooting_Godot/src/game/ui/RoomUI.cs @@ -54,6 +54,8 @@ ReloadBar = GetNode("ViewNode/ReloadBar"); ReloadBar.Visible = false; + + Generator.UiGenerator.GenerateUi(this); } public override void _Ready() diff --git a/DungeonShooting_Godot/src/game/ui/roomUI/RoomUI.cs b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUI.cs new file mode 100644 index 0000000..0a9ac2e --- /dev/null +++ b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUI.cs @@ -0,0 +1,277 @@ +namespace UI; + +public abstract partial class RoomUI : UiBase +{ + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control + /// + public UiNode1_Control Control + { + get + { + if (_Control == null) _Control = new UiNode1_Control(GetNode("Control")); + return _Control; + } + } + private UiNode1_Control _Control; + + + + /// + /// 类型: , 路径: RoomUI.Control.HealthBar.HpSlot.HpBar + /// + public class UiNode4_HpBar + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.HealthBar.HpSlot.HpBar + /// + public Godot.TextureRect Instance { get; } + + public UiNode4_HpBar(Godot.TextureRect node) => Instance = node; + public UiNode4_HpBar Clone() => new ((Godot.TextureRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.HealthBar.HpSlot + /// + public class UiNode3_HpSlot + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.HealthBar.HpSlot + /// + public Godot.NinePatchRect Instance { get; } + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.HealthBar.HpBar + /// + public UiNode4_HpBar HpBar + { + get + { + if (_HpBar == null) _HpBar = new UiNode4_HpBar(Instance.GetNode("HpBar")); + return _HpBar; + } + } + private UiNode4_HpBar _HpBar; + + public UiNode3_HpSlot(Godot.NinePatchRect node) => Instance = node; + public UiNode3_HpSlot Clone() => new ((Godot.NinePatchRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.HealthBar.ShieldSlot.ShieldBar + /// + public class UiNode6_ShieldBar + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.HealthBar.ShieldSlot.ShieldBar + /// + public Godot.TextureRect Instance { get; } + + public UiNode6_ShieldBar(Godot.TextureRect node) => Instance = node; + public UiNode6_ShieldBar Clone() => new ((Godot.TextureRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.HealthBar.ShieldSlot + /// + public class UiNode5_ShieldSlot + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.HealthBar.ShieldSlot + /// + public Godot.NinePatchRect Instance { get; } + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.HealthBar.ShieldBar + /// + public UiNode6_ShieldBar ShieldBar + { + get + { + if (_ShieldBar == null) _ShieldBar = new UiNode6_ShieldBar(Instance.GetNode("ShieldBar")); + return _ShieldBar; + } + } + private UiNode6_ShieldBar _ShieldBar; + + public UiNode5_ShieldSlot(Godot.NinePatchRect node) => Instance = node; + public UiNode5_ShieldSlot Clone() => new ((Godot.NinePatchRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.HealthBar + /// + public class UiNode2_HealthBar + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.HealthBar + /// + public Godot.TextureRect Instance { get; } + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.HpSlot + /// + public UiNode3_HpSlot HpSlot + { + get + { + if (_HpSlot == null) _HpSlot = new UiNode3_HpSlot(Instance.GetNode("HpSlot")); + return _HpSlot; + } + } + private UiNode3_HpSlot _HpSlot; + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.ShieldSlot + /// + public UiNode5_ShieldSlot ShieldSlot + { + get + { + if (_ShieldSlot == null) _ShieldSlot = new UiNode5_ShieldSlot(Instance.GetNode("ShieldSlot")); + return _ShieldSlot; + } + } + private UiNode5_ShieldSlot _ShieldSlot; + + public UiNode2_HealthBar(Godot.TextureRect node) => Instance = node; + public UiNode2_HealthBar Clone() => new ((Godot.TextureRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.MapBar + /// + public class UiNode7_MapBar + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.MapBar + /// + public Godot.TextureRect Instance { get; } + + public UiNode7_MapBar(Godot.TextureRect node) => Instance = node; + public UiNode7_MapBar Clone() => new ((Godot.TextureRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.GunBar.GunSprite + /// + public class UiNode9_GunSprite + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.GunBar.GunSprite + /// + public Godot.TextureRect Instance { get; } + + public UiNode9_GunSprite(Godot.TextureRect node) => Instance = node; + public UiNode9_GunSprite Clone() => new ((Godot.TextureRect)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.GunBar.BulletText + /// + public class UiNode10_BulletText + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.GunBar.BulletText + /// + public Godot.Label Instance { get; } + + public UiNode10_BulletText(Godot.Label node) => Instance = node; + public UiNode10_BulletText Clone() => new ((Godot.Label)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control.GunBar + /// + public class UiNode8_GunBar + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control.GunBar + /// + public Godot.Control Instance { get; } + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.GunSprite + /// + public UiNode9_GunSprite GunSprite + { + get + { + if (_GunSprite == null) _GunSprite = new UiNode9_GunSprite(Instance.GetNode("GunSprite")); + return _GunSprite; + } + } + private UiNode9_GunSprite _GunSprite; + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.Control.BulletText + /// + public UiNode10_BulletText BulletText + { + get + { + if (_BulletText == null) _BulletText = new UiNode10_BulletText(Instance.GetNode("BulletText")); + return _BulletText; + } + } + private UiNode10_BulletText _BulletText; + + public UiNode8_GunBar(Godot.Control node) => Instance = node; + public UiNode8_GunBar Clone() => new ((Godot.Control)Instance.Duplicate()); + } + + /// + /// 类型: , 路径: RoomUI.Control + /// + public class UiNode1_Control + { + /// + /// Ui节点实例, 节点类型: , 节点路径: RoomUI.Control + /// + public Godot.Control Instance { get; } + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.HealthBar + /// + public UiNode2_HealthBar HealthBar + { + get + { + if (_HealthBar == null) _HealthBar = new UiNode2_HealthBar(Instance.GetNode("HealthBar")); + return _HealthBar; + } + } + private UiNode2_HealthBar _HealthBar; + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.MapBar + /// + public UiNode7_MapBar MapBar + { + get + { + if (_MapBar == null) _MapBar = new UiNode7_MapBar(Instance.GetNode("MapBar")); + return _MapBar; + } + } + private UiNode7_MapBar _MapBar; + + /// + /// 使用 Instance 属性获取当前节点实例对象, 节点类型: , 节点路径: RoomUI.GunBar + /// + public UiNode8_GunBar GunBar + { + get + { + if (_GunBar == null) _GunBar = new UiNode8_GunBar(Instance.GetNode("GunBar")); + return _GunBar; + } + } + private UiNode8_GunBar _GunBar; + + public UiNode1_Control(Godot.Control node) => Instance = node; + public UiNode1_Control Clone() => new ((Godot.Control)Instance.Duplicate()); + } + +} diff --git a/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs new file mode 100644 index 0000000..c623816 --- /dev/null +++ b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs @@ -0,0 +1,14 @@ +namespace UI; + +public partial class RoomUIPanel : RoomUI +{ + public override void OnOpen(params object[] args) + { + + } + + public override void OnClose() + { + + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/test/TestUi/TestPanel.cs b/DungeonShooting_Godot/src/test/TestUi/TestPanel.cs index 58f134e..a65c268 100644 --- a/DungeonShooting_Godot/src/test/TestUi/TestPanel.cs +++ b/DungeonShooting_Godot/src/test/TestUi/TestPanel.cs @@ -1,6 +1,6 @@ namespace UI; -public class TestPanel : Test +public partial class TestPanel : Test { public override void OnOpen(params object[] args) {