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

89 lines
2.2 KiB
C#
Raw Normal View History

2023-07-08 00:02:32 +08:00
using Godot;
using System;
2023-07-08 13:54:17 +08:00
using System.Collections.Generic;
2023-07-08 00:02:32 +08:00
using BITKit;
namespace BITFactory;
public partial class IDIS_RegisterService : Node
{
[Export] private IDIS_Service service;
[Export] private IDIS_TemplateService templateService;
[Export] private ItemList templateList;
[Export] private LineEdit handleEdit;
[Export] private Button generateHandleButton;
[Export] private Control container;
[Export] private Button registerButton;
[Export] private Label hints;
2023-07-08 13:54:17 +08:00
private readonly Dictionary<int,KeyValuePair<string,string>> _currentValues = new();
2023-07-08 00:02:32 +08:00
public override void _Ready()
{
templateList.Clear();
foreach (var x in templateService.templates)
{
templateList.AddItem(x.TemplateName);
}
var lineEdit = new LineEdit();
lineEdit.PlaceholderText = "请输入标识名称";
templateList.ItemClicked += OnItemClicked;
registerButton.Pressed += Register;
generateHandleButton.Pressed += () =>
{
handleEdit.Text = $"88.123.99/{Mathf.Abs(Guid.NewGuid().GetHashCode())}";
};
}
private void OnItemClicked(long index, Vector2 atPosition, long mouseButtonIndex)
{
MathNode.RemoveAllChild(container);
var template = templateService.templates[(int) index];
var grid = new GridContainer();
grid.Columns = 2;
grid.AddThemeConstantOverride("h_separation", 64);
container.AddChild(grid);
2023-07-08 13:54:17 +08:00
_currentValues.Clear();
var _dirIndex = 0;
2023-07-08 00:02:32 +08:00
foreach (var x in template.Formats)
{
var label = new Label();
var lineEdit = new LineEdit();
2023-07-08 13:54:17 +08:00
var myIndex = _dirIndex++;
2023-07-08 00:02:32 +08:00
label.Text = x.format;
lineEdit.PlaceholderText = x.hint;
lineEdit.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
2023-07-08 13:54:17 +08:00
_currentValues.Add(myIndex,new KeyValuePair<string, string>(x.format,x.hint));
lineEdit.TextChanged += (s) =>
{
_currentValues[myIndex] = new KeyValuePair<string, string>(x.format, s);
};
2023-07-08 00:02:32 +08:00
grid.AddChild(label);
grid.AddChild(lineEdit);
}
}
private void Register()
{
2023-07-08 13:54:17 +08:00
var handle = handleEdit.Text;
var json = JsonHelper.Get(_currentValues);
BIT4Log.Log<IDIS_RegisterService>($"注册标识:{handle}");
BIT4Log.Log<IDIS_RegisterService>($"\n{json}");
foreach (var x in _currentValues)
{
service.Register(handle, x.Value.Key, x.Value.Value);
}
2023-07-08 00:02:32 +08:00
}
}