Skip to content

Commit

Permalink
🖥️ Render editor fields
Browse files Browse the repository at this point in the history
  • Loading branch information
heytherewill committed Jan 19, 2024
1 parent 8cac5cf commit 41e3814
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Murder.Editor/CustomFields/ImmutableArrayConstraintField.cs
Original file line number Diff line number Diff line change
@@ -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<IConstraint>))]
internal class ConstraintArrayField : ImmutableArrayField<IConstraint>
{
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;
}
}
15 changes: 15 additions & 0 deletions src/Murder.Editor/Utilities/Gui/SearchBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -604,5 +605,19 @@ public static bool Search<T>(

return modified;
}


public static Type? SearchConstraints()
{
string selected = "Select a constraint";

// Find all non-repeating components
IEnumerable<Type> types = ReflectionHelper.SafeGetAllTypesInAllAssemblies()
.Where(p => !p.IsInterface && typeof(IConstraint).IsAssignableFrom(p));

Lazy<Dictionary<string, Type>> 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;
}
}
}

0 comments on commit 41e3814

Please sign in to comment.