Esempio n. 1
0
        private static void DisplayCustomMenu(Rect position, GUIContent label,
                                              System.Type baseType, System.Type selectedType,
                                              bool allowAbstractTypes           = false, bool allowInterfaces       = false,
                                              System.Type defaultType           = null, System.Type[] excludedTypes = null,
                                              TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            if (_window != null)
            {
                _window.Close();
            }

            var pos = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));

            position.x = pos.x;
            position.y = pos.y;

            _window                    = EditorWindow.CreateInstance <TypeSelectionDropDownWindow>();
            _window.BaseType           = baseType;
            _window.AllowAbstractTypes = allowAbstractTypes;
            _window.AllowInterface     = allowInterfaces;
            _window.DefaultType        = defaultType;
            _window.ExcludedTypes      = excludedTypes;
            _window.ListingStyle       = listType;

            _window.ShowAsDropDown(position, new Vector2(position.width, 320f));
            _window.Focus();
        }
Esempio n. 2
0
        public static System.Type TypeDropDown(GUIContent label,
                                               System.Type baseType, System.Type selectedType,
                                               bool allowAbstractTypes           = false, bool allowInterfaces       = false,
                                               System.Type defaultType           = null, System.Type[] excludedTypes = null,
                                               TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace,
                                               System.Predicate <System.Type> searchPredicate = null)
        {
            var position = EditorGUILayout.GetControlRect(true);

            return(SPEditorGUI.TypeDropDown(position, label, baseType, selectedType, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType, searchPredicate));
        }
Esempio n. 3
0
        public static void ShowAndCallbackOnSelect(Rect positionUnder, System.Type baseType,
                                                   System.Action <System.Type> callback,
                                                   bool allowAbstractTypes           = false, bool allowInterfaces       = false,
                                                   System.Type defaultType           = null, System.Type[] excludedTypes = null,
                                                   TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            int controlID = GUIUtility.GetControlID(TypeSelectionDropDownWindow.s_TypeCustomHash, EditorGUIUtility.native, positionUnder);

            CallbackInfo.instance = new CallbackInfo(controlID, null, callback);
            TypeSelectionDropDownWindow.DisplayCustomMenu(positionUnder, GUIContent.none, baseType, null, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType);
        }
Esempio n. 4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);


            var  baseType           = typeof(object);
            bool allowAbstractTypes = false;
            bool allowInterfaces    = false;

            System.Type              defaultType   = null;
            System.Type[]            excludedTypes = null;
            TypeDropDownListingStyle style         = TypeDropDownListingStyle.Namespace;

            System.Predicate <System.Type> searchPredicate = null;

            if (_isManuallyConfigured)
            {
                baseType           = _inheritsFromType ?? typeof(object);
                allowAbstractTypes = _allowAbstractTypes;
                allowInterfaces    = _allowInterfaces;
                defaultType        = _defaultType;
                excludedTypes      = _excludedTypes;
                style           = _dropDownStyle;
                searchPredicate = _searchPredicate;
            }
            else if (this.fieldInfo != null)
            {
                var attrib = this.fieldInfo.GetCustomAttributes(typeof(TypeReference.ConfigAttribute), true).FirstOrDefault() as TypeReference.ConfigAttribute;
                if (attrib != null)
                {
                    baseType           = attrib.inheritsFromType;
                    allowAbstractTypes = attrib.allowAbstractClasses;
                    allowInterfaces    = attrib.allowInterfaces;
                    defaultType        = attrib.defaultType;
                    excludedTypes      = attrib.excludedTypes;
                    style           = attrib.dropDownStyle;
                    searchPredicate = null;
                }
            }

            var tp = GetTypeFromTypeReference(property);

            EditorGUI.BeginChangeCheck();
            tp = SPEditorGUI.TypeDropDown(position, label, baseType, tp, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, style, searchPredicate);
            if (EditorGUI.EndChangeCheck())
            {
                SetTypeToTypeReference(property, tp);
            }

            EditorGUI.EndProperty();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            var  attrib             = this.fieldInfo.GetCustomAttributes(typeof(TypeReference.ConfigAttribute), true).FirstOrDefault() as TypeReference.ConfigAttribute;
            var  baseType           = typeof(Object);
            bool allowAbstractTypes = false;
            bool allowInterfaces    = false;

            System.Type              defaultType   = null;
            System.Type[]            excludedTypes = null;
            TypeDropDownListingStyle style         = TypeDropDownListingStyle.Namespace;

            if (attrib != null)
            {
                baseType           = attrib.inheritsFromType;
                allowAbstractTypes = attrib.allowAbstractClasses;
                allowInterfaces    = attrib.allowInterfaces;
                defaultType        = attrib.defaultType;
                excludedTypes      = attrib.excludedTypes;
                style = attrib.dropDownStyle;
            }



            //var tpref = EditorHelper.GetTargetObjectOfProperty(property) as TypeReference;
            //if(tpref == null)
            //{
            //    tpref = new TypeReference();
            //    EditorHelper.SetTargetObjectOfProperty(property, tpref);
            //    property.serializedObject.ApplyModifiedProperties();
            //}

            //EditorGUI.BeginChangeCheck();
            //tpref.Type = SPEditorGUI.TypeDropDown(position, label, baseType, tpref.Type, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, style);
            //if (EditorGUI.EndChangeCheck())
            //    property.serializedObject.Update();



            var tp = GetTypeFromTypeReference(property);

            EditorGUI.BeginChangeCheck();
            tp = SPEditorGUI.TypeDropDown(position, label, baseType, tp, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, style);
            if (EditorGUI.EndChangeCheck())
            {
                SetTypeToTypeReference(property, tp);
            }

            EditorGUI.EndProperty();
        }
        public static System.Type Popup(Rect position, GUIContent label,
                                        System.Type baseType, System.Type selectedType,
                                        bool allowAbstractTypes           = false, bool allowInterfaces       = false,
                                        System.Type defaultType           = null, System.Type[] excludedTypes = null,
                                        TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace,
                                        System.Predicate <System.Type> searchPredicate = null)
        {
            int controlID = GUIUtility.GetControlID(TypeSelectionDropDownWindow.s_TypePopupHash, EditorGUIUtility.native, position);

            position = EditorGUI.PrefixLabel(position, controlID, label);

            selectedType = CallbackInfo.GetSelectedValueForControl(controlID, selectedType);

            var content = GetTypeLabel(selectedType);
            var current = Event.current;
            var type    = current.type;

            switch (type)
            {
            case EventType.KeyDown:
            {
                //TODO?
                //EditorStyles.popup.Draw(position, content, controlID);
            }
            break;

            case EventType.Repaint:
            {
                EditorStyles.popup.Draw(position, content, controlID);
            }
            break;

            case EventType.MouseDown:
            {
                if (current.button == 0 && position.Contains(current.mousePosition))
                {
                    CallbackInfo.instance = new CallbackInfo(controlID, selectedType);
                    TypeSelectionDropDownWindow.DisplayCustomMenu(position, label, baseType, selectedType, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType, searchPredicate);
                    GUIUtility.keyboardControl = controlID;
                    current.Use();
                }
            }
            break;
            }

            return(selectedType);
        }
        public static System.Type TypeDropDown(Rect position, GUIContent label,
                                               System.Type baseType, System.Type selectedType,
                                               bool allowAbstractTypes           = false, bool allowInterfaces       = false,
                                               System.Type defaultType           = null, System.Type[] excludedTypes = null,
                                               TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            /*
             *
             * if (!TypeUtil.IsType(selectedType, baseType)) selectedType = null;
             *
             * //var knownTypes = (from ass in System.AppDomain.CurrentDomain.GetAssemblies()
             * //                  from tp in ass.GetTypes()
             * //                  where TypeUtil.IsType(tp, baseType) && (allowAbstractTypes || !tp.IsAbstract) && (allowInterfaces || !tp.IsInterface)
             * //                  orderby tp.FullName.Substring(tp.FullName.LastIndexOf(".") + 1) ascending
             * //                  select tp).ToArray();
             * var knownTypes = (from tp in TypeUtil.GetTypesAssignableFrom(baseType)
             *                where (allowAbstractTypes || !tp.IsAbstract) && (allowInterfaces || !tp.IsInterface) && (excludedTypes == null || System.Array.IndexOf(excludedTypes, tp) < 0)
             *                orderby tp.FullName.Substring(tp.FullName.LastIndexOf(".") + 1) ascending
             *                select tp).ToArray();
             * GUIContent[] knownTypeNames = null;
             * switch (listType)
             * {
             *  case TypeDropDownListingStyle.Namespace:
             *      knownTypeNames = knownTypes.Select((tp) =>
             *      {
             *          return new GUIContent(tp.FullName.Replace(".", "/"));
             *      }).ToArray();
             *      break;
             *  case TypeDropDownListingStyle.Flat:
             *      knownTypeNames = (from tp in knownTypes select new GUIContent(tp.Name)).ToArray();
             *      break;
             *  case TypeDropDownListingStyle.ComponentMenu:
             *      knownTypeNames = knownTypes.Select((tp) =>
             *      {
             *          var menuAttrib = tp.GetCustomAttributes(typeof(AddComponentMenu), false).FirstOrDefault() as AddComponentMenu;
             *          if (menuAttrib != null && !string.IsNullOrEmpty(menuAttrib.componentMenu))
             *          {
             *              return new GUIContent(menuAttrib.componentMenu);
             *          }
             *          else if (tp.FullName == tp.Name)
             *          {
             *              return new GUIContent("Scripts/" + tp.Name);
             *          }
             *          else
             *          {
             *              if (tp.FullName.StartsWith("UnityEngine."))
             *              {
             *                  return new GUIContent(tp.FullName.Replace(".", "/"));
             *              }
             *              else
             *              {
             *                  return new GUIContent("Scripts/" + tp.FullName.Replace(".", "/"));
             *              }
             *          }
             *      }).ToArray();
             *      break;
             *  default:
             *      knownTypeNames = new GUIContent[0];
             *      break;
             * }
             *
             * if (defaultType == null)
             * {
             *  knownTypes = knownTypes.Prepend(null).ToArray();
             *  knownTypeNames = knownTypeNames.Prepend(new GUIContent("Nothing")).ToArray();
             * }
             *
             * int index = knownTypes.IndexOf(selectedType);
             * index = EditorGUI.Popup(position, label, index, knownTypeNames);
             * return (index >= 0) ? knownTypes[index] : defaultType;
             *
             */

            return(TypeSelectionDropDownWindow.Popup(position, label, baseType, selectedType, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType));
        }
        public static System.Type TypeDropDown(Rect position, GUIContent label, System.Type baseType, System.Type selectedType, bool allowAbstractTypes = false, bool allowInterfaces = false, System.Type defaultType = null, TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            if (!TypeUtil.IsType(selectedType, baseType)) selectedType = null;

            //var knownTypes = (from ass in System.AppDomain.CurrentDomain.GetAssemblies()
            //                  from tp in ass.GetTypes()
            //                  where TypeUtil.IsType(tp, baseType) && (allowAbstractTypes || !tp.IsAbstract) && (allowInterfaces || !tp.IsInterface)
            //                  orderby tp.FullName.Substring(tp.FullName.LastIndexOf(".") + 1) ascending
            //                  select tp).ToArray();
            var knownTypes = (from tp in TypeUtil.GetTypesAssignableFrom(baseType)
                              where (allowAbstractTypes || !tp.IsAbstract) && (allowInterfaces || !tp.IsInterface)
                              orderby tp.FullName.Substring(tp.FullName.LastIndexOf(".") + 1) ascending
                              select tp).ToArray();
            GUIContent[] knownTypeNames = null;
            switch (listType)
            {
                case TypeDropDownListingStyle.Namespace:
                    knownTypeNames = knownTypes.Select((tp) =>
                    {
                        return new GUIContent(tp.FullName.Replace(".", "/"));
                    }).ToArray();
                    break;
                case TypeDropDownListingStyle.Flat:
                    knownTypeNames = (from tp in knownTypes select new GUIContent(tp.Name)).ToArray();
                    break;
                case TypeDropDownListingStyle.ComponentMenu:
                    knownTypeNames = knownTypes.Select((tp) =>
                    {
                        var menuAttrib = tp.GetCustomAttributes(typeof(AddComponentMenu), false).FirstOrDefault() as AddComponentMenu;
                        if (menuAttrib != null && !string.IsNullOrEmpty(menuAttrib.componentMenu))
                        {
                            return new GUIContent(menuAttrib.componentMenu);
                        }
                        else if (tp.FullName == tp.Name)
                        {
                            return new GUIContent("Scripts/" + tp.Name);
                        }
                        else
                        {
                            if (tp.FullName.StartsWith("UnityEngine."))
                            {
                                return new GUIContent(tp.FullName.Replace(".", "/"));
                            }
                            else
                            {
                                return new GUIContent("Scripts/" + tp.FullName.Replace(".", "/"));
                            }
                        }
                    }).ToArray();
                    break;
                default:
                    knownTypeNames = new GUIContent[0];
                    break;
            }

            if (defaultType == null)
            {
                knownTypes = knownTypes.Prepend(null).ToArray();
                knownTypeNames = knownTypeNames.Prepend(new GUIContent("Nothing")).ToArray();
            }

            int index = knownTypes.IndexOf(selectedType);
            index = EditorGUI.Popup(position, label, index, knownTypeNames);
            return (index >= 0) ? knownTypes[index] : defaultType;
        }
        private static void DisplayCustomMenu(Rect position, GUIContent label,
                                               System.Type baseType, System.Type selectedType,
                                               bool allowAbstractTypes = false, bool allowInterfaces = false,
                                               System.Type defaultType = null, System.Type[] excludedTypes = null,
                                               TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            if (_window != null)
            {
                _window.Close();
            }

            var pos = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
            position.x = pos.x;
            position.y = pos.y;

            _window = EditorWindow.CreateInstance<TypeSelectionDropDownWindow>();
            _window.BaseType = baseType;
            _window.AllowAbstractTypes = allowAbstractTypes;
            _window.AllowInterface = allowInterfaces;
            _window.DefaultType = defaultType;
            _window.ExcludedTypes = excludedTypes;
            _window.ListingStyle = listType;

            _window.ShowAsDropDown(position, new Vector2(position.width, 320f));
            _window.Focus();
        }
 public static void ShowAndCallbackOnSelect(Rect positionUnder, System.Type baseType,
                                    System.Action<System.Type> callback,
                                    bool allowAbstractTypes = false, bool allowInterfaces = false,
                                 System.Type defaultType = null, System.Type[] excludedTypes = null,
                                 TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
 {
     int controlID = GUIUtility.GetControlID(TypeSelectionDropDownWindow.s_TypeCustomHash, EditorGUIUtility.native, positionUnder);
     CallbackInfo.instance = new CallbackInfo(controlID, null, callback);
     TypeSelectionDropDownWindow.DisplayCustomMenu(positionUnder, GUIContent.none, baseType, null, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType);
 }
        public static System.Type Popup(Rect position, GUIContent label,
                                               System.Type baseType, System.Type selectedType,
                                               bool allowAbstractTypes = false, bool allowInterfaces = false,
                                               System.Type defaultType = null, System.Type[] excludedTypes = null,
                                               TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
        {
            int controlID = GUIUtility.GetControlID(TypeSelectionDropDownWindow.s_TypePopupHash, EditorGUIUtility.native, position);
            position = EditorGUI.PrefixLabel(position, controlID, label);

            selectedType = CallbackInfo.GetSelectedValueForControl(controlID, selectedType);

            var content = GetTypeLabel(selectedType);
            var current = Event.current;
            var type = current.type;
            switch(type)
            {
                case EventType.KeyDown:
                    {
                        //TODO?
                        //EditorStyles.popup.Draw(position, content, controlID);
                    }
                    break;
                case EventType.Repaint:
                    {
                        EditorStyles.popup.Draw(position, content, controlID);
                    }
                    break;
                case EventType.MouseDown:
                    {
                        if(current.button == 0 && position.Contains(current.mousePosition))
                        {
                            CallbackInfo.instance = new CallbackInfo(controlID, selectedType);
                            TypeSelectionDropDownWindow.DisplayCustomMenu(position, label, baseType, selectedType, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType);
                            GUIUtility.keyboardControl = controlID;
                            current.Use();
                        }
                    }
                    break;
            }

            return selectedType;
        }
 public static System.Type TypeDropDown(GUIContent label, System.Type baseType, System.Type selectedType, bool allowAbstractTypes = false, bool allowInterfaces = false, System.Type defaultType = null, System.Type[] excludedTypes = null, TypeDropDownListingStyle listType = TypeDropDownListingStyle.Namespace)
 {
     var position = EditorGUILayout.GetControlRect(true);
     return SPEditorGUI.TypeDropDown(position, label, baseType, selectedType, allowAbstractTypes, allowInterfaces, defaultType, excludedTypes, listType);
 }