Esempio n. 1
0
        //Show stuff
        public override void OnGUI(Rect rect)
        {
            var e = Event.current;

            EditorGUIUtility.SetIconSize(Vector2.zero);
            hoveringIndex = Mathf.Clamp(hoveringIndex, -1, currentNode.children.Count - 1);


            ///HELP AREA (shown at bottom, but done first though)
            var hoveringNode = hoveringIndex >= 0? currentNode.children.Values.ToList()[hoveringIndex] : null;

            helpRectHeight = hoveringNode != null && hoveringNode.item != null && !string.IsNullOrEmpty(hoveringNode.item.content.tooltip)? 64 : 0;
            var helpRect = new Rect(rect.x, rect.yMax - helpRectHeight, rect.width, helpRectHeight);

            GUILayout.BeginArea(helpRect);
            GUILayout.BeginVertical();
            if (hoveringNode != null && hoveringNode.item != null)
            {
                EditorGUILayout.HelpBox(hoveringNode.item.content.tooltip, MessageType.Info);
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();



            ///MAIN AREA
            mainRect = new Rect(rect.x, rect.y, rect.width, rect.height - helpRectHeight);
            GUI.Box(mainRect, "", (GUIStyle)"AnimationCurveEditorBackground");
            GUILayout.BeginArea(mainRect);

            //HEADER
            GUILayout.Space(5);
            GUI.color = new Color(1, 1, 1, 0.7f);
            GUILayout.Label(string.Format("<size=20><b>{0}</b></size>", title), headerStyle);
            GUI.color = Color.white;


            ///SEARCH
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField");
            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = string.Empty;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();
            EditorUtils.BoldSeparator();


            ///BACK
            if (currentNode.parent != null && string.IsNullOrEmpty(search))
            {
                GUILayout.BeginHorizontal("box");
                if (GUILayout.Button(string.Format("<b><size=18>◄ {0}</size></b>", currentNode.path), (GUIStyle)"label"))
                {
                    currentNode = currentNode.parent;
                }
                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();
                if (lastRect.Contains(e.mousePosition))
                {
                    GUI.DrawTexture(lastRect, EditorUtils.GetTexture(hoverColor));
                    base.editorWindow.Repaint();
                }
            }


            ///TREE
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
            GUILayout.BeginVertical();

            if (search != lastSearch)
            {
                if (!string.IsNullOrEmpty(search))
                {
                    var searchRootNode = new Node();
                    foreach (var item in items)
                    {
                        var itemText = item.content.text.Split('/').Last();
                        if (itemText.ToLower().Trim().Contains(search.ToLower().Trim()))
                        {
                            var childNode = new Node();
                            childNode.item = item;
                            searchRootNode.children[itemText] = childNode;
                        }
                    }
                    currentNode = searchRootNode;
                }
                else
                {
                    currentNode = rootNode;
                }
                lastSearch = search;
            }



            var i = 0;

            foreach (var childPair in currentNode.children)
            {
                var leafItem = childPair.Value.item;
                var icon     = leafItem != null? leafItem.content.image : EditorUtils.folderIcon;

                GUILayout.BeginHorizontal("box");

                //Prefix icon
                GUILayout.Label(icon, GUILayout.Width(32), GUILayout.Height(16));

                //Content
                if (GUILayout.Button(string.Format("<size=12>{0}</size>",
                                                   (leafItem == null? string.Format("<b>{0}</b>", childPair.Key) : childPair.Key)),
                                     (GUIStyle)"label", GUILayout.Width(0), GUILayout.ExpandWidth(true)))
                {
                    if (leafItem != null)
                    {
                        ExecuteItemFunc(leafItem);
                        break;
                    }
                    else
                    {
                        currentNode   = childPair.Value;
                        hoveringIndex = 0;
                        break;
                    }
                }

                //Suffix icon
                GUILayout.Label(leafItem != null? "<b>+</b>" : "►", GUILayout.Width(20));

                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();

                if (e.isMouse && lastRect.Contains(e.mousePosition))
                {
                    hoveringIndex = i;
                }

                if (hoveringIndex == i)
                {
                    GUI.DrawTexture(lastRect, EditorUtils.GetTexture(hoverColor));
                    base.editorWindow.Repaint();
                }

                i++;
            }

            //handle the events
            HandeEvents(e);

            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
            GUILayout.EndArea();
            EditorGUIUtility.SetIconSize(Vector2.zero);
        }
Esempio n. 2
0
        //Show stuff
        public override void OnGUI(Rect rect)
        {
            var e = Event.current;

            EditorGUIUtility.SetIconSize(Vector2.zero);
            hoveringIndex = Mathf.Clamp(hoveringIndex, -1, currentNode.children.Count - 1);

            ///MAIN AREA
            mainRect = new Rect(rect.x, rect.y, rect.width, rect.height - helpRectHeight);
            GUI.Box(mainRect, "", (GUIStyle)"AnimationCurveEditorBackground");
            GUILayout.BeginArea(mainRect);

            //HEADER
            GUILayout.Space(5);
            GUILayout.Label(string.Format("<color=#dddddd><size=20><b>{0}</b></size></color>", title), headerStyle);

            ///SEARCH
            if (e.keyCode == KeyCode.DownArrow)
            {
                GUIUtility.keyboardControl = 0;
            }
            if (e.keyCode == KeyCode.UpArrow)
            {
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            GUI.SetNextControlName("SearchToolbar");
            search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField");
            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = string.Empty;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            searchMode      = (SearchMode)EditorGUILayout.EnumPopup(searchMode);
            filterFavorites = EditorGUILayout.ToggleLeft("Filter Favorites", filterFavorites);
            GUILayout.EndHorizontal();

            EditorUtils.BoldSeparator();

            ///BACK
            if (currentNode.parent != null && string.IsNullOrEmpty(search))
            {
                GUILayout.BeginHorizontal("box");
                if (GUILayout.Button(string.Format("<b><size=18>◄ {0}/{1}</size></b>", currentNode.parent.name, currentNode.name), (GUIStyle)"label"))
                {
                    currentNode = currentNode.parent;
                }
                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();
                if (lastRect.Contains(e.mousePosition))
                {
                    GUI.DrawTexture(lastRect, EditorUtils.GetTexture(hoverColor));
                    base.editorWindow.Repaint();
                    hoveringIndex = -1;
                }
            }

            //Go back with right click as well...
            if (e.type == EventType.MouseDown && e.button == 1)
            {
                e.Use();
                if (currentNode.parent != null)
                {
                    currentNode = currentNode.parent;
                }
            }


            ///TREE
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
            GUILayout.BeginVertical();

            if (search != lastSearch)
            {
                hoveringIndex = 0;
                if (!string.IsNullOrEmpty(search) && search.Length >= 2)
                {
                    var searchRootNode = new Node()
                    {
                        name = "Search Root"
                    };
                    var a = GetCleanSearchString(search);
                    foreach (var node in leafNodes)
                    {
                        var b     = GetCleanSearchString(node.name);
                        var match = false;
                        if (searchMode == SearchMode.Contains && b.Contains(a))
                        {
                            match = true;
                        }
                        if (searchMode == SearchMode.StartsWith && b.StartsWith(a))
                        {
                            match = true;
                        }
                        if (match)
                        {
                            searchRootNode.children[node.name] = node;
                        }
                    }
                    currentNode = searchRootNode;
                }
                else
                {
                    currentNode = rootNode;
                }
                lastSearch = search;
            }



            var  i          = 0;
            var  itemAdded  = false;
            Node lastParent = null;

            foreach (var childPair in currentNode.children)
            {
                var node = childPair.Value;

                if (search != null && search.Length >= 2)
                {
                    var currentParent = node.parent;
                    if (currentParent != lastParent)
                    {
                        lastParent = currentParent;
                        GUI.color  = EditorGUIUtility.isProSkin? Color.black : Color.white;
                        GUILayout.BeginHorizontal("box");
                        GUI.color = Color.white;
                        if (GUILayout.Button(currentParent.unfolded? "▼" : "▶", (GUIStyle)"label", GUILayout.Width(16)))
                        {
                            currentParent.unfolded = !currentParent.unfolded;
                        }
                        GUILayout.Label(string.Format("<size=12><b>{0}</b></size>", currentParent.fullPath));
                        GUILayout.EndHorizontal();
                    }

                    if (!node.parent.unfolded)
                    {
                        continue;
                    }
                }

                if (filterFavorites && !node.isFavorite && !node.HasAnyFavoriteChild())
                {
                    continue;
                }

                itemAdded = true;
                var leafItem     = node.item;
                var icon         = leafItem != null? leafItem.content.image : EditorUtils.folderIcon;
                var itemDisabled = leafItem != null && leafItem.func == null && leafItem.func2 == null;

                GUI.color = EditorGUIUtility.isProSkin? Color.white : new Color(0.8f, 0.8f, 0.8f, 1);
                GUILayout.BeginHorizontal("box");
                GUI.color = Color.white;

                //Prefix icon
                GUILayout.Label(icon, GUILayout.Width(32), GUILayout.Height(16));

                GUI.enabled = !itemDisabled;

                //Favorite
                GUI.color = node.isFavorite? Color.white : (node.HasAnyFavoriteChild()? new Color(1, 1, 1, 0.2f) : new Color(0f, 0f, 0f, 0.4f));
                if (GUILayout.Button(EditorUtils.favoriteIcon, GUIStyle.none, GUILayout.Width(16), GUILayout.Height(16)))
                {
                    node.ToggleFavorite();
                }
                GUI.color = Color.white;

                //Content
                var label = node.name;
                if (search != null && search.Length >= 2)                  //simple highlight
                {
                    label = Regex.Replace(label, search, "<b>$&</b>", RegexOptions.IgnoreCase);
                }

                if (GUILayout.Button(string.Format("<size=12>{0}</size>",
                                                   (leafItem == null? string.Format("<b>{0}</b>", label) : label)),
                                     (GUIStyle)"label", GUILayout.Width(0), GUILayout.ExpandWidth(true)))
                {
                    if (leafItem != null)
                    {
                        ExecuteItemFunc(leafItem);
                        break;
                    }
                    else
                    {
                        currentNode   = node;
                        hoveringIndex = 0;
                        break;
                    }
                }

                //Suffix icon
                GUILayout.Label(leafItem != null? "<b>+</b>" : "►", GUILayout.Width(20));

                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();

                if (lastRect.Contains(e.mousePosition) && e.type == EventType.MouseMove)
                {
                    hoveringIndex = i;
                }

                if (hoveringIndex == i)
                {
                    GUI.DrawTexture(lastRect, EditorUtils.GetTexture(hoverColor));
                    base.editorWindow.Repaint();
                }

                i++;

                GUI.enabled = true;
            }


            if (!itemAdded)
            {
                GUILayout.Label("No results to display with current search and filter combination");
            }

            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
            GUILayout.EndArea();


            GUILayout.FlexibleSpace();


            ///HELP AREA
            var hoveringNode = hoveringIndex >= 0 && currentNode.children.Count > 0? currentNode.children.Values.ToList()[hoveringIndex] : null;
            var helpRect     = new Rect(rect.x + 2, rect.yMax - helpRectHeight + 2, rect.width - 4, helpRectHeight - 2);

            GUI.color = new Color(0, 0, 0, 0.3f);
            GUI.Box(helpRect, "", (GUIStyle)"TextField");
            GUI.color = Color.white;
            GUILayout.BeginArea(helpRect);
            GUILayout.BeginVertical();
            if (hoveringNode != null && hoveringNode.item != null)
            {
                string doc        = hoveringNode.item.content.tooltip;
                var    memberInfo = hoveringNode.item.userData as MemberInfo;
                if (memberInfo != null && string.IsNullOrEmpty(doc))
                {
                    doc = DocsByReflection.GetMemberSummary(memberInfo);
                }

                GUILayout.Label(doc, EditorStyles.wordWrappedLabel);
            }
            else
            {
                GUILayout.Label("");
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();


            //handle the events
            HandeEvents(e);
            if (!init)
            {
                init = true;
                EditorGUI.FocusTextInControl("SearchToolbar");
            }

            EditorGUIUtility.SetIconSize(Vector2.zero);
        }