Example #1
0
        private float ElementHeightCallback(SerializedProperty property, int index)
        {
            var height   = 3f;
            var iterProp = property.GetArrayElementAtIndex(index);

            if (iterProp.isExpanded || !ElementHeights.ContainsKey(index))
            {
                var displayName = new GUIContent(iterProp.displayName);
                if (ElementNameCallback != null)
                {
                    var elementName = ElementNameCallback(index);
                    displayName = elementName == null ? GUIContent.none : new GUIContent(elementName);
                }

                if (iterProp.IsInspectableObjectData())
                {
                    height += EasyGUI.GetInspectableObjectHeight(iterProp, IsDrawObjectReference);
                }
                else
                {
                    height += EasyGUI.GetPropertyFieldHeight(iterProp, displayName, IsDrawObjectReference);
                }
                if (!iterProp.isExpanded)
                {
                    ElementHeights.Add(index, height);
                }
            }
            else
            {
                height = ElementHeights[index];
            }

            return(height);
        }
        public void AddProperty(SerializedProperty property)
        {
            // Check if this property actually belongs to the same direct child
            if (!property.GetRootPath().Equals(Parent))
            {
                return;
            }

            if (reorderableListDict.ContainsKey(property.propertyPath))
            {
                return;
            }

            var propList = new ReorderableList(
                property.serializedObject, property,
                draggable: true, displayHeader: false,
                displayAddButton: true, displayRemoveButton: true)
            {
                headerHeight = 5
            };

            propList.drawElementBackgroundCallback = (Rect position, int index, bool active, bool focused) =>
            {
                if (DrawBackgroundCallback != null)
                {
                    Rect backgroundRect = new Rect(position);
                    if (index <= 0)
                    {
                        backgroundRect.yMin -= 8;
                    }
                    if (index >= propList.count - 1)
                    {
                        backgroundRect.yMax += 3;
                    }
                    EditorGUI.DrawRect(backgroundRect, DrawBackgroundCallback(active, focused));
                }
                else
                {
                    propList.drawElementBackgroundCallback = null;
                }
            };

            propList.drawElementCallback = (Rect position, int index, bool active, bool focused) =>
            {
                var iterProp    = property.GetArrayElementAtIndex(index);
                var elementName = iterProp.displayName;
                if (ElementNameCallback != null)
                {
                    elementName = ElementNameCallback(index);
                }

                position.xMin  += 5;
                position.height = EditorGUIUtility.singleLineHeight;
                EasyGUI.PropertyField(position, iterProp, new GUIContent(elementName), ElementAttributes);
            };

            propList.elementHeightCallback = index => ElementHeightCallback(property, index);

            reorderableListDict.Add(property.propertyPath, propList);
        }
        public void AddProperty(InspectableProperty property)
        {
            // Check if this property actually belongs to the same direct child
            if (!property.GetRootPath().Equals(Parent))
            {
                return;
            }

            if (extendListIndex.ContainsKey(property.PropertyPath))
            {
                return;
            }

            InspectableReorderableList propList = new InspectableReorderableList(
                property.InspectableObject.SerializedObject, property,
                draggable: true, displayHeader: false,
                displayAddButton: true, displayRemoveButton: true)
            {
                headerHeight = 5
            };

            propList.drawElementBackgroundCallback = delegate(Rect position, int index, bool active, bool focused)
            {
                if (DrawBackgroundCallback != null)
                {
                    Rect backgroundRect = new Rect(position);
                    if (index <= 0)
                    {
                        backgroundRect.yMin -= 8;
                    }
                    if (index >= propList.count - 1)
                    {
                        backgroundRect.yMax += 3;
                    }
                    EditorGUI.DrawRect(backgroundRect, DrawBackgroundCallback(active, focused));
                }
                else
                {
                    propList.drawElementBackgroundCallback = null;
                }
            };

            propList.drawElementCallback = delegate(Rect position, int index, bool active, bool focused)
            {
                var iterProp    = property.GetArrayElementAtIndex(index);
                var displayName = new GUIContent(iterProp.DisplayName);
                if (ElementNameCallback != null)
                {
                    var elementName = ElementNameCallback(index);
                    displayName = elementName == null ? GUIContent.none : new GUIContent(elementName);
                }

                EasyGUI.TryDrawInspectableObject(position, iterProp, displayName, IsDrawObjectReference);
            };

            propList.elementHeightCallback = index => ElementHeightCallback(property, index);

            extendListIndex.Add(property.PropertyPath, propList);
        }
Example #4
0
        private string DoHeader(InspectableProperty property, Rect position, string displayName)
        {
            displayName = string.IsNullOrEmpty(displayName) ? property.DisplayName : displayName;
            string headerName = string.Format(HeaderStr, displayName, property.ArraySize);

            EasyGUI.PropertyField(position, property, new GUIContent(headerName), false);
            return(headerName);
        }
 public static float GetObjectReferenceHeight(SerializedProperty property, GUIContent label)
 {
     if (property != null && property.propertyType == SerializedPropertyType.ObjectReference)
     {
         var headerHeight = EditorGUI.GetPropertyHeight(property, label, false) - EditorGUIUtility.singleLineHeight;
         return(GetObjectReferenceHeight(property.objectReferenceValue, EasyGUI.CreateCachedEditorWithContext <ReorderableListDrawer>(property.objectReferenceValue, property.serializedObject.targetObject)) + headerHeight);
     }
     return(EditorGUI.GetPropertyHeight(property, label, false));
 }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var typePopupProperty = GetTypePopupProperty(property);

            if (typePopupProperty == null)
            {
                EditorGUI.PropertyField(position, property, label, property.isExpanded);
                return;
            }

            List <Type> types;

            string[] displayedOptions;
            int[]    optionValues;
            int      selectedIndex = GetTypePopupFromProperty(typePopupProperty, attribute as TypePopupAttribute, out types, out displayedOptions, out optionValues);

            if (!useSearchDict.ContainsKey(typePopupProperty.propertyPath))
            {
                useSearchDict.Add(typePopupProperty.propertyPath, false);
            }
            useSearchDict[typePopupProperty.propertyPath] = EditorGUI.Foldout(new Rect(position.position, new Vector2(EasyGUI.Indent, EditorGUIUtility.singleLineHeight + 3)), useSearchDict[typePopupProperty.propertyPath], GUIContent.none);
            EditorGUI.BeginChangeCheck();
            var index = EditorGUI.IntPopup(new Rect(position.position, new Vector2(position.width, EditorGUIUtility.singleLineHeight + 3)), typePopupProperty.displayName, selectedIndex, displayedOptions, optionValues);

            if (EditorGUI.EndChangeCheck())
            {
                typePopupProperty.stringValue = index == 0 ? null : types[index].FullName;
            }
            if (useSearchDict[typePopupProperty.propertyPath])
            {
                var height         = EditorGUIUtility.singleLineHeight + 5;
                var searchPosition = new Rect(new Vector2(position.x - EasyGUI.Indent, position.y + height), new Vector2(position.width, height));

                searchCondition = EasyGUI.SearchFieldWithPopupMenu(searchPosition, SearchLabelStr, searchField, searchCondition, popupMenu =>
                {
                    foreach (var type in types.Where(type => type != null && type.FullName.ToLower().Contains(searchCondition.ToLower())))
                    {
                        popupMenu.AddItem(new GUIContent(type.Name), false, () =>
                        {
                            typePopupProperty.stringValue = type.FullName;
                            typePopupProperty.serializedObject.ApplyModifiedProperties();
                            searchCondition = EmptyStr;
                        });
                    }
                }, SearchLabelHtmlString, SearchLabelTooltipStr);
            }

            if (property.type == SerializableEventObjectTypeStr)
            {
                var targetProperty = property.FindPropertyRelative(TargetStr);
                var targetPosition = new Rect(new Vector2(position.x - EasyGUI.Indent, position.y + (useSearchDict[typePopupProperty.propertyPath] ? (2 * EditorGUIUtility.singleLineHeight + 5) : EditorGUIUtility.singleLineHeight) + 5), new Vector2(position.width, EditorGUIUtility.singleLineHeight));
                EditorGUI.ObjectField(targetPosition, targetProperty);
            }
        }
Example #7
0
        public override float GetPropertyHeight(InspectableProperty property, GUIContent label)
        {
            // Try to get the sortable list this property belongs to
            ReorderableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.PropertyPath.StartsWith(data.Parent));
            }

            return(listData != null?listData.GetPropertyHeight(property) : EasyGUI.GetPropertyHeight(property, label, property.IsExpanded));
        }
Example #8
0
        private void DrawPropertySortableArray(Rect position, InspectableProperty property, GUIContent label)
        {
            // Try to get the sortable list this property belongs to
            ReorderableListData listData = null;

            if (listIndex.Count > 0)
            {
                listData = listIndex.Find(data => property.PropertyPath.StartsWith(data.Parent));
            }

            UnityEditor.Editor scriptableEditor;
            bool isScriptableEditor = editableIndex.TryGetValue(property.PropertyPath, out scriptableEditor);

            // Has ReorderableList and Try to show the list
            if (listData != null && listData.DoProperty(position, property))
            {
            }
            // Else try to draw ScriptableObject editor
            else if (isScriptableEditor)
            {
                bool hasHeader = property.HasAttribute <HeaderAttribute>();
                bool hasSpace  = property.HasAttribute <SpaceAttribute>();

                hasSpace |= hasHeader;

                // No data in property, draw property field with create button
                if (scriptableEditor == null)
                {
                    var fieldPosition  = new Rect(position);
                    var buttonPosition = new Rect(position);
                    fieldPosition.width -= hasSpace ? 60 : 50;
                    buttonPosition.xMin  = fieldPosition.xMax;

                    EasyGUI.PropertyField(fieldPosition, property);
                    bool doCreate = GUI.Button(buttonPosition, Create, EditorStyles.miniButton);

                    if (doCreate)
                    {
                        var propType = property.GetTypeReflection();
                        ScriptableObjectUtility.CreateAssetWithSavePrompt(propType, postCreated: createdAsset =>
                        {
                            property.ObjectReference = createdAsset;
                            property.IsExpanded      = true;
                            return(false);
                        });
                    }
                }
            }
            else
            {
                EasyGUI.PropertyField(position, property, label, property.IsExpanded);
            }
        }
 public static bool TryDrawObjectReference(Rect position, SerializedProperty property, GUIContent label)
 {
     if (property != null && property.propertyType == SerializedPropertyType.ObjectReference)
     {
         position.height = EditorGUI.GetPropertyHeight(property, label, false);
         if (TryDrawObjectReference(position, property.objectReferenceValue, EasyGUI.CreateCachedEditorWithContext <ReorderableListDrawer>(property.objectReferenceValue, property.serializedObject.targetObject)))
         {
             EditorGUI.PropertyField(position, property, label, false);
             return(true);
         }
     }
     return(false);
 }
        private float ElementHeightCallback(SerializedProperty property, int index)
        {
            var height      = 3f;
            var iterProp    = property.GetArrayElementAtIndex(index);
            var elementName = iterProp.displayName;

            if (ElementNameCallback != null)
            {
                elementName = ElementNameCallback(index);
            }

            height += EasyGUI.GetPropertyHeight(iterProp, ElementAttributes, new GUIContent(elementName));

            return(height);
        }
Example #11
0
 public static float GetPropertyHeight(System.Type type, GUIContent label)
 {
     if (type == RuntimeSerializedPropertyType.Vector3 || type == RuntimeSerializedPropertyType.Vector2 || type == RuntimeSerializedPropertyType.Vector4)
     {
         return(((EasyGUI.LabelHasContent(label) && !EditorGUIUtility.wideMode) ? kSingleLineHeight : 0f) + kSingleLineHeight);
     }
     if (type == RuntimeSerializedPropertyType.Rect || type == RuntimeSerializedPropertyType.RectInt)
     {
         return(((EasyGUI.LabelHasContent(label) && !EditorGUIUtility.wideMode) ? kSingleLineHeight : 0f) + 2 * kSingleLineHeight);
     }
     if (type == RuntimeSerializedPropertyType.Bounds || type == RuntimeSerializedPropertyType.BoundsInt)
     {
         return((EasyGUI.LabelHasContent(label) ? kSingleLineHeight : 0f) + 2 * kSingleLineHeight);
     }
     return(kSingleLineHeight);
 }
Example #12
0
        protected virtual void EmitPropertyField(Rect position, InspectableProperty targetInspectableProperty, GUIContent label)
        {
            var multiline = GetMultilineAttribute();

            if (multiline == null)
            {
                var range = GetRangeAttribute();
                if (range == null)
                {
                    EasyGUI.PropertyField(position, targetInspectableProperty, label, includeChildren: true);
                }
                else
                {
                    if (targetInspectableProperty.PropertyType == InspectablePropertyType.Float)
                    {
                        EasyGUI.Slider(position, targetInspectableProperty, range.Min, range.Max, label);
                    }
                    else if (targetInspectableProperty.PropertyType == InspectablePropertyType.Integer)
                    {
                        EasyGUI.IntSlider(position, targetInspectableProperty, (int)range.Min, (int)range.Max, label);
                    }
                    else
                    {
                        EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
                    }
                }
            }
            else
            {
                var property = targetInspectableProperty;

                label = EasyGUI.BeginProperty(position, label, property);
                var method = typeof(EditorGUI).GetMethod("MultiFieldPrefixLabel", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic);
                position = (Rect)method.Invoke(null, new object[] { position, 0, label, 1 });

                EditorGUI.BeginChangeCheck();
                int indentLevel = EditorGUI.indentLevel;
                EditorGUI.indentLevel = 0;
                var stringValue = EditorGUI.TextArea(position, property.StringValue);
                EditorGUI.indentLevel = indentLevel;
                if (EditorGUI.EndChangeCheck())
                {
                    property.StringValue = stringValue;
                }
                EasyGUI.EndProperty();
            }
        }
Example #13
0
        public override void OnGUI(Rect position, InspectableProperty property, GUIContent label)
        {
            RangeAttribute rangeAttribute = (RangeAttribute)base.Attribute;

            if (property.PropertyType == InspectablePropertyType.Float)
            {
                EasyGUI.Slider(position, property, rangeAttribute.min, rangeAttribute.max, label);
            }
            else if (property.PropertyType == InspectablePropertyType.Integer)
            {
                EasyGUI.IntSlider(position, property, (int)rangeAttribute.min, (int)rangeAttribute.max, label);
            }
            else
            {
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
            }
        }
        protected float GetPropertyHeight(SerializedProperty property)
        {
            var height   = 0f;
            var listData = GetReorderableListData(property);

            UnityEditor.Editor scriptableEditor;
            var isScriptableEditor = editableDict.TryGetValue(property.propertyPath, out scriptableEditor);

            if (listData != null)
            {
                height += listData.GetPropertyHeight(property);
                if (height == 0f)
                {
                    height += EditorGUI.GetPropertyHeight(property, false);
                    if (property.isExpanded)
                    {
                        height += IterateGetPropertyHeight(property.Copy());
                    }
                }
            }
            else if (isScriptableEditor)
            {
                if (scriptableEditor == null)
                {
                    height += EditorGUI.GetPropertyHeight(property, false);
                }
                else
                {
                    height += EasyGUI.GetPropertyHeight(property, null, null, true);
                }
            }
            else
            {
                var isStartProp = property.propertyPath.StartsWith(M_ScriptStr);
                if (isStartProp && IgnoreHeader)
                {
                }
                else
                {
                    height += EditorGUI.GetPropertyHeight(property, property.isExpanded);
                }
            }
            return(height);
        }
Example #15
0
        public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float height = 0f;

            if (DecoratorDrawers != null && !IsCurrentlyNested)
            {
                foreach (var drawer in DecoratorDrawers)
                {
                    height += drawer.GetHeight();
                }
            }

            if (PropertyDrawer != null)
            {
                height += PropertyDrawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtilityHelper.TempContent(property.displayName));
            }
            else if (!includeChildren)
            {
                height += EasyGUI.GetSinglePropertyHeight(property, label);
            }
            else
            {
                property = property.Copy();

                // First property with custom label
                height += EasyGUI.GetSinglePropertyHeight(property, label);
                bool childrenAreExpanded = property.isExpanded && EasyGUI.HasVisibleChildFields(property);

                // Loop through all child properties
                var tc = EditorGUIUtilityHelper.TempContent(property.displayName);
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = property.GetEndProperty();
                    while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
                    {
                        height += ScriptAttributeUtility.GetHandler(property, null).GetHeight(property, tc, true);
                        childrenAreExpanded = false;
                        height += EasyGUI.kControlVerticalSpacing;
                    }
                }
            }

            return(height);
        }
        public static float GetObjectReferenceHeight(Object o, ReorderableListDrawer drawer = null)
        {
            var height = EditorGUIUtility.singleLineHeight + 3f;

            if (o != null)
            {
                var objectData = o.GetSerializedObjectData();
                if (drawer != null)
                {
                    drawer.IgnoreHeader = true;
                    if (objectData.Foldout)
                    {
                        height += drawer.GetPropertiesAllHeights();
                    }
                }
                else
                {
                    var iterProp = objectData.Object.GetIterator();

                    if (iterProp.NextVisible(true))
                    {
                        int depth = iterProp.depth;
                        do
                        {
                            if (depth != iterProp.depth)
                            {
                                break;
                            }
                            if (iterProp.name.Equals(M_ScriptStr))
                            {
                                continue;
                            }
                            if (objectData.Foldout)
                            {
                                height += EasyGUI.GetPropertyHeight(iterProp, null, new GUIContent(iterProp.displayName), iterProp.isExpanded);
                            }
                        } while (iterProp.NextVisible(false));
                    }
                }
            }
            return(height);
        }
        public float GetPropertyHeight(InspectableProperty property)
        {
            var height = EasyGUI.GetPropertyHeight(property, new GUIContent(property.DisplayName), false);

            if (property.IsExpanded)
            {
                for (int i = 0; i < property.ArraySize; i++)
                {
                    var iterProp    = property.GetArrayElementAtIndex(i);
                    var displayName = new GUIContent(iterProp.DisplayName);
                    if (ElementNameCallback != null)
                    {
                        iterProp.DisplayName = ElementNameCallback(i);
                        displayName          = iterProp.DisplayName == null ? GUIContent.none : new GUIContent(iterProp.DisplayName);
                    }
                    height += EasyGUI.GetInspectableObjectHeight(iterProp, displayName, IsDrawObjectReference);
                }
                height += 3 * EditorGUIUtility.singleLineHeight;
            }
            return(height);
        }
        public override void OnGUI(Rect position, InspectableProperty property, GUIContent label)
        {
            List <Type> types;

            string[] displayedOptions;
            int[]    optionValues;
            int      selectedIndex = GetTypePopupFromProperty(property, Attribute as TypePopupAttribute, out types, out displayedOptions, out optionValues);

            if (!useSearchIndex.ContainsKey(property.PropertyPath))
            {
                useSearchIndex.Add(property.PropertyPath, false);
            }
            useSearchIndex[property.PropertyPath] = EditorGUI.Foldout(new Rect(position.position, new Vector2(EasyGUI.Indent, EditorGUIUtility.singleLineHeight + 3)), useSearchIndex[property.PropertyPath], GUIContent.none);
            EditorGUI.BeginChangeCheck();
            var index = EditorGUI.IntPopup(new Rect(position.position, new Vector2(position.width, EditorGUIUtility.singleLineHeight + 3)), property.DisplayName, selectedIndex, displayedOptions, optionValues);

            if (EditorGUI.EndChangeCheck())
            {
                property.StringValue = index == 0 ? null : types[index].FullName;
            }
            if (useSearchIndex[property.PropertyPath])
            {
                var height         = EditorGUIUtility.singleLineHeight + 3;
                var searchPosition = new Rect(new Vector2(position.x - EasyGUI.Indent, position.y + height), new Vector2(position.width, height));

                searchCondition = EasyGUI.SearchFieldWithPopupMenu(searchPosition, SearchLabelStr, searchField, searchCondition, popupMenu =>
                {
                    foreach (var type in types.Where(type => type != null && type.FullName.ToLower().Contains(searchCondition.ToLower())))
                    {
                        popupMenu.AddItem(new GUIContent(type.Name), false, () =>
                        {
                            property.StringValue = type.FullName;
                            property.InspectableObject.ApplyModifiedProperties();
                            searchCondition = EmptyStr;
                        });
                    }
                }, SearchLabelHtmlString, SearchLabelTooltipStr);
            }
        }
        public bool DoProperty(Rect position, InspectableProperty property)
        {
            if (!extendListIndex.ContainsKey(property.PropertyPath))
            {
                return(false);
            }

            headerPosition        = new Rect(position);
            headerPosition.height = EasyGUI.GetPropertyHeight(property, GUIContent.none, false);
            // Draw the background
            if (DrawBackgroundCallback != null)
            {
                Rect backgroundPosition = new Rect(headerPosition);
                if (property.IsExpanded)
                {
                    backgroundPosition.yMax += 19;
                }
                EditorGUI.DrawRect(backgroundPosition, DrawBackgroundCallback(false, false));
            }

            // Draw header
            if (HeaderCallback != null)
            {
                HeaderCallback(headerPosition);
            }
            else
            {
                string headerName = string.Format(Header, property.DisplayName, property.ArraySize);
                EasyGUI.PropertyField(headerPosition, property, new GUIContent(headerName), false);
            }

            // Draw the reorderable list for the property
            if (property.IsExpanded)
            {
                EditorGUI.BeginDisabledGroup(!Editable);
                if (!property.Editable)
                {
                    EditorGUI.indentLevel++;
                }
                EditorGUI.BeginChangeCheck();
                var sizePosition = new Rect(position);
                sizePosition.yMin = headerPosition.yMax;
                sizePosition.yMax = headerPosition.yMax + EditorGUIUtility.singleLineHeight;
                var newArraySize = Mathf.Clamp(EditorGUI.IntField(sizePosition, Size, property.ArraySize), 0, int.MaxValue);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(property.InspectableObject.SerializedObject.targetObject, SetArraySize);
                    property.ArraySize = newArraySize;
                    EditorUtility.SetDirty(property.InspectableObject.SerializedObject.targetObject);
                }
                var listPosition = new Rect(position);
                listPosition.xMin += 15;
                listPosition.yMin  = sizePosition.yMax;
                extendListIndex[property.PropertyPath].DoList(listPosition);
                if (!property.Editable)
                {
                    EditorGUI.indentLevel--;
                }
                EditorGUI.EndDisabledGroup();
            }

            return(true);
        }
        protected void DrawProperty(Rect position, SerializedProperty property)
        {
            // Try to get the sortable list this property belongs to
            var listData = GetReorderableListData(property);

            UnityEditor.Editor scriptableEditor;
            var isScriptableEditor = editableDict.TryGetValue(property.propertyPath, out scriptableEditor);

            // Has ReorderableList
            if (listData != null)
            {
                // Try to show the list
                if (!listData.DoProperty(position, property))
                {
                    var headerPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
                    EditorGUI.PropertyField(headerPosition, property, false);
                    position.y += EditorGUIUtility.singleLineHeight;
                    if (property.isExpanded)
                    {
                        EditorGUI.indentLevel++;
                        IterateDrawProperty(position, property.Copy());
                        EditorGUI.indentLevel--;
                    }
                }
            }
            // Else try to draw ScriptableObject editor
            else if (isScriptableEditor)
            {
                bool hasHeader = property.HasAttribute <HeaderAttribute>();
                bool hasSpace  = property.HasAttribute <SpaceAttribute>();

                hasSpace |= hasHeader;

                // No data in property, draw property field with create button
                if (scriptableEditor == null)
                {
                    var objPosition = new Rect(position);
                    var btnPosition = new Rect(position);

                    objPosition.xMax -= hasSpace ? 66 : 56;
                    btnPosition.xMin  = btnPosition.xMax - 56;
                    EditorGUI.PropertyField(objPosition, property, false);
                    var doCreate = GUI.Button(btnPosition, Create, EditorStyles.miniButton);

                    if (doCreate)
                    {
                        var propType = property.GetTypeReflection();
                        ScriptableObjectUtility.CreateAssetWithSavePrompt(propType, postCreated: createdAsset =>
                        {
                            property.objectReferenceValue = createdAsset;
                            property.isExpanded           = true;
                            return(false);
                        });
                    }
                }
                // Has data in property, draw foldout and editor
                else
                {
                    EasyGUI.PropertyField(position, property, new GUIContent(property.displayName), true, null);
                }
            }
            else
            {
                var isStartProp = property.propertyPath.StartsWith(M_ScriptStr);
                if (isStartProp && IgnoreHeader)
                {
                }
                else
                {
                    using (new EditorGUI.DisabledScope(isStartProp))
                    {
                        EditorGUI.PropertyField(position, property, property.isExpanded);
                    }
                }
            }
        }
Example #21
0
        public bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren, Rect visibleArea)
        {
            float oldLabelWidth, oldFieldWidth;

            float propHeight = position.height;

            position.height = 0;
            if (DecoratorDrawers != null && !IsCurrentlyNested)
            {
                foreach (var decorator in DecoratorDrawers)
                {
                    position.height = decorator.GetHeight();

                    oldLabelWidth = EditorGUIUtility.labelWidth;
                    oldFieldWidth = EditorGUIUtility.fieldWidth;
                    decorator.OnGUI(position);
                    EditorGUIUtility.labelWidth = oldLabelWidth;
                    EditorGUIUtility.fieldWidth = oldFieldWidth;

                    position.y += position.height;
                    propHeight -= position.height;
                }
            }

            position.height = propHeight;
            if (PropertyDrawer != null)
            {
                // Remember widths
                oldLabelWidth = EditorGUIUtility.labelWidth;
                oldFieldWidth = EditorGUIUtility.fieldWidth;
                // Draw with custom drawer
                PropertyDrawer.OnGUISafe(position, property.Copy(), label ?? EditorGUIUtilityHelper.TempContent(property.displayName));
                // Restore widths
                EditorGUIUtility.labelWidth = oldLabelWidth;
                EditorGUIUtility.fieldWidth = oldFieldWidth;

                return(false);
            }
            else
            {
                if (!includeChildren)
                {
                    return(EasyGUI.DefaultPropertyField(position, property, label));
                }
                // Remember state
                Vector2 oldIconSize = EditorGUIUtility.GetIconSize();
                bool    wasEnabled  = GUI.enabled;
                int     origIndent  = EditorGUI.indentLevel;

                int relIndent = origIndent - property.depth;

                SerializedProperty prop = property.Copy();

                position.height = EasyGUI.GetSinglePropertyHeight(prop, label);

                // First property with custom label
                EditorGUI.indentLevel = prop.depth + relIndent;
                bool childrenAreExpanded = EasyGUI.DefaultPropertyField(position, prop, label) && EasyGUI.HasVisibleChildFields(prop);
                position.y += position.height + EasyGUI.kControlVerticalSpacing;

                // Loop through all child properties
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = prop.GetEndProperty();
                    while (prop.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(prop, endProperty))
                    {
                        var handler = ScriptAttributeUtility.GetHandler(prop, null);
                        EditorGUI.indentLevel = prop.depth + relIndent;
                        position.height       = handler.GetHeight(prop, null, false);

                        if (position.Overlaps(visibleArea))
                        {
                            EditorGUI.BeginChangeCheck();
                            childrenAreExpanded = handler.OnGUI(position, prop, null, false) && EasyGUI.HasVisibleChildFields(prop);
                            // Changing child properties (like array size) may invalidate the iterator,
                            // so stop now, or we may get errors.
                            if (EditorGUI.EndChangeCheck())
                            {
                                break;
                            }
                        }

                        position.y += position.height + EasyGUI.kControlVerticalSpacing;
                    }
                }

                // Restore state
                GUI.enabled = wasEnabled;
                EditorGUIUtility.SetIconSize(oldIconSize);
                EditorGUI.indentLevel = origIndent;

                return(false);
            }
        }
Example #22
0
        public static Object ValidateObjectFieldAssignment(Object[] references, System.Type objType, RuntimeSerializedProperty property, EasyGUI.ObjectFieldValidatorOptions options)
        {
            if (references.Length > 0)
            {
                bool dragAssignment = DragAndDrop.objectReferences.Length > 0;
                bool isTextureRef   = (references[0] != null && references[0] is Texture2D);

                if (objType == typeof(Sprite) && isTextureRef && dragAssignment)
                {
                    return(SpriteUtilityHelper.TextureToSprite(references[0] as Texture2D));
                }

                if (property != null)
                {
                    if (references[0] != null && ValidateObjectReferenceValue(property, references[0], options))
                    {
                        if (EditorSceneManager.preventCrossSceneReferences && EasyGUI.CheckForCrossSceneReferencing(references[0], property.RuntimeSerializedObject.TargetObject))
                        {
                            return(null);
                        }

                        if (objType != null)
                        {
                            if (references[0] is GameObject && typeof(Component).IsAssignableFrom(objType))
                            {
                                GameObject go = (GameObject)references[0];
                                references = go.GetComponents(typeof(Component));
                            }
                            foreach (Object i in references)
                            {
                                if (i != null && objType.IsAssignableFrom(i.GetType()))
                                {
                                    return(i);
                                }
                            }
                        }
                        else
                        {
                            return(references[0]);
                        }
                    }

                    // If array, test against the target arrayElementType, if not test against the target Type.
                    var type = property.PropertyType;
                    if (property.IsArray)
                    {
                        type = property.ArrayElementType;
                    }

                    if (type == RuntimeSerializedPropertyType.Sprite && isTextureRef && dragAssignment)
                    {
                        return(SpriteUtilityHelper.TextureToSprite(references[0] as Texture2D));
                    }
                }
                else
                {
                    if (references[0] != null && references[0] is GameObject && typeof(Component).IsAssignableFrom(objType))
                    {
                        GameObject gameObject = (GameObject)references[0];
                        references = gameObject.GetComponents(typeof(Component));
                    }
                    foreach (Object i in references)
                    {
                        if (i != null && objType.IsAssignableFrom(i.GetType()))
                        {
                            return(i);
                        }
                    }
                }
            }
            return(null);
        }
Example #23
0
        public static bool DefaultPropertyField(Rect position, RuntimeSerializedProperty property, GUIContent label)
        {
            label = BeginProperty(position, label, property);

            System.Type propertyType = property.PropertyType;

            bool childrenAreExpanded = false;

            // Should we inline? All one-line vars as well as Vector2, Vector3, Rect and Bounds properties are inlined.
            if (!HasVisibleChildFields(property))
            {
                if (propertyType == RuntimeSerializedPropertyType.Bool)
                {
                    EditorGUI.BeginChangeCheck();
                    bool boolValue = EditorGUI.Toggle(position, label, property.BoolValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoolValue = boolValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Byte)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.ByteValue);
                    if (intValue >= byte.MaxValue)
                    {
                        intValue = byte.MaxValue;
                    }
                    else if (intValue <= byte.MinValue)
                    {
                        intValue = byte.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ByteValue = (byte)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Char)
                {
                    char[] value = new char[] {
                        property.CharValue
                    };
                    bool changed = GUI.changed;
                    GUI.changed = false;
                    string text = EditorGUI.TextField(position, label, new string(value));
                    if (GUI.changed)
                    {
                        if (text.Length == 1)
                        {
                            property.CharValue = (char)text[0];
                        }
                        else
                        {
                            GUI.changed = false;
                        }
                    }
                    GUI.changed |= changed;
                }
                else if (propertyType == RuntimeSerializedPropertyType.Short)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.ShortValue);
                    if (intValue >= short.MaxValue)
                    {
                        intValue = short.MaxValue;
                    }
                    else if (intValue <= short.MinValue)
                    {
                        intValue = short.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ShortValue = (short)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Integer)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.IntValue = intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Long)
                {
                    EditorGUI.BeginChangeCheck();
                    long longValue = EditorGUI.LongField(position, label, property.LongValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.LongValue = longValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.sByte)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.sByteValue);
                    if (intValue >= sbyte.MaxValue)
                    {
                        intValue = sbyte.MaxValue;
                    }
                    else if (intValue <= sbyte.MinValue)
                    {
                        intValue = sbyte.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.sByteValue = (sbyte)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uShort)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EditorGUI.IntField(position, label, property.uShortValue);
                    if (intValue >= ushort.MaxValue)
                    {
                        intValue = ushort.MaxValue;
                    }
                    else if (intValue <= ushort.MinValue)
                    {
                        intValue = ushort.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.uShortValue = (ushort)intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uInteger)
                {
                    EditorGUI.BeginChangeCheck();
                    long longValue = EditorGUI.LongField(position, label, property.uIntValue);
                    if (longValue >= uint.MaxValue)
                    {
                        longValue = uint.MaxValue;
                    }
                    else if (longValue <= uint.MinValue)
                    {
                        longValue = uint.MinValue;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.uIntValue = (uint)longValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.uLong)
                {
                    EditorGUI.BeginChangeCheck();
                    string stringValue = EditorGUI.TextField(position, label, property.uLongValue.ToString());
                    if (EditorGUI.EndChangeCheck())
                    {
                        ulong ulongValue = property.uLongValue;
                        if (ulong.TryParse(stringValue, out ulongValue))
                        {
                            property.uLongValue = ulongValue;
                        }
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Float)
                {
                    EditorGUI.BeginChangeCheck();
                    float floatValue = EditorGUI.FloatField(position, label, property.FloatValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.FloatValue = floatValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Double)
                {
                    EditorGUI.BeginChangeCheck();
                    double doubleValue = EditorGUI.DoubleField(position, label, property.DoubleValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.DoubleValue = doubleValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.String)
                {
                    EditorGUI.BeginChangeCheck();
                    string stringValue = EditorGUI.TextField(position, label, property.StringValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.StringValue = stringValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Color)
                {
                    EditorGUI.BeginChangeCheck();
                    Color colorValue = EditorGUI.ColorField(position, label, property.ColorValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ColorValue = colorValue;
                    }
                }
                else if (propertyType.IsSameOrSubclassOf(RuntimeSerializedPropertyType.Object))
                {
                    GUIStyle style = new GUIStyle();
                    style.richText = true;
                    EditorGUI.LabelField(position, "<color=red>Reference type is not supported!</color>", style);
                }
                else if (propertyType == RuntimeSerializedPropertyType.LayerMask)
                {
                    EditorGUI.BeginChangeCheck();
                    LayerMask layerMaskValue   = property.LayerMaskValue;
                    string[]  displayedOptions = RuntimeSerializedProperty.GetLayerMaskNames(0);
                    int       num = EditorGUI.MaskField(position, label, layerMaskValue.value, displayedOptions);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.LayerMaskValue = num;
                    }
                }
                else if (propertyType.IsEnum)
                {
                    EditorGUI.BeginChangeCheck();
                    System.Enum enumValue = EditorGUI.EnumPopup(position, label, property.EnumValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.EnumValue = enumValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector2)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector2 vector2Value = EditorGUI.Vector2Field(position, label, property.Vector2Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector2Value = vector2Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector3)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 vector3Value = EditorGUI.Vector3Field(position, label, property.Vector3Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector3Value = vector3Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector4)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector4 vector4Value = EditorGUI.Vector4Field(position, label, property.Vector4Value);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector4Value = vector4Value;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Rect)
                {
                    EditorGUI.BeginChangeCheck();
                    Rect rectValue = EditorGUI.RectField(position, label, property.RectValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.RectValue = rectValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.ArraySize)
                {
                    EditorGUI.BeginChangeCheck();
                    int intValue = EasyGUI.ArraySizeField(position, label, property.ArraySize, EditorStyles.numberField);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.ArraySize = intValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.AnimationCurve)
                {
                    EditorGUI.BeginChangeCheck();
                    if (property.AnimationCurveValue == null)
                    {
                        property.AnimationCurveValue = new AnimationCurve(new Keyframe[] {
                            new Keyframe(0, 1),
                            new Keyframe(1, 1)
                        });
                    }
                    AnimationCurve animationCurveValue = EditorGUI.CurveField(position, label, property.AnimationCurveValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.AnimationCurveValue = animationCurveValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Bounds)
                {
                    EditorGUI.BeginChangeCheck();
                    Bounds boundsValue = EditorGUI.BoundsField(position, label, property.BoundsValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoundsValue = boundsValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Gradient)
                {
                    EditorGUI.BeginChangeCheck();
                    Gradient gradientValue = EasyGUI.GradientField(label, position, property.GradientValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.GradientValue = gradientValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.FixedBufferSize)
                {
                    EditorGUI.IntField(position, label, property.IntValue);
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector2Int)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector2Int vector2IntValue = EditorGUI.Vector2IntField(position, label, property.Vector2IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector2IntValue = vector2IntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.Vector3Int)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3Int vector3IntValue = EditorGUI.Vector3IntField(position, label, property.Vector3IntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.Vector3IntValue = vector3IntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.RectInt)
                {
                    EditorGUI.BeginChangeCheck();
                    RectInt rectIntValue = EditorGUI.RectIntField(position, label, property.RectIntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.RectIntValue = rectIntValue;
                    }
                }
                else if (propertyType == RuntimeSerializedPropertyType.BoundsInt)
                {
                    EditorGUI.BeginChangeCheck();
                    BoundsInt boundsIntValue = EditorGUI.BoundsIntField(position, label, property.BoundsIntValue);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.BoundsIntValue = boundsIntValue;
                    }
                }
                else
                {
                    int num = GUIUtility.GetControlID(s_GenericField, FocusType.Keyboard, position);
                    EditorGUI.PrefixLabel(position, num, label);
                }
            }
            // Handle Foldout
            else
            {
                Event tempEvent = new Event(Event.current);

                // Handle the actual foldout first, since that's the one that supports keyboard control.
                // This makes it work more consistent with PrefixLabel.
                childrenAreExpanded = property.IsExpanded;

                bool newChildrenAreExpanded = childrenAreExpanded;
                using (new EditorGUI.DisabledScope(!property.Editable))
                {
                    GUIStyle foldoutStyle = (DragAndDrop.activeControlID != -10) ? EditorStyles.foldout : EditorStyles.foldoutPreDrop;
                    newChildrenAreExpanded = EditorGUI.Foldout(position, childrenAreExpanded, s_PropertyFieldTempContent, true, foldoutStyle);
                }
                if (childrenAreExpanded && property.IsArray && property.ArraySize > property.RuntimeSerializedObject.SerializedObject.maxArraySizeForMultiEditing && property.RuntimeSerializedObject.SerializedObject.isEditingMultipleObjects)
                {
                    Rect boxRect = position;
                    boxRect.xMin += EditorGUIUtility.labelWidth - EasyGUI.Indent;

                    s_ArrayMultiInfoContent.text = s_ArrayMultiInfoContent.tooltip = string.Format(s_ArrayMultiInfoFormatString, property.RuntimeSerializedObject.SerializedObject.maxArraySizeForMultiEditing);
                    EditorGUI.LabelField(boxRect, GUIContent.none, s_ArrayMultiInfoContent, EditorStyles.helpBox);
                }

                if (newChildrenAreExpanded != childrenAreExpanded)
                {
                    // Recursive set expanded
                    if (Event.current.alt)
                    {
                        SetExpandedRecurse(property, newChildrenAreExpanded);
                    }
                    // Expand one element only
                    else
                    {
                        property.IsExpanded = newChildrenAreExpanded;
                    }
                }
                childrenAreExpanded = newChildrenAreExpanded;


                // Check for drag & drop events here, to add objects to an array by dragging to the foldout.
                // The event may have already been used by the Foldout control above, but we want to also use it here,
                // so we use the event copy we made prior to calling the Foldout method.

                // We need to use last s_LastControlID here to ensure we do not break duplicate functionality (fix for case 598389)
                // If we called GetControlID here s_LastControlID would be incremented and would not longer be in sync with GUIUtililty.keyboardFocus that
                // is used for duplicating (See DoPropertyFieldKeyboardHandling)
                int id = EditorGUIUtilityHelper.s_LastControlID;
                switch (tempEvent.type)
                {
                case EventType.DragExited:
                    if (GUI.enabled)
                    {
                        HandleUtility.Repaint();
                    }

                    break;

                case EventType.DragUpdated:
                case EventType.DragPerform:

                    if (position.Contains(tempEvent.mousePosition) && GUI.enabled)
                    {
                        Object[] references = DragAndDrop.objectReferences;

                        // Check each single object, so we can add multiple objects in a single drag.
                        Object[] oArray        = new Object[1];
                        bool     didAcceptDrag = false;
                        foreach (Object o in references)
                        {
                            oArray[0] = o;
                            Object validatedObject = ValidateObjectFieldAssignment(oArray, null, property, EasyGUI.ObjectFieldValidatorOptions.None);
                            if (validatedObject != null)
                            {
                                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                                if (tempEvent.type == EventType.DragPerform)
                                {
                                    property.AppendFoldoutPPtrValue(validatedObject);
                                    didAcceptDrag = true;
                                    DragAndDrop.activeControlID = 0;
                                }
                                else
                                {
                                    DragAndDrop.activeControlID = id;
                                }
                            }
                        }
                        if (didAcceptDrag)
                        {
                            GUI.changed = true;
                            DragAndDrop.AcceptDrag();
                        }
                    }
                    break;
                }
            }

            EndProperty();

            return(childrenAreExpanded);
        }
        private static bool TryDrawObjectReference(Rect position, Object o, ReorderableListDrawer drawer = null)
        {
            var result = false;

            if (o != null)
            {
                var objectData     = o.GetSerializedObjectData();
                var headerPosition = new Rect(position);

                headerPosition.yMin = headerPosition.yMax - EditorGUIUtility.singleLineHeight;

                if (drawer != null)
                {
                    drawer.IgnoreHeader = true;
                    if (objectData.Foldout)
                    {
                        drawer.serializedObject.Update();
                        var listPosition = new Rect(headerPosition);
                        listPosition.xMin += 15;
                        listPosition.y    += EditorGUIUtility.singleLineHeight;
                        drawer.DrawPropertiesAll(listPosition);
                        drawer.serializedObject.ApplyModifiedProperties();
                    }
                    result = true;
                }
                else
                {
                    var iterProp = objectData.Object.GetIterator();

                    position.yMin   = headerPosition.yMax;
                    position.height = 0f;
                    EditorGUI.BeginChangeCheck();
                    if (iterProp.NextVisible(true))
                    {
                        EditorGUI.indentLevel++;
                        int depth = iterProp.depth;
                        do
                        {
                            if (depth != iterProp.depth)
                            {
                                break;
                            }
                            if (iterProp.name.Equals(M_ScriptStr))
                            {
                                continue;
                            }
                            if (objectData.Foldout)
                            {
                                var displayName = new GUIContent(iterProp.displayName);
                                position.yMin  += position.height;
                                position.height = EasyGUI.GetPropertyHeight(iterProp, null, displayName, iterProp.isExpanded);
                                EasyGUI.PropertyField(position, iterProp, displayName, iterProp.isExpanded, null);
                            }
                            result = true;
                        } while (iterProp.NextVisible(false));
                        EditorGUI.indentLevel--;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        objectData.Object.ApplyModifiedProperties();
                        EditorSceneManager.MarkAllScenesDirty();
                    }
                }

                if (result)
                {
                    var indentLevel = EditorGUI.indentLevel;
                    headerPosition.xMin  += EasyGUI.Indent;
                    headerPosition.width  = 5;
                    EditorGUI.indentLevel = 0;
                    objectData.Foldout    = EditorGUI.Foldout(headerPosition, objectData.Foldout, GUIContent.none, false);
                    EditorGUI.indentLevel = indentLevel;
                }
            }
            return(result);
        }
        private void DoDraggingAndSelection(Rect listRect)
        {
            Event evt      = Event.current;
            int   oldIndex = m_ActiveElement;
            bool  clicked  = false;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.KeyDown:
                if (GUIUtility.keyboardControl != id)
                {
                    return;
                }
                // if we have keyboard focus, arrow through the list
                if (evt.keyCode == KeyCode.DownArrow)
                {
                    m_ActiveElement += 1;
                    evt.Use();
                }
                if (evt.keyCode == KeyCode.UpArrow)
                {
                    m_ActiveElement -= 1;
                    evt.Use();
                }
                if (evt.keyCode == KeyCode.Escape && GUIUtility.hotControl == id)
                {
                    GUIUtility.hotControl = 0;
                    m_Dragging            = false;
                    evt.Use();
                }
                // don't allow arrowing through the ends of the list
                m_ActiveElement = Mathf.Clamp(m_ActiveElement, 0, (m_Elements != null) ? m_Elements.ArraySize - 1 : m_ElementList.Count - 1);
                break;

            case EventType.MouseDown:
                if (!listRect.Contains(Event.current.mousePosition) || Event.current.button != 0)
                {
                    break;
                }
                // clicking on the list should end editing any existing edits
                EasyGUI.EndEditingActiveTextField();
                // pick the active element based on click position
                m_ActiveElement = GetRowIndex(Event.current.mousePosition.y - listRect.y);

                if (m_Draggable)
                {
                    // if we can drag, set the hot control and start dragging (storing the offset)
                    m_DragOffset = (Event.current.mousePosition.y - listRect.y) - GetElementYOffset(m_ActiveElement);
                    UpdateDraggedY(listRect);
                    GUIUtility.hotControl = id;
                    m_SlideGroup.Reset();
                    m_NonDragTargetIndices = new List <int>();
                }
                GrabKeyboardFocus();
                evt.Use();
                clicked = true;
                break;

            case EventType.MouseDrag:
                if (!m_Draggable || GUIUtility.hotControl != id)
                {
                    break;
                }

                // Set m_Dragging state on first MouseDrag event after we got hotcontrol (to prevent animating elements when deleting elements by context menu)
                m_Dragging = true;

                // if we are dragging, update the position
                UpdateDraggedY(listRect);
                evt.Use();
                break;

            case EventType.MouseUp:
                if (!m_Draggable)
                {
                    // if mouse up was on the same index as mouse down we fire a mouse up callback (useful if for beginning renaming on mouseup)
                    if (onMouseUpCallback != null && IsMouseInsideActiveElement(listRect))
                    {
                        // set the keyboard control
                        onMouseUpCallback(this);
                    }
                    break;
                }

                // hotcontrol is only set when list is draggable
                if (GUIUtility.hotControl != id)
                {
                    break;
                }
                evt.Use();
                m_Dragging = false;

                try
                {
                    // What will be the index of this if we release?
                    int targetIndex = CalculateRowIndex();
                    if (m_ActiveElement != targetIndex)
                    {
                        // if the target index is different than the current index...
                        if (m_SerializedObject != null && m_Elements != null)
                        {
                            // if we are working with Runtime Serialized Properties, we can handle it for you
                            m_Elements.MoveArrayElement(m_ActiveElement, targetIndex);
                            m_SerializedObject.ApplyModifiedProperties();
                            m_SerializedObject.Update();
                        }
                        else if (m_ElementList != null)
                        {
                            // we are working with the IList, which is probably of a fixed length
                            System.Object tempObject = m_ElementList[m_ActiveElement];
                            for (int i = 0; i < m_ElementList.Count - 1; i++)
                            {
                                if (i >= m_ActiveElement)
                                {
                                    m_ElementList[i] = m_ElementList[i + 1];
                                }
                            }
                            for (int i = m_ElementList.Count - 1; i > 0; i--)
                            {
                                if (i > targetIndex)
                                {
                                    m_ElementList[i] = m_ElementList[i - 1];
                                }
                            }
                            m_ElementList[targetIndex] = tempObject;
                        }

                        var oldActiveElement = m_ActiveElement;
                        var newActiveElement = targetIndex;

                        // update the active element, now that we've moved it
                        m_ActiveElement = targetIndex;
                        // give the user a callback
                        if (onReorderCallbackWithDetails != null)
                        {
                            onReorderCallbackWithDetails(this, oldActiveElement, newActiveElement);
                        }
                        else if (onReorderCallback != null)
                        {
                            onReorderCallback(this);
                        }

                        if (onChangedCallback != null)
                        {
                            onChangedCallback(this);
                        }
                    }
                    else
                    {
                        // if mouse up was on the same index as mouse down we fire a mouse up callback (useful if for beginning renaming on mouseup)
                        if (onMouseUpCallback != null)
                        {
                            onMouseUpCallback(this);
                        }
                    }
                }
                finally
                {
                    // It's quite possible a call to EndGUI was made in one of our callbacks
                    // (and thus an ExitGUIException thrown). We still need to cleanup before
                    // we exitGUI proper.
                    GUIUtility.hotControl  = 0;
                    m_NonDragTargetIndices = null;
                }
                break;
            }
            // if the index has changed and there is a selected callback, call it
            if ((m_ActiveElement != oldIndex || clicked) && onSelectCallback != null)
            {
                onSelectCallback(this);
            }
        }
Example #26
0
 public virtual void OnGUI(Rect position, InspectableProperty property, GUIContent label)
 {
     EasyGUI.DefaultPropertyField(position, property, label);
     EditorGUI.LabelField(position, label, EditorGUIUtilityHelper.TempContent("No GUI Implemented"));
 }
 protected virtual bool DoElementHeader(Rect position, GUIContent label, InspectableObject inspectableObject)
 {
     return(EasyGUI.TryDrawDefaultElementHeader(position, label, inspectableObject));
 }