コード例 #1
0
        private bool DrawReorderableListFor(PanelCanvas.PanelProperties panelProperties)
        {
            isReorderableListSelected = false;
            float elementHeight = (showIDs ? 4 : 3) * EditorGUIUtility.singleLineHeight + 2;

            List <PanelCanvas.PanelTabProperties> tabs = panelProperties.tabs;

            if (reorderableLists.Count > reorderableListIndex)
            {
                reorderableLists[reorderableListIndex].list          = tabs;
                reorderableLists[reorderableListIndex].elementHeight = elementHeight;
            }
            else
            {
                ReorderableList reorderableList = new ReorderableList(tabs, typeof(PanelCanvas.PanelTabProperties), true, true, true, true)
                {
                    elementHeight = elementHeight,
                    onAddCallback = (thisList) =>
                    {
                        Undo.IncrementCurrentGroup();
                        Undo.RecordObject((PanelCanvas)target, "Add Tab");

                        ReorderableList.defaultBehaviours.DoAddButton(thisList);
                    },
                    onRemoveCallback = (thisList) =>
                    {
                        Undo.IncrementCurrentGroup();
                        Undo.RecordObject((PanelCanvas)target, "Remove Tab");

                        ReorderableList.defaultBehaviours.DoRemoveButton(thisList);
                    },
                    drawHeaderCallback = (rect) => GUI.Label(rect, "Tabs")
                };

                reorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    isReorderableListSelected |= isActive && isFocused;
                    DrawReorderableListItem(rect, index, (PanelCanvas.PanelTabProperties)reorderableList.list[index]);
                };

                reorderableLists.Add(reorderableList);
            }

            reorderableLists[reorderableListIndex].DoLayoutList();
            reorderableListIndex++;

            return(isReorderableListSelected);
        }
コード例 #2
0
        private void DrawAnchoredPanelsPreview(Rect rect, PanelCanvas.AnchoredPanelProperties props)
        {
            bool shouldDrawSelf = leaveFreeSpace.boolValue || props != settings.InitialPanelsAnchored || props.subPanels == null || props.subPanels.Count == 0;

            if (props.subPanels != null && props.subPanels.Count > 0)
            {
                int horizontal = 1, vertical = 1;
                for (int i = 0; i < props.subPanels.Count; i++)
                {
                    PanelDirection anchorDirection = props.subPanels[i].anchorDirection;
                    if (anchorDirection == PanelDirection.Left || anchorDirection == PanelDirection.Right)
                    {
                        horizontal++;
                    }
                    else
                    {
                        vertical++;
                    }
                }

                if (!shouldDrawSelf)
                {
                    PanelDirection anchorDirection = props.subPanels[props.subPanels.Count - 1].anchorDirection;
                    if (anchorDirection == PanelDirection.Left || anchorDirection == PanelDirection.Right)
                    {
                        if (horizontal > 1)
                        {
                            horizontal--;
                        }
                    }
                    else
                    {
                        if (vertical > 1)
                        {
                            vertical--;
                        }
                    }
                }

                float perWidth  = rect.width / horizontal;
                float perHeight = rect.height / vertical;
                for (int i = 0; i < props.subPanels.Count; i++)
                {
                    Rect           subRect         = new Rect(rect);
                    PanelDirection anchorDirection = props.subPanels[i].anchorDirection;
                    if (anchorDirection == PanelDirection.Left)
                    {
                        rect.x       += perWidth;
                        rect.width   -= perWidth;
                        subRect.width = perWidth;
                    }
                    else if (anchorDirection == PanelDirection.Top)
                    {
                        rect.y        += perHeight;
                        rect.height   -= perHeight;
                        subRect.height = perHeight;
                    }
                    else if (anchorDirection == PanelDirection.Right)
                    {
                        rect.width   -= perWidth;
                        subRect.width = perWidth;
                        subRect.x     = rect.xMax;
                    }
                    else
                    {
                        rect.height   -= perHeight;
                        subRect.height = perHeight;
                        subRect.y      = rect.yMax;
                    }

                    DrawAnchoredPanelsPreview(subRect, props.subPanels[i]);
                }
            }

            if (!shouldDrawSelf)
            {
                return;
            }

            string label;

            if (props == settings.InitialPanelsAnchored)
            {
                label = "Free space";
            }
            else
            {
                label = "Panel";

                List <PanelCanvas.PanelTabProperties> tabs = props.panel.tabs;
                for (int i = 0; i < tabs.Count; i++)
                {
                    if (!string.IsNullOrEmpty(tabs[i].tabLabel))
                    {
                        label = tabs[i].tabLabel;
                        break;
                    }
                }

                if (tabs.Count == 1)
                {
                    label = string.Concat(label, "\n1 tab");
                }
                else
                {
                    label = string.Concat(label, "\n", tabs.Count.ToString(), " tabs");
                }
            }

            if (selectedAnchoredPanel == props)
            {
                Color guiColor = GUI.color;
                GUI.color = Color.cyan;
                GUI.Box(rect, label, anchoredPanelGUIStyle);
                GUI.color = guiColor;
            }
            else
            {
                GUI.Box(rect, label, anchoredPanelGUIStyle);
            }

            int controlID = GUIUtility.GetControlID(FocusType.Passive);

            Event ev = Event.current;

            switch (ev.GetTypeForControl(controlID))
            {
            case EventType.MouseDown:
                if (rect.Contains(ev.mousePosition) && ev.button == 0)
                {
                    GUIUtility.hotControl    = controlID;
                    justClickedAnchoredPanel = props;
                }

                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == controlID && props != settings.InitialPanelsAnchored)
                {
                    GUIUtility.hotControl = 0;

                    // Credit: https://forum.unity.com/threads/editor-draganddrop-bug-system-needs-to-be-initialized-by-unity.219342/#post-1464056
                    DragAndDrop.PrepareStartDrag();
                    DragAndDrop.objectReferences = new Object[] { null };
                    DragAndDrop.SetGenericData("props", props);
                    DragAndDrop.StartDrag("AnchoredPanelProperties");

                    ev.Use();
                }

                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    GUIUtility.hotControl = 0;
                }

                break;

            case EventType.DragPerform:
            case EventType.DragUpdated:
                if (props != settings.InitialPanelsAnchored && rect.Contains(ev.mousePosition))
                {
                    PanelCanvas.AnchoredPanelProperties drag = DragAndDrop.GetGenericData("props") as PanelCanvas.AnchoredPanelProperties;
                    if (drag == null)
                    {
                        int      i;
                        Object[] draggedObjects = DragAndDrop.objectReferences;
                        for (i = 0; i < draggedObjects.Length; i++)
                        {
                            if (draggedObjects[i] is GameObject || draggedObjects[i] is Component)
                            {
                                break;
                            }
                        }

                        if (i == draggedObjects.Length)
                        {
                            break;
                        }
                    }

                    DragAndDrop.visualMode = DragAndDropVisualMode.Move;
                    if (ev.type == EventType.DragPerform)
                    {
                        DragAndDrop.AcceptDrag();

                        if (drag != null)
                        {
                            Undo.IncrementCurrentGroup();
                            Undo.RecordObject((PanelCanvas)target, "Swap Tabs");

                            PanelCanvas.PanelProperties temp = props.panel;
                            props.panel = drag.panel;
                            drag.panel  = temp;
                        }
                        else
                        {
                            Undo.IncrementCurrentGroup();
                            Undo.RecordObject((PanelCanvas)target, "Add Tabs");

                            Object[] draggedObjects = DragAndDrop.objectReferences;
                            for (int i = 0; i < draggedObjects.Length; i++)
                            {
                                RectTransform transform;
                                if (draggedObjects[i] is GameObject)
                                {
                                    transform = ((GameObject)draggedObjects[i]).transform as RectTransform;
                                }
                                else
                                {
                                    transform = ((Component)draggedObjects[i]).transform as RectTransform;
                                }

                                if (transform != null)
                                {
                                    if (props.panel.tabs.Find((tab) => tab.content == transform) == null)
                                    {
                                        props.panel.tabs.Add(new PanelCanvas.PanelTabProperties()
                                        {
                                            content = transform
                                        });
                                    }
                                }
                            }
                        }
                    }

                    ev.Use();
                }

                break;
            }

            if (ev.isMouse && GUIUtility.hotControl == controlID)
            {
                ev.Use();
            }
        }