Exemple #1
0
        public void OnGUI(bool drawRoot = true, bool collapse = false)
        {
            if (_tree == null)
            {
                return;
            }
            if (_buttonStyle == null)
            {
                _buttonStyle = new GUIStyle(GUI.skin.button)
                {
                    alignment = TextAnchor.MiddleLeft, stretchHeight = true
                }
            }
            ;
            if (_valueStyle == null)
            {
                _valueStyle = new GUIStyle(GUI.skin.box)
                {
                    alignment = TextAnchor.MiddleLeft, stretchHeight = true
                }
            }
            ;

            int startIndexUBound = Math.Max(0, _nodesCount - MaxRows);

            // mouse wheel & fix scroll position
            if (Event.current.type == EventType.Layout)
            {
                _totalNodeCount = _tree.RootNode.ChildrenCount;
                if (startIndexUBound > 0)
                {
                    if (_mouseOver)
                    {
                        var delta = Input.mouseScrollDelta;
                        if (delta.y > 0 && _startIndex > 0)
                        {
                            _startIndex--;
                        }
                        else if (delta.y < 0 && _startIndex < startIndexUBound)
                        {
                            _startIndex++;
                        }
                    }
                    if (_startIndex > startIndexUBound)
                    {
                        _startIndex = startIndexUBound;
                    }
                }
                else
                {
                    _startIndex = 0;
                }
            }
            using (new GUILayout.VerticalScope()) {
                // tool-bar
                using (new GUILayout.HorizontalScope()) {
                    if (GUILayout.Button("Collapse", GUILayout.ExpandWidth(false)))
                    {
                        collapse    = true;
                        _skipLevels = 0;
                    }
                    if (GUILayout.Button("Refresh", GUILayout.ExpandWidth(false)))
                    {
                        _tree.RootNode.SetDirty();
                    }
                    GUILayout.Space(10f);
                    //GUIHelper.AdjusterButton(ref _skipLevels, "Skip Levels:", 0);
                    //GUILayout.Space(10f);
                    Main.settings.maxRows = GUIHelper.AdjusterButton(Main.settings.maxRows, "Max Rows:", 10);
                    GUILayout.Space(10f);
#if false
                    GUILayout.Label("Title Width:", GUILayout.ExpandWidth(false));
                    TitleMinWidth = GUILayout.HorizontalSlider(TitleMinWidth, 0f, Screen.width / 2, GUILayout.Width(100f));

                    GUILayout.Space(10f);
#endif
                    GUILayout.Label($"Scroll: {_startIndex} / {_totalNodeCount}", GUILayout.ExpandWidth(false));
                    GUILayout.Space(10f);
                    UI.ActionTextField(ref searchText, "searhText", (text) => { }, () => {
                        searchText = searchText.Trim();
                        ReflectionSearch.Shared.StartSearch(_tree.RootNode, searchText, updateCounts, _searchResults);
                    }, UI.Width(250));
                    GUILayout.Space(10f);
                    bool isSearching = ReflectionSearch.Shared.isSearching;
                    UI.ActionButton(isSearching ? "Stop" : "Search", () => {
                        if (isSearching)
                        {
                            ReflectionSearch.Shared.Stop();
                        }
                        else
                        {
                            searchText = searchText.Trim();
                            ReflectionSearch.Shared.StartSearch(_tree.RootNode, searchText, updateCounts, _searchResults);
                        }
                    }, UI.AutoWidth());
                    GUILayout.Space(10f);
                    if (GUIHelper.AdjusterButton(ref Main.settings.maxSearchDepth, "Max Depth:", 0))
                    {
                        ReflectionSearch.Shared.StartSearch(_tree.RootNode, searchText, updateCounts, _searchResults);
                    }
                    GUILayout.Space(10f);
                    if (visitCount > 0)
                    {
                        GUILayout.Label($"found {_searchResults.Count}".Cyan() + $" visited: {visitCount} (d: {searchDepth} b: {searchBreadth})".Orange());
                    }
                    GUILayout.FlexibleSpace();
                }
                // view
                using (new GUILayout.VerticalScope()) {
                    using (new GUILayout.ScrollViewScope(new Vector2(), GUIStyle.none, GUIStyle.none, GUILayout.Height(_height))) {
                        using (new GUILayout.HorizontalScope(GUI.skin.box)) {
                            // nodes
                            using (new GUILayout.VerticalScope()) {
                                _nodesCount = 0;
                                if (searchText.Length > 0)
                                {
                                    _searchResults.Traverse((node, depth) => {
                                        var toggleState = node.ToggleState;
                                        if (!node.Node.hasChildren)
                                        {
                                            toggleState = ToggleState.None;
                                        }
                                        else if (node.ToggleState == ToggleState.None)
                                        {
                                            toggleState = ToggleState.Off;
                                        }
                                        if (node.Node.NodeType == NodeType.Root)
                                        {
                                            if (node.matches.Count == 0)
                                            {
                                                return(false);
                                            }
                                            GUILayout.Label("Search Results".Cyan().Bold());
                                        }
                                        else
                                        {
                                            DrawNodePrivate(node.Node, depth, ref toggleState);
                                        }
                                        if (node.ToggleState != toggleState)
                                        {
                                            Main.Log(node.ToString());
                                        }
                                        node.ToggleState = toggleState;
                                        if (toggleState.IsOn())
                                        {
                                            DrawChildren(node.Node, depth + 1, collapse);
                                        }
                                        return(true); // toggleState == ToggleState.On;
                                    }, 0);
                                }
                                if (drawRoot)
                                {
                                    DrawNode(_tree.RootNode, 0, collapse);
                                }
                                else
                                {
                                    DrawChildren(_tree.RootNode, 0, collapse);
                                }
                            }

                            // scrollbar
                            //                            if (startIndexUBound > 0)
                            _startIndex = (int)GUILayout.VerticalScrollbar(_startIndex, MaxRows, 0f, Math.Max(MaxRows, _totalNodeCount), GUILayout.ExpandHeight(true));
                        }

                        // cache height
                        if (Event.current.type == EventType.Repaint)
                        {
                            var mousePos = Event.current.mousePosition;
                            _mouseOver = _viewerRect.Contains(Event.current.mousePosition);
                            //Main.Log($"mousePos: {mousePos} Rect: {_viewerRect} --> {_mouseOver}");
                            _viewerRect = GUILayoutUtility.GetLastRect();
                            _height     = _viewerRect.height + 5f;
                        }
                    }
                }
            }
        }