summaryrefslogtreecommitdiff
path: root/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs
blob: 561796c25a7cb9b28e6e68ba566eaee9c88868f5 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

[CustomPropertyDrawer(typeof(TargetConstraintAttribute))]
[CustomPropertyDrawer(typeof(SerializableCallbackBase), true)]
public class SerializableCallbackDrawer : PropertyDrawer {

	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
		// Without this, you can't edit fields above the SerializedProperty
		property.serializedObject.ApplyModifiedProperties();

		// Indent label
		label.text = " " + label.text;

#if UNITY_2019_1_OR_NEWER
		GUI.Box(position, "");
#else
		GUI.Box(position, "", (GUIStyle)
			"flow overlay box");
#endif
		position.y += 4;
		// Using BeginProperty / EndProperty on the parent property means that
		// prefab override logic works on the entire property.
		property.serializedObject.Update();
		EditorGUI.BeginProperty(position, label, property);
		// Draw label
		Rect pos = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

		Rect targetRect = new Rect(pos.x, pos.y, pos.width, EditorGUIUtility.singleLineHeight);

		// Get target
		SerializedProperty targetProp = property.FindPropertyRelative("_target");
		object target = targetProp.objectReferenceValue;
		if (attribute != null && attribute is TargetConstraintAttribute) {
			Type targetType = (attribute as TargetConstraintAttribute).targetType;
			EditorGUI.ObjectField(targetRect, targetProp, targetType, GUIContent.none);
		} else EditorGUI.PropertyField(targetRect, targetProp, GUIContent.none);

		if (target == null) {
			Rect helpBoxRect = new Rect(position.x + 8, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width - 16, EditorGUIUtility.singleLineHeight);
			string msg = "Call not set. Execution will be slower.";
			EditorGUI.HelpBox(helpBoxRect, msg, MessageType.Warning);
		} else if (target is MonoScript) {
			Rect helpBoxRect = new Rect(position.x + 8, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width - 16, EditorGUIUtility.singleLineHeight + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
			string msg = "Assign a GameObject, Component or a ScriptableObject, not a script.";
			EditorGUI.HelpBox(helpBoxRect, msg, MessageType.Warning);
		} else {
			int indent = EditorGUI.indentLevel;
			EditorGUI.indentLevel++;

			// Get method name
			SerializedProperty methodProp = property.FindPropertyRelative("_methodName");
			string methodName = methodProp.stringValue;

			// Get args
			SerializedProperty argProps = property.FindPropertyRelative("_args");
			Type[] argTypes = GetArgTypes(argProps);

			// Get dynamic
			SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic");
			bool dynamic = dynamicProp.boolValue;

			// Get active method
			MethodInfo activeMethod = GetMethod(target, methodName, argTypes);

			GUIContent methodlabel = new GUIContent("n/a");
			if (activeMethod != null) methodlabel = new GUIContent(PrettifyMethod(activeMethod));
			else if (!string.IsNullOrEmpty(methodName)) methodlabel = new GUIContent("Missing (" + PrettifyMethod(methodName, argTypes) + ")");

			Rect methodRect = new Rect(position.x, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight);

			// Method select button
			pos = EditorGUI.PrefixLabel(methodRect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(dynamic ? "Method (dynamic)" : "Method"));
			if (EditorGUI.DropdownButton(pos, methodlabel, FocusType.Keyboard)) {
				MethodSelector(property);
			}

			if (activeMethod != null && !dynamic) {
				// Args
				ParameterInfo[] activeParameters = activeMethod.GetParameters();
				Rect argRect = new Rect(position.x, methodRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight);
				string[] types = new string[argProps.arraySize];
				for (int i = 0; i < types.Length; i++) {
					SerializedProperty argProp = argProps.FindPropertyRelative("Array.data[" + i + "]");
					GUIContent argLabel = new GUIContent(ObjectNames.NicifyVariableName(activeParameters[i].Name));

					EditorGUI.BeginChangeCheck();
					switch ((Arg.ArgType) argProp.FindPropertyRelative("argType").enumValueIndex) {
						case Arg.ArgType.Bool:
							EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("boolValue"), argLabel);
							break;
						case Arg.ArgType.Int:
							EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("intValue"), argLabel);
							break;
						case Arg.ArgType.Float:
							EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("floatValue"), argLabel);
							break;
						case Arg.ArgType.String:
							EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("stringValue"), argLabel);
							break;
						case Arg.ArgType.Object:
							SerializedProperty typeProp = argProp.FindPropertyRelative("_typeName");
							SerializedProperty objProp = argProp.FindPropertyRelative("objectValue");

							if (typeProp != null) {
								Type objType = Type.GetType(typeProp.stringValue, false);
								EditorGUI.BeginChangeCheck();
								Object obj = objProp.objectReferenceValue;
								obj = EditorGUI.ObjectField(argRect, argLabel, obj, objType, true);
								if (EditorGUI.EndChangeCheck()) {
									objProp.objectReferenceValue = obj;
								}
							} else {
								EditorGUI.PropertyField(argRect, objProp, argLabel);
							}
							break;
					}
					if (EditorGUI.EndChangeCheck()) {
						property.FindPropertyRelative("dirty").boolValue = true;
					}
					argRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
				}
			}
			EditorGUI.indentLevel = indent;
		}

		// Set indent back to what it was
		EditorGUI.EndProperty();
		property.serializedObject.ApplyModifiedProperties();
	}

	private class MenuItem {
		public GenericMenu.MenuFunction action;
		public string path;
		public GUIContent label;

		public MenuItem(string path, string name, GenericMenu.MenuFunction action) {
			this.action = action;
			this.label = new GUIContent(path + '/' + name);
			this.path = path;
		}
	}
	void MethodSelector(SerializedProperty property) {
		// Return type constraint
		Type returnType = null;
		// Arg type constraint
		Type[] argTypes = new Type[0];

		// Get return type and argument constraints
		SerializableCallbackBase dummy = GetDummyFunction(property);
		Type[] genericTypes = dummy.GetType().BaseType.GetGenericArguments();
		// SerializableEventBase is always void return type
		if (dummy is SerializableEventBase) {
			returnType = typeof(void);
			if (genericTypes.Length > 0) {
				argTypes = new Type[genericTypes.Length];
				Array.Copy(genericTypes, argTypes, genericTypes.Length);
			}
		} else {
			if (genericTypes != null && genericTypes.Length > 0) {
				// The last generic argument is the return type
				returnType = genericTypes[genericTypes.Length - 1];
				if (genericTypes.Length > 1) {
					argTypes = new Type[genericTypes.Length - 1];
					Array.Copy(genericTypes, argTypes, genericTypes.Length - 1);
				}
			}
		}

		SerializedProperty targetProp = property.FindPropertyRelative("_target");

		List<MenuItem> dynamicItems = new List<MenuItem>();
		List<MenuItem> staticItems = new List<MenuItem>();

		List<Object> targets = new List<Object>() { targetProp.objectReferenceValue };

		if (targets[0] is Component) {
			targets = (targets[0] as Component).gameObject.GetComponents<Component>().ToList<Object>();
			targets.Add((targetProp.objectReferenceValue as Component).gameObject);
		} else if (targets[0] is GameObject) {
			targets = (targets[0] as GameObject).GetComponents<Component>().ToList<Object>();
			targets.Add(targetProp.objectReferenceValue as GameObject);
		}
		for (int c = 0; c < targets.Count; c++) {
			Object t = targets[c];
			MethodInfo[] methods = targets[c].GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

			for (int i = 0; i < methods.Length; i++) {
				MethodInfo method = methods[i];

				// Skip methods with wrong return type
				if (returnType != null && method.ReturnType != returnType) continue;
				// Skip methods with null return type
				// if (method.ReturnType == typeof(void)) continue;
				// Skip generic methods
				if (method.IsGenericMethod) continue;

				Type[] parms = method.GetParameters().Select(x => x.ParameterType).ToArray();

				// Skip methods with more than 4 args
				if (parms.Length > 4) continue;
				// Skip methods with unsupported args
				if (parms.Any(x => !Arg.IsSupported(x))) continue;

				string methodPrettyName = PrettifyMethod(methods[i]);
				staticItems.Add(new MenuItem(targets[c].GetType().Name + "/" + methods[i].DeclaringType.Name, methodPrettyName, () => SetMethod(property, t, method, false)));

				// Skip methods with wrong constrained args
				if (argTypes.Length == 0 || !Enumerable.SequenceEqual(argTypes, parms)) continue;

				dynamicItems.Add(new MenuItem(targets[c].GetType().Name + "/" + methods[i].DeclaringType.Name, methods[i].Name, () => SetMethod(property, t, method, true)));
			}
		}

		// Construct and display context menu
		GenericMenu menu = new GenericMenu();
		if (dynamicItems.Count > 0) {
			string[] paths = dynamicItems.GroupBy(x => x.path).Select(x => x.First().path).ToArray();
			foreach (string path in paths) {
				menu.AddItem(new GUIContent(path + "/Dynamic " + PrettifyTypes(argTypes)), false, null);
			}
			for (int i = 0; i < dynamicItems.Count; i++) {
				menu.AddItem(dynamicItems[i].label, false, dynamicItems[i].action);
			}
			foreach (string path in paths) {
				menu.AddItem(new GUIContent(path + "/  "), false, null);
				menu.AddItem(new GUIContent(path + "/Static parameters"), false, null);
			}
		}
		for (int i = 0; i < staticItems.Count; i++) {
			menu.AddItem(staticItems[i].label, false, staticItems[i].action);
		}
		if (menu.GetItemCount() == 0) menu.AddDisabledItem(new GUIContent("No methods with return type '" + GetTypeName(returnType) + "'"));
		menu.ShowAsContext();
	}

	string PrettifyMethod(string methodName, Type[] parmTypes) {
		string parmnames = PrettifyTypes(parmTypes);
		return methodName + "(" + parmnames + ")";
	}

	string PrettifyMethod(MethodInfo methodInfo) {
		if (methodInfo == null) throw new ArgumentNullException("methodInfo");
		ParameterInfo[] parms = methodInfo.GetParameters();
		string parmnames = PrettifyTypes(parms.Select(x => x.ParameterType).ToArray());
		return GetTypeName(methodInfo.ReturnParameter.ParameterType) + " " + methodInfo.Name + "(" + parmnames + ")";
	}

	string PrettifyTypes(Type[] types) {
		if (types == null) throw new ArgumentNullException("types");
		return string.Join(", ", types.Select(x => GetTypeName(x)).ToArray());
	}

	MethodInfo GetMethod(object target, string methodName, Type[] types) {
		MethodInfo activeMethod = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, CallingConventions.Any, types, null);
		return activeMethod;
	}

	private Type[] GetArgTypes(SerializedProperty argsProp) {
		Type[] types = new Type[argsProp.arraySize];
		for (int i = 0; i < argsProp.arraySize; i++) {
			SerializedProperty argProp = argsProp.GetArrayElementAtIndex(i);
			SerializedProperty typeNameProp = argProp.FindPropertyRelative("_typeName");
			if (typeNameProp != null) types[i] = Type.GetType(typeNameProp.stringValue, false);
			if (types[i] == null) types[i] = Arg.RealType((Arg.ArgType) argProp.FindPropertyRelative("argType").enumValueIndex);
		}
		return types;
	}

	private void SetMethod(SerializedProperty property, UnityEngine.Object target, MethodInfo methodInfo, bool dynamic) {
		SerializedProperty targetProp = property.FindPropertyRelative("_target");
		targetProp.objectReferenceValue = target;
		SerializedProperty methodProp = property.FindPropertyRelative("_methodName");
		methodProp.stringValue = methodInfo.Name;
		SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic");
		dynamicProp.boolValue = dynamic;
		SerializedProperty argProp = property.FindPropertyRelative("_args");
		ParameterInfo[] parameters = methodInfo.GetParameters();
		argProp.arraySize = parameters.Length;
		for (int i = 0; i < parameters.Length; i++) {
			argProp.FindPropertyRelative("Array.data[" + i + "].argType").enumValueIndex = (int) Arg.FromRealType(parameters[i].ParameterType);
			argProp.FindPropertyRelative("Array.data[" + i + "]._typeName").stringValue = parameters[i].ParameterType.AssemblyQualifiedName;
		}
		property.FindPropertyRelative("dirty").boolValue = true;
		property.serializedObject.ApplyModifiedProperties();
		property.serializedObject.Update();
	}

	private static string GetTypeName(Type t) {
		if (t == typeof(int)) return "int";
		else if (t == typeof(float)) return "float";
		else if (t == typeof(string)) return "string";
		else if (t == typeof(bool)) return "bool";
		else if (t == typeof(void)) return "void";
		else return t.Name;
	}

	public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
		float lineheight = EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
		SerializedProperty targetProp = property.FindPropertyRelative("_target");
		SerializedProperty argProps = property.FindPropertyRelative("_args");
		SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic");
		float height = lineheight + lineheight;
		if (targetProp.objectReferenceValue != null && targetProp.objectReferenceValue is MonoScript) height += lineheight;
		else if (targetProp.objectReferenceValue != null && !dynamicProp.boolValue) height += argProps.arraySize * lineheight;
		height += 8;
		return height;
	}

	private static SerializableCallbackBase GetDummyFunction(SerializedProperty prop) {
		string stringValue = prop.FindPropertyRelative("_typeName").stringValue;
		Type type = Type.GetType(stringValue, false);
		SerializableCallbackBase result;
		if (type == null) {
			return null;
		} else {
			result = (Activator.CreateInstance(type) as SerializableCallbackBase);
		}
		return result;
	}
}