Esempio n. 1
0
        protected virtual void DrawTypeSelector(Rect rect)
        {
            var t = GetFieldType(true);

            if (t.IsValueType == false)
            {
                var r = rect;
                r.width -= EditorGUIUtility.labelWidth + 15;
                r.x     += EditorGUIUtility.labelWidth;
                r.x     += 15; // Indentation

                if (required && value == null)
                {
                    GUI.color = Color.red;
                }

                GUI.Button(r, new GUIContent(value == null ? "(empty)" : GetFieldTypeName(false)), "MiniPopup");
                if (r.Contains(Event.current.mousePosition) && Event.current.type == EventType.Used)
                {
                    var implementableTypes = ReflectionDrawerUtility.GetDerivedTypesFrom(GetFieldType(true), onlyDerivedTypesAttribute != null ? onlyDerivedTypesAttribute.type : null);

                    var n = new GenericMenu();
                    for (int i = 0; i < implementableTypes.types.Length; i++)
                    {
                        int index = i;
                        n.AddItem(implementableTypes.content[i], false, (obj) =>
                        {
                            ChangeImplementation(implementableTypes.types[index]);
                            Update();
                        }, implementableTypes.types[i]);
                    }

                    n.ShowAsContext();
                    Event.current.Use();
                }

                GUI.color = Color.white;
            }
        }
Esempio n. 2
0
        private void DrawArrayAddButton(Rect rect)
        {
            rect.height = EditorGUIUtility.singleLineHeight;
            if (GUI.Button(rect, "Add type", "DropDownButton"))
            {
                var derivedTypeInfo = ReflectionDrawerUtility.GetDerivedTypesFrom(fieldInfo.FieldType.GetElementType(), onlyDerivedTypesAttribute != null ? onlyDerivedTypesAttribute.type : null);

                var arrFieldType = fieldInfo.FieldType.GetElementType();
                if (onlyDerivedTypesAttribute != null)
                {
                    arrFieldType = onlyDerivedTypesAttribute.type;
                }

                rect.width -= 25;
                if (rect.Contains(Event.current.mousePosition) && arrFieldType.IsAbstract == false && arrFieldType.IsInterface == false && ReflectionDrawerUtility.IsGenericTypeWithoutGenericArguments(arrFieldType) == false)
                {
                    // Clicked left side
                    AddElement(arrFieldType);
                    Update();
                }
                else
                {
                    // Clicked right side (pick implementation)
                    var n = new GenericMenu();
                    for (int i = 0; i < derivedTypeInfo.types.Length; i++)
                    {
                        n.AddItem(derivedTypeInfo.content[i], false, (obj) =>
                        {
                            AddElement((Type)obj);
                            Update();
                        }, derivedTypeInfo.types[i]);
                    }

                    n.ShowAsContext();
                    Event.current.Use();
                }
            }
        }