Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Alchemy/Assets/Alchemy/Editor/Elements/MethodButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ public sealed class MethodButton : VisualElement
{
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);
Expand Down Expand Up @@ -59,7 +69,6 @@ public MethodButton(object target, MethodInfo methodInfo)
{
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;
Expand Down
4 changes: 2 additions & 2 deletions Alchemy/Assets/Alchemy/Editor/Elements/ReflectionField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void Rebuild(object target, MemberInfo memberInfo)

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;
Expand Down
4 changes: 2 additions & 2 deletions Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ public static VisualElement CreateMemberElement(SerializedObject serializedObjec
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public sealed class OrderAttribute : Attribute
}

[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 { }
Expand Down