Exemple #1
0
        /// <inheritdoc />
        public override void Initialize(LayoutElementsContainer layout)
        {
            _visibleIfCaches = null;

            // Collect items to edit
            List <ItemInfo> items;

            if (!HasDifferentTypes)
            {
                var value = Values[0];
                if (value == null)
                {
                    // Check if it's an object type that can be created in editor
                    var type = Values.Type;
                    if (type != null && type.GetConstructor(Type.EmptyTypes) != null)
                    {
                        layout = layout.Space(20);

                        const float ButtonSize = 14.0f;
                        var         button     = new Button
                        {
                            Text        = "+",
                            TooltipText = "Create a new instance of the object",
                            Height      = ButtonSize,
                            Width       = ButtonSize,
                            X           = layout.ContainerControl.Width - ButtonSize - 4,
                            AnchorStyle = AnchorStyle.CenterRight,
                            Parent      = layout.ContainerControl
                        };
                        button.Clicked += () =>
                        {
                            var newType = Values.Type;
                            SetValue(Activator.CreateInstance(newType));
                            RebuildLayoutOnRefresh();
                        };
                    }

                    layout.Label("<null>");
                    return;
                }

                items = GetItemsForType(value.GetType());
            }
            else
            {
                var types = ValuesTypes;
                items = new List <ItemInfo>(GetItemsForType(types[0]));
                for (int i = 1; i < types.Length && items.Count > 0; i++)
                {
                    var otherItems = GetItemsForType(types[i]);

                    // Merge items
                    for (int j = 0; j < items.Count && items.Count > 0; j++)
                    {
                        bool isInvalid = true;
                        for (int k = 0; k < otherItems.Count; k++)
                        {
                            var a = items[j];
                            var b = otherItems[k];

                            if (ItemInfo.CanMerge(a, b))
                            {
                                isInvalid = false;
                                break;
                            }
                        }

                        if (isInvalid)
                        {
                            items.RemoveAt(j--);
                        }
                    }
                }
            }

            // Sort items
            items.Sort();

            // Add items
            GroupElement lastGroup = null;

            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];

                // Check if use group
                LayoutElementsContainer itemLayout;
                if (item.UseGroup)
                {
                    if (lastGroup == null || lastGroup.Panel.HeaderText != item.Display.Group)
                    {
                        lastGroup = layout.Group(item.Display.Group);
                    }
                    itemLayout = lastGroup;
                }
                else
                {
                    lastGroup  = null;
                    itemLayout = layout;
                }

                // Space
                if (item.Space != null)
                {
                    itemLayout.Space(item.Space.Height);
                }

                // Header
                if (item.Header != null)
                {
                    itemLayout.Header(item.Header.Text);
                }

                // Peek values
                ValueContainer itemValues;
                try
                {
                    itemValues = item.GetValues(Values);
                }
                catch (Exception ex)
                {
                    Editor.LogWarning("Failed to get object values for item " + item);
                    Editor.LogWarning(ex.Message);
                    Editor.LogWarning(ex.StackTrace);
                    return;
                }

                // Spawn property editor
                SpawnProperty(itemLayout, itemValues, item);

                // Expand all parent groups if need to
                if (item.ExpandGroups)
                {
                    var c = itemLayout.ContainerControl;
                    do
                    {
                        if (c is DropPanel dropPanel)
                        {
                            dropPanel.Open(false);
                        }
                        else if (c is CustomEditorPresenter.PresenterPanel)
                        {
                            break;
                        }
                        c = c.Parent;
                    } while (c != null);
                }
            }
        }
Exemple #2
0
        /// <inheritdoc />
        public override void Initialize(LayoutElementsContainer layout)
        {
            // Collect items to edit
            List <ItemInfo> items;

            if (!HasDiffrentTypes)
            {
                var value = Values[0];
                if (value == null)
                {
                    layout.Label("<null>");
                    return;
                }

                items = GetItemsForType(value.GetType());
            }
            else
            {
                var types = ValuesTypes;
                items = new List <ItemInfo>(GetItemsForType(types[0]));
                for (int i = 1; i < types.Length && items.Count > 0; i++)
                {
                    var otherItems = GetItemsForType(types[i]);

                    // Merge items
                    for (int j = 0; j < items.Count && items.Count > 0; j++)
                    {
                        bool isInvalid = true;
                        for (int k = 0; k < otherItems.Count; k++)
                        {
                            var a = items[j];
                            var b = otherItems[k];

                            if (ItemInfo.CanMerge(a, b))
                            {
                                isInvalid = false;
                                break;
                            }
                        }

                        if (isInvalid)
                        {
                            items.RemoveAt(j--);
                        }
                    }
                }
            }

            // Sort items
            items.Sort();

            // Add items
            GroupElement lastGroup = null;

            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];

                // Check if use group
                LayoutElementsContainer itemLayout;
                if (item.UseGroup)
                {
                    if (lastGroup == null || lastGroup.Panel.Name != item.Display.Group)
                    {
                        lastGroup = layout.Group(item.Display.Group);
                    }
                    itemLayout = lastGroup;
                }
                else
                {
                    lastGroup  = null;
                    itemLayout = layout;
                }

                // Space
                if (item.Space != null)
                {
                    itemLayout.Space(item.Space.Height);
                }

                // Header
                if (item.Header != null)
                {
                    itemLayout.Header(item.Header.Text);
                }

                // Peek values
                ValueContainer itemValues;
                try
                {
                    itemValues = item.GetValues(Values);
                }
                catch (Exception ex)
                {
                    Editor.LogWarning("Failed to get object values for item " + item);
                    Editor.LogWarning(ex.Message);
                    Editor.LogWarning(ex.StackTrace);
                    return;
                }

                // Spawn property editor
                SpawnProperty(itemLayout, itemValues, item);
            }
        }