Example #1
0
        public MenuItemNode CreateNode(string p_name)
        {
            var node = new MenuItemNode(p_name, this);

            Nodes.Add(node);
            return(node);
        }
Example #2
0
        // TODO Possible type caching?
        public static MenuItemNode GenerateMenuItemNodeTree(RuntimeGenericMenu p_menu)
        {
            MenuItemNode rootNode = new MenuItemNode();

            if (p_menu == null)
            {
                return(rootNode);
            }

            foreach (var menuItem in p_menu.Items)
            {
                string       path        = menuItem.content.text;
                string[]     splitPath   = path.Split('/');
                MenuItemNode currentNode = rootNode;
                for (int i = 0; i < splitPath.Length; i++)
                {
                    currentNode = (i < splitPath.Length - 1)
                        ? currentNode.GetOrCreateNode(splitPath[i])
                        : currentNode.CreateNode(splitPath[i]);
                }

                currentNode.item = menuItem;
                //
                // if (menuItem.separator)
                // {
                //     currentNode.item.separator = true;
                // }
                // else
                // {
                //     currentNode.content = content;
                //     currentNode.func = (GenericMenu.MenuFunction) menuItemType.GetField("func").GetValue(menuItem);
                //     currentNode.func2 = (GenericMenu.MenuFunction2) menuItemType.GetField("func2").GetValue(menuItem);
                //     currentNode.userData = menuItemType.GetField("userData").GetValue(menuItem);
                //     currentNode.on = (bool) menuItemType.GetField("on").GetValue(menuItem);
                // }
            }

            return(rootNode);
        }
Example #3
0
        private void DrawNodeTree(Rect p_rect)
        {
            if (_currentNode != _rootNode)
            {
                _contentHeight += 22;
                GUILayout.Space(2);
                if (GUILayout.Button(_currentNode.GetPath(), BackStyle, GUILayout.Height(20)))
                {
                    _currentNode = _currentNode.parent;
                }
            }

            foreach (var node in _currentNode.Nodes)
            {
                if (node.item != null && node.item.separator)
                {
                    GUILayout.Space(4);
                    _contentHeight += 4;
                    continue;
                }

                _contentHeight += 21;
                GUI.color       = _hoverNode == node ? Color.white : Color.gray;
                GUIStyle style = new GUIStyle();
                style.normal.background = Texture2D.grayTexture;
                GUILayout.BeginHorizontal(style);

                if (showOnStatus)
                {
                    style = new GUIStyle("box");
                    style.normal.background = Texture2D.whiteTexture;
                    GUI.color = (node.item != null && node.item.state) ? new Color(0, .6f, .8f, .5f) : new Color(.2f, .2f, .2f, .2f);
                    GUILayout.Box("", style, GUILayout.Width(14), GUILayout.Height(14));
                }

                GUI.color       = _hoverNode == node ? Color.white : Color.white;
                style           = new GUIStyle("label");
                style.fontStyle = node.Nodes.Count > 0 ? FontStyle.Bold : FontStyle.Normal;
                GUILayout.Label(node.name, style);

                GUILayout.EndHorizontal();

                var nodeRect = GUILayoutUtility.GetLastRect();
                if (Event.current.type == EventType.Repaint || Event.current.isMouse)
                {
                    if (nodeRect.Contains(Event.current.mousePosition))
                    {
                        if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
                        {
                            if (node.Nodes.Count > 0)
                            {
                                _currentNode = node;
                                _repaint     = true;
                            }
                            else
                            {
                                node.Execute();
                                base.editorWindow.Close();
                            }

                            break;
                        }

                        if (_hoverNode != node)
                        {
                            _hoverNode = node;
                            _repaint   = true;
                        }
                    }
                    else if (_hoverNode == node)
                    {
                        _hoverNode = null;
                        _repaint   = true;
                    }
                }

                if (node.Nodes.Count > 0)
                {
                    Rect lastRect = GUILayoutUtility.GetLastRect();
                    GUI.Label(new Rect(lastRect.x + lastRect.width - 16, lastRect.y - 2, 20, 20), "+", PlusStyle);
                }
            }
        }
Example #4
0
        private void DrawNodeSearch(Rect p_rect)
        {
            List <MenuItemNode> search = _rootNode.Search(_search);

            search.Sort((n1, n2) =>
            {
                string p1 = n1.parent.GetPath();
                string p2 = n2.parent.GetPath();
                if (p1 == p2)
                {
                    return(n1.name.CompareTo(n2.name));
                }

                return(p1.CompareTo(p2));
            });

            string lastPath = "";

            foreach (var node in search)
            {
                string nodePath = node.parent.GetPath();
                if (nodePath != lastPath)
                {
                    _contentHeight += 21;
                    GUILayout.Label(nodePath);
                    lastPath = nodePath;
                }

                _contentHeight += 21;
                GUI.color       = _hoverNode == node ? Color.white : Color.gray;
                GUIStyle style = new GUIStyle();
                style.normal.background = Texture2D.grayTexture;
                GUILayout.BeginHorizontal(style);

                if (showOnStatus)
                {
                    style = new GUIStyle("box");
                    style.normal.background = Texture2D.whiteTexture;
                    GUI.color = node.item.state ? new Color(0, .6f, .8f) : new Color(.2f, .2f, .2f);
                    GUILayout.Box("", style, GUILayout.Width(14), GUILayout.Height(14));
                }

                GUI.color = _hoverNode == node ? Color.white : Color.white;
                GUILayout.Label(node.name);

                GUILayout.EndHorizontal();

                var nodeRect = GUILayoutUtility.GetLastRect();
                if (Event.current.type == EventType.Repaint || Event.current.isMouse)
                {
                    if (nodeRect.Contains(Event.current.mousePosition))
                    {
                        if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
                        {
                            if (node.Nodes.Count > 0)
                            {
                                _currentNode = node;
                                _repaint     = true;
                            }
                            else
                            {
                                node.Execute();
                                base.editorWindow.Close();
                            }

                            break;
                        }

                        if (_hoverNode != node)
                        {
                            _hoverNode = node;
                            _repaint   = true;
                        }
                    }
                    else if (_hoverNode == node)
                    {
                        _hoverNode = null;
                        _repaint   = true;
                    }
                }
            }

            if (search.Count == 0)
            {
                GUILayout.Label("No result found for specified search.");
            }
        }
Example #5
0
 public MenuItemNode(string p_name = "", MenuItemNode p_parent = null)
 {
     name   = p_name;
     parent = p_parent;
     Nodes  = new List <MenuItemNode>();
 }
Example #6
0
 public GenericMenuPopup(RuntimeGenericMenu p_menu, string p_title)
 {
     _title       = p_title;
     showTitle    = !string.IsNullOrWhiteSpace(_title);
     _currentNode = _rootNode = GenerateMenuItemNodeTree(p_menu);
 }