From 5d9973a791419c97b06a36d5ddd3c56167312565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 6 Jan 2024 13:12:50 +0100 Subject: [PATCH] ScriptCore: Add Entry for dictionaries --- ScriptCore/Editor/DictionaryEditor.cs | 49 +++++++++++++++++++++++++-- ScriptCore/Editor/EntityEditor.cs | 6 +++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/ScriptCore/Editor/DictionaryEditor.cs b/ScriptCore/Editor/DictionaryEditor.cs index a2c6502..767bd9d 100644 --- a/ScriptCore/Editor/DictionaryEditor.cs +++ b/ScriptCore/Editor/DictionaryEditor.cs @@ -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(); } diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index e69de8c..3c60329 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -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? attributes) + private static object? ShowPrimitiveEditor(object? reference, Type fieldType, string fieldName, IEnumerable? 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; }