Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
anosatsuk124 committed Aug 2, 2024
1 parent efbf251 commit 576a206
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
61 changes: 37 additions & 24 deletions Sources/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ private void GenerateClassFile()

private void GenerateClass()
{
source.AppendLine($"public partial class {className} : Control");
var type = root.Name.LocalName;
source.AppendLine($"public partial class {className} : {type}");
source.AppendLine("{");

GenerateProperties(root);

GenerateReadyMethod();

GenerateGetRootMethod(root);
GenerateGetRootMethod();

source.AppendLine("}");
}
Expand Down Expand Up @@ -85,14 +86,17 @@ private void GenerateReadyMethod()

Root("this");

GenerateInstantiationBlock(root);
GenerateSetPropertyStatemement("this", root);

var children = root.Elements();
GenerateInstantiationBlock(children);

source.AppendLine("}");
}

private void GenerateGetRootMethod(XElement element)
private void GenerateGetRootMethod()
{
var type = element.Name.LocalName;
var type = root.Name.LocalName;
var getChild = $"GetChild<{type}>(0)";

source.AppendLine($"public {type} GetRoot()");
Expand Down Expand Up @@ -143,8 +147,24 @@ private void SetProperty(string target, string property, string value)
source.AppendLine($"{target}.{property} = \"{value}\";");
}

private void GenerateSetPropertyStatemement(string node, XElement element)
{
var id = element.Attribute(IDAttribute);
if (id is not null)
{
var name = id.Value;
source.AppendLine($"this.{name} = {node};");
}

var attrs = element.Attributes().Where(a => a.Name != IDAttribute && a.Name != ProtoSceneAttribute);
foreach (var attr in attrs)
{
SetProperty(node, attr.Name.LocalName, attr.Value);
}
}

// TODO: Refactor this with splitting the code into multiple methods.
private void GenerateInstantiationBlock(XElement element)
private void GenerateInstantiationStatements(XElement element)
{
var type = element.Name.LocalName;

Expand All @@ -162,30 +182,23 @@ private void GenerateInstantiationBlock(XElement element)
node = Node($"new {type}()");
}

var id = element.Attribute(IDAttribute);
if (id is not null)
{
var name = id.Value;
source.AppendLine($"this.{name} = {node};");
}

var attrs = element.Attributes().Where(a => a.Name != IDAttribute && a.Name != ProtoSceneAttribute);
foreach (var attr in attrs)
{
SetProperty(node, attr.Name.LocalName, attr.Value);
}
GenerateSetPropertyStatemement(node, element);

AddChild(rootStack.Peek(), node);

var elements = element.Elements();
if (elements.Count() == 0) return;

Root(node);
foreach (var elem in elements)
}

private void GenerateInstantiationBlock(IEnumerable<XElement> elements)
{
foreach (var element in elements)
{
GenerateInstantiationBlock(elem);
GenerateInstantiationStatements(element);

var children = element.Elements();
GenerateInstantiationBlock(children);
rootStack.Pop();
}
rootStack.Pop();
}
}

50 changes: 26 additions & 24 deletions examples/TestNode.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@ public partial class TestNode : Control
public override void _Ready()
{
var root_0 = this;
var scene_0 = GD.Load<PackedScene>("res://my_window.tscn");
var node_0 = scene_0.Instantiate<Control>();
this.root = node_0;
this.root = this;
var node_0 = new Button();
this.unique1 = node_0;
node_0.Text = "Hello";
root_0.AddChild(node_0);
var root_1 = node_0;
var node_1 = new Button();
this.unique1 = node_1;
node_1.Text = "Hello";
var node_1 = new Label();
this.unique2 = node_1;
node_1.Text = "World";
root_1.AddChild(node_1);
var root_2 = node_1;
var node_2 = new Label();
this.unique2 = node_2;
node_2.Text = "World";
root_2.AddChild(node_2);
var node_3 = new RichTextLabel();
this.unique4 = node_3;
root_2.AddChild(node_3);
var node_2 = new RichTextLabel();
this.unique4 = node_2;
root_1.AddChild(node_2);
var root_3 = node_2;
var node_3 = new Label();
this.unique3 = node_3;
root_1.AddChild(node_3);
var root_4 = node_3;
var node_4 = new Label();
this.unique3 = node_4;
root_2.AddChild(node_4);
var node_5 = new Label();
this.unique5 = node_5;
root_1.AddChild(node_5);
var node_6 = new Button();
node_6.Text = "Hello No id";
root_1.AddChild(node_6);
var scene_1 = GD.Load<PackedScene>("res://my_label.tscn");
var node_7 = scene_1.Instantiate<Label>();
root_1.AddChild(node_7);
this.unique5 = node_4;
root_0.AddChild(node_4);
var root_5 = node_4;
var node_5 = new Button();
node_5.Text = "Hello No id";
root_0.AddChild(node_5);
var root_6 = node_5;
var scene_0 = GD.Load<PackedScene>("res://my_label.tscn");
var node_6 = scene_0.Instantiate<Label>();
root_0.AddChild(node_6);
var root_7 = node_6;
}
public Control GetRoot()
{
Expand Down
15 changes: 6 additions & 9 deletions examples/TestNode2.generated.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using Godot;
public partial class TestNode2 : Control
public partial class TestNode2 : TestNode
{
public TestNode Nyan;
public override void _Ready()
{
var root_0 = this;
var scene_0 = GD.Load<PackedScene>("res://Scenes/TestNode.tscn");
var node_0 = scene_0.Instantiate<TestNode>();
var node_0 = new Button();
root_0.AddChild(node_0);
var root_1 = node_0;
var node_1 = new Button();
var scene_0 = GD.Load<PackedScene>("res://Scenes/TestNode.tscn");
var node_1 = scene_0.Instantiate<TestNode>();
this.Nyan = node_1;
node_1.TestValue = "Myaow";
root_1.AddChild(node_1);
var root_2 = node_1;
var scene_1 = GD.Load<PackedScene>("res://Scenes/TestNode.tscn");
var node_2 = scene_1.Instantiate<TestNode>();
this.Nyan = node_2;
node_2.TestValue = "Myaow";
root_2.AddChild(node_2);
}
public TestNode GetRoot()
{
Expand Down

0 comments on commit 576a206

Please sign in to comment.