Example #1
0
        public static void ShowNodeContext(Node root)
        {
            var menu  = new GenericMenu();
            var nodes = UserDatabase.selection.nodes;

            if (nodes != null && nodes.Length > 0)
            {
                menu.AddItem(new GUIContent(Locale.L_Copy), false, () =>
                {
                    UserUtil.CopyNodes(root);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent(Locale.L_Copy));
            }
            if (UserClipBoard.data != null &&
                UserClipBoard.data is Node[] &&
                ((Node[])UserClipBoard.data).Length > 0)
            {
                menu.AddItem(new GUIContent(Locale.L_Paste), false, () =>
                {
                    UserUtil.PasteNodes(root);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent(Locale.L_Paste));
            }
            menu.AddSeparator("");
            if (nodes != null && nodes.Length > 0)
            {
                menu.AddItem(new GUIContent(Locale.L_Duplicate), false, () =>
                {
                    UserUtil.DuplicateNodes(root);
                });
                menu.AddItem(new GUIContent(Locale.L_Delete), false, UserUtil.DeleteNodes);
            }
            else
            {
                menu.AddDisabledItem(new GUIContent(Locale.L_Duplicate));
                menu.AddDisabledItem(new GUIContent(Locale.L_Delete));
            }
            menu.AddSeparator("");
            menu.AddItem(new GUIContent(Locale.L_CreateEmpty), false, () =>
            {
                UserUtil.CreateControl(typeof(Node), root);
            });
            var uiTypes = new Type[] { typeof(Text), typeof(Image), typeof(Button), typeof(Toggle), typeof(TextField), typeof(Scrollbar) };

            foreach (var type in uiTypes)
            {
                var uiType = type;
                menu.AddItem(new GUIContent(Locale.L_UI + "/" + uiType.Name), false, () =>
                {
                    UserUtil.CreateControl(uiType, root);
                });
            }
            menu.ShowAsContext();
        }
Example #2
0
        public PersistentProperty[] ListChildren()
        {
            var members = new List <MemberInfo>();

            UserUtil.GetDisplayedMembersInType(members, type);
            return((from m in members
                    select new PersistentProperty(this, m.Name)).ToArray());
        }
Example #3
0
        private void SaveAs()
        {
            var path = UserUtil.SaveFileAs(root);

            if (!string.IsNullOrEmpty(path))
            {
                Command.Anchor();
                file = path;
            }
        }
Example #4
0
        protected virtual void OnTitleGUI()
        {
            var title     = new GUIContent(target.type.Name);
            var rect      = EditorGUILayout.GetControlRect(false, EditorStyles.toolbarButton.fixedHeight);
            var fsize     = EditorStyles.foldout.CalcSize(GUIContent.none);
            var frect     = new Rect(rect.x + 5, rect.y + (rect.height - fsize.y) * 0.5f, fsize.x, fsize.y);
            var tsize     = EditorStyles.toggle.CalcSize(GUIContent.none);
            var trect     = new Rect(frect.xMax, rect.y + (rect.height - tsize.y) * 0.5f - 1, tsize.x, tsize.y);
            var lheight   = EditorStyles.boldLabel.CalcHeight(title, rect.width);
            var lrect     = new Rect(trect.xMax, rect.y + (rect.height - lheight) * 0.5f, rect.width, lheight);
            var controlId = GUIUtility.GetControlID(FocusType.Passive);
            var eventType = Event.current.GetTypeForControl(controlId);

            switch (eventType)
            {
            case EventType.Repaint:
            {
                EditorStyles.toolbarButton.Draw(rect, GUIContent.none, controlId, mFoldout);
                EditorStyles.foldout.Draw(frect, GUIContent.none, controlId, mFoldout);
                break;
            }

            case EventType.MouseDown:
            {
                if (rect.Contains(Event.current.mousePosition) &&
                    !trect.Contains(Event.current.mousePosition))
                {
                    if (Event.current.button == 0)
                    {
                        mFoldout = !mFoldout;
                        Event.current.Use();
                    }
                    else if (Event.current.button == 1)
                    {
                        var menu = new GenericMenu();
                        menu.AddItem(new GUIContent(Locale.L_RemoveLeaf), false, () =>
                            {
                                var leaves = target.GetValues <Leaf>();
                                UserUtil.RemoveLeaf(leaves);
                            });
                        menu.ShowAsContext();
                        Event.current.Use();
                    }
                }
                break;
            }
            }
            if (enableDisplayed)
            {
                PersistentGUI.PropertyField(trect, GUIContent.none, target.Find("enabled"), false);
            }
            EditorGUI.LabelField(lrect, title, EditorStyles.boldLabel);
        }
Example #5
0
 private void Save()
 {
     if (!string.IsNullOrEmpty(file) && File.Exists(file))
     {
         UserUtil.SaveFile(root, file);
         Command.Anchor();
     }
     else
     {
         SaveAs();
     }
 }
Example #6
0
        private void Open()
        {
            Node   node;
            string path;

            UserUtil.LoadFile(out node, out path);
            if (node != null)
            {
                ClearCache();
                root = node;
                file = path;
                if (container != null)
                {
                    container.SetRoot(root);
                }
            }
        }
Example #7
0
        private void OnPreEvent()
        {
            var controlId = GUIUtility.GetControlID(FocusType.Keyboard);
            var eventType = Event.current.GetTypeForControl(controlId);
            var mousePos  = Event.current.mousePosition;

            switch (eventType)
            {
            case EventType.MouseUp:
            {
                if (focused && nativeRect.Contains(mousePos) && mTemps != null)
                {
                    UserUtil.SelectNodes(mTemps);
                    mTemps = null;
                }

                break;
            }
            }
        }
Example #8
0
        protected override void OnGUI()
        {
            if (target == null)
            {
                EditorGUI.LabelField(nativeRect, Locale.L_Inspector, UserSetting.FrameTipsLabelStyle);
                return;
            }

            PersistentGUI.BeginLabelWidth(100);
            var brect   = EditorGUILayout.GetControlRect(false, EditorStyles.toolbarButton.fixedHeight);
            var tsize   = EditorStyles.toggle.CalcSize(GUIContent.none);
            var trect   = new Rect(brect.x + 5, brect.y + (brect.height - tsize.y) * 0.5f - 1, tsize.x, tsize.y);
            var lheight = EditorStyles.textField.CalcHeight(GUIContent.none, brect.width);
            var lrect   = new Rect(trect.xMax, brect.y + (brect.height - lheight) * 0.5f, brect.width - trect.xMax,
                                   lheight);

            PersistentGUI.PropertyField(trect, GUIContent.none, target.Find("enabled"));
            PersistentGUI.PropertyField(lrect, GUIContent.none, target.Find("name"));

            var nodes = target.GetValues <Node>();

            if (nodes.Length > 1)
            {
                PersistentGUILayout.PropertyField(target.Find("anchoredPosition"));
                PersistentGUILayout.PropertyField(target.Find("size"));
            }
            else
            {
                var node      = nodes[0];
                var anchorMin = node.anchorMin;
                var anchorMax = node.anchorMax;
                var labels    = new GUIContent[4];
                var props     = new PersistentProperty[4];
                if (anchorMin.x == anchorMax.x)
                {
                    labels[0] = new GUIContent("Pos X");
                    props[0]  = target.Find("anchoredPosition.x");
                    labels[2] = new GUIContent("Width");
                    props[2]  = target.Find("size.x");
                }
                else
                {
                    labels[0] = new GUIContent("Left");
                    props[0]  = target.Find("offsetMin.x");
                    labels[2] = new GUIContent("Right");
                    props[2]  = target.Find("offsetMax.x");
                }

                if (anchorMin.y == anchorMax.y)
                {
                    labels[1] = new GUIContent("Pos Y");
                    props[1]  = target.Find("anchoredPosition.y");
                    labels[3] = new GUIContent("Height");
                    props[3]  = target.Find("size.y");
                }
                else
                {
                    labels[1] = new GUIContent("Top");
                    props[1]  = target.Find("offsetMin.y");
                    labels[3] = new GUIContent("Bottom");
                    props[3]  = target.Find("offsetMax.y");
                }

                var position = EditorGUILayout.GetControlRect(false,
                                                              EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 2 : 3));
                position.height = EditorGUIUtility.singleLineHeight;
                PersistentGUI.MultiPropertyField2x2(position, new GUIContent("Position"), labels, props, 50);
            }

            PersistentGUILayout.PropertyField(target.Find("anchorMin"));
            PersistentGUILayout.PropertyField(target.Find("anchorMax"));
            PersistentGUILayout.PropertyField(target.Find("pivot"));
            PersistentGUILayout.FloatSlider(target.Find("localAngle"), 0f, 360f);
            PersistentGUILayout.PropertyField(target.Find("localScale"));

            var leafTypes = new List <Type>();

            foreach (var node in nodes)
            {
                var leaves = node.GetAllLeaves();
                foreach (var leaf in leaves)
                {
                    var leafType = leaf.GetType();
                    if (!leafTypes.Contains(leafType))
                    {
                        leafTypes.Add(leafType);
                    }
                }
            }

            leafTypes = leafTypes.FindAll(t => nodes.All(n => n.GetLeaf(t) != null));
            leafTypes.Sort((t1, t2) => String.Compare(t1.Name, t2.Name, StringComparison.Ordinal));
            foreach (var leafType in leafTypes)
            {
                var leaves = Array.ConvertAll(nodes, n => n.GetLeaf(leafType));
                var obj    = new PersistentObject(leaves);
                PersistentGUILayout.UserDrawerLayout(obj);
                EditorGUILayout.Space();
            }

            if (nodes.Length > 1 && nodes.Any(n => n.GetAllLeaves(true).Count() != leafTypes.Count))
            {
                EditorGUILayout.Separator();
                PersistentGUI.BeginColor(Color.yellow);
                EditorGUILayout.LabelField(Locale.L_MultiEditLeavesTips);
                PersistentGUI.EndColor();
            }

            EditorGUILayout.Separator();
            if (GUILayout.Button(Locale.L_AddLeaf, GUILayout.MaxWidth(100)))
            {
                var allTypes = CoreUtil.FindSubTypes(typeof(Leaf));
                Array.Sort(allTypes, (t1, t2) => String.Compare(t1.Name, t2.Name, StringComparison.Ordinal));
                var menu = new GenericMenu();
                foreach (var type in allTypes)
                {
                    var leafType = type;
                    menu.AddItem(new GUIContent(leafType.Name), false, () => { UserUtil.AddLeaf(nodes, leafType); });
                }

                menu.ShowAsContext();
            }

            PersistentGUI.EndLabelWidth();
        }
Example #9
0
        private void OnCommonEvent()
        {
            var controlId = GUIUtility.GetControlID(FocusType.Keyboard);
            var eventType = Event.current.GetTypeForControl(controlId);

            switch (eventType)
            {
            case EventType.MouseMove:
            {
                if (!nativeRect.Contains(Event.current.mousePosition))
                {
                    break;
                }
                var selectedNodes = UserDatabase.selection.nodes;
                if (selectedNodes == null || selectedNodes.Length == 0)
                {
                    break;
                }
                var flag = false;
                foreach (var selectedNode in selectedNodes)
                {
                    PersistentGUI.BeginMatrix(GUI.matrix * selectedNode.guiMatrix);
                    var mousePos  = Event.current.mousePosition;
                    var localRect = selectedNode.localRect;
                    var scale     = selectedNode.worldScale;
                    scale.x = Mathf.Max(scale.x, 0.01f);
                    scale.y = Mathf.Max(scale.y, 0.01f);
                    var dragRects = CalcDragSizeRect(localRect, 4f / scale.x, 4f / scale.y);
                    var dir       = Array.FindIndex(dragRects, r => r.Contains(mousePos));
                    switch (dir)
                    {
                    case Direction.Top:
                    case Direction.Bottom:
                    {
                        Cursor.SetState(Cursor.State.Vertical);
                        Event.current.Use();
                        break;
                    }

                    case Direction.Left:
                    case Direction.Right:
                    {
                        Cursor.SetState(Cursor.State.Horizontal);
                        Event.current.Use();
                        break;
                    }

                    case Direction.TopRight:
                    case Direction.BottomLeft:
                    {
                        Cursor.SetState(Cursor.State.Diagonal1);
                        Event.current.Use();
                        break;
                    }

                    case Direction.TopLeft:
                    case Direction.BottomRight:
                    {
                        Cursor.SetState(Cursor.State.Diagonal2);
                        Event.current.Use();
                        break;
                    }
                    }

                    PersistentGUI.EndMatrix();
                    if (dir >= 0)
                    {
                        Cursor.matrix = Node.BuildGUIRotationMatrix(selectedNode);
                        flag          = true;
                        break;
                    }
                }

                if (!flag)
                {
                    Cursor.SetState(Cursor.State.Default);
                    Cursor.matrix = Matrix4x4.identity;
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseDown:
            {
                if (!nativeRect.Contains(Event.current.mousePosition))
                {
                    break;
                }
                if (Event.current.button != 0)
                {
                    break;
                }
                GUIUtility.hotControl = controlId;
                var dragState = (DragState)GUIUtility.GetStateObject(typeof(DragState), controlId);
                dragState.dir      = -1;
                dragState.mousePos = Event.current.mousePosition;
                var selectedNodes = UserDatabase.selection.nodes;
                if (selectedNodes == null || selectedNodes.Length == 0)
                {
                    break;
                }
                foreach (var selectedNode in selectedNodes)
                {
                    PersistentGUI.BeginMatrix(GUI.matrix * selectedNode.guiMatrix);
                    var mousePos  = Event.current.mousePosition;
                    var localRect = selectedNode.localRect;
                    var scale     = selectedNode.worldScale;
                    scale.x = Mathf.Max(scale.x, 0.01f);
                    scale.y = Mathf.Max(scale.y, 0.01f);
                    var dragRects = CalcDragSizeRect(localRect, 4f / scale.x, 4f / scale.y);
                    var dir       = Array.FindIndex(dragRects, r => r.Contains(mousePos));
                    if (dir >= 0 || localRect.Contains(mousePos))
                    {
                        dragState.dir           = dir;
                        dragState.localPosition = selectedNodes.Select(i => i.localPosition).ToArray();
                        dragState.size          = selectedNodes.Select(i => i.size).ToArray();
                        break;
                    }

                    PersistentGUI.EndMatrix();
                }

                break;
            }

            case EventType.MouseDrag:
            {
                if (!nativeRect.Contains(Event.current.mousePosition))
                {
                    break;
                }
                if (Event.current.button != 0 || GUIUtility.hotControl != controlId)
                {
                    break;
                }
                var selectedNodes = UserUtil.FilterNested(UserDatabase.selection.nodes);
                if (selectedNodes == null || selectedNodes.Length == 0)
                {
                    break;
                }
                foreach (var selectedNode in selectedNodes)
                {
                    PersistentGUI.BeginMatrix(GUI.matrix * selectedNode.guiMatrix);
                    var delta = Event.current.delta;
                    var scale = selectedNode.localScale;
                    scale.x = Mathf.Abs(scale.x);
                    scale.y = Mathf.Abs(scale.y);
                    var pivot = selectedNode.pivot;
                    selectedNode.pivot = Vector2.zero;
                    var deltaMatrix = Matrix4x4.TRS(Vector3.zero,
                                                    Quaternion.Euler(0, 0, selectedNode.localAngle), scale);
                    var dragState = (DragState)GUIUtility.GetStateObject(typeof(DragState), controlId);
                    switch (dragState.dir)
                    {
                    case Direction.Top:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(0, delta.y));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        selectedNode.size          += new Vector2(0, -delta.y);
                        break;
                    }

                    case Direction.Bottom:
                    {
                        selectedNode.size += new Vector2(0, delta.y);
                        break;
                    }

                    case Direction.Left:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(delta.x, 0));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        selectedNode.size          += new Vector2(-delta.x, 0);
                        break;
                    }

                    case Direction.Right:
                    {
                        selectedNode.size += new Vector2(delta.x, 0);
                        break;
                    }

                    case Direction.TopLeft:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(delta.x, delta.y));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        selectedNode.size          += new Vector2(-delta.x, -delta.y);
                        break;
                    }

                    case Direction.TopRight:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(0, delta.y));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        selectedNode.size          += new Vector2(delta.x, -delta.y);
                        break;
                    }

                    case Direction.BottomRight:
                    {
                        selectedNode.size += new Vector2(delta.x, delta.y);
                        break;
                    }

                    case Direction.BottomLeft:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(delta.x, 0));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        selectedNode.size          += new Vector2(-delta.x, delta.y);
                        break;
                    }

                    default:
                    {
                        var offset = deltaMatrix.MultiplyVector(new Vector2(delta.x, delta.y));
                        selectedNode.localPosition += new Vector2(offset.x, offset.y);
                        break;
                    }
                    }

                    selectedNode.pivot = pivot;
                    PersistentGUI.EndMatrix();
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseUp:
            {
                if (!nativeRect.Contains(Event.current.mousePosition))
                {
                    break;
                }
                if (Event.current.button == 1)
                {
                    UserMenu.ShowNodeContext(root);
                    Event.current.Use();
                }

                if (GUIUtility.hotControl != controlId)
                {
                    break;
                }
                var dragState = (DragState)GUIUtility.GetStateObject(typeof(DragState), controlId);
                if (Event.current.button == 0)
                {
                    var selectedNodes = UserDatabase.selection.nodes;
                    if (selectedNodes != null && selectedNodes.Length > 0 && dragState.localPosition != null &&
                        selectedNodes.Length == dragState.localPosition.Length)
                    {
                        var commands = new List <Command>();
                        for (var i = 0; i < selectedNodes.Length; i++)
                        {
                            var selectedNode = selectedNodes[i];
                            PersistentGUI.BeginMatrix(GUI.matrix * selectedNode.guiMatrix);
                            var localPosition        = dragState.localPosition[i];
                            var size                 = dragState.size[i];
                            var currentLocalPosition = selectedNode.localPosition;
                            var currentSize          = selectedNode.size;
                            if (Vector2.Distance(localPosition, currentLocalPosition) >
                                UserSetting.DistanceComparisionTolerance ||
                                Vector2.Distance(size, currentSize) > UserSetting.DistanceComparisionTolerance)
                            {
                                selectedNode.localPosition = localPosition;
                                selectedNode.size          = size;
                                commands.Add(new UpdateMemberCommand(selectedNode, "localPosition",
                                                                     currentLocalPosition));
                                commands.Add(new UpdateMemberCommand(selectedNode, "size", currentSize));
                            }

                            PersistentGUI.EndMatrix();
                        }

                        if (commands.Count > 0)
                        {
                            Command.Execute(new CombinedCommand(commands.ToArray()));
                            Event.current.Use();
                        }
                    }
                }

                var mousePos = Event.current.mousePosition;
                if (Event.current.button == 0 &&
                    Vector2.Distance(dragState.mousePos, mousePos) <
                    UserSetting.DistanceComparisionTolerance)
                {
                    var hits = Raycaster.RaycastAll(mousePos, root);
                    if (hits.Length > 0)
                    {
                        var current = UserDatabase.selection.node;
                        if (current == null)
                        {
                            UserUtil.SelectNodes(new[] { hits[0] });
                            Event.current.Use();
                        }
                        else
                        {
                            var hold  = hits[0];
                            var nodes = CoreUtil.CopyArray(UserDatabase.selection.nodes);
                            foreach (var node in nodes)
                            {
                                var index = Array.IndexOf(hits, node);
                                if (index >= 0)
                                {
                                    ArrayUtility.Remove(ref nodes, node);
                                    var next = index + 1;
                                    if (next >= hits.Length)
                                    {
                                        next = 0;
                                    }
                                    hold = hits[next];
                                    break;
                                }
                            }

                            if (Event.current.control)
                            {
                                if (nodes.Length > 0)
                                {
                                    ArrayUtility.Add(ref nodes, hold);
                                }
                            }
                            else
                            {
                                nodes = new Node[] { hold };
                            }

                            UserUtil.SelectNodes(nodes);
                            Event.current.Use();
                        }
                    }
                    else
                    {
                        UserUtil.SelectNodes(null);
                        Event.current.Use();
                    }
                }

                GUIUtility.hotControl = 0;

                break;
            }

            case EventType.KeyDown:
            {
                if (!focused)
                {
                    break;
                }
                switch (Event.current.keyCode)
                {
                case KeyCode.Delete:
                {
                    UserUtil.DeleteNodes();
                    Event.current.Use();
                    break;
                }

                case KeyCode.LeftArrow:
                case KeyCode.RightArrow:
                case KeyCode.UpArrow:
                case KeyCode.DownArrow:
                {
                    var selectedNodes = UserUtil.FilterNested(UserDatabase.selection.nodes);
                    if (selectedNodes == null || selectedNodes.Length == 0)
                    {
                        break;
                    }
                    foreach (var selectedNode in selectedNodes)
                    {
                        var distance = 1;
                        if (Event.current.control)
                        {
                            distance = 5;
                        }
                        else if (Event.current.shift)
                        {
                            distance = 10;
                        }
                        switch (Event.current.keyCode)
                        {
                        case KeyCode.LeftArrow:
                        {
                            Command.Execute(new UpdateMemberCommand(selectedNode, "localPosition",
                                                                    selectedNode.localPosition + new Vector2(-distance, 0)));
                            Event.current.Use();
                            break;
                        }

                        case KeyCode.UpArrow:
                        {
                            Command.Execute(new UpdateMemberCommand(selectedNode, "localPosition",
                                                                    selectedNode.localPosition + new Vector2(0, -distance)));
                            Event.current.Use();
                            break;
                        }

                        case KeyCode.RightArrow:
                        {
                            Command.Execute(new UpdateMemberCommand(selectedNode, "localPosition",
                                                                    selectedNode.localPosition + new Vector2(distance, 0)));
                            Event.current.Use();
                            break;
                        }

                        case KeyCode.DownArrow:
                        {
                            Command.Execute(new UpdateMemberCommand(selectedNode, "localPosition",
                                                                    selectedNode.localPosition + new Vector2(0, distance)));
                            Event.current.Use();
                            break;
                        }
                        }
                    }

                    break;
                }
                }

                break;
            }

            case EventType.ValidateCommand:
            {
                if (!focused)
                {
                    break;
                }
                if (Event.current.commandName == "Copy" ||
                    Event.current.commandName == "Paste" ||
                    Event.current.commandName == "Duplicate")
                {
                    Event.current.Use();
                }
                break;
            }

            case EventType.ExecuteCommand:
            {
                if (!focused)
                {
                    break;
                }
                switch (Event.current.commandName)
                {
                case "Copy":
                {
                    UserUtil.CopyNodes(root);
                    Event.current.Use();
                    break;
                }

                case "Paste":
                {
                    UserUtil.PasteNodes(root);
                    Event.current.Use();
                    break;
                }

                case "Duplicate":
                {
                    UserUtil.DuplicateNodes(root);
                    Event.current.Use();
                    break;
                }
                }

                break;
            }
            }
        }
Example #10
0
        private void OnNodeGUI(Node node)
        {
            var nodeRect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);

            nodeRect.height += EditorGUIUtility.standardVerticalSpacing;
            var selected = selectedNodes != null && selectedNodes.Contains(node);

            if (selected)
            {
                EditorGUI.DrawRect(nodeRect,
                                   focused ? UserSetting.HierarchySelectedFocusedColor : UserSetting.HierarchySelectedColor);
            }
            var controlId  = GUIUtility.GetControlID(FocusType.Keyboard);
            var eventType  = Event.current.GetTypeForControl(controlId);
            var mousePos   = Event.current.mousePosition;
            var foldout    = UserDatabase.caches.GetHierarchyFoldout(node);
            var topRect    = new Rect(nodeRect.x, nodeRect.y - 2, nodeRect.width, 4);
            var bottomRect = new Rect(nodeRect.x, nodeRect.yMax - 1, nodeRect.width, 4);
            var centerRect = new Rect(nodeRect.x, nodeRect.y + 2, nodeRect.width, nodeRect.height - 4);
            var fsize      = EditorStyles.foldout.CalcSize(GUIContent.none);
            var frect      = new Rect(nodeRect.x + PersistentGUI.indent, nodeRect.y, fsize.x, fsize.y);

            switch (eventType)
            {
            case EventType.Repaint:
            {
                if (node.childCount > 0)
                {
                    EditorStyles.foldout.Draw(frect, GUIContent.none, controlId, foldout);
                }
                if (UserDatabase.highlight.node == node)
                {
                    if (mHighlightProgress == 0)
                    {
                        UserDatabase.highlight.node = null;
                    }
                    else
                    {
                        var color = UserSetting.HierarchyHighlightColor;
                        color.a = (float)mHighlightProgress / mHighlightDuration;
                        PersistentGUI.DrawAAPolyLine(nodeRect, 2, color);
                    }
                    mHighlightProgress -= 1;
                }
                break;
            }

            case EventType.MouseDown:
            {
                if (node.childCount > 0 &&
                    frect.Contains(mousePos))
                {
                    foldout = !foldout;
                    UserDatabase.caches.SetHierarchyFoldout(node, foldout);
                    Event.current.Use();
                }
                else if (nodeRect.Contains(mousePos) &&
                         (Event.current.button == 0 ||
                          Event.current.button == 1))
                {
                    var nodes = UserDatabase.selection.nodes != null
                            ? CoreUtil.CopyArray(UserDatabase.selection.nodes)
                            : new Node[0];

                    if (Event.current.control)
                    {
                        if (selected)
                        {
                            ArrayUtility.Remove(ref nodes, node);
                        }
                        else
                        {
                            ArrayUtility.Add(ref nodes, node);
                            var list = GUIUtility.GetStateObject(typeof(List <Node>), controlId) as List <Node>;
                            list.Add(node);
                        }

                        mTemps = nodes;
                    }
                    else if (Event.current.shift && nodes.Length > 0)
                    {
                        var filter = new List <Node>();
                        var list   = new List <Node>();
                        foreach (var n in root)
                        {
                            FlattenNode(list, n);
                        }
                        var index  = list.IndexOf(node);
                        var nIndex = nodes.Select(n => list.IndexOf(n)).Min();
                        var from   = Mathf.Min(nIndex, index);
                        var to     = Mathf.Max(nIndex, index);
                        for (var i = @from; i <= to; i++)
                        {
                            if (!filter.Contains(list[i]))
                            {
                                filter.Add(list[i]);
                            }
                        }

                        mTemps = filter.ToArray();
                    }
                    else
                    {
                        if (!selected)
                        {
                            mTemps = new[] { node }
                        }
                        ;
                        if (node.childCount > 0 && Event.current.clickCount == 2)
                        {
                            foldout = !foldout;
                            UserDatabase.caches.SetHierarchyFoldout(node, foldout);
                        }
                    }

                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseUp:
            {
                if (!focused || frect.Contains(mousePos))
                {
                    break;
                }
                if (UserDragDrop.dragging)
                {
                    var nodes = UserDragDrop.data as Node[];
                    if (nodes != null && nodes.Length > 0)
                    {
                        if (topRect.Contains(mousePos))
                        {
                            UserUtil.MoveNodes(nodes, node.parent, node.GetSiblingIndex());
                            UserDragDrop.StopDrag();
                            Cursor.SetState(Cursor.State.Default);
                            UserDatabase.caches.SetHierarchyFoldout(node.parent, true);
                            Event.current.Use();
                        }
                        else if (bottomRect.Contains(mousePos))
                        {
                            UserUtil.MoveNodes(nodes, node.parent, node.GetSiblingIndex() + 1);
                            UserDragDrop.StopDrag();
                            Cursor.SetState(Cursor.State.Default);
                            UserDatabase.caches.SetHierarchyFoldout(node.parent, true);
                            Event.current.Use();
                        }
                        else if (centerRect.Contains(mousePos))
                        {
                            UserUtil.MoveNodes(nodes, node, -1);
                            UserDragDrop.StopDrag();
                            Cursor.SetState(Cursor.State.Default);
                            UserDatabase.caches.SetHierarchyFoldout(node, true);
                            Event.current.Use();
                        }
                    }
                }
                else
                {
                    if (Event.current.button == 0 && nodeRect.Contains(mousePos))
                    {
                        var nodes = UserDatabase.selection.nodes != null
                                ? CoreUtil.CopyArray(UserDatabase.selection.nodes)
                                : new Node[0];

                        if (Event.current.control)
                        {
                            if (selected)
                            {
                                var list =
                                    GUIUtility.QueryStateObject(typeof(List <Node>), controlId) as List <Node>;
                                if (list != null && list.Count > 0)
                                {
                                    if (list[0] != node)
                                    {
                                        ArrayUtility.Remove(ref nodes, node);
                                        UserUtil.SelectNodes(nodes);
                                    }

                                    list.Clear();
                                }
                            }
                        }
                        else if (!Event.current.shift)
                        {
                            if (selected)
                            {
                                UserUtil.SelectNodes(new[] { node });
                            }
                        }

                        if (Event.current.button != 1)
                        {
                            Event.current.Use();
                        }
                    }
                }

                break;
            }

            case EventType.MouseDrag:
            {
                if (focused && Event.current.button == 0 && !UserDragDrop.dragging)
                {
                    var nodes = selectedNodes;
                    if (nodes != null && nodes.Contains(node) && nodeRect.Contains(mousePos))
                    {
                        var data = new Node[nodes.Length];
                        Array.Copy(nodes, data, data.Length);
                        UserDragDrop.StartDrag(data);
                        Cursor.SetState(Cursor.State.DragAnything);
                        Event.current.Use();
                    }
                }

                break;
            }
            }

            EditorGUI.LabelField(new Rect(nodeRect.x + 12, nodeRect.y, nodeRect.width, nodeRect.height),
                                 new GUIContent(node.name));
            if (UserDragDrop.dragging)
            {
                var indentOffset = PersistentGUI.indent + 12;
                if (topRect.Contains(mousePos))
                {
                    PersistentGUI.DrawAAPolyLine(
                        new Rect(topRect.x + indentOffset, topRect.y, topRect.width - indentOffset, topRect.height), 2,
                        UserSetting.HierarchyDragTipsColor);
                }
                else if (centerRect.Contains(mousePos))
                {
                    PersistentGUI.DrawAAPolyLine(
                        new Rect(centerRect.x + indentOffset, centerRect.y, centerRect.width - indentOffset,
                                 centerRect.height), 2, UserSetting.HierarchyDragTipsColor);
                }
            }

            if (foldout && node.childCount > 0)
            {
                EditorGUI.indentLevel += 1;
                for (int i = 0; i < node.childCount; i++)
                {
                    OnNodeGUI(node.GetChild(i));
                }

                EditorGUI.indentLevel -= 1;
            }
            else
            {
                if (UserDragDrop.dragging)
                {
                    if (bottomRect.Contains(mousePos))
                    {
                        var indentOffset = PersistentGUI.indent + 12;
                        PersistentGUI.DrawAAPolyLine(
                            new Rect(bottomRect.x + indentOffset, bottomRect.y, bottomRect.width - indentOffset,
                                     bottomRect.height), 2, UserSetting.HierarchyDragTipsColor);
                    }
                }
            }
        }
Example #11
0
        private void OnPostEvent()
        {
            var controlId = GUIUtility.GetControlID(FocusType.Keyboard);
            var eventType = Event.current.GetTypeForControl(controlId);
            var mousePos  = Event.current.mousePosition;

            switch (eventType)
            {
            case EventType.MouseDrag:
            {
                if (!nativeRect.Contains(mousePos) && mTemps != null)
                {
                    mTemps = null;
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseDown:
            {
                if (Event.current.button == 0 &&
                    nativeRect.Contains(mousePos))
                {
                    UserUtil.SelectNodes(null);
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseUp:
            {
                if (nativeRect.Contains(mousePos))
                {
                    if (Event.current.button == 0)
                    {
                        if (UserDragDrop.dragging)
                        {
                            var nodes = UserDragDrop.data as Node[];
                            if (nodes != null && nodes.Length > 0)
                            {
                                UserUtil.MoveNodes(nodes, root, root.childCount);
                                UserDragDrop.StopDrag();
                                Cursor.SetState(Cursor.State.Default);
                                Event.current.Use();
                            }
                        }
                    }
                    else if (Event.current.button == 1)
                    {
                        UserMenu.ShowNodeContext(root);
                        Event.current.Use();
                    }
                }

                break;
            }

            case EventType.KeyDown:
            {
                if (!focused)
                {
                    break;
                }
                switch (Event.current.keyCode)
                {
                case KeyCode.Delete:
                {
                    UserUtil.DeleteNodes();
                    Event.current.Use();
                    break;
                }

                case KeyCode.UpArrow:
                case KeyCode.DownArrow:
                case KeyCode.LeftArrow:
                case KeyCode.RightArrow:
                {
                    var node = UserDatabase.selection.node;
                    if (node != null)
                    {
                        List <Node> list = new List <Node>();
                        foreach (var n in root)
                        {
                            FlattenNode(list, n);
                        }
                        var index = list.IndexOf(node);
                        switch (Event.current.keyCode)
                        {
                        case KeyCode.UpArrow:
                        {
                            if (index > 0)
                            {
                                UserUtil.SelectNodes(new Node[] { list[index - 1] });
                            }
                            break;
                        }

                        case KeyCode.DownArrow:
                        {
                            if (index < list.Count - 1)
                            {
                                UserUtil.SelectNodes(new Node[] { list[index + 1] });
                            }
                            break;
                        }

                        case KeyCode.LeftArrow:
                        {
                            if (node.childCount > 0)
                            {
                                UserDatabase.caches.SetHierarchyFoldout(node, false);
                            }
                            break;
                        }

                        case KeyCode.RightArrow:
                        {
                            if (node.childCount > 0)
                            {
                                UserDatabase.caches.SetHierarchyFoldout(node, true);
                            }
                            break;
                        }
                        }

                        Event.current.Use();
                    }

                    break;
                }
                }

                break;
            }

            case EventType.ValidateCommand:
            {
                if (!focused)
                {
                    break;
                }
                if (Event.current.commandName == "Copy" ||
                    Event.current.commandName == "Paste" ||
                    Event.current.commandName == "Duplicate" ||
                    Event.current.commandName == "SelectAll")
                {
                    Event.current.Use();
                }
                break;
            }

            case EventType.ExecuteCommand:
            {
                if (!focused)
                {
                    break;
                }
                switch (Event.current.commandName)
                {
                case "Copy":
                {
                    UserUtil.CopyNodes(root);
                    Event.current.Use();
                    break;
                }

                case "Paste":
                {
                    UserUtil.PasteNodes(root);
                    Event.current.Use();
                    break;
                }

                case "Duplicate":
                {
                    UserUtil.DuplicateNodes(root);
                    Event.current.Use();
                    break;
                }

                case "SelectAll":
                {
                    var list = new List <Node>();
                    foreach (var n in root)
                    {
                        FlattenNode(list, n);
                    }
                    UserUtil.SelectNodes(list.ToArray());
                    Event.current.Use();
                    break;
                }
                }

                break;
            }
            }
        }