Add ButtonAttribute to display methods in editor properties panel

#1917
This commit is contained in:
Wojtek Figat
2024-12-06 16:10:50 +01:00
parent 1088a71e69
commit b6d2a3683c
8 changed files with 122 additions and 0 deletions

View File

@@ -127,12 +127,41 @@ namespace FlaxEditor.CustomEditors
_isSetBlocked = true;
Initialize(layout);
ShowButtons();
Refresh();
_isSetBlocked = false;
CurrentCustomEditor = prev;
}
private void ShowButtons()
{
var values = Values;
if (values == null || values.HasDifferentTypes)
return;
var type = TypeUtils.GetObjectType(values[0]);
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var method in methods)
{
if (!method.HasAttribute(typeof(ButtonAttribute)) ||
method.ParametersCount != 0)
continue;
var attribute = method.GetAttribute<ButtonAttribute>();
var text = string.IsNullOrEmpty(attribute.Text) ? Utilities.Utils.GetPropertyNameUI(method.Name) : attribute.Text;
var tooltip = string.IsNullOrEmpty(attribute.Tooltip) ? Editor.Instance.CodeDocs.GetTooltip(method) : attribute.Tooltip;
var button = _layout.Button(text, tooltip);
button.Button.Tag = method;
button.Button.ButtonClicked += OnButtonClicked;
}
}
private void OnButtonClicked(Button button)
{
var method = (ScriptMemberInfo)button.Tag;
var obj = method.IsStatic ? null : Values[0];
method.Invoke(obj);
}
internal static CustomEditor CurrentCustomEditor;
internal void OnChildCreated(CustomEditor child)