PopupOption DrawProviderOptions(Rect position, ReceiverLinkerItem linkerItem, ReceiverMethod method, object targetObject) { var controlPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(CleanMethodName(method.Name))); var options = new List <PopupOption> { // None-option new PopupOption(receiverTypeName: ReceiverLinker.IgnoreReceiver), // Automatic option where the receiver type is the parameter type new PopupOption(method.Type, receiverTypeName: "") }; options.AddRange(GetOptions(targetObject, method.Type)); var guiOptions = options.ConvertAll(option => option.PopupString); //guiOptions[0] = "<none>"; var selectedIndex = options.FindIndex(option => option.MatchesLinker(linkerItem)); if (selectedIndex < 0) { // Can happen if class or method name is changed selectedIndex = options.Count; options.Add(new PopupOption(path: linkerItem.Path.Split('/'), receiverTypeName: linkerItem.ReceiverType)); guiOptions.Add(linkerItem.ReceiverType + '/' + linkerItem.Path); } int index = EditorGUI.Popup( new Rect(controlPosition.x, controlPosition.y, controlPosition.width, EditorGUIUtility.singleLineHeight), selectedIndex, guiOptions.ToArray() ); return(options[index]); }
void DrawProviderPreview(Rect position, ReceiverLinkerItem linkerItem, ReceiverMethod method, Type receiverType, MonoBehaviour targetObject) { var controlPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(" ")); IDataProviderObject provider = null; if (receiverType != null) { provider = ReceiverComponentBuilder.FindProvider(targetObject, receiverType, ReceiverComponentBuilder.TypeDataProviderObject); } if (provider != null) { GUI.enabled = false; EditorGUI.ObjectField( new Rect(controlPosition.x, controlPosition.y, controlPosition.width, EditorGUIUtility.singleLineHeight), provider as Component, typeof(Component), true ); GUI.enabled = true; } else { var style = new GUIStyle(); style.normal.textColor = Color.red; EditorGUI.LabelField( new Rect(controlPosition.x, controlPosition.y, controlPosition.width, EditorGUIUtility.singleLineHeight), "Provider not found!", style ); } }
private ReceiverLinkerItem LoadObject(SerializedProperty element) { var linkerItem = new ReceiverLinkerItem(); linkerItem.Receiver = element.FindPropertyRelative(nameof(linkerItem.Receiver)).stringValue; linkerItem.ReceiverType = element.FindPropertyRelative(nameof(linkerItem.ReceiverType)).stringValue; linkerItem.Path = element.FindPropertyRelative(nameof(linkerItem.Path)).stringValue; return(linkerItem); }
public bool MatchesLinker(ReceiverLinkerItem linkerItem) { if (linkerItem == null) { return(string.IsNullOrEmpty(ReceiverTypeName)); } if (linkerItem.ReceiverType == ReceiverTypeName) { return(Path == linkerItem.Path); } return(false); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.serializedObject.isEditingMultipleObjects) { return; } EditorGUI.BeginProperty(position, label, property); var linker = new ReceiverLinker(); var members = property.FindPropertyRelative(nameof(linker.Members)); var accessorList = new List <ReceiverLinkerItem>(); { for (int i = 0; i < members.arraySize; i++) { accessorList.Add(LoadObject(members.GetArrayElementAtIndex(i))); } } members.ClearArray(); var targetObject = property.serializedObject.targetObject as MonoBehaviour; var targetObjectType = targetObject.GetType(); foreach (var method in ReceiverMethod.GetAll(targetObject)) { var option = DrawProviderOptions(position, accessorList.Find(item => item.Receiver == method.Name), method, targetObject); position.y += EditorGUIUtility.singleLineHeight; var linkerItem = new ReceiverLinkerItem(); linkerItem.Receiver = method.Name; linkerItem.ReceiverType = option.ReceiverTypeName; linkerItem.Path = option.Path; if (!string.IsNullOrEmpty(option.ReceiverTypeName)) { int size = members.arraySize; members.InsertArrayElementAtIndex(size); var obj = members.GetArrayElementAtIndex(size); SaveObject(obj, linkerItem); } if (linkerItem.ReceiverType != ReceiverLinker.IgnoreReceiver) { DrawProviderPreview(position, linkerItem, method, option.ReceiverType, targetObject); } position.y += EditorGUIUtility.singleLineHeight; } EditorGUI.EndProperty(); }
private void SaveObject(SerializedProperty obj, ReceiverLinkerItem linkerItem) { obj.FindPropertyRelative(nameof(linkerItem.Receiver)).stringValue = linkerItem.Receiver; obj.FindPropertyRelative(nameof(linkerItem.ReceiverType)).stringValue = linkerItem.ReceiverType; obj.FindPropertyRelative(nameof(linkerItem.Path)).stringValue = linkerItem.Path; }