Skip to content

Commit 20faf1b

Browse files
committed
Add drawer for Button with parameters
1 parent 58bbdb5 commit 20faf1b

File tree

5 files changed

+105
-28
lines changed

5 files changed

+105
-28
lines changed

Editor.Extras/Drawers/ButtonDrawer.cs

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Reflection;
33
using TriInspector;
44
using TriInspector.Drawers;
5+
using TriInspector.Elements;
56
using TriInspector.Resolvers;
7+
using TriInspector.Utilities;
68
using UnityEditor;
79
using UnityEngine;
810

@@ -16,12 +18,10 @@ public class ButtonDrawer : TriAttributeDrawer<ButtonAttribute>
1618

1719
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
1820
{
19-
var isValidMethod = propertyDefinition.TryGetMemberInfo(out var memberInfo) &&
20-
memberInfo is MethodInfo mi &&
21-
mi.GetParameters().Length == 0;
21+
var isValidMethod = propertyDefinition.TryGetMemberInfo(out var memberInfo) && memberInfo is MethodInfo;
2222
if (!isValidMethod)
2323
{
24-
return "[Button] valid only on methods without parameters";
24+
return "[Button] valid only on methods";
2525
}
2626

2727
_nameResolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Name);
@@ -33,33 +33,94 @@ memberInfo is MethodInfo mi &&
3333
return TriExtensionInitializationResult.Ok;
3434
}
3535

36-
public override float GetHeight(float width, TriProperty property, TriElement next)
36+
public override TriElement CreateElement(TriProperty property, TriElement next)
3737
{
38-
if (Attribute.ButtonSize != 0)
39-
{
40-
return Attribute.ButtonSize;
41-
}
42-
43-
return EditorGUIUtility.singleLineHeight;
38+
return new TriButtonElement(property, Attribute, _nameResolver);
4439
}
4540

46-
public override void OnGUI(Rect position, TriProperty property, TriElement next)
41+
private class TriButtonElement : TriHeaderGroupBaseElement
4742
{
48-
var name = _nameResolver.GetValue(property);
43+
private readonly TriProperty _property;
44+
private readonly ButtonAttribute _attribute;
45+
private readonly ValueResolver<string> _nameResolver;
46+
private readonly object[] _invocationArgs;
4947

50-
if (string.IsNullOrEmpty(name))
48+
public TriButtonElement(TriProperty property, ButtonAttribute attribute,
49+
ValueResolver<string> nameResolver)
5150
{
52-
name = property.DisplayName;
51+
_property = property;
52+
_attribute = attribute;
53+
_nameResolver = nameResolver;
54+
55+
var mi = property.TryGetMemberInfo(out var memberInfo)
56+
? (MethodInfo) memberInfo
57+
: throw new Exception("TriButtonElement requires MethodInfo");
58+
59+
var parameters = mi.GetParameters();
60+
61+
_invocationArgs = new object[parameters.Length];
62+
63+
for (var i = 0; i < parameters.Length; i++)
64+
{
65+
var pIndex = i;
66+
var pInfo = parameters[pIndex];
67+
68+
if (pInfo.HasDefaultValue)
69+
{
70+
_invocationArgs[pIndex] = pInfo.DefaultValue;
71+
}
72+
73+
var pTriDefinition = TriPropertyDefinition.CreateForGetterSetter(
74+
pIndex, pInfo.Name, pInfo.ParameterType,
75+
((self, targetIndex) => _invocationArgs[pIndex]),
76+
((self, targetIndex, value) => _invocationArgs[pIndex] = value));
77+
78+
var pTriProperty = new TriProperty(_property.PropertyTree, _property, pTriDefinition, null);
79+
80+
AddChild(new TriPropertyElement(pTriProperty));
81+
}
5382
}
5483

55-
if (string.IsNullOrEmpty(name))
84+
protected override float GetHeaderHeight(float width)
5685
{
57-
name = property.RawName;
86+
return GetButtonHeight();
87+
}
88+
89+
protected override void DrawHeader(Rect position)
90+
{
91+
if (_invocationArgs.Length > 0)
92+
{
93+
TriEditorGUI.DrawBox(position, TriEditorStyles.TabOnlyOne);
94+
}
95+
96+
var name = _nameResolver.GetValue(_property);
97+
98+
if (string.IsNullOrEmpty(name))
99+
{
100+
name = _property.DisplayName;
101+
}
102+
103+
if (string.IsNullOrEmpty(name))
104+
{
105+
name = _property.RawName;
106+
}
107+
108+
var buttonRect = new Rect(position)
109+
{
110+
height = GetButtonHeight(),
111+
};
112+
113+
if (GUI.Button(buttonRect, name))
114+
{
115+
InvokeButton(_property, _invocationArgs);
116+
}
58117
}
59118

60-
if (GUI.Button(position, name))
119+
private float GetButtonHeight()
61120
{
62-
InvokeButton(property, Array.Empty<object>());
121+
return _attribute.ButtonSize != 0
122+
? _attribute.ButtonSize
123+
: EditorGUIUtility.singleLineHeight;
63124
}
64125
}
65126

Editor.Samples/Buttons/Buttons_ButtonSample.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ private void DoButton()
88
{
99
Debug.Log("Button clicked!");
1010
}
11+
12+
[Button(ButtonSizes.Large)]
13+
private void DoButtonWithParameters(Vector3 vec, string str = "default value")
14+
{
15+
Debug.Log($"Button with parameters: {vec} {str}");
16+
}
1117
}

Editor/TriPropertyDefinition.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class TriPropertyDefinition
1818
private readonly List<string> _extensionErrors = new List<string>();
1919
private readonly MemberInfo _memberInfo;
2020
private readonly List<Attribute> _attributes;
21-
private readonly bool _isNonPolymorphicSerializedByUnity;
21+
private readonly bool _skipNullValuesFix;
2222

2323
private TriPropertyDefinition _arrayElementDefinitionBackingField;
2424

@@ -53,6 +53,14 @@ private static TriPropertyDefinition CreateForMemberInfo(
5353
memberInfo, ownerType, order, propertyName, propertyType, valueGetter, valueSetter, attributes, false);
5454
}
5555

56+
internal static TriPropertyDefinition CreateForGetterSetter(
57+
int order, string name, Type fieldType,
58+
ValueGetterDelegate valueGetter, ValueSetterDelegate valueSetter)
59+
{
60+
return new TriPropertyDefinition(
61+
null, null, order, name, fieldType, valueGetter, valueSetter, null, false);
62+
}
63+
5664
internal TriPropertyDefinition(
5765
MemberInfo memberInfo,
5866
Type ownerType,
@@ -74,9 +82,7 @@ internal TriPropertyDefinition(
7482
_valueGetter = valueGetter;
7583
_valueSetter = valueSetter;
7684

77-
_isNonPolymorphicSerializedByUnity = memberInfo is FieldInfo fi &&
78-
TriUnitySerializationUtilities.IsSerializableByUnity(fi) &&
79-
fi.GetCustomAttribute<SerializeReference>() == null;
85+
_skipNullValuesFix = memberInfo != null && memberInfo.GetCustomAttribute<SerializeReference>() != null;
8086

8187
Order = order;
8288
IsReadOnly = _valueSetter == null || Attributes.TryGet(out ReadOnlyAttribute _);
@@ -150,7 +156,7 @@ public object GetValue(TriProperty property, int targetIndex)
150156
{
151157
var value = _valueGetter(property, targetIndex);
152158

153-
if (value == null && _isNonPolymorphicSerializedByUnity)
159+
if (value == null && !_skipNullValuesFix)
154160
{
155161
value = TriUnitySerializationUtilities.PopulateUnityDefaultValueForType(FieldType);
156162

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,16 @@ public float val;
544544

545545
#### Button
546546

547-
![Button](https://user-images.githubusercontent.com/26966368/168235907-2b5ed6d4-d00b-4cd6-999c-432abd0a2230.png)
547+
![Button](https://github.com/codewriter-packages/Tri-Inspector/assets/26966368/76f4a3a4-4bf9-4f58-8615-17adb986ab81)
548548

549549
```csharp
550550
[Button("Click me!")]
551-
private void DoButton()
551+
private void Button() => Debug.Log("Button clicked!");
552+
553+
[Button(ButtonSizes.Large)]
554+
private void ButtonWithParameters(Vector3 vec, string str = "default value")
552555
{
553-
Debug.Log("Button clicked!");
556+
Debug.Log($"Button with parameters: {vec} {str}");
554557
}
555558
```
556559

Runtime/Attributes/ButtonAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public ButtonAttribute(string name)
1616
Name = name;
1717
}
1818

19-
public ButtonAttribute(ButtonSizes buttonSize)
19+
public ButtonAttribute(ButtonSizes buttonSize, string name = null)
2020
{
2121
ButtonSize = (int) buttonSize;
22+
Name = name;
2223
}
2324

2425
public string Name { get; set; }

0 commit comments

Comments
 (0)