Example #1
0
 // default add button behavior
 public void DoAddButton(InspectableReorderableList list)
 {
     if (list.inspectableProperty != null)
     {
         list.inspectableProperty.ArraySize += 1;
         list.index = list.inspectableProperty.ArraySize - 1;
     }
     else
     {
         // this is ugly but there are a lot of cases like null types and default constructors
         Type elementType = list.list.GetType().GetElementType();
         if (elementType == typeof(string))
         {
             list.index = list.list.Add("");
         }
         else if (elementType != null && elementType.GetConstructor(Type.EmptyTypes) == null)
         {
             Debug.LogError("Cannot add element. Type " + elementType.ToString() + " has no default constructor. Implement a default constructor or implement your own add behaviour.");
         }
         else if (list.list.GetType().GetGenericArguments()[0] != null)
         {
             list.index = list.list.Add(Activator.CreateInstance(list.list.GetType().GetGenericArguments()[0]));
         }
         else if (elementType != null)
         {
             list.index = list.list.Add(Activator.CreateInstance(elementType));
         }
         else
         {
             Debug.LogError("Cannot add element of type Null.");
         }
     }
 }
        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 #3
0
 // default remove button behavior
 public void DoRemoveButton(InspectableReorderableList list)
 {
     if (list.inspectableProperty != null)
     {
         list.inspectableProperty.DeleteArrayElementAtIndex(list.index);
         if (list.index >= list.inspectableProperty.ArraySize - 1)
         {
             list.index = list.inspectableProperty.ArraySize - 1;
         }
     }
     else
     {
         list.list.RemoveAt(list.index);
         if (list.index >= list.list.Count - 1)
         {
             list.index = list.list.Count - 1;
         }
     }
 }
Example #4
0
            // draw the default footer
            public void DrawFooter(Rect rect, InspectableReorderableList list)
            {
                float rightEdge = rect.xMax;
                float leftEdge  = rightEdge - 8f;

                if (list.displayAdd)
                {
                    leftEdge -= 25;
                }
                if (list.displayRemove)
                {
                    leftEdge -= 25;
                }
                rect = new Rect(leftEdge, rect.y, rightEdge - leftEdge, rect.height);
                Rect addRect    = new Rect(leftEdge + 4, rect.y - 3, 25, 13);
                Rect removeRect = new Rect(rightEdge - 29, rect.y - 3, 25, 13);

                if (Event.current.type == EventType.Repaint)
                {
                    footerBackground.Draw(rect, false, false, false, false);
                }
                if (list.displayAdd)
                {
                    using (new EditorGUI.DisabledScope(
                               list.onCanAddCallback != null && !list.onCanAddCallback(list)))
                    {
                        if (GUI.Button(addRect, list.onAddDropdownCallback != null ? iconToolbarPlusMore : iconToolbarPlus, preButton))
                        {
                            if (list.onAddDropdownCallback != null)
                            {
                                list.onAddDropdownCallback(addRect, list);
                            }
                            else if (list.onAddCallback != null)
                            {
                                list.onAddCallback(list);
                            }
                            else
                            {
                                DoAddButton(list);
                            }

                            if (list.onChangedCallback != null)
                            {
                                list.onChangedCallback(list);
                            }
                        }
                    }
                }
                if (list.displayRemove)
                {
                    using (new EditorGUI.DisabledScope(
                               list.index < 0 || list.index >= list.count ||
                               (list.onCanRemoveCallback != null && !list.onCanRemoveCallback(list))))
                    {
                        if (GUI.Button(removeRect, iconToolbarMinus, preButton))
                        {
                            if (list.onRemoveCallback == null)
                            {
                                DoRemoveButton(list);
                            }
                            else
                            {
                                list.onRemoveCallback(list);
                            }

                            if (list.onChangedCallback != null)
                            {
                                list.onChangedCallback(list);
                            }
                        }
                    }
                }
            }