Ejemplo n.º 1
0
        /// <summary>
        /// Opens up a new category. Any new fields will be parented to this category. Category must be closed by calling
        /// <see cref="EndCategory"/> or by calling this method with a new category.
        /// </summary>
        /// <param name="name">Name of the category.</param>
        public void BeginCategory(string name)
        {
            if (category != null)
            {
                EndCategory();
            }

            string categoryPath = string.IsNullOrEmpty(path) ? $"[{name}]" : $"{path}/[{name}]";

            category = new InspectableCategory(context, name, categoryPath, depth,
                                               new InspectableFieldLayout(layout));

            category.Initialize(rootIndex);
            category.Refresh(rootIndex);
            rootIndex += category.GetNumLayoutElements();

            Fields.Add(category);

            categoryName  = name;
            categoryIndex = 0;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates inspectable fields all the fields/properties of the specified object.
        /// </summary>
        /// <param name="obj">Object whose fields the GUI will be drawn for.</param>
        /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
        /// <param name="path">Full path to the field this provided object was retrieved from.</param>
        /// <param name="depth">
        /// Determines how deep within the inspector nesting hierarchy is this objects. Some fields may contain other
        /// fields, in which case you should increase this value by one.
        /// </param>
        /// <param name="layout">Parent layout that all the field GUI elements will be added to.</param>
        /// <param name="overrideCallback">
        /// Optional callback that allows you to override the look of individual fields in the object. If non-null the
        /// callback will be called with information about every field in the provided object. If the callback returns
        /// non-null that inspectable field will be used for drawing the GUI, otherwise the default inspector field type
        /// will be used.
        /// </param>
        public static List <InspectableField> CreateFields(SerializableObject obj, InspectableContext context, string path,
                                                           int depth, GUILayoutY layout, FieldOverrideCallback overrideCallback = null)
        {
            // Retrieve fields and sort by order
            SerializableField[] fields = obj.Fields;
            Array.Sort(fields,
                       (x, y) =>
            {
                int orderX = x.Flags.HasFlag(SerializableFieldAttributes.Order) ? x.Style.Order : 0;
                int orderY = y.Flags.HasFlag(SerializableFieldAttributes.Order) ? y.Style.Order : 0;

                return(orderX.CompareTo(orderY));
            });

            // Generate per-field GUI while grouping by category
            int    rootIndex             = 0;
            int    categoryIndex         = 0;
            string categoryName          = null;
            InspectableCategory category = null;

            List <InspectableField> inspectableFields = new List <InspectableField>();

            foreach (var field in fields)
            {
                if (!field.Flags.HasFlag(SerializableFieldAttributes.Inspectable))
                {
                    continue;
                }

                if (field.Flags.HasFlag(SerializableFieldAttributes.Category))
                {
                    string newCategory = field.Style.CategoryName;
                    if (!string.IsNullOrEmpty(newCategory) && categoryName != newCategory)
                    {
                        string categoryPath = path + "/[" + newCategory + "]";
                        category = new InspectableCategory(context, newCategory, categoryPath, depth,
                                                           new InspectableFieldLayout(layout));

                        category.Initialize(rootIndex);
                        category.Refresh(rootIndex);
                        rootIndex += category.GetNumLayoutElements();

                        inspectableFields.Add(category);

                        categoryName  = newCategory;
                        categoryIndex = 0;
                    }
                    else
                    {
                        categoryName = null;
                        category     = null;
                    }
                }

                int        currentIndex;
                GUILayoutY parentLayout;
                if (category != null)
                {
                    currentIndex = categoryIndex;
                    parentLayout = category.ChildLayout;
                }
                else
                {
                    currentIndex = rootIndex;
                    parentLayout = layout;
                }

                string fieldName = GetReadableIdentifierName(field.Name);
                string childPath = string.IsNullOrEmpty(path) ? fieldName : path + "/" + fieldName;

                InspectableField inspectableField = null;

                if (overrideCallback != null)
                {
                    inspectableField = overrideCallback(field, context, path, new InspectableFieldLayout(parentLayout),
                                                        currentIndex, depth);
                }

                if (inspectableField == null)
                {
                    inspectableField = CreateField(context, fieldName, childPath,
                                                   currentIndex, depth, new InspectableFieldLayout(parentLayout), field.GetProperty(),
                                                   InspectableFieldStyle.Create(field));
                }

                if (category != null)
                {
                    category.AddChild(inspectableField);
                }
                else
                {
                    inspectableFields.Add(inspectableField);
                }

                currentIndex += inspectableField.GetNumLayoutElements();

                if (category != null)
                {
                    categoryIndex = currentIndex;
                }
                else
                {
                    rootIndex = currentIndex;
                }
            }

            return(inspectableFields);
        }