From 41e3814de831a9dc39dc759b6b1491c29a6511da Mon Sep 17 00:00:00 2001 From: William Barbosa Date: Fri, 19 Jan 2024 18:17:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=96=A5=EF=B8=8F=20Render=20editor=20field?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImmutableArrayConstraintField.cs | 31 +++++++++++++++++++ src/Murder.Editor/Utilities/Gui/SearchBox.cs | 15 +++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/Murder.Editor/CustomFields/ImmutableArrayConstraintField.cs diff --git a/src/Murder.Editor/CustomFields/ImmutableArrayConstraintField.cs b/src/Murder.Editor/CustomFields/ImmutableArrayConstraintField.cs new file mode 100644 index 000000000..67c0ab75b --- /dev/null +++ b/src/Murder.Editor/CustomFields/ImmutableArrayConstraintField.cs @@ -0,0 +1,31 @@ +using System.Collections.Immutable; +using ImGuiNET; +using Murder.Core.Ui; +using Murder.Editor.ImGuiExtended; +using Murder.Editor.Reflection; + +namespace Murder.Editor.CustomFields; + +[CustomFieldOf(typeof(ImmutableArray))] +internal class ConstraintArrayField : ImmutableArrayField +{ + protected override bool Add(in EditorMember member, out IConstraint? element) + { + element = null; + ImGui.PushID($"Add ${member.Member.ReflectedType}"); + + var result = false; + if (SearchBox.SearchConstraints() is { } constraintType) + { + if (Activator.CreateInstance(constraintType) is IConstraint constraint) + { + element = constraint; + result = true; + } + } + + ImGui.PopID(); + + return result; + } +} \ No newline at end of file diff --git a/src/Murder.Editor/Utilities/Gui/SearchBox.cs b/src/Murder.Editor/Utilities/Gui/SearchBox.cs index fce3138c2..242d8bf31 100644 --- a/src/Murder.Editor/Utilities/Gui/SearchBox.cs +++ b/src/Murder.Editor/Utilities/Gui/SearchBox.cs @@ -6,6 +6,7 @@ using Murder.Core.Dialogs; using Murder.Core.Geometry; using Murder.Core.Sounds; +using Murder.Core.Ui; using Murder.Editor.Utilities; using Murder.Prefabs; using Murder.Utilities; @@ -604,5 +605,19 @@ public static bool Search( return modified; } + + + public static Type? SearchConstraints() + { + string selected = "Select a constraint"; + + // Find all non-repeating components + IEnumerable types = ReflectionHelper.SafeGetAllTypesInAllAssemblies() + .Where(p => !p.IsInterface && typeof(IConstraint).IsAssignableFrom(p)); + + Lazy> candidates = new(CollectionHelper.ToStringDictionary(types, t => t.Name, t => t)); + + return Search(id: "constraint_search", hasInitialValue: false, selected, values: candidates, SearchBoxFlags.None, out Type? chosen) ? chosen : default; + } } } \ No newline at end of file