EntityEditor: Refactored type dropdown

This commit is contained in:
Simon Lübeß
2024-03-22 10:12:55 +01:00
parent d357f44a39
commit 94fc620662
+38 -10
View File
@@ -650,17 +650,11 @@ internal class EntityEditor
{ {
if (reference == null) if (reference == null)
{ {
if (ImGui.BeginCombo("##Create", "Create instance...", ImGuiComboFlags.HeightSmall | ImGuiComboFlags.NoArrowButton)) Type? selectedType = ShowTypeDropdown("##Create", "Create instance...", fieldType);
{
foreach (Type t in TypeExtension.FindDerivedTypes(fieldType))
{
if (ImGui.Selectable(t.Name))
{
newValue = ActivatorExtension.CreateInstanceSafe(t);
}
}
ImGui.EndCombo(); if (selectedType != null)
{
newValue = ActivatorExtension.CreateInstanceSafe(selectedType);
} }
} }
else else
@@ -690,6 +684,40 @@ internal class EntityEditor
return newValue; return newValue;
} }
/// <summary>
/// Shows a dropdown of all types that can be assigned to the given baseType.
/// </summary>
/// <param name="label">The label of the combo box.</param>
/// <param name="previewValue">The preview value that will be shown in the combo box.</param>
/// <param name="baseType">The base type of the types that can be selected.</param>
/// <returns>The selected type; or <see langword="null"/> if no type was selected.</returns>
private static Type? ShowTypeDropdown(string label, string? previewValue, Type baseType)
{
Type? selectedType = null;
if (ImGui.BeginCombo(label, previewValue, ImGuiComboFlags.HeightSmall | ImGuiComboFlags.NoArrowButton))
{
foreach (Type t in TypeExtension.FindDerivedTypes(baseType))
{
if (t.IsGenericType)
{
// TODO: Generic types
}
else
{
if (ImGui.Selectable(t.Name))
{
selectedType = t;
}
}
}
ImGui.EndCombo();
}
return selectedType;
}
private static object? ShowComponentDropTarget(string fieldName, Type fieldType, object? currentValue, IEnumerable<Attribute>? attributes) private static object? ShowComponentDropTarget(string fieldName, Type fieldType, object? currentValue, IEnumerable<Attribute>? attributes)
{ {
object? newValue = DidNotChange; object? newValue = DidNotChange;