iFactory.Godot/Mods/工业数据采集与分析应用分享/Scripts/IDIS_TemplateService.cs

168 lines
4.8 KiB
C#
Raw Normal View History

2023-07-08 00:02:32 +08:00
using Godot;
using System;
using System.Collections.Generic;
using System.Globalization;
2023-07-08 00:02:32 +08:00
using System.IO;
using System.Net;
using BITKit;
using BITKit.IO;
using RosSharp.RosBridgeClient.MessageTypes.Moveit;
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;
2023-07-08 00:02:32 +08:00
public List<(string format,string hint)> 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<IDIS_Template> templates=new();
private readonly Queue<IDIS_Template> selectedQueue = new();
[ExportCategory("Quick Start")]
[Export] private Button createButton;
[Export] private Button newFormatButton;
[Export] private ItemList itemList;
[Export] private LineEdit templateNameEdit;
[Export] private LineEdit templateDescriptionEdit;
[Export] private Control container;
[Export] private Label templateCreateTimeLabel;
[Export] private Label templateUpdateTimeLabel;
2023-07-08 00:02:32 +08:00
[ExportCategory("Template")]
[Export] private PackedScene templateContainer;
private bool isDirty;
private IDIS_Template _selectedTemplate=>_selectedIndex==-1?null:templates[_selectedIndex];
private int _selectedIndex=-1;
public override void _Ready()
{
if (File.Exists(assetPath) is false)
{
BIT4Log.Log<IDIS_TemplateService>($"未找到配置文件:{assetPath}");
}
var temp = BITAssets.Read<List<IDIS_Template>>(assetPath, templateName);
templates.AddRange(temp);
BIT4Log.Log<IDIS_TemplateService>($"已加载配置文件:{assetPath}");
createButton.Pressed += CreateTemplate;
newFormatButton.Pressed += CreateFormat;
itemList.ItemSelected += OnItemSelected;
itemList.ItemClicked += OnItemClicked;
templateNameEdit.TextChanged += OnTemplateNameChanged;
templateDescriptionEdit.TextChanged += OnTemplateDescriptionChanged;
MathNode.RemoveAllChild(container);
EnsureConfigure();
}
private void CreateFormat()
{
if (_selectedTemplate is null) return;
_selectedTemplate.Formats ??= new();
_selectedTemplate.Formats.Add(("新的数据格式","格式类型"));
_selectedTemplate.UpdateTime= DateTime.Now;
2023-07-08 00:02:32 +08:00
EnsureConfigure();
}
public override void _ExitTree()
{
BITAssets.Build(assetPath,new IAsset[]
{
new BITAsset(templateName,JsonHelper.Get(templates))
});
BIT4Log.Log<IDIS_TemplateService>($"已创建配置文件:{assetPath}");
}
private void CreateTemplate()
{
templates.Add(new IDIS_Template()
{
TemplateName = "新的标识注册模板"
});
EnsureConfigure();
}
private void OnTemplateNameChanged(string text)
{
if (_selectedTemplate is null) return;
_selectedTemplate.TemplateName = text;
_selectedTemplate.UpdateTime= DateTime.Now;
2023-07-08 00:02:32 +08:00
}
private void OnTemplateDescriptionChanged(string text)
{
if (_selectedTemplate is null) return;
_selectedTemplate.TemplateDescription = text;
_selectedTemplate.UpdateTime= DateTime.Now;
2023-07-08 00:02:32 +08:00
}
private void EnsureConfigure()
{
itemList.Clear();
MathNode.RemoveAllChild(container);
foreach (var x in templates)
{
itemList.AddItem(x.TemplateName);
}
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);
2023-07-08 00:02:32 +08:00
for (var i = 0; i < _selectedTemplate.Formats.Count; i++)
{
var x = _selectedTemplate.Formats[i];
var _container = templateContainer.Instantiate<UXContainer>();
_container.lineEdits[0].Text = x.format;
_container.lineEdits[1].Text = x.hint;
var index = i;
_container.lineEdits[0].TextChanged += (s) =>
{
var current = _selectedTemplate.Formats[index];
current.format = s;
_selectedTemplate.Formats[index] = current;
_selectedTemplate.UpdateTime= DateTime.Now;
2023-07-08 00:02:32 +08:00
};
_container.lineEdits[1].TextChanged += s =>
{
var current = _selectedTemplate.Formats[index];
current.hint = s;
_selectedTemplate.Formats[index] = current;
};
_container.button.Pressed += () =>
{
_selectedTemplate.Formats.RemoveAt(index);
EnsureConfigure();
};
container.AddChild(_container);
}
}
private void OnItemSelected(long id)
{
}
private void OnItemClicked(long index, Vector2 atPosition, long mouseButtonIndex)
{
// var currentItem = _templates[(int)index];
// templateNameEdit.Text = currentItem.TemplateName;
_selectedIndex = (int)index;
EnsureConfigure();
}
}