102 lines
2.1 KiB
C#
102 lines
2.1 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
// ReSharper disable MemberCanBePrivate.Global
|
|
|
|
namespace BITKit;
|
|
|
|
public partial class TemplateBuilder : Node
|
|
{
|
|
[Export] public TemplateResource template;
|
|
|
|
[ExportCategory("Index")]
|
|
[Export] private NodeBuilder indexBuilder;
|
|
|
|
[ExportCategory("Weaver")]
|
|
[Export] private FormBuilder formBuilder;
|
|
|
|
[ExportCategory("UI 绑定")]
|
|
[Export] private Button createTemplateButton;
|
|
[Export] private Control templateBody;
|
|
|
|
public FormResource CurrentTemplate { get; private set; }
|
|
private ButtonGroup _buttonGroup;
|
|
|
|
private readonly Dictionary<string, Button> indexDictionary = new();
|
|
|
|
public override void _Ready()
|
|
{
|
|
_buttonGroup = new ButtonGroup();
|
|
|
|
if (template.IsSupportCreateTemplate && createTemplateButton is not null)
|
|
{
|
|
createTemplateButton.Pressed += () =>
|
|
{
|
|
CurrentTemplate = template.CreateTemplate();
|
|
Rebuild();
|
|
};
|
|
}
|
|
|
|
Rebuild();
|
|
}
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
template.OnStart();
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
template.OnStop();
|
|
}
|
|
|
|
public void Rebuild()
|
|
{
|
|
indexBuilder.Clear();
|
|
|
|
templateBody.Hide();
|
|
|
|
if (CurrentTemplate is not null && template.GetTemplateNames().Any(x => x == CurrentTemplate.Name))
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
CurrentTemplate = null;
|
|
}
|
|
|
|
foreach (var name in template.GetTemplateNames())
|
|
{
|
|
var container = indexBuilder.Build<UXContainer>();
|
|
var _template = this.template.GetTemplate(name);
|
|
var guid = Guid.NewGuid().ToString();
|
|
|
|
|
|
container.button.Text = name;
|
|
container.button.ButtonGroup = _buttonGroup;
|
|
container.button.ToggleMode = true;
|
|
|
|
container.button.Pressed += () =>
|
|
{
|
|
Entry(_template);
|
|
};
|
|
|
|
_template.SetMeta("RuntimeId",guid);
|
|
indexDictionary.Add(guid,container.button);
|
|
}
|
|
if (CurrentTemplate is null) return;
|
|
|
|
Entry(CurrentTemplate);
|
|
if(indexDictionary.TryGetValue(CurrentTemplate.GetMeta("RuntimeId").AsString(),out var _currentButton))
|
|
_currentButton.ButtonPressed = true;
|
|
}
|
|
public void Entry(FormResource _template)
|
|
{
|
|
CurrentTemplate = _template;
|
|
formBuilder.Build(_template);
|
|
templateBody.Show();
|
|
}
|
|
}
|