示例#1
0
        private void DrawEnumItem(OdinMenuItem obj)
        {
            if (Event.current.type == EventType.MouseDown && obj.Rect.Contains(Event.current.mousePosition))
            {
                obj.Select();
                Event.current.Use();
                wasMouseDown = true;
            }

            if (wasMouseDown)
            {
                GUIHelper.RequestRepaint();
            }

            if (wasMouseDown == true && Event.current.type == EventType.MouseDrag && obj.Rect.Contains(Event.current.mousePosition))
            {
                obj.Select();
            }

            if (Event.current.type == EventType.MouseUp)
            {
                wasMouseDown = false;
                if (obj.IsSelected && obj.Rect.Contains(Event.current.mousePosition))
                {
                    obj.MenuTree.Selection.ConfirmSelection();
                }
            }
        }
        private void EnableSingleClickToSelect(OdinMenuItem obj)
        {
            var t = Event.current.type;

            if (t == EventType.Layout)
            {
                return;
            }

            if (obj.Rect.Contains(Event.current.mousePosition) == false)
            {
                return;
            }

            GUIHelper.RequestRepaint();

            if (Event.current.type == EventType.MouseDrag)
            {
                if (obj is T)
                {
                    if (this.IsValidSelection(Enumerable.Repeat((T)obj.Value, 1)))
                    {
                        obj.Select();
                    }
                }
            }

            bool confirm = t == EventType.MouseUp && (obj.ChildMenuItems.Count == 0);

            if (confirm)
            {
                obj.MenuTree.Selection.ConfirmSelection();
                Event.current.Use();
            }
        }
        /// <summary>
        /// Handles the keybaord menu navigation. Call this at the end of your GUI scope, to prevent the menu tree from stealing input events from text fields and such.
        /// </summary>
        /// <returns>Returns true, if anything was changed via the keyboard.</returns>
        public bool HandleKeybaordMenuNavigation()
        {
            if (Event.current.type != EventType.KeyDown)
            {
                return(false);
            }

            GUIHelper.RequestRepaint();

            if (this.Selection.Count == 0)
            {
                OdinMenuItem next = null;
                if (Event.current.keyCode == KeyCode.DownArrow)
                {
                    next = this.MenuItems.FirstOrDefault();
                }
                else if (Event.current.keyCode == KeyCode.UpArrow)
                {
                    next = this.MenuItems.LastOrDefault();
                }
                else if (Event.current.keyCode == KeyCode.LeftAlt)
                {
                    next = this.MenuItems.FirstOrDefault();
                }
                else if (Event.current.keyCode == KeyCode.RightAlt)
                {
                    next = this.MenuItems.FirstOrDefault();
                }

                if (next != null)
                {
                    Event.current.Use();
                    next.Select();
                    return(true);
                }
            }
            else
            {
                if (Event.current.keyCode == KeyCode.RightArrow)
                {
                    foreach (var curr in this.Selection.ToList())
                    {
                        curr.Toggled = true;

                        if ((Event.current.modifiers & EventModifiers.Alt) != 0)
                        {
                            foreach (var item in curr.GetChildMenuItemsRecursive(false))
                            {
                                item.Toggled = curr.Toggled;
                            }
                        }
                    }

                    Event.current.Use();
                }
                else if (Event.current.keyCode == KeyCode.LeftArrow)
                {
                    foreach (var curr in this.Selection.ToList())
                    {
                        if (curr.Parent != null && (!curr.ChildMenuItems.Any() || curr.Parent.Toggled == false))
                        {
                            curr.Parent.Select();
                            return(true);
                        }
                        else
                        {
                            curr.Toggled = false;

                            if ((Event.current.modifiers & EventModifiers.Alt) != 0)
                            {
                                foreach (var item in curr.GetChildMenuItemsRecursive(false))
                                {
                                    item.Toggled = curr.Toggled;
                                }
                            }
                        }
                    }

                    Event.current.Use();
                }
                else if (Event.current.keyCode == KeyCode.UpArrow)
                {
                    if ((Event.current.modifiers & EventModifiers.Shift) != 0)
                    {
                        var last = this.Selection.Last();
                        var prev = last.PrevVisualMenuItem;

                        if (prev != null)
                        {
                            if (prev.IsSelected)
                            {
                                last.Deselect();
                                return(true);
                            }
                            else
                            {
                                prev.Select(true);
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        var prev = this.Selection.Last().PrevVisualMenuItem;
                        if (prev != null)
                        {
                            prev.Select();
                            return(true);
                        }
                    }

                    Event.current.Use();
                }
                else if (Event.current.keyCode == KeyCode.DownArrow)
                {
                    if ((Event.current.modifiers & EventModifiers.Shift) != 0)
                    {
                        var last = this.Selection.Last();
                        var next = last.NextVisualMenuItem;

                        if (next != null)
                        {
                            if (next.IsSelected)
                            {
                                last.Deselect();
                                return(true);
                            }
                            else
                            {
                                next.Select(true);
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        var next = this.Selection.Last().NextVisualMenuItem;
                        if (next != null)
                        {
                            next.Select();
                            return(true);
                        }
                    }

                    Event.current.Use();
                }
            }

            return(false);
        }
示例#4
0
        /// <summary>
        /// Handles the keybaord menu navigation. Call this at the end of your GUI scope, to prevent the menu tree from stealing input events from other text fields.
        /// </summary>
        /// <returns>Returns true, if anything was changed via the keyboard.</returns>
        public bool HandleKeybaordMenuNavigation()
        {
            if (Event.current.type != EventType.KeyDown)
            {
                return(false);
            }

            if (OdinMenuTree.ActiveMenuTree != this)
            {
                return(false);
            }

            GUIHelper.RequestRepaint();

            var keycode = Event.current.keyCode;

            // Select first or last if no visisble items is slected.
            if (this.Selection.Count == 0 || !this.Selection.Any(x => x._IsVisible()))
            {
                var query = this.DrawInSearchMode ? this.FlatMenuTree : this.EnumerateTree().Where(x => x._IsVisible());

                OdinMenuItem next = null;
                if (keycode == KeyCode.DownArrow)
                {
                    next = query.FirstOrDefault();
                }
                else if (keycode == KeyCode.UpArrow)
                {
                    next = query.LastOrDefault();
                }
                else if (keycode == KeyCode.LeftAlt)
                {
                    next = query.FirstOrDefault();
                }
                else if (keycode == KeyCode.RightAlt)
                {
                    next = query.FirstOrDefault();
                }

                if (next != null)
                {
                    next.Select();
                    Event.current.Use();
                    return(true);
                }
            }
            else
            {
                if (keycode == KeyCode.LeftArrow && !this.DrawInSearchMode)
                {
                    bool goUp = true;
                    foreach (var curr in this.Selection.ToList())
                    {
                        if (curr.Toggled == true && curr.ChildMenuItems.Any())
                        {
                            goUp         = false;
                            curr.Toggled = false;
                        }

                        if ((Event.current.modifiers & EventModifiers.Alt) != 0)
                        {
                            goUp = false;
                            foreach (var item in curr.GetChildMenuItemsRecursive(false))
                            {
                                item.Toggled = curr.Toggled;
                            }
                        }
                    }

                    if (goUp)
                    {
                        keycode = KeyCode.UpArrow;
                    }

                    Event.current.Use();
                }

                if (keycode == KeyCode.RightArrow && !this.DrawInSearchMode)
                {
                    bool goDown = true;
                    foreach (var curr in this.Selection.ToList())
                    {
                        if (curr.Toggled == false && curr.ChildMenuItems.Any())
                        {
                            curr.Toggled = true;
                            goDown       = false;
                        }

                        if ((Event.current.modifiers & EventModifiers.Alt) != 0)
                        {
                            goDown = false;

                            foreach (var item in curr.GetChildMenuItemsRecursive(false))
                            {
                                item.Toggled = curr.Toggled;
                            }
                        }
                    }

                    if (goDown)
                    {
                        keycode = KeyCode.DownArrow;
                    }

                    Event.current.Use();
                }

                if (keycode == KeyCode.UpArrow)
                {
                    if ((Event.current.modifiers & EventModifiers.Shift) != 0)
                    {
                        var last = this.Selection.Last();
                        var prev = last.PrevVisualMenuItem;

                        if (prev != null)
                        {
                            if (prev.IsSelected)
                            {
                                last.Deselect();
                            }
                            else
                            {
                                prev.Select(true);
                            }

                            Event.current.Use();
                            return(true);
                        }
                    }
                    else
                    {
                        var prev = this.Selection.Last().PrevVisualMenuItem;
                        if (prev != null)
                        {
                            prev.Select();
                            Event.current.Use();
                            return(true);
                        }
                    }
                }

                if (keycode == KeyCode.DownArrow)
                {
                    if ((Event.current.modifiers & EventModifiers.Shift) != 0)
                    {
                        var last = this.Selection.Last();
                        var next = last.NextVisualMenuItem;

                        if (next != null)
                        {
                            if (next.IsSelected)
                            {
                                last.Deselect();
                            }
                            else
                            {
                                next.Select(true);
                            }

                            Event.current.Use();
                            return(true);
                        }
                    }
                    else
                    {
                        var next = this.Selection.Last().NextVisualMenuItem;
                        if (next != null)
                        {
                            next.Select();
                            Event.current.Use();
                            return(true);
                        }
                    }
                }

                if (keycode == KeyCode.Return)
                {
                    this.Selection.ConfirmSelection();
                    Event.current.Use();
                    return(true);
                }
            }

            return(false);
        }
        private void DrawCheckboxMenuItems(OdinMenuItem xx)
        {
            var allChilds = xx.GetChildMenuItemsRecursive(true)
                            .Select(x => x.Value)
                            .OfType <T>().ToList();

            bool isEmpty      = allChilds.Count == 0;
            bool isSelected   = false;
            bool isMixed      = false;
            int  prevUpdateId = -1;

            Action validate = () =>
            {
                if (isEmpty)
                {
                    return;
                }
                isSelected = this.selection.Contains(allChilds[0]);
                //var a = this.selection.ToList();
                var b = allChilds[0];
                isMixed = false;
                for (int i = 1; i < allChilds.Count; i++)
                {
                    var sel = this.selection.Contains(allChilds[i]);
                    if (sel != isSelected)
                    {
                        isMixed = true;
                        break;
                    }
                }
            };

            xx.OnDrawItem += (menuItem) =>
            {
                if (isEmpty)
                {
                    return;
                }

                var checkboxRect = xx.LabelRect;
                checkboxRect    = checkboxRect.AlignMiddle(18).AlignLeft(16);
                checkboxRect.x -= 16;
                if (xx.IconGetter())
                {
                    checkboxRect.x -= 16;
                }

                if (Event.current.type != EventType.Repaint && xx.ChildMenuItems.Count == 0)
                {
                    checkboxRect = xx.Rect;
                }
                //else if (menuItem.ChildMenuItems.Count > 0)
                //{
                //    rect = xx.Rect;
                //}

                if (prevUpdateId != this.checkboxUpdateId)
                {
                    validate();
                    prevUpdateId = this.checkboxUpdateId;
                }

                EditorGUI.showMixedValue = isMixed;
                EditorGUI.BeginChangeCheck();
                var newSelected = EditorGUI.Toggle(checkboxRect, isSelected);
                if (EditorGUI.EndChangeCheck())
                {
                    for (int i = 0; i < allChilds.Count; i++)
                    {
                        if (newSelected)
                        {
                            this.selection.Add(allChilds[i]);
                        }
                        else
                        {
                            this.selection.Remove(allChilds[i]);
                        }
                    }
                    xx.Select();
                    validate();
                    this.requestCheckboxUpdate = true;
                    GUIHelper.RemoveFocusControl();
                }
                EditorGUI.showMixedValue = false;
            };
        }