Add a useParameters bool to the ButtonAttribute.

Also add tooltip with the method signature, pretty print the method name
( doThing() -> Do Thing ), and make method buttons that do have parameters use any default parameters in the invocation foldout too.
This commit is contained in:
Pete Goodfellow 2024-03-26 20:54:16 +00:00
parent 93a56757a8
commit 827ccf06cb
4 changed files with 26 additions and 13 deletions

View File

@ -8,22 +8,32 @@ namespace Alchemy.Editor.Elements
{
const string ButtonLabelText = "Invoke";
public MethodButton(object target, MethodInfo methodInfo)
public MethodButton(object target, MethodInfo methodInfo, bool useParameters)
{
var parameters = methodInfo.GetParameters();
var parameterObjects = new object[parameters.Length];
for(int i = 0; i < parameters.Length; i++)
{
if(parameters[i].HasDefaultValue)
parameterObjects[i] = parameters[i].DefaultValue;
else
parameterObjects[i] = TypeHelper.CreateDefaultInstance(parameters[i].ParameterType);
}
// Create parameterless button
if (parameters.Length == 0)
if (!useParameters || parameters.Length == 0)
{
button = new Button(() => methodInfo.Invoke(target, null))
{
text = methodInfo.Name
};
if(parameters.Length > 0)
button = new Button(() => methodInfo.Invoke(target, parameterObjects));
else
button = new Button(() => methodInfo.Invoke(target, null));
button.text = ObjectNames.NicifyVariableName(methodInfo.Name);
button.tooltip = methodInfo.ToString();
Add(button);
return;
}
var parameterObjects = new object[parameters.Length];
var box = new HelpBox();
Add(box);
@ -59,7 +69,6 @@ namespace Alchemy.Editor.Elements
{
var index = i;
var parameter = parameters[index];
parameterObjects[index] = TypeHelper.CreateDefaultInstance(parameter.ParameterType);
var element = new GenericField(parameterObjects[index], parameter.ParameterType, ObjectNames.NicifyVariableName(parameter.Name));
element.OnValueChanged += x => parameterObjects[index] = x;
element.style.paddingRight = 4f;

View File

@ -20,9 +20,9 @@ namespace Alchemy.Editor.Elements
if (memberInfo is MethodInfo methodInfo)
{
if (methodInfo.HasCustomAttribute<ButtonAttribute>())
if (methodInfo.TryGetCustomAttribute<ButtonAttribute>(out var buttonAttribute))
{
var button = new MethodButton(target, methodInfo);
var button = new MethodButton(target, methodInfo, buttonAttribute.useParameters);
Add(button);
}
return;

View File

@ -199,9 +199,9 @@ namespace Alchemy.Editor
switch (memberInfo)
{
case MethodInfo methodInfo:
if (methodInfo.HasCustomAttribute<ButtonAttribute>())
if (methodInfo.TryGetCustomAttribute<ButtonAttribute>(out var buttonAttribute))
{
return new MethodButton(target, methodInfo);
return new MethodButton(target, methodInfo, buttonAttribute.useParameters);
}
break;
case FieldInfo:

View File

@ -18,7 +18,11 @@ namespace Alchemy.Inspector
}
[AttributeUsage(AttributeTargets.Method)]
public sealed class ButtonAttribute : Attribute { }
public sealed class ButtonAttribute : Attribute
{
public ButtonAttribute(bool useParameters = true) => this.useParameters = useParameters;
public readonly bool useParameters;
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class ShowInInspectorAttribute : Attribute { }