using Godot; using System; using System.Collections.Generic; // ReSharper disable MemberCanBePrivate.Global namespace BITKit; public partial class NodeBuilder : Node { [Export] private bool clearOnStart = true; [Export] private bool clearTemplateOnly = true; [ExportCategory("UX")] [Export] private Control emptyHints; [ExportCategory("Template")] [Export] private PackedScene template; public Node[] Instances => _instances.ToArray(); private readonly List _instances = new List(); public override void _Ready() { if (clearOnStart) { Clear(); } } public T Build() where T : Node, new() { Node instance; if (template is not null) { instance = template.Instantiate(); AddChild(instance); } else { instance = this.Create(); } emptyHints?.Hide(); _instances.Add(instance); return (T)instance; } public void Clear() { _instances.Clear(); emptyHints?.Show(); if (clearTemplateOnly && template is not null) { foreach (var x in GetChildren()) { switch (template) { case null: x.QueueFree(); break; case (var y) when y.ResourcePath == x.SceneFilePath: x.QueueFree(); break; } } } else { MathNode.ClearChild(this); } } }