|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.EventSystems; |
| 4 | + |
| 5 | +namespace UnityEditor.EventSystems |
| 6 | +{ |
| 7 | + [CustomEditor(typeof(EventTrigger), true)] |
| 8 | + public class EventTriggerEditor : Editor |
| 9 | + { |
| 10 | + private SerializedProperty m_DelegatesProperty; |
| 11 | + |
| 12 | + private GUIContent m_IconToolbarMinus; |
| 13 | + |
| 14 | + private GUIContent m_EventIDName; |
| 15 | + |
| 16 | + private GUIContent[] m_EventTypes; |
| 17 | + |
| 18 | + private GUIContent m_AddButonContent; |
| 19 | + |
| 20 | + protected virtual void OnEnable() |
| 21 | + { |
| 22 | + this.m_DelegatesProperty = base.serializedObject.FindProperty("m_Delegates"); |
| 23 | + this.m_AddButonContent = new GUIContent("Add New Event Type"); |
| 24 | + this.m_EventIDName = new GUIContent(""); |
| 25 | + this.m_IconToolbarMinus = new GUIContent(EditorGUIUtility.IconContent("Toolbar Minus")); |
| 26 | + this.m_IconToolbarMinus.tooltip = "Remove all events in this list."; |
| 27 | + string[] names = Enum.GetNames(typeof(EventTriggerType)); |
| 28 | + this.m_EventTypes = new GUIContent[names.Length]; |
| 29 | + for (int i = 0; i < names.Length; i++) |
| 30 | + { |
| 31 | + this.m_EventTypes[i] = new GUIContent(names[i]); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public override void OnInspectorGUI() |
| 36 | + { |
| 37 | + base.serializedObject.Update(); |
| 38 | + int num = -1; |
| 39 | + EditorGUILayout.Space(); |
| 40 | + Vector2 vector = GUIStyle.none.CalcSize(this.m_IconToolbarMinus); |
| 41 | + for (int i = 0; i < this.m_DelegatesProperty.arraySize; i++) |
| 42 | + { |
| 43 | + SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex(i); |
| 44 | + SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("eventID"); |
| 45 | + SerializedProperty property = arrayElementAtIndex.FindPropertyRelative("callback"); |
| 46 | + this.m_EventIDName.text = serializedProperty.enumDisplayNames[serializedProperty.enumValueIndex]; |
| 47 | + EditorGUILayout.PropertyField(property, this.m_EventIDName, new GUILayoutOption[0]); |
| 48 | + Rect lastRect = GUILayoutUtility.GetLastRect(); |
| 49 | + Rect position = new Rect(lastRect.xMax - vector.x - 8f, lastRect.y + 1f, vector.x, vector.y); |
| 50 | + if (GUI.Button(position, this.m_IconToolbarMinus, GUIStyle.none)) |
| 51 | + { |
| 52 | + num = i; |
| 53 | + } |
| 54 | + EditorGUILayout.Space(); |
| 55 | + } |
| 56 | + if (num > -1) |
| 57 | + { |
| 58 | + this.RemoveEntry(num); |
| 59 | + } |
| 60 | + Rect rect = GUILayoutUtility.GetRect(this.m_AddButonContent, GUI.skin.button); |
| 61 | + rect.x += (rect.width - 200f) / 2f; |
| 62 | + rect.width = 200f; |
| 63 | + if (GUI.Button(rect, this.m_AddButonContent)) |
| 64 | + { |
| 65 | + this.ShowAddTriggermenu(); |
| 66 | + } |
| 67 | + base.serializedObject.ApplyModifiedProperties(); |
| 68 | + } |
| 69 | + |
| 70 | + private void RemoveEntry(int toBeRemovedEntry) |
| 71 | + { |
| 72 | + this.m_DelegatesProperty.DeleteArrayElementAtIndex(toBeRemovedEntry); |
| 73 | + } |
| 74 | + |
| 75 | + private void ShowAddTriggermenu() |
| 76 | + { |
| 77 | + GenericMenu genericMenu = new GenericMenu(); |
| 78 | + for (int i = 0; i < this.m_EventTypes.Length; i++) |
| 79 | + { |
| 80 | + bool flag = true; |
| 81 | + for (int j = 0; j < this.m_DelegatesProperty.arraySize; j++) |
| 82 | + { |
| 83 | + SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex(j); |
| 84 | + SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("eventID"); |
| 85 | + if (serializedProperty.enumValueIndex == i) |
| 86 | + { |
| 87 | + flag = false; |
| 88 | + } |
| 89 | + } |
| 90 | + if (flag) |
| 91 | + { |
| 92 | + genericMenu.AddItem(this.m_EventTypes[i], false, new GenericMenu.MenuFunction2(this.OnAddNewSelected), i); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + genericMenu.AddDisabledItem(this.m_EventTypes[i]); |
| 97 | + } |
| 98 | + } |
| 99 | + genericMenu.ShowAsContext(); |
| 100 | + Event.current.Use(); |
| 101 | + } |
| 102 | + |
| 103 | + private void OnAddNewSelected(object index) |
| 104 | + { |
| 105 | + int enumValueIndex = (int)index; |
| 106 | + this.m_DelegatesProperty.arraySize++; |
| 107 | + SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex(this.m_DelegatesProperty.arraySize - 1); |
| 108 | + SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("eventID"); |
| 109 | + serializedProperty.enumValueIndex = enumValueIndex; |
| 110 | + base.serializedObject.ApplyModifiedProperties(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments