137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using ImGuiNET;
|
|
using System.Numerics;
|
|
using SzCore;
|
|
using SzCore.DataObjects;
|
|
|
|
namespace SzGui.Windows;
|
|
|
|
public class SzDataObjectTemplateEditorWindow : SzGuiWindowBase
|
|
{
|
|
private readonly ISzTemplate _template;
|
|
private string _name;
|
|
private string _type;
|
|
private string _description = "";
|
|
private List<SzTemplateField> _fields;
|
|
|
|
// Split ID components
|
|
private string _baseId = "";
|
|
private string _version = "1_0";
|
|
|
|
private string _newFieldId = "";
|
|
private int _selectedTypeIndex = 0;
|
|
|
|
public SzDataObjectTemplateEditorWindow(SzDataObjectTemplate template)
|
|
{
|
|
_template = template;
|
|
_name = template.Name;
|
|
_type = template.DataObjectType;
|
|
_fields = template.TemplateFields.Values.ToList();
|
|
|
|
if (template.Id.Contains('@'))
|
|
{
|
|
var parts = template.Id.Split('@');
|
|
_baseId = parts[0];
|
|
_version = parts[1];
|
|
}
|
|
else
|
|
{
|
|
_baseId = template.Id;
|
|
}
|
|
|
|
WindowName = $"Edit Template: {template.Id}";
|
|
}
|
|
|
|
protected override void RenderContent()
|
|
{
|
|
ImGui.SeparatorText("Metadata");
|
|
|
|
ImGui.InputText("Display Name", ref _name, 128);
|
|
ImGui.InputText("Object Type", ref _type, 128);
|
|
|
|
ImGui.Spacing();
|
|
|
|
ImGui.Text("Internal Identity");
|
|
ImGui.InputText("Base ID", ref _baseId, 64);
|
|
ImGui.SameLine();
|
|
ImGui.Text("@");
|
|
ImGui.SameLine();
|
|
ImGui.SetNextItemWidth(100);
|
|
ImGui.InputText("Version", ref _version, 16);
|
|
|
|
string fullIdPreview = $"{_baseId}@{_version}";
|
|
ImGui.TextDisabled($"Final ID Preview: {fullIdPreview}");
|
|
|
|
ImGui.SeparatorText("Fields Configuration");
|
|
|
|
if (ImGui.BeginTable("TemplateFieldsTable", 3, ImGuiTableFlags.Borders | ImGuiTableFlags.RowBg))
|
|
{
|
|
ImGui.TableSetupColumn("Field ID", ImGuiTableColumnFlags.WidthStretch);
|
|
ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthFixed, 100);
|
|
ImGui.TableSetupColumn("Action", ImGuiTableColumnFlags.WidthFixed, 70);
|
|
ImGui.TableHeadersRow();
|
|
|
|
for (int i = 0; i < _fields.Count; i++)
|
|
{
|
|
ImGui.TableNextRow();
|
|
ImGui.TableNextColumn();
|
|
ImGui.TextUnformatted(_fields[i].Id);
|
|
|
|
ImGui.TableNextColumn();
|
|
ImGui.TextUnformatted(_fields[i].FieldType.ToString());
|
|
|
|
ImGui.TableNextColumn();
|
|
if (ImGui.SmallButton($"Remove##{i}")) _fields.RemoveAt(i);
|
|
}
|
|
ImGui.EndTable();
|
|
}
|
|
|
|
ImGui.Spacing();
|
|
ImGui.SeparatorText("Add New Field");
|
|
|
|
ImGui.InputText("Field ID", ref _newFieldId, 64);
|
|
string[] typeNames = Enum.GetNames<SzFieldType>();
|
|
ImGui.Combo("Type", ref _selectedTypeIndex, typeNames, typeNames.Length);
|
|
|
|
if (ImGui.Button("Add Field") && !string.IsNullOrWhiteSpace(_newFieldId))
|
|
{
|
|
var result = SZ.DataHandler.CreateTemplateField(_newFieldId, (SzFieldType)_selectedTypeIndex);
|
|
if (result.IsSuccess)
|
|
{
|
|
_fields.Add(result.Value!);
|
|
_newFieldId = "";
|
|
}
|
|
}
|
|
|
|
ImGui.Separator();
|
|
|
|
bool isNewVersion = fullIdPreview != _template.Id;
|
|
if (isNewVersion)
|
|
{
|
|
ImGui.PushStyleColor(ImGuiCol.Button, new Vector4(0.2f, 0.6f, 0.2f, 1.0f));
|
|
if (ImGui.Button("Save as New Version", new Vector2(0, 30))) Save(fullIdPreview);
|
|
ImGui.PopStyleColor();
|
|
}
|
|
else
|
|
{
|
|
if (ImGui.Button("Overwrite Template", new Vector2(0, 30))) Save(fullIdPreview);
|
|
}
|
|
}
|
|
|
|
private async void Save(string finalId)
|
|
{
|
|
var result = SZ.DataHandler.CreateDataObjectTemplate(
|
|
_name,
|
|
_type,
|
|
_version,
|
|
finalId,
|
|
null,
|
|
_fields
|
|
);
|
|
|
|
if (result.IsSuccess && result.Value != null)
|
|
{
|
|
await SZ.DataHandler.SaveTemplateAsync(result.Value);
|
|
IsOpen = false;
|
|
}
|
|
}
|
|
} |