60 lines
1.1 KiB
C#
60 lines
1.1 KiB
C#
|
using Godot;
|
||
|
using System;
|
||
|
// ReSharper disable MemberCanBePrivate.Global
|
||
|
|
||
|
namespace BITKit;
|
||
|
public partial class NodeBuilder : Node
|
||
|
{
|
||
|
[Export] private bool clearOnStart = true;
|
||
|
[Export] private bool clearTemplateOnly = true;
|
||
|
[Export] private Control container;
|
||
|
[ExportCategory("UX")]
|
||
|
[Export] private Control emptyHints;
|
||
|
[ExportCategory("Template")]
|
||
|
[Export] private PackedScene template;
|
||
|
|
||
|
public override void _Ready()
|
||
|
{
|
||
|
if (clearOnStart)
|
||
|
{
|
||
|
Clear();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Node Build()
|
||
|
{
|
||
|
var instance = template.Instantiate();
|
||
|
if (container is not null)
|
||
|
{
|
||
|
container.AddChild(instance);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
AddChild(instance);
|
||
|
}
|
||
|
emptyHints?.Hide();
|
||
|
return instance;
|
||
|
}
|
||
|
public T Build<T>() where T : Node
|
||
|
{
|
||
|
var instance = Build() as T;
|
||
|
return instance;
|
||
|
}
|
||
|
public void Clear()
|
||
|
{
|
||
|
emptyHints?.Show();
|
||
|
if (clearTemplateOnly)
|
||
|
{
|
||
|
foreach (var x in container is not null ? container.GetChildren() : GetChildren())
|
||
|
{
|
||
|
if(x.SceneFilePath == template.ResourcePath)
|
||
|
x.QueueFree();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
MathNode.ClearChild(container is not null ? container:this);
|
||
|
}
|
||
|
}
|
||
|
}
|