File size: 10,900 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 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 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.EventSystems;
namespace UnityEditor.Events
{
[CustomPreview(typeof(GameObject))]
/// <summary>
/// Custom preview drawing that will draw the intercepted events of a given object.
/// </summary>
class InterceptedEventsPreview : ObjectPreview
{
protected class ComponentInterceptedEvents
{
public GUIContent componentName;
public int[] interceptedEvents;
}
class Styles
{
public GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
public GUIStyle componentName = new GUIStyle(EditorStyles.boldLabel);
public Styles()
{
Color fontColor = new Color(0.7f, 0.7f, 0.7f);
labelStyle.padding.right += 20;
labelStyle.normal.textColor = fontColor;
labelStyle.active.textColor = fontColor;
labelStyle.focused.textColor = fontColor;
labelStyle.hover.textColor = fontColor;
labelStyle.onNormal.textColor = fontColor;
labelStyle.onActive.textColor = fontColor;
labelStyle.onFocused.textColor = fontColor;
labelStyle.onHover.textColor = fontColor;
componentName.normal.textColor = fontColor;
componentName.active.textColor = fontColor;
componentName.focused.textColor = fontColor;
componentName.hover.textColor = fontColor;
componentName.onNormal.textColor = fontColor;
componentName.onActive.textColor = fontColor;
componentName.onFocused.textColor = fontColor;
componentName.onHover.textColor = fontColor;
}
}
private Dictionary<GameObject, List<ComponentInterceptedEvents>> m_TargetEvents;
private bool m_InterceptsAnyEvent = false;
private GUIContent m_Title;
private Styles m_Styles = new Styles();
public override void Initialize(UnityEngine.Object[] targets)
{
Profiler.BeginSample("ComponentInterceptedEvents.Initialize");
base.Initialize(targets);
m_TargetEvents = new Dictionary<GameObject, List<ComponentInterceptedEvents>>(targets.Length);
m_InterceptsAnyEvent = false;
for (int i = 0; i < targets.Length; ++i)
{
GameObject go = targets[i] as GameObject;
List<ComponentInterceptedEvents> interceptedEvents = GetEventsInfo(go);
m_TargetEvents.Add(go, interceptedEvents);
if (interceptedEvents.Any())
m_InterceptsAnyEvent = true;
}
Profiler.EndSample();
}
public override GUIContent GetPreviewTitle()
{
if (m_Title == null)
{
m_Title = EditorGUIUtility.TrTextContent("Intercepted Events");
}
return m_Title;
}
public override bool HasPreviewGUI()
{
return m_TargetEvents != null && m_InterceptsAnyEvent;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
if (Event.current.type != EventType.Repaint)
return;
Profiler.BeginSample("InterceptedEventsPreview.OnPreviewGUI");
if (m_Styles == null)
m_Styles = new Styles();
Vector2 maxEventLabelSize = Vector2.zero;
int totalInterceptedEvents = 0;
List<ComponentInterceptedEvents> componentIncerceptedEvents = m_TargetEvents[target as GameObject];
// Find out the maximum size needed for any given label.
foreach (ComponentInterceptedEvents componentInterceptedEvents in componentIncerceptedEvents)
{
foreach (int eventIndex in componentInterceptedEvents.interceptedEvents)
{
GUIContent eventContent = s_PossibleEvents[eventIndex];
++totalInterceptedEvents;
Vector2 labelSize = m_Styles.labelStyle.CalcSize(eventContent);
if (maxEventLabelSize.x < labelSize.x)
{
maxEventLabelSize.x = labelSize.x;
}
if (maxEventLabelSize.y < labelSize.y)
{
maxEventLabelSize.y = labelSize.y;
}
}
}
// Apply padding
RectOffset previewPadding = new RectOffset(-5, -5, -5, -5);
r = previewPadding.Add(r);
// Figure out how many rows and columns we can/should have
int columns = Mathf.Max(Mathf.FloorToInt(r.width / maxEventLabelSize.x), 1);
int rows = Mathf.Max(totalInterceptedEvents / columns, 1) + componentIncerceptedEvents.Count;
// Centering
float initialX = r.x + Mathf.Max(0, (r.width - (maxEventLabelSize.x * columns)) / 2);
float initialY = r.y + Mathf.Max(0, (r.height - (maxEventLabelSize.y * rows)) / 2);
Rect labelRect = new Rect(initialX, initialY, maxEventLabelSize.x, maxEventLabelSize.y);
int currentColumn = 0;
foreach (ComponentInterceptedEvents componentInterceptedEvents in componentIncerceptedEvents)
{
GUI.Label(labelRect, componentInterceptedEvents.componentName, m_Styles.componentName);
labelRect.y += labelRect.height;
labelRect.x = initialX;
foreach (int eventIndex in componentInterceptedEvents.interceptedEvents)
{
GUIContent eventContent = s_PossibleEvents[eventIndex];
GUI.Label(labelRect, eventContent, m_Styles.labelStyle);
if (currentColumn < columns - 1)
{
labelRect.x += labelRect.width;
}
else
{
labelRect.y += labelRect.height;
labelRect.x = initialX;
}
currentColumn = (currentColumn + 1) % columns;
}
if (labelRect.x != initialX)
{
labelRect.y += labelRect.height;
labelRect.x = initialX;
}
}
Profiler.EndSample();
}
//Lookup cache to avoid recalculating which types uses which events:
//Caches all interfaces that inherit from IEventSystemHandler
static List<Type> s_EventSystemInterfaces = null;
//Caches all GUIContents in a single list to avoid creating too much GUIContent and strings.
private static List<GUIContent> s_PossibleEvents = null;
//Caches all events used by each interface
static Dictionary<Type, List<int>> s_InterfaceEventSystemEvents = null;
//Caches each concrete type and it's events
static readonly Dictionary<Type, ComponentInterceptedEvents> s_ComponentEvents2 = new Dictionary<Type, ComponentInterceptedEvents>();
protected static List<ComponentInterceptedEvents> GetEventsInfo(GameObject gameObject)
{
InitializeEvetnsInterfaceCacheIfNeeded();
List<ComponentInterceptedEvents> componentEvents = new List<ComponentInterceptedEvents>();
MonoBehaviour[] mbs = gameObject.GetComponents<MonoBehaviour>();
for (int i = 0, imax = mbs.Length; i < imax; ++i)
{
ComponentInterceptedEvents componentEvent = null;
MonoBehaviour mb = mbs[i];
if (mb == null)
continue;
Type type = mb.GetType();
if (!s_ComponentEvents2.ContainsKey(type))
{
List<int> events = null;
Profiler.BeginSample("ComponentInterceptedEvents.GetEventsInfo.NewType");
if (typeof(IEventSystemHandler).IsAssignableFrom(type))
{
for (int index = 0; index < s_EventSystemInterfaces.Count; index++)
{
var eventInterface = s_EventSystemInterfaces[index];
if (!eventInterface.IsAssignableFrom(type))
continue;
if (events == null)
events = new List<int>();
events.AddRange(s_InterfaceEventSystemEvents[eventInterface]);
}
}
if (events != null)
{
componentEvent = new ComponentInterceptedEvents();
componentEvent.componentName = new GUIContent(type.Name);
componentEvent.interceptedEvents = events.OrderBy(index => s_PossibleEvents[index].text).ToArray();
}
s_ComponentEvents2.Add(type, componentEvent);
Profiler.EndSample();
}
else
{
componentEvent = s_ComponentEvents2[type];
}
if (componentEvent != null)
{
componentEvents.Add(componentEvent);
}
}
return componentEvents;
}
private static void InitializeEvetnsInterfaceCacheIfNeeded()
{
if (s_EventSystemInterfaces != null)
return;
s_EventSystemInterfaces = new List<Type>();
s_PossibleEvents = new List<GUIContent>();
s_InterfaceEventSystemEvents = new Dictionary<Type, List<int>>();
TypeCache.TypeCollection types = TypeCache.GetTypesDerivedFrom<IEventSystemHandler>();
foreach (var type in types)
{
if (!type.IsInterface)
continue;
s_EventSystemInterfaces.Add(type);
List<int> eventIndexList = new List<int>();
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
for (int mi = 0; mi < methodInfos.Length; mi++)
{
MethodInfo methodInfo = methodInfos[mi];
eventIndexList.Add(s_PossibleEvents.Count);
s_PossibleEvents.Add(new GUIContent(methodInfo.Name));
}
s_InterfaceEventSystemEvents.Add(type, eventIndexList);
}
}
}
}
|