Add type picker when creating abstract class or interface

This commit is contained in:
2026-07-22 11:18:32 +02:00
parent 2723f91905
commit 7e48786ffc
2 changed files with 64 additions and 13 deletions
@@ -711,21 +711,44 @@ namespace FlaxEditor.CustomEditors.Editors
{
// Check if it's an object type that can be created in editor
var type = Values.Type;
if (type != ScriptMemberInfo.Null && type.CanCreateInstance)
if (type != ScriptMemberInfo.Null)
{
layout = layout.Space(20);
const float ButtonSize = 14.0f;
var button = new Button
ScriptType[] types = null;
if (type.IsAbstract || type.IsInterface)
{
Text = "+",
TooltipText = "Create a new instance of the object",
Size = new Float2(ButtonSize, ButtonSize),
AnchorPreset = AnchorPresets.MiddleRight,
Parent = layout.ContainerControl,
Location = new Float2(layout.ContainerControl.Width - ButtonSize - 4, (layout.ContainerControl.Height - ButtonSize) * 0.5f),
};
button.Clicked += () => SetValue(Values.Type.CreateInstance());
// Show picker with all types that implement specific class/interface but are not abstract
types = Editor.Instance.CodeEditing.All.Get().Where(x => !x.IsAbstract && x.CanCreateInstance && type.IsAssignableFrom(x)).ToArray();
}
else if (type.CanCreateInstance)
{
types = [type];
}
if (types != null && types.Length != 0)
{
layout = layout.Space(20);
const float ButtonSize = 14.0f;
var button = new Button
{
Text = "+",
TooltipText = "Create a new instance of the object",
Size = new Float2(ButtonSize, ButtonSize),
AnchorPreset = AnchorPresets.MiddleRight,
Parent = layout.ContainerControl,
Location = new Float2(layout.ContainerControl.Width - ButtonSize - 4, (layout.ContainerControl.Height - ButtonSize) * 0.5f),
};
if (types.Length == 1)
{
// Single type
button.Clicked += () => SetValue(Values.Type.CreateInstance());
}
else
{
// Picker
button.Clicked += () => FlaxEditor.GUI.TypeSearchPopup.Show(button, new Float2(0, button.Height), types, scriptType => { SetValue(scriptType.CreateInstance()); });
}
}
}
layout.Label("<null>");
@@ -1,6 +1,7 @@
// Copyright (c) Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using FlaxEditor.History;
@@ -131,6 +132,18 @@ namespace FlaxEditor.GUI
SortItems();
}
private TypeSearchPopup(IEnumerable<ScriptType> items, Action<ScriptType> selected)
{
_isValid = null;
_selected = selected;
ItemClicked += OnItemClicked;
foreach (var item in items)
AddItem(new TypeItemView(item));
SortItems();
}
private bool IsHideAttributes(object[] attributes)
{
return attributes.FirstOrDefault(IsHideAttribute) == null;
@@ -162,6 +175,21 @@ namespace FlaxEditor.GUI
return popup;
}
/// <summary>
/// Shows the popup.
/// </summary>
/// <param name="showTarget">The show target.</param>
/// <param name="showTargetLocation">The show target location.</param>
/// <param name="items">Collection of types to display available to pick.</param>
/// <param name="selected">Event called on asset item pick.</param>
/// <returns>The dialog.</returns>
public static TypeSearchPopup Show(Control showTarget, Float2 showTargetLocation, IEnumerable<ScriptType> items, Action<ScriptType> selected)
{
var popup = new TypeSearchPopup(items, selected);
popup.Show(showTarget, showTargetLocation);
return popup;
}
/// <inheritdoc />
public override void OnDestroy()
{