using System; using System.Collections.Generic; using System.Globalization; using System.IO; using BITKit; using BITKit.IO; using Godot; namespace BITFactory; [Serializable] public class IDIS_Template { public string TemplateName="标识注册模板"; public string TemplateDescription="该模板通常用于xxx用途"; public string IconPath="Register"; public DateTime CreateTime=DateTime.Now; public DateTime UpdateTime=DateTime.Now; public List<(string format,string hint,string category)> Formats=new(); } public partial class IDIS_TemplateService : Node { private static string assetPath => PathHelper.GetPath("标识注册模板.zip"); private const string templateName = $"{nameof(IDIS_Template)}.json"; public readonly List templates=new(); [ExportCategory("Quick Start")] [Export] private Button createButton; [Export] private Button newFormatButton; [Export] private NodeBuilder templateIndexBuilder; [Export] private NodeBuilder templateFormatBuilder; [Export] private LineEdit templateNameEdit; [Export] private LineEdit templateDescriptionEdit; [Export] private Label templateCreateTimeLabel; [Export] private Label templateUpdateTimeLabel; [ExportCategory("Template")] [Export] private PackedScene templateContainer; private bool isDirty; private IDIS_Template _selectedTemplate; public override void _Ready() { if (File.Exists(assetPath) is false) { BIT4Log.Log($"未找到配置文件:{assetPath}"); Save(); } var temp = BITAssets.Read>(assetPath, templateName); templates.AddRange(temp); BIT4Log.Log($"已加载配置文件:{assetPath}"); createButton.Pressed += CreateTemplate; newFormatButton.Pressed += CreateFormat; templateNameEdit.TextChanged += OnTemplateNameChanged; templateDescriptionEdit.TextChanged += OnTemplateDescriptionChanged; EnsureConfigure(); } private void CreateFormat() { if (_selectedTemplate is null) return; _selectedTemplate.Formats ??= new(); _selectedTemplate.Formats.Add(("新的数据格式","格式类型","数据类型")); _selectedTemplate.UpdateTime= DateTime.Now; EnsureConfigure(); } public override void _ExitTree() { Save(); } private void CreateTemplate() { templates.Add(new IDIS_Template() { TemplateName = "新的标识注册模板" }); EnsureConfigure(); } private void Save() { BITAssets.Build(assetPath,new IAsset[] { new BITAsset(templateName,JsonHelper.Get(templates)) }); BIT4Log.Log($"已创建配置文件:{assetPath}"); } private void OnTemplateNameChanged(string text) { if (_selectedTemplate is null) return; _selectedTemplate.TemplateName = text; _selectedTemplate.UpdateTime= DateTime.Now; } private void OnTemplateDescriptionChanged(string text) { if (_selectedTemplate is null) return; _selectedTemplate.TemplateDescription = text; _selectedTemplate.UpdateTime= DateTime.Now; } private void EnsureConfigure() { templateIndexBuilder.Clear(); templateFormatBuilder.Clear(); foreach (var x in templates) { var _container = templateIndexBuilder.Build(); _container.button.Text = x.TemplateName; _container.button.Pressed += () => { _selectedTemplate = x; EnsureConfigure(); }; } if (_selectedTemplate is null) return; templateNameEdit.Text = _selectedTemplate.TemplateName; templateDescriptionEdit.Text = _selectedTemplate.TemplateDescription; templateCreateTimeLabel.Text = _selectedTemplate.CreateTime.ToString(CultureInfo.InvariantCulture); templateUpdateTimeLabel.Text = _selectedTemplate.UpdateTime.ToString(CultureInfo.InvariantCulture); for (var i = 0; i < _selectedTemplate.Formats.Count; i++) { var x = _selectedTemplate.Formats[i]; var _container = templateFormatBuilder.Build(); _container.lineEdits[0].Text = x.format; _container.lineEdits[1].Text = x.hint; _container.lineEdits[2].Text = x.category; var index = i; _container.lineEdits[0].TextChanged += (s) => { var current = _selectedTemplate.Formats[index]; current.format = s; _selectedTemplate.Formats[index] = current; _selectedTemplate.UpdateTime = DateTime.Now; }; _container.lineEdits[1].TextChanged += s => { var current = _selectedTemplate.Formats[index]; current.hint = s; _selectedTemplate.Formats[index] = current; }; _container.lineEdits[2].TextChanged += s => { var current = _selectedTemplate.Formats[index]; current.category = s; _selectedTemplate.Formats[index] = current; }; _container.button.Pressed += () => { _selectedTemplate.Formats.RemoveAt(index); EnsureConfigure(); }; } } }