iFactory.Godot/BITKit/Scripts/Builder/TemplateBuilder/TemplateBuilder.cs

102 lines
2.1 KiB
C#
Raw Normal View History

2023-07-17 04:10:14 +08:00
using Godot;
using System;
2023-07-18 20:57:02 +08:00
using System.Collections.Generic;
using System.Linq;
2023-07-17 04:10:14 +08:00
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit;
2023-07-18 20:57:02 +08:00
2023-07-17 04:10:14 +08:00
public partial class TemplateBuilder : Node
{
[Export] public TemplateResource template;
2023-07-18 20:57:02 +08:00
2023-07-17 04:10:14 +08:00
[ExportCategory("Index")]
[Export] private NodeBuilder indexBuilder;
[ExportCategory("Weaver")]
[Export] private FormBuilder formBuilder;
2023-07-18 20:57:02 +08:00
[ExportCategory("UI 绑定")]
[Export] private Button createTemplateButton;
[Export] private Control templateBody;
2023-07-17 04:10:14 +08:00
public FormResource CurrentTemplate { get; private set; }
private ButtonGroup _buttonGroup;
2023-07-18 20:57:02 +08:00
private readonly Dictionary<string, Button> indexDictionary = new();
2023-07-17 04:10:14 +08:00
public override void _Ready()
{
_buttonGroup = new ButtonGroup();
if (template.IsSupportCreateTemplate && createTemplateButton is not null)
{
2023-07-18 20:57:02 +08:00
createTemplateButton.Pressed += () =>
{
CurrentTemplate = template.CreateTemplate();
Rebuild();
};
2023-07-17 04:10:14 +08:00
}
2023-07-18 20:57:02 +08:00
2023-07-17 04:10:14 +08:00
Rebuild();
}
public override void _EnterTree()
{
template.OnStart();
}
public override void _ExitTree()
{
template.OnStop();
}
public void Rebuild()
{
indexBuilder.Clear();
2023-07-18 20:57:02 +08:00
templateBody.Hide();
if (CurrentTemplate is not null && template.GetTemplateNames().Any(x => x == CurrentTemplate.Name))
{
}
else
{
CurrentTemplate = null;
}
2023-07-17 04:10:14 +08:00
foreach (var name in template.GetTemplateNames())
{
var container = indexBuilder.Build<UXContainer>();
var _template = this.template.GetTemplate(name);
2023-07-18 20:57:02 +08:00
var guid = Guid.NewGuid().ToString();
2023-07-17 04:10:14 +08:00
container.button.Text = name;
container.button.ButtonGroup = _buttonGroup;
2023-07-18 20:57:02 +08:00
container.button.ToggleMode = true;
2023-07-17 04:10:14 +08:00
container.button.Pressed += () =>
{
2023-07-18 20:57:02 +08:00
Entry(_template);
2023-07-17 04:10:14 +08:00
};
2023-07-18 20:57:02 +08:00
_template.SetMeta("RuntimeId",guid);
indexDictionary.Add(guid,container.button);
2023-07-17 04:10:14 +08:00
}
2023-07-18 20:57:02 +08:00
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();
2023-07-17 04:10:14 +08:00
}
}