Example #1
0
        public static Object ObjectField(Rect position, GUIContent label, Object obj, Type type,
                                         bool hasMultipleDifferentValues = false)
        {
            var controlId = GUIUtility.GetControlID(s_PropertyHash, FocusType.Keyboard, position);
            var rect      = EditorGUI.PrefixLabel(position, controlId, label);
            var mousePos  = Event.current.mousePosition;
            var eventType = Event.current.GetTypeForControl(controlId);

            switch (eventType)
            {
            case EventType.Repaint:
            {
                var text = "";
                if (hasMultipleDifferentValues)
                {
                    text = "----";
                }
                else if (obj != null)
                {
                    text = obj.name;
                    if (typeof(Leaf).IsAssignableFrom(type))
                    {
                        var node = (obj as Leaf).node;
                        if (node != null)
                        {
                            text = node.name;
                        }
                    }

                    text += "(" + type.Name + ")";
                }

                var state  = (ObjectFieldState)GUIUtility.GetStateObject(typeof(ObjectFieldState), controlId);
                var active = state != null && state.active;
                EditorStyles.objectField.Draw(rect, new GUIContent(text), controlId, active);
                break;
            }

            case EventType.MouseDown:
            {
                if (!rect.Contains(mousePos) || !GUI.enabled)
                {
                    break;
                }
                EditorGUIUtility.editingTextField = false;
                GUIUtility.keyboardControl        = controlId;
                if (obj != null)
                {
                    if (typeof(Node).IsAssignableFrom(type))
                    {
                        UserDatabase.highlight.node = obj as Node;
                    }
                    else if (typeof(Leaf).IsAssignableFrom(type))
                    {
                        UserDatabase.highlight.node = (obj as Leaf).node;
                    }
                    else
                    {
                        break;
                    }
                }

                Event.current.Use();
                break;
            }

            case EventType.MouseDrag:
            {
                var state = (ObjectFieldState)GUIUtility.GetStateObject(typeof(ObjectFieldState), controlId);
                state.active = false;
                if (!rect.Contains(mousePos) || !GUI.enabled)
                {
                    break;
                }
                if (!UserDragDrop.dragging)
                {
                    break;
                }
                var data = UserDragDrop.data as Node[];
                if (data == null)
                {
                    break;
                }
                if (typeof(Node).IsAssignableFrom(type))
                {
                    if (data.Length != 1)
                    {
                        break;
                    }
                }
                else if (typeof(Leaf).IsAssignableFrom(type))
                {
                    if (data.Count(i => i.GetLeaf(type) != null) != 1)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }

                state.active = true;
                Event.current.Use();
                break;
            }

            case EventType.MouseUp:
            {
                if (!rect.Contains(mousePos) || !GUI.enabled)
                {
                    break;
                }
                if (!UserDragDrop.dragging)
                {
                    break;
                }
                var data = UserDragDrop.data as Node[];
                if (data == null)
                {
                    break;
                }
                Object target = null;
                if (typeof(Node).IsAssignableFrom(type))
                {
                    if (data.Length != 1)
                    {
                        break;
                    }
                    target = data[0];
                }
                else if (typeof(Leaf).IsAssignableFrom(type))
                {
                    var leaves = data.Select(i => i.GetLeaf(type)).Where(i => i != null);
                    if (leaves.Count() != 1)
                    {
                        break;
                    }
                    target = leaves.ElementAt(0);
                }
                else
                {
                    break;
                }

                UserDragDrop.StopDrag();
                Cursor.SetState(Cursor.State.Default);
                if (target != null && (target != obj || hasMultipleDifferentValues))
                {
                    obj         = target;
                    GUI.changed = true;
                }

                var state = (ObjectFieldState)GUIUtility.GetStateObject(typeof(ObjectFieldState), controlId);
                state.active = false;
                Event.current.Use();
                break;
            }
            }

            return(obj);
        }
Example #2
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 #3
0
        private void OnEvent()
        {
            switch (Event.current.type)
            {
            case EventType.MouseLeaveWindow:
            {
                if (Cursor.GetState() != Cursor.State.Default)
                {
                    Cursor.SetState(Cursor.State.Default);
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseMove:
            {
                if (container == null || !container.rect.Contains(Event.current.mousePosition))
                {
                    if (Cursor.GetState() != Cursor.State.Default)
                    {
                        Cursor.SetState(Cursor.State.Default);
                        Event.current.Use();
                    }
                }

                break;
            }

            case EventType.MouseDown:
            {
                Event.current.Use();
                break;
            }

            case EventType.MouseUp:
            {
                if (UserDragDrop.dragging)
                {
                    UserDragDrop.StopDrag();
                    Cursor.ResetState();
                    Event.current.Use();
                }

                break;
            }

            case EventType.MouseDrag:
            {
                if (UserDragDrop.dragging)
                {
                    if (!new Rect(0, 0, position.width, position.height).Contains(Event.current.mousePosition))
                    {
                        UserDragDrop.StopDrag();
                        Cursor.ResetState();
                    }

                    Event.current.Use();
                }

                break;
            }
            }
        }
Example #4
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;
            }
            }
        }