ScriptCore: Add Entry for dictionaries

This commit is contained in:
Simon Lübeß
2024-01-06 13:12:50 +01:00
parent 13baf2f4bc
commit 5d9973a791
2 changed files with 52 additions and 3 deletions
+47 -2
View File
@@ -12,6 +12,7 @@ namespace GlitchyEngine.Editor;
public class DictionaryEditor
{
private static object? dictionaryForNewValue = null;
private static DictionaryEntry newDictionaryValue = new();
public static object? ShowEditor(object? reference, Type fieldType, string fieldName)
{
@@ -53,9 +54,11 @@ public class DictionaryEditor
//if (newKey != null)
// dictionary!.Add(newKey, newValue);
newDictionaryValue = new DictionaryEntry();
}
ImGuiExtension.AttachTooltip("Add a new Element to the dictionary.");
ImGuiExtension.AttachTooltip("Add a new Entry to the dictionary.");
if (listOpen)
{
@@ -102,7 +105,49 @@ public class DictionaryEditor
ImGui.TreePop();
}
ImGui.PopID();
}
if (ReferenceEquals(dictionaryForNewValue, dictionary))
{
ImGui.PushID("NewEntry");
ImGui.SetNextItemOpen(true);
bool isEntryOpen = ImGui.TreeNode("");
ImGui.SameLine(ImGui.GetWindowContentRegionMax().X - removeButtonWidth);
if (ImGui.SmallButton("-"))
{
dictionaryForNewValue = null;
newDictionaryValue = new();
}
ImGuiExtension.AttachTooltip("Remove the Entry from the dictionary");
if (isEntryOpen)
{
object newKey = EntityEditor.ShowFieldEditor(newDictionaryValue.Key, keyType, "Key");
if (newKey != EntityEditor.DidNotChange)
{
newEntries.Add(new DictionaryEntry(newKey, newDictionaryValue.Value));
dictionaryForNewValue = null;
newDictionaryValue = new();
}
object newValue = EntityEditor.ShowFieldEditor(newDictionaryValue.Value, newDictionaryValue.Value?.GetType() ?? valueType, "Value");
if (newValue != EntityEditor.DidNotChange)
{
newDictionaryValue.Value = newValue;
}
ImGui.TreePop();
}
ImGui.PopID();
}
+5 -1
View File
@@ -111,7 +111,7 @@ internal class EntityEditor
return (T?)attributes?.FirstOrDefault(a => a is T);
}
private static object ShowPrimitiveEditor(object reference, Type fieldType, string fieldName, IEnumerable<Attribute>? attributes)
private static object? ShowPrimitiveEditor(object? reference, Type fieldType, string fieldName, IEnumerable<Attribute>? attributes)
{
object newValue = DidNotChange;
@@ -230,6 +230,10 @@ internal class EntityEditor
ImGui.TextColored(new Vector4(1, 0, 0, 1), $"{fieldName}: Type {fieldType} is not implemented.");
}
}
else
{
return ActivatorExtension.CreateInstanceSafe(fieldType);
}
return newValue;
}