Support schema field options in entity editors

This commit is contained in:
cthangover committed 2026-10-27 01:07:29 +01:00
1 parent cbc9472738
commit 5be5bed0c5
8 files changed
+40 -26

No files matched your search

+1 -1
View File
@@ -201,7 +201,7 @@ namespace Cthangover.Tools.EntityEditor
private void BuildSchemaFields()
{
var described = SchemaCatalog.Current.FieldsOf(kind)
var described = Cthangover.Mods.Schemas.SchemaCatalog.Current.FieldsOf(kind)
.Where(f => !string.Equals(f.Key, "Id", StringComparison.OrdinalIgnoreCase) && !fields.Any(e => string.Equals(e.Key, f.Key, StringComparison.OrdinalIgnoreCase)))
.ToList();
if (described.Count == 0)
+3 -1
View File
@@ -1,13 +1,15 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System.Text.Json.Nodes;
/// <summary>One field's editor in the data editor form: shows the field's JSON and gives it back edited.</summary>
public interface ISchemaFieldEditor
{
/// <summary>The described field.</summary>
FieldSchema Field { get; }
SchemaFieldDefinition Field { get; }
/// <summary>Shows <paramref name="node"/> (null: the field is absent).</summary>
void Load(JsonNode node);
+9 -7
View File
@@ -1,6 +1,8 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -12,7 +14,7 @@ namespace Cthangover.Tools.Schemas
using Godot;
/// <summary>
/// Edits a <c>list</c> field — an array of objects described by <see cref="FieldSchema.Of"/>
/// Edits a <c>list</c> field — an array of objects described by <see cref="SchemaFieldDefinition.Of"/>
/// (an item's traits, an archetype's learnings, a shop's goods) — as a stack of small
/// cards with an add and a remove button, each card a form of the sub-fields.
/// <para/>
@@ -22,7 +24,7 @@ namespace Cthangover.Tools.Schemas
/// </summary>
public class ListFieldEditor : ISchemaFieldEditor
{
private readonly FieldSchema field;
private readonly SchemaFieldDefinition field;
private readonly Action changed;
private readonly VBoxContainer cards;
private readonly List<List<SchemaFieldControl>> rows = new();
@@ -30,10 +32,10 @@ namespace Cthangover.Tools.Schemas
private bool suppress;
/// <inheritdoc />
public FieldSchema Field => field;
public SchemaFieldDefinition Field => field;
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
public ListFieldEditor(Container parent, FieldSchema field, Action changed)
public ListFieldEditor(Container parent, SchemaFieldDefinition field, Action changed)
{
this.field = field;
this.changed = changed;
@@ -87,7 +89,7 @@ namespace Cthangover.Tools.Schemas
{
var item = new JsonObject();
foreach (var control in card.Where(c => c.Value.Length > 0))
item[control.Field.Key] = SchemaJson.ToNode(control.Field, control.Value);
item[control.Field.Key] = SchemaJsonValues.ToNode(control.Field, control.Value);
if (item.Count > 0)
array.Add(item);
}
@@ -126,10 +128,10 @@ namespace Cthangover.Tools.Schemas
var column = new VBoxContainer();
card.AddChild(column);
var controls = new List<SchemaFieldControl>();
foreach (var sub in field.Of ?? new List<FieldSchema>())
foreach (var sub in field.Of ?? new List<SchemaFieldDefinition>())
{
var control = new SchemaFieldControl(column, sub, OnChanged);
control.Value = SchemaJson.ToText(Find(item, sub.Key));
control.Value = SchemaJsonValues.ToText(Find(item, sub.Key));
controls.Add(control);
}
if (!IsValues)
+5 -3
View File
@@ -1,6 +1,8 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
@@ -18,7 +20,7 @@ namespace Cthangover.Tools.Schemas
/// </summary>
public class LocaleFieldEditor : ISchemaFieldEditor
{
private readonly FieldSchema field;
private readonly SchemaFieldDefinition field;
private readonly Action changed;
private readonly LineEdit keyEdit;
private readonly VBoxContainer texts;
@@ -28,10 +30,10 @@ namespace Cthangover.Tools.Schemas
private bool suppress;
/// <inheritdoc />
public FieldSchema Field => field;
public SchemaFieldDefinition Field => field;
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
public LocaleFieldEditor(Container parent, FieldSchema field, Action changed)
public LocaleFieldEditor(Container parent, SchemaFieldDefinition field, Action changed)
{
this.field = field;
this.changed = changed;
+8 -6
View File
@@ -1,6 +1,8 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -11,24 +13,24 @@ namespace Cthangover.Tools.Schemas
using Godot;
/// <summary>
/// Edits a <c>map</c> field — an object whose keys are ids of <see cref="FieldSchema.Kind"/>
/// Edits a <c>map</c> field — an object whose keys are ids of <see cref="SchemaFieldDefinition.Kind"/>
/// and whose values are numbers or formulas (a character's own <c>Stats</c>, an archetype's
/// growth): one row per key with a key picker, a value box and a remove button. A number
/// is written as a number, anything else as a formula text.
/// </summary>
public class MapFieldEditor : ISchemaFieldEditor
{
private readonly FieldSchema field;
private readonly SchemaFieldDefinition field;
private readonly Action changed;
private readonly VBoxContainer lines;
private readonly List<(OptionButton Key, List<string> Keys, LineEdit Value, Control Row)> rows = new();
private bool suppress;
/// <inheritdoc />
public FieldSchema Field => field;
public SchemaFieldDefinition Field => field;
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
public MapFieldEditor(Container parent, FieldSchema field, Action changed)
public MapFieldEditor(Container parent, SchemaFieldDefinition field, Action changed)
{
this.field = field;
this.changed = changed;
@@ -56,7 +58,7 @@ namespace Cthangover.Tools.Schemas
if (node is JsonObject obj)
{
foreach (var pair in obj)
AddRow(pair.Key, SchemaJson.ToText(pair.Value));
AddRow(pair.Key, SchemaJsonValues.ToText(pair.Value));
}
suppress = false;
}
@@ -69,7 +71,7 @@ namespace Cthangover.Tools.Schemas
{
var key = row.Key.Selected >= 0 && row.Key.Selected < row.Keys.Count ? row.Keys[row.Key.Selected] : null;
if (!string.IsNullOrEmpty(key) && row.Value.Text.Trim().Length > 0)
obj[key] = SchemaJson.NumberOrText(row.Value.Text.Trim());
obj[key] = SchemaJsonValues.NumberOrText(row.Value.Text.Trim());
}
return obj.Count == 0 ? null : obj;
}
+6 -4
View File
@@ -1,6 +1,8 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System;
using System.Text.Json.Nodes;
using Godot;
@@ -11,18 +13,18 @@ namespace Cthangover.Tools.Schemas
private readonly SchemaFieldControl control;
/// <summary>Adds the row for <paramref name="field"/> to <paramref name="parent"/>.</summary>
public ScalarFieldEditor(Container parent, FieldSchema field, Action changed)
public ScalarFieldEditor(Container parent, SchemaFieldDefinition field, Action changed)
{
control = new SchemaFieldControl(parent, field, changed);
}
/// <inheritdoc />
public FieldSchema Field => control.Field;
public SchemaFieldDefinition Field => control.Field;
/// <inheritdoc />
public void Load(JsonNode node) => control.Value = SchemaJson.ToText(node);
public void Load(JsonNode node) => control.Value = SchemaJsonValues.ToText(node);
/// <inheritdoc />
public JsonNode Save() => SchemaJson.ToNode(control.Field, control.Value);
public JsonNode Save() => SchemaJsonValues.ToNode(control.Field, control.Value);
}
}
+5 -3
View File
@@ -1,6 +1,8 @@
namespace Cthangover.Tools.Schemas
{
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -10,7 +12,7 @@ namespace Cthangover.Tools.Schemas
using Godot;
/// <summary>
/// The form control of one scalar <see cref="FieldSchema"/>, chosen by its type, as a
/// The form control of one scalar <see cref="SchemaFieldDefinition"/>, chosen by its type, as a
/// "label | editor" row. Everything reads and writes a text value, the way the DSL and
/// most JSON fields hold it, so the palette and the data editor window share these rows:
/// <list type="bullet">
@@ -36,7 +38,7 @@ namespace Cthangover.Tools.Schemas
private bool suppress;
/// <summary>Adds the row for <paramref name="field"/> to <paramref name="parent"/>; <paramref name="changed"/> runs on every edit.</summary>
public SchemaFieldControl(Container parent, FieldSchema field, Action changed)
public SchemaFieldControl(Container parent, SchemaFieldDefinition field, Action changed)
{
Field = field;
this.changed = changed;
@@ -97,7 +99,7 @@ namespace Cthangover.Tools.Schemas
}
/// <summary>The described field.</summary>
public FieldSchema Field { get; }
public SchemaFieldDefinition Field { get; }
/// <summary>The editor control (the row's right part).</summary>
public Control Editor { get; }
+3 -1
View File
@@ -2,6 +2,8 @@ namespace Cthangover.Tools.Schemas
{
using System;
using Cthangover.Mods.Schemas;
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
using Godot;
/// <summary>
@@ -13,7 +15,7 @@ namespace Cthangover.Tools.Schemas
public static class SchemaFieldEditors
{
/// <summary>Adds the editor of <paramref name="field"/> to <paramref name="parent"/>; <paramref name="changed"/> runs on every edit.</summary>
public static ISchemaFieldEditor Create(Container parent, FieldSchema field, Action changed)
public static ISchemaFieldEditor Create(Container parent, SchemaFieldDefinition field, Action changed)
{
switch (field.Type?.ToLowerInvariant())
{