-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathButtonDrawer.cs
More file actions
183 lines (157 loc) · 6.86 KB
/
ButtonDrawer.cs
File metadata and controls
183 lines (157 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Reflection;
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using TriInspector.Resolvers;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeDrawer(typeof(ButtonDrawer), TriDrawerOrder.Drawer)]
namespace TriInspector.Drawers
{
public class ButtonDrawer : TriAttributeDrawer<ButtonAttribute>
{
private ValueResolver<string> _nameResolver;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
var isValidMethod = propertyDefinition.TryGetMemberInfo(out var memberInfo) && memberInfo is MethodInfo;
if (!isValidMethod)
{
return "[Button] valid only on methods";
}
_nameResolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Name);
if (_nameResolver.TryGetErrorString(out var error))
{
return error;
}
return TriExtensionInitializationResult.Ok;
}
public override TriElement CreateElement(TriProperty property, TriElement next)
{
return new TriButtonElement(property, Attribute, _nameResolver);
}
private class TriButtonElement : TriHeaderGroupBaseElement
{
private readonly TriProperty _property;
private readonly ButtonAttribute _attribute;
private readonly ValueResolver<string> _nameResolver;
private readonly object[] _invocationArgs;
public TriButtonElement(TriProperty property, ButtonAttribute attribute,
ValueResolver<string> nameResolver)
{
_property = property;
_attribute = attribute;
_nameResolver = nameResolver;
var mi = property.TryGetMemberInfo(out var memberInfo)
? (MethodInfo) memberInfo
: throw new Exception("TriButtonElement requires MethodInfo");
var parameters = mi.GetParameters();
_invocationArgs = new object[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
var pIndex = i;
var pInfo = parameters[pIndex];
var key = $"TriInspector.{_property.PropertyTree.TargetObjectType}.{_property.RawName}.{pInfo.Name}";
if (pInfo.HasDefaultValue)
{
_invocationArgs[pIndex] = GetValue(key, pInfo.ParameterType, pInfo.DefaultValue);
}
var pTriDefinition = TriPropertyDefinition.CreateForGetterSetter(
pIndex, pInfo.Name, pInfo.ParameterType,
((self, targetIndex) => _invocationArgs[pIndex]),
((self, targetIndex, value) =>
{
SetValue(key, pInfo.ParameterType, value);
return _invocationArgs[pIndex] = value;
}));
var pTriProperty = new TriProperty(_property.PropertyTree, _property, pTriDefinition, null);
AddChild(new TriPropertyElement(pTriProperty));
static object GetValue(string key, Type type, object defaultValue)
{
return type switch
{
Type t when t == typeof(string) => EditorPrefs.GetString(key, (string) defaultValue),
Type t when t == typeof(int) => EditorPrefs.GetInt(key, (int) defaultValue),
Type t when t == typeof(bool) => EditorPrefs.GetBool(key, (bool) defaultValue),
Type t when t == typeof(float) => EditorPrefs.GetFloat(key, (float) defaultValue),
_ => defaultValue,
};
}
static void SetValue(string key, Type type, object value)
{
if (EditorApplication.isPlaying)
return;
switch (type)
{
case Type t when t == typeof(string):
EditorPrefs.SetString(key, (string) value);
break;
case Type t when t == typeof(int):
EditorPrefs.SetInt(key, (int) value);
break;
case Type t when t == typeof(bool):
EditorPrefs.SetBool(key, (bool) value);
break;
case Type t when t == typeof(float):
EditorPrefs.SetFloat(key, (float) value);
break;
}
}
}
}
protected override float GetHeaderHeight(float width)
{
return GetButtonHeight();
}
protected override void DrawHeader(Rect position)
{
if (_invocationArgs.Length > 0)
{
TriEditorGUI.DrawBox(position, TriEditorStyles.TabOnlyOne);
}
var name = _nameResolver.GetValue(_property);
if (string.IsNullOrEmpty(name))
{
name = _property.DisplayName;
}
if (string.IsNullOrEmpty(name))
{
name = _property.RawName;
}
var buttonRect = new Rect(position)
{
height = GetButtonHeight(),
};
if (GUI.Button(buttonRect, name))
{
InvokeButton(_property, _invocationArgs);
}
}
private float GetButtonHeight()
{
return _attribute.ButtonSize != 0
? _attribute.ButtonSize
: EditorGUIUtility.singleLineHeight;
}
}
private static void InvokeButton(TriProperty property, object[] parameters)
{
if (property.TryGetMemberInfo(out var memberInfo) && memberInfo is MethodInfo methodInfo)
{
property.ModifyAndRecordForUndo(targetIndex =>
{
try
{
var parentValue = property.Parent.GetValue(targetIndex);
methodInfo.Invoke(parentValue, parameters);
}
catch (Exception e)
{
Debug.LogException(e);
}
});
}
}
}
}