예제 #1
0
        private static void FilterTypes(Assembly assembly, ClassTypeConstraintAttribute filter, List <Type> output)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (!type.IsVisible)
                {
                    continue;
                }

                if (filter != null && !filter.IsConstraintSatisfied(type))
                {
                    continue;
                }

                output.Add(type);
            }
        }
예제 #2
0
        private static List <Type> GetFilteredTypes(ClassTypeConstraintAttribute filter)
        {
            var types = new List <Type>();

            var assembly = Assembly.GetExecutingAssembly();

            FilterTypes(assembly, filter, types);

            foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
            {
                FilterTypes(Assembly.Load(referencedAssembly), filter, types);
            }

            types.Sort((a, b) => a.FullName.CompareTo(b.FullName));

            return(types);
        }
예제 #3
0
        private static void DrawTypeSelectionControl(Rect position, SerializedProperty property, GUIContent label, ClassTypeConstraintAttribute filter)
        {
            try
            {
                bool restoreShowMixedValue = EditorGUI.showMixedValue;
                EditorGUI.showMixedValue = property.hasMultipleDifferentValues;

                property.stringValue = DrawTypeSelectionControl(position, label, property.stringValue, filter);

                EditorGUI.showMixedValue = restoreShowMixedValue;
            }
            finally
            {
                ExcludedTypeCollectionGetter = null;
            }
        }
예제 #4
0
        private static void DisplayDropDown(Rect position, List <Type> types, Type selectedType, ClassTypeConstraintAttribute filter)
        {
            var menu = new GenericMenu();

            if (filter == null || filter.ShowNoneOption)
            {
                menu.AddItem(new GUIContent("(None)"), selectedType == null, s_OnSelectedTypeName, null);
                menu.AddSeparator("");
            }

            if (filter != null)
            {
                foreach (KeyValuePair <Type, string> customDisplay in filter.CustomDisplays)
                {
                    menu.AddItem(new GUIContent(customDisplay.Value), selectedType == customDisplay.Key,
                                 s_OnSelectedTypeName, customDisplay.Key);
                }
            }

            ClassGrouping grouping = ClassGrouping.ByNamespace;

            if (filter != null)
            {
                grouping = filter.Grouping;
            }

            for (int i = 0; i < types.Count; ++i)
            {
                var type = types[i];

                var customLogic = filter == null ? null : filter.CustomGroupingLogic;

                string menuLabel = FormatGroupedTypeName(type, grouping, customLogic);

                if (string.IsNullOrEmpty(menuLabel))
                {
                    continue;
                }

                var content = new GUIContent(menuLabel);
                menu.AddItem(content, type == selectedType, s_OnSelectedTypeName, type);
            }

            menu.DropDown(position);
        }
예제 #5
0
        public static string DrawTypeSelectionControl(Rect position, GUIContent label, string classRef, ClassTypeConstraintAttribute filter)
        {
            if (label != null && label != GUIContent.none)
            {
                position = EditorGUI.PrefixLabel(position, label);
            }

            int controlID = GUIUtility.GetControlID(s_ControlHint, FocusType.Keyboard, position);

            bool triggerDropDown = false;

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.ExecuteCommand:
                if (Event.current.commandName == "TypeReferenceUpdated")
                {
                    if (s_SelectionControlID == controlID)
                    {
                        if (classRef != s_SelectedClassRef)
                        {
                            classRef    = s_SelectedClassRef;
                            GUI.changed = true;
                        }

                        s_SelectionControlID = 0;
                        s_SelectedClassRef   = null;
                    }
                }
                break;

            case EventType.MouseDown:
                if (GUI.enabled && position.Contains(Event.current.mousePosition))
                {
                    GUIUtility.keyboardControl = controlID;
                    triggerDropDown            = true;
                    Event.current.Use();
                }
                break;

            case EventType.KeyDown:
                if (GUI.enabled && GUIUtility.keyboardControl == controlID)
                {
                    if (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.Space)
                    {
                        triggerDropDown = true;
                        Event.current.Use();
                    }
                }
                break;

            case EventType.Repaint:
                // Remove assembly name from content of popup control.
                var classRefParts = classRef.Split(',');

                s_TempContent.text = classRefParts[0].Trim();

                if (filter.RemoveNamespaceFromSelected)
                {
                    int lastPeriod = s_TempContent.text.LastIndexOf('.');

                    if (lastPeriod >= 0)
                    {
                        s_TempContent.text = s_TempContent.text.Substring(lastPeriod + 1);
                    }
                }

                Type selectedType = Type.GetType(classRef);

                if (selectedType != null)
                {
                    string customDisplay;

                    if (filter.CustomDisplays.TryGetValue(selectedType, out customDisplay))
                    {
                        s_TempContent.text = customDisplay;
                    }
                }

                if (s_TempContent.text == "")
                {
                    s_TempContent.text = "(None)";
                }
                else if (ResolveType(classRef) == null)
                {
                    s_TempContent.text += " {Missing}";
                }

                EditorStyles.popup.Draw(position, s_TempContent, controlID);
                break;
            }

            if (triggerDropDown)
            {
                s_SelectionControlID = controlID;
                s_SelectedClassRef   = classRef;

                var filteredTypes = GetFilteredTypes(filter);
                DisplayDropDown(position, filteredTypes, ResolveType(classRef), filter);
            }

            return(classRef);
        }