File size: 4,804 Bytes
18a519f | 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 | using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UnityEditor.EventSystems
{
[CustomEditor(typeof(EventTrigger), true)]
public class EventTriggerEditor : Editor
{
SerializedProperty m_DelegatesProperty;
GUIContent m_IconToolbarMinus;
GUIContent m_EventIDName;
GUIContent[] m_EventTypes;
GUIContent m_AddButonContent;
protected virtual void OnEnable()
{
m_DelegatesProperty = serializedObject.FindProperty("m_Delegates");
m_AddButonContent = EditorGUIUtility.TrTextContent("Add New Event Type");
m_EventIDName = new GUIContent("");
// Have to create a copy since otherwise the tooltip will be overwritten.
m_IconToolbarMinus = new GUIContent(EditorGUIUtility.IconContent("Toolbar Minus"));
m_IconToolbarMinus.tooltip = "Remove all events in this list.";
string[] eventNames = Enum.GetNames(typeof(EventTriggerType));
m_EventTypes = new GUIContent[eventNames.Length];
for (int i = 0; i < eventNames.Length; ++i)
{
m_EventTypes[i] = new GUIContent(eventNames[i]);
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
int toBeRemovedEntry = -1;
EditorGUILayout.Space();
Vector2 removeButtonSize = GUIStyle.none.CalcSize(m_IconToolbarMinus);
for (int i = 0; i < m_DelegatesProperty.arraySize; ++i)
{
SerializedProperty delegateProperty = m_DelegatesProperty.GetArrayElementAtIndex(i);
SerializedProperty eventProperty = delegateProperty.FindPropertyRelative("eventID");
SerializedProperty callbacksProperty = delegateProperty.FindPropertyRelative("callback");
m_EventIDName.text = eventProperty.enumDisplayNames[eventProperty.enumValueIndex];
EditorGUILayout.PropertyField(callbacksProperty, m_EventIDName);
Rect callbackRect = GUILayoutUtility.GetLastRect();
Rect removeButtonPos = new Rect(callbackRect.xMax - removeButtonSize.x - 8, callbackRect.y + 1, removeButtonSize.x, removeButtonSize.y);
if (GUI.Button(removeButtonPos, m_IconToolbarMinus, GUIStyle.none))
{
toBeRemovedEntry = i;
}
EditorGUILayout.Space();
}
if (toBeRemovedEntry > -1)
{
RemoveEntry(toBeRemovedEntry);
}
Rect btPosition = GUILayoutUtility.GetRect(m_AddButonContent, GUI.skin.button);
const float addButonWidth = 200f;
btPosition.x = btPosition.x + (btPosition.width - addButonWidth) / 2;
btPosition.width = addButonWidth;
if (GUI.Button(btPosition, m_AddButonContent))
{
ShowAddTriggermenu();
}
serializedObject.ApplyModifiedProperties();
}
private void RemoveEntry(int toBeRemovedEntry)
{
m_DelegatesProperty.DeleteArrayElementAtIndex(toBeRemovedEntry);
}
void ShowAddTriggermenu()
{
// Now create the menu, add items and show it
GenericMenu menu = new GenericMenu();
for (int i = 0; i < m_EventTypes.Length; ++i)
{
bool active = true;
// Check if we already have a Entry for the current eventType, if so, disable it
for (int p = 0; p < m_DelegatesProperty.arraySize; ++p)
{
SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(p);
SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
if (eventProperty.enumValueIndex == i)
{
active = false;
}
}
if (active)
menu.AddItem(m_EventTypes[i], false, OnAddNewSelected, i);
else
menu.AddDisabledItem(m_EventTypes[i]);
}
menu.ShowAsContext();
Event.current.Use();
}
private void OnAddNewSelected(object index)
{
int selected = (int)index;
m_DelegatesProperty.arraySize += 1;
SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(m_DelegatesProperty.arraySize - 1);
SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
eventProperty.enumValueIndex = selected;
serializedObject.ApplyModifiedProperties();
}
}
}
|