public MenuItemNode CreateNode(string p_name) { var node = new MenuItemNode(p_name, this); Nodes.Add(node); return(node); }
// TODO Possible type caching? public static MenuItemNode GenerateMenuItemNodeTree(GenericMenu p_menu) { MenuItemNode rootNode = new MenuItemNode(); if (p_menu == null) { return(rootNode); } var menuItemsField = TryGetField("menuItems"); if (menuItemsField == null) { menuItemsField = TryGetField("m_MenuItems"); } var menuItems = menuItemsField.GetValue(p_menu) as IEnumerable; foreach (var menuItem in menuItems) { var menuItemType = menuItem.GetType(); GUIContent content = (GUIContent)menuItemType.GetField("content").GetValue(menuItem); bool separator = (bool)menuItemType.GetField("separator").GetValue(menuItem); string path = 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]); } if (separator) { currentNode.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); FieldInfo TryGetField(string fieldName) { return(p_menu.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic)); } }
public MenuItemNode(string p_name = "", MenuItemNode p_parent = null) { name = p_name; parent = p_parent; Nodes = new List <MenuItemNode>(); }
private void DrawNodeTree(Rect p_rect) { if (_currentNode != _rootNode) { _contentHeight += 21; if (GUILayout.Button(_currentNode.GetPath(), BackStyle)) { _currentNode = _currentNode.parent; } } foreach (var node in _currentNode.Nodes) { if (node.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.on ? 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.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); } } }
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.on ? 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.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."); } }
public GenericMenuPopup(GenericMenu p_menu, string p_title) { _title = p_title; showTitle = !string.IsNullOrWhiteSpace(_title); _currentNode = _rootNode = GenerateMenuItemNodeTree(p_menu); }