Example #1
0
        public override float   GetHeight(object instance)
        {
            float h = Constants.SingleLineHeight;

            if (this.isExpanded == true)
            {
                ICollectionModifier collection = NGTools.Utility.GetCollectionModifier(instance);

                if (collection != null)
                {
                    h += 2F + Constants.SingleLineHeight;

                    try
                    {
                        Type subType = collection.SubType;

                        while (this.drawers.Count < collection.Size)
                        {
                            this.drawers.Add(TypeDrawerManager.GetDrawer(this.path + '.' + this.drawers.Count.ToCachedString(), "Element " + this.drawers.Count.ToCachedString(), subType));
                        }

                        for (int i = 0; i < collection.Size; i++)
                        {
                            h += 2F + this.drawers[i].GetHeight(collection.Get(i));
                        }
                    }
                    finally
                    {
                        NGTools.Utility.ReturnCollectionModifier(collection);
                    }
                }
            }

            return(h);
        }
        private MemberDrawer[]          GetMembers(Type type)
        {
            MemberDrawer[] drawers;

            if (type.IsGenericTypeDefinition == true)
            {
                return(NGStaticInspectorWindow.Empty);
            }

            if (this.typesMembers.TryGetValue(type, out drawers) == false)
            {
                List <MemberDrawer> list       = new List <MemberDrawer>();
                FieldInfo[]         fields     = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                PropertyInfo[]      properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                drawers = new MemberDrawer[fields.Length + properties.Length];

                for (int i = 0; i < fields.Length; i++)
                {
                    list.Add(new MemberDrawer(TypeDrawerManager.GetDrawer(type.GetShortAssemblyType() + '.' + fields[i].Name, fields[i].Name, fields[i].FieldType), new FieldModifier(fields[i])));
                }

                for (int i = 0; i < properties.Length; i++)
                {
                    if (properties[i].GetGetMethod() != null)
                    {
                        string niceName = Utility.NicifyVariableName(properties[i].Name);
                        int    j        = 0;

                        for (; j < list.Count; j++)
                        {
                            if (list[j].typeDrawer.label == niceName)
                            {
                                break;
                            }
                        }

                        if (j == list.Count)
                        {
                            list.Add(new MemberDrawer(TypeDrawerManager.GetDrawer(type.GetShortAssemblyType() + '.' + properties[i].Name, properties[i].Name, properties[i].PropertyType), new PropertyModifier(properties[i])));
                        }
                    }
                }

                drawers = list.ToArray();

                this.typesMembers.Add(type, drawers);
            }

            return(drawers);
        }
Example #3
0
        private void    InitMembers()
        {
            if (this.members != null)
            {
                return;
            }

            List <MemberDrawer> list = new List <MemberDrawer>();

            FieldInfo[]    fields     = this.type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            PropertyInfo[] properties = this.type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            for (int i = 0; i < fields.Length; i++)
            {
                list.Add(new MemberDrawer(TypeDrawerManager.GetDrawer(this.path + '.' + fields[i].Name, fields[i].Name, fields[i].FieldType), new FieldModifier(fields[i])));
            }

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].GetGetMethod() != null && properties[i].GetIndexParameters().Length == 0)
                {
                    string niceName = Utility.NicifyVariableName(properties[i].Name);
                    int    j        = 0;

                    for (; j < list.Count; j++)
                    {
                        if (list[j].typeDrawer.label == niceName)
                        {
                            break;
                        }
                    }

                    if (j == list.Count)
                    {
                        list.Add(new MemberDrawer(TypeDrawerManager.GetDrawer(this.path + '.' + properties[i].Name, properties[i].Name, properties[i].PropertyType), new PropertyModifier(properties[i])));
                    }
                }
            }

            this.members = list.ToArray();
        }
Example #4
0
        public override object  OnGUI(Rect r, object instance)
        {
            r.height = Constants.SingleLineHeight;

            --EditorGUI.indentLevel;
            r.x += 3F;
            if (instance == null)
            {
                EditorGUI.BeginChangeCheck();
                EditorGUI.Foldout(r, false, this.label + " (Null)", false);
                if (EditorGUI.EndChangeCheck() == true)
                {
                    GUI.changed = false;
                }
            }
            else
            {
                EditorGUI.BeginChangeCheck();
                this.isExpanded = EditorGUI.Foldout(r, this.isExpanded, this.label, true);
                if (EditorGUI.EndChangeCheck() == true)
                {
                    NGEditorPrefs.SetBool(path, this.isExpanded);
                    GUI.changed = false;
                }
            }
            r.x -= 3F;
            ++EditorGUI.indentLevel;

            if (this.isExpanded == true)
            {
                r.y += r.height + 2F;
                ++EditorGUI.indentLevel;

                ICollectionModifier collection = NGTools.Utility.GetCollectionModifier(instance);

                if (collection != null)
                {
                    try
                    {
                        Type subType = collection.SubType;

                        EditorGUI.BeginChangeCheck();
                        int size = EditorGUI.DelayedIntField(r, "Size", collection.Size);
                        if (EditorGUI.EndChangeCheck() == true)
                        {
                            size = Mathf.Max(0, size);

                            if (collection.Size != size)
                            {
                                --EditorGUI.indentLevel;

                                if (instance is Array)
                                {
                                    Array newArray = Array.CreateInstance(subType, size);

                                    Array.Copy(instance as Array, newArray, Mathf.Min(collection.Size, size));

                                    if (collection.Size > 0)
                                    {
                                        object lastValue = collection.Get(collection.Size - 1);

                                        for (int i = collection.Size; i < size; i++)
                                        {
                                            newArray.SetValue(lastValue, i);
                                        }
                                    }

                                    instance = newArray;
                                }
                                else if (instance is IList)
                                {
                                    IList list = instance as IList;

                                    if (list.Count < size)
                                    {
                                        object lastValue = list.Count > 0 ? list[list.Count - 1] : (subType.IsValueType == true ? Activator.CreateInstance(subType) : null);

                                        while (list.Count < size)
                                        {
                                            list.Add(lastValue);
                                        }
                                    }
                                    else
                                    {
                                        while (list.Count > size)
                                        {
                                            list.RemoveAt(list.Count - 1);
                                        }
                                    }

                                    instance = list;
                                }
                                else
                                {
                                    throw new NotImplementedException("Collection of type \"" + instance.GetType() + "\" is not supported.");
                                }
                            }

                            return(instance);
                        }

                        while (this.drawers.Count < collection.Size)
                        {
                            this.drawers.Add(TypeDrawerManager.GetDrawer(this.path + '.' + this.drawers.Count.ToCachedString(), "Element " + this.drawers.Count.ToCachedString(), subType));
                        }

                        for (int i = 0; i < collection.Size; i++)
                        {
                            r.y += r.height + 2F;

                            object element = collection.Get(i);
                            r.height = this.drawers[i].GetHeight(element);

                            EditorGUI.BeginChangeCheck();
                            object value = this.drawers[i].OnGUI(r, element);
                            if (EditorGUI.EndChangeCheck() == true)
                            {
                                collection.Set(i, value);
                            }
                        }
                    }
                    finally
                    {
                        NGTools.Utility.ReturnCollectionModifier(collection);
                    }
                }

                --EditorGUI.indentLevel;
            }

            return(instance);
        }