Support schema field options in entity editors
This commit is contained in:
1 parent
cbc9472738
commit
5be5bed0c5
8 files changed
+40
-26
No files matched your search
@@ -201,7 +201,7 @@ namespace Cthangover.Tools.EntityEditor
|
|||||||
|
|
||||||
private void BuildSchemaFields()
|
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)))
|
.Where(f => !string.Equals(f.Key, "Id", StringComparison.OrdinalIgnoreCase) && !fields.Any(e => string.Equals(e.Key, f.Key, StringComparison.OrdinalIgnoreCase)))
|
||||||
.ToList();
|
.ToList();
|
||||||
if (described.Count == 0)
|
if (described.Count == 0)
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System.Text.Json.Nodes;
|
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>
|
/// <summary>One field's editor in the data editor form: shows the field's JSON and gives it back edited.</summary>
|
||||||
public interface ISchemaFieldEditor
|
public interface ISchemaFieldEditor
|
||||||
{
|
{
|
||||||
/// <summary>The described field.</summary>
|
/// <summary>The described field.</summary>
|
||||||
FieldSchema Field { get; }
|
SchemaFieldDefinition Field { get; }
|
||||||
|
|
||||||
/// <summary>Shows <paramref name="node"/> (null: the field is absent).</summary>
|
/// <summary>Shows <paramref name="node"/> (null: the field is absent).</summary>
|
||||||
void Load(JsonNode node);
|
void Load(JsonNode node);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
@@ -12,7 +14,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
/// (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.
|
/// cards with an add and a remove button, each card a form of the sub-fields.
|
||||||
/// <para/>
|
/// <para/>
|
||||||
@@ -22,7 +24,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ListFieldEditor : ISchemaFieldEditor
|
public class ListFieldEditor : ISchemaFieldEditor
|
||||||
{
|
{
|
||||||
private readonly FieldSchema field;
|
private readonly SchemaFieldDefinition field;
|
||||||
private readonly Action changed;
|
private readonly Action changed;
|
||||||
private readonly VBoxContainer cards;
|
private readonly VBoxContainer cards;
|
||||||
private readonly List<List<SchemaFieldControl>> rows = new();
|
private readonly List<List<SchemaFieldControl>> rows = new();
|
||||||
@@ -30,10 +32,10 @@ namespace Cthangover.Tools.Schemas
|
|||||||
private bool suppress;
|
private bool suppress;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public FieldSchema Field => field;
|
public SchemaFieldDefinition Field => field;
|
||||||
|
|
||||||
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
|
/// <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.field = field;
|
||||||
this.changed = changed;
|
this.changed = changed;
|
||||||
@@ -87,7 +89,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
{
|
{
|
||||||
var item = new JsonObject();
|
var item = new JsonObject();
|
||||||
foreach (var control in card.Where(c => c.Value.Length > 0))
|
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)
|
if (item.Count > 0)
|
||||||
array.Add(item);
|
array.Add(item);
|
||||||
}
|
}
|
||||||
@@ -126,10 +128,10 @@ namespace Cthangover.Tools.Schemas
|
|||||||
var column = new VBoxContainer();
|
var column = new VBoxContainer();
|
||||||
card.AddChild(column);
|
card.AddChild(column);
|
||||||
var controls = new List<SchemaFieldControl>();
|
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);
|
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);
|
controls.Add(control);
|
||||||
}
|
}
|
||||||
if (!IsValues)
|
if (!IsValues)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
@@ -18,7 +20,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class LocaleFieldEditor : ISchemaFieldEditor
|
public class LocaleFieldEditor : ISchemaFieldEditor
|
||||||
{
|
{
|
||||||
private readonly FieldSchema field;
|
private readonly SchemaFieldDefinition field;
|
||||||
private readonly Action changed;
|
private readonly Action changed;
|
||||||
private readonly LineEdit keyEdit;
|
private readonly LineEdit keyEdit;
|
||||||
private readonly VBoxContainer texts;
|
private readonly VBoxContainer texts;
|
||||||
@@ -28,10 +30,10 @@ namespace Cthangover.Tools.Schemas
|
|||||||
private bool suppress;
|
private bool suppress;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public FieldSchema Field => field;
|
public SchemaFieldDefinition Field => field;
|
||||||
|
|
||||||
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
|
/// <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.field = field;
|
||||||
this.changed = changed;
|
this.changed = changed;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,24 +13,24 @@ namespace Cthangover.Tools.Schemas
|
|||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
/// 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
|
/// 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.
|
/// is written as a number, anything else as a formula text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MapFieldEditor : ISchemaFieldEditor
|
public class MapFieldEditor : ISchemaFieldEditor
|
||||||
{
|
{
|
||||||
private readonly FieldSchema field;
|
private readonly SchemaFieldDefinition field;
|
||||||
private readonly Action changed;
|
private readonly Action changed;
|
||||||
private readonly VBoxContainer lines;
|
private readonly VBoxContainer lines;
|
||||||
private readonly List<(OptionButton Key, List<string> Keys, LineEdit Value, Control Row)> rows = new();
|
private readonly List<(OptionButton Key, List<string> Keys, LineEdit Value, Control Row)> rows = new();
|
||||||
private bool suppress;
|
private bool suppress;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public FieldSchema Field => field;
|
public SchemaFieldDefinition Field => field;
|
||||||
|
|
||||||
/// <summary>Adds the editor for <paramref name="field"/> to <paramref name="parent"/>.</summary>
|
/// <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.field = field;
|
||||||
this.changed = changed;
|
this.changed = changed;
|
||||||
@@ -56,7 +58,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
if (node is JsonObject obj)
|
if (node is JsonObject obj)
|
||||||
{
|
{
|
||||||
foreach (var pair in obj)
|
foreach (var pair in obj)
|
||||||
AddRow(pair.Key, SchemaJson.ToText(pair.Value));
|
AddRow(pair.Key, SchemaJsonValues.ToText(pair.Value));
|
||||||
}
|
}
|
||||||
suppress = false;
|
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;
|
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)
|
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;
|
return obj.Count == 0 ? null : obj;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
using Godot;
|
using Godot;
|
||||||
@@ -11,18 +13,18 @@ namespace Cthangover.Tools.Schemas
|
|||||||
private readonly SchemaFieldControl control;
|
private readonly SchemaFieldControl control;
|
||||||
|
|
||||||
/// <summary>Adds the row for <paramref name="field"/> to <paramref name="parent"/>.</summary>
|
/// <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);
|
control = new SchemaFieldControl(parent, field, changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public FieldSchema Field => control.Field;
|
public SchemaFieldDefinition Field => control.Field;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Load(JsonNode node) => control.Value = SchemaJson.ToText(node);
|
public void Load(JsonNode node) => control.Value = SchemaJsonValues.ToText(node);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public JsonNode Save() => SchemaJson.ToNode(control.Field, control.Value);
|
public JsonNode Save() => SchemaJsonValues.ToNode(control.Field, control.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
namespace Cthangover.Tools.Schemas
|
namespace Cthangover.Tools.Schemas
|
||||||
{
|
{
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -10,7 +12,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
/// "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:
|
/// most JSON fields hold it, so the palette and the data editor window share these rows:
|
||||||
/// <list type="bullet">
|
/// <list type="bullet">
|
||||||
@@ -36,7 +38,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
private bool suppress;
|
private bool suppress;
|
||||||
|
|
||||||
/// <summary>Adds the row for <paramref name="field"/> to <paramref name="parent"/>; <paramref name="changed"/> runs on every edit.</summary>
|
/// <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;
|
Field = field;
|
||||||
this.changed = changed;
|
this.changed = changed;
|
||||||
@@ -97,7 +99,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The described field.</summary>
|
/// <summary>The described field.</summary>
|
||||||
public FieldSchema Field { get; }
|
public SchemaFieldDefinition Field { get; }
|
||||||
|
|
||||||
/// <summary>The editor control (the row's right part).</summary>
|
/// <summary>The editor control (the row's right part).</summary>
|
||||||
public Control Editor { get; }
|
public Control Editor { get; }
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ namespace Cthangover.Tools.Schemas
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using Cthangover.Mods.Schemas;
|
using Cthangover.Mods.Schemas;
|
||||||
|
using SchemaFieldDefinition = Cthangover.Mods.Schemas.FieldSchema;
|
||||||
|
using SchemaJsonValues = Cthangover.Mods.Schemas.SchemaJson;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -13,7 +15,7 @@ namespace Cthangover.Tools.Schemas
|
|||||||
public static class SchemaFieldEditors
|
public static class SchemaFieldEditors
|
||||||
{
|
{
|
||||||
/// <summary>Adds the editor of <paramref name="field"/> to <paramref name="parent"/>; <paramref name="changed"/> runs on every edit.</summary>
|
/// <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())
|
switch (field.Type?.ToLowerInvariant())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user