Exemplo n.º 1
0
 public static TooltipWindow Show(Vector2 pos, IList <GUIContent> contents, float width = 300, float height = 300)
 {
     if (window == null)
     {
         window = CreateInstance(typeof(TooltipWindow)) as TooltipWindow;
     }
     window.width        = width;
     window.height       = height;
     window.contents     = contents;
     window.isInitailzed = false;
     window.isOver       = false;
     window.position     = new Rect(pos.x, pos.y, width, height);
     window.Repaint();
     if (showTooltipMethod != null)             //Hack internal method so other window will not lose focus like an build in tooltip.
     {
         showTooltipMethod.Invoke(window, null);
     }
     else
     {
         window.ShowPopup();
     }
     //window.ShowAsDropDown(WindowUtility.MousePosToRect(pos, Vector2.zero), new Vector2(width, height));
     return(window);
 }
Exemplo n.º 2
0
        public override void OnGUI(Rect rect)
        {
            base.OnGUI(rect);
            if (Event.current.type == EventType.MouseEnterWindow || Event.current.type == EventType.MouseLeaveWindow)
            {
                if (tooltipWindow != null)
                {
                    tooltipWindow.Close();
                }
            }
            if (Event.current.type == EventType.Repaint)
            {
                if (hoverItem != null && lastHoverItem != hoverItem)
                {
                    List <GUIContent> contents = new List <GUIContent>();
                    if (hoverItem is TypeTreeView)
                    {
                        if (uNodePreference.GetPreference().showDocumentation&& XmlDoc.hasLoadDoc)
                        {
                            TypeTreeView item = hoverItem as TypeTreeView;

                            Texture icon = uNodeEditorUtility.GetIcon(item.type);
                            contents.Add(new GUIContent(item.displayName, icon));
                            contents.Add(new GUIContent("Target	: " + item.type.MemberType.ToString()));
                            contents.Add(new GUIContent("Type	: "+ item.type.PrettyName(true), uNodeEditorUtility.GetTypeIcon(item.type)));
                            XmlElement documentation = XmlDoc.XMLFromType(item.type);
                            if (documentation != null)
                            {
                                contents.Add(new GUIContent("<b>Documentation</b> ▼ " + documentation["summary"].InnerText.Trim().AddLineInFirst()));
                            }
                        }
                    }
                    else if (hoverItem is MemberTreeView)
                    {
                        var     item = hoverItem as MemberTreeView;
                        Texture icon = uNodeEditorUtility.GetIcon(item.member);
                        contents.Add(new GUIContent(item.displayName, icon));
                        contents.Add(new GUIContent("Target	: " + item.member.MemberType.ToString()));
                        contents.Add(new GUIContent("Static	: " + ReflectionUtils.GetMemberIsStatic(item.member)));
                        if (item.member != null)
                        {
                            var mType = ReflectionUtils.GetMemberType(item.member);
                            contents.Add(new GUIContent("Return	: " + mType.PrettyName(true), uNodeEditorUtility.GetTypeIcon(mType)));
                        }
                        if (uNodePreference.GetPreference().showDocumentation&& XmlDoc.hasLoadDoc)
                        {
                            XmlElement documentation = XmlDoc.XMLFromMember(item.member);
                            if (documentation != null)
                            {
                                contents.Add(new GUIContent("<b>Documentation ▼</b> " + documentation["summary"].InnerText.Trim().AddLineInFirst()));
                            }
                            switch (item.member.MemberType)
                            {
                            case MemberTypes.Method:
                            case MemberTypes.Constructor:
                                var parameters = (item.member as MethodBase).GetParameters();
                                if (parameters.Length > 0)
                                {
                                    for (int x = 0; x < parameters.Length; x++)
                                    {
                                        System.Type PType = parameters[x].ParameterType;
                                        if (PType != null)
                                        {
                                            contents.Add(new GUIContent("<b>" + parameters[x].Name + " : " + PType.PrettyName() + "</b>",
                                                                        uNodeEditorUtility.GetTypeIcon(PType)));
                                            if (documentation != null && documentation["param"] != null)
                                            {
                                                XmlNode paramDoc = null;
                                                XmlNode doc      = documentation["param"];
                                                while (doc.NextSibling != null)
                                                {
                                                    if (doc.Attributes["name"] != null && doc.Attributes["name"].Value.Equals(parameters[x].Name))
                                                    {
                                                        paramDoc = doc;
                                                        break;
                                                    }
                                                    doc = doc.NextSibling;
                                                }
                                                if (paramDoc != null && !string.IsNullOrEmpty(paramDoc.InnerText))
                                                {
                                                    contents.Add(new GUIContent(paramDoc.InnerText.Trim()));
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                    if (contents.Count > 0)
                    {
                        var position = window.position;
                        if (position.x + position.width + 300 <= Screen.currentResolution.width)
                        {
                            tooltipWindow = TooltipWindow.Show(new Vector2(position.x + position.width, position.y), contents, 300, 600);
                        }
                        else
                        {
                            tooltipWindow = TooltipWindow.Show(new Vector2(position.x - 300, position.y), contents, 300, 600);
                        }
                    }
                    else if (tooltipWindow != null)
                    {
                        tooltipWindow.Close();
                    }
                    lastHoverItem = hoverItem;
                }
            }
        }
Exemplo n.º 3
0
        private void OnGUI()
        {
            textEditor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);

            autocompleteBox.HandleEvents();
            HandleHistory();
            DoAutoComplete();
            HandleRequests();

            //{//Border
            //	GUI.color = new Color(0.78f, 0.78f, 0.78f);
            //	GUI.Label(new Rect(2, 2, position.width - 4, position.height - 4), "", Styles.resultsBorderStyle);
            //	GUI.color = Color.white;
            //}
            //Draw GUI
            //GUILayout.BeginArea(new Rect(1, 4, position.width - 2, position.height - 8));
            {
                float height = 20;
                EditorGUILayout.BeginVertical();
                {                //Draw TextField
                    var current = Event.current;

                    if (!_hasFocus)
                    {
                        GUI.FocusControl(textAreaControlName);
                        if (current.type == EventType.Repaint)
                        {
                            _hasFocus       = true;
                            textEditor.text = _text ?? "";
                            textEditor.MoveTextEnd();
                        }
                    }
                    if (current.type == EventType.KeyDown)
                    {
                        if (current.keyCode == KeyCode.Return && !current.shift)
                        {
                            textEditor.MoveTextEnd();
                        }
                        else if (current.keyCode == KeyCode.Tab)
                        {
                            current.Use();
                            _hasFocus = false;
                            textEditor.MoveTextEnd();
                        }
                    }
                    GUI.SetNextControlName(textAreaControlName);
                    GUILayout.TextField(text, EditorStyles.textField, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
                }
                completions = completionEvaluator.completions;
                if (completions != null && completions.Length > 0)
                {
                    autocompleteBox.results = completions;
                    bool flag = true;
                    if (completions.Length == 1)
                    {
                        if (completions[0].name.Trim() == text.Trim() ||
                            completions[0].isSymbol ||
                            completions[0].isDot ||
                            completions[0].kind == CompletionKind.Literal)
                        {
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        height += height * completions.Length + 3;
                        if (height > 300)
                        {
                            height = 300;
                        }
                        Rect completionRect = uNodeGUIUtility.GetRect(EditorGUIUtility.fieldWidth, height - 20);
                        autocompleteBox.OnGUI(lastWord, completionRect);
                    }
                }
                if (Event.current.type == EventType.KeyUp)
                {
                    if (Event.current.keyCode == KeyCode.UpArrow)
                    {
                        newOverrideIndex--;
                        if (newOverrideIndex < 0)
                        {
                            newOverrideIndex = 0;
                        }
                        Event.current.Use();
                    }
                    else if (Event.current.keyCode == KeyCode.DownArrow)
                    {
                        newOverrideIndex++;
                        Event.current.Use();
                    }
                }
                if (Event.current.type == EventType.Repaint && (memberPaths != completionEvaluator.memberPaths || newOverrideIndex != overrideIndex))
                {
                    overrideIndex = newOverrideIndex;
                    memberPaths   = completionEvaluator.memberPaths;
                    bool       closeTooltip  = true;
                    MethodBase methodBase    = null;
                    int        numOfOverload = 0;
                    if (completionEvaluator.memberPaths.Count > 0)
                    {
                        bool flag = false;
                        for (int i = memberPaths.Count - 1; i > 0; i--)
                        {
                            var mPath = memberPaths[i];
                            if (mPath.isSymbol)
                            {
                                switch (mPath.name)
                                {
                                case "(":
                                case "<": {                                        //For constructor, function and genric.
                                    var member = memberPaths[i - 1];
                                    if (member.member is MethodInfo)
                                    {
                                        MethodInfo[] memberInfos = null;
                                        if (member.member.ReflectedType != null)
                                        {
                                            memberInfos = member.member.ReflectedType.GetMethods();
                                        }
                                        else if (member.member.DeclaringType != null)
                                        {
                                            memberInfos = member.member.DeclaringType.GetMethods();
                                        }
                                        if (memberInfos != null)
                                        {
                                            memberInfos = memberInfos.Where(m =>
                                                                            m.Name.Equals(member.name)).ToArray();
                                            if (memberInfos != null && memberInfos.Length > 0)
                                            {
                                                if (overrideIndex + 1 > memberInfos.Length)
                                                {
                                                    overrideIndex    = memberInfos.Length - 1;
                                                    newOverrideIndex = overrideIndex;
                                                }
                                                methodBase    = memberInfos[overrideIndex];
                                                numOfOverload = memberInfos.Length;
                                            }
                                        }
                                    }
                                    else if (member.member is ConstructorInfo)
                                    {
                                        ConstructorInfo[] memberInfos = null;
                                        if (member.member.ReflectedType != null)
                                        {
                                            memberInfos = member.member.ReflectedType.GetConstructors(
                                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                                        }
                                        else if (member.member.DeclaringType != null)
                                        {
                                            memberInfos = member.member.DeclaringType.GetConstructors(
                                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                                        }
                                        if (memberInfos != null)
                                        {
                                            memberInfos = memberInfos.Where(m =>
                                                                            m.Name.Equals(member.name)).ToArray();
                                            if (memberInfos != null && memberInfos.Length > 0)
                                            {
                                                if (overrideIndex + 1 > memberInfos.Length)
                                                {
                                                    overrideIndex    = memberInfos.Length - 1;
                                                    newOverrideIndex = overrideIndex;
                                                }
                                                methodBase    = memberInfos[overrideIndex];
                                                numOfOverload = memberInfos.Length;
                                            }
                                        }
                                    }
                                    break;
                                }

                                case "[":                                        //For indexer

                                    break;

                                case ")":
                                case ">":
                                case "]":
                                    flag = true;
                                    break;
                                }
                            }
                            if (methodBase != null || flag)
                            {
                                break;
                            }
                        }
                    }
                    List <GUIContent> contents = new List <GUIContent>();
                    if (autocompleteBox.selectedCompletion != null)
                    {
                        var selectedCompletion = autocompleteBox.selectedCompletion;
                        switch (selectedCompletion.kind)
                        {
                        case CompletionKind.Type: {
                            Type type = selectedCompletion.member as Type;
                            if (type != null)
                            {
                                contents.Add(new GUIContent(type.PrettyName(true), uNodeEditorUtility.GetIcon(type)));
                            }
                            break;
                        }

                        case CompletionKind.Field: {
                            FieldInfo field = selectedCompletion.member as FieldInfo;
                            if (field != null)
                            {
                            }
                            break;
                        }

                        case CompletionKind.Property: {
                            PropertyInfo property = selectedCompletion.member as PropertyInfo;
                            if (property != null)
                            {
                            }
                            break;
                        }

                        case CompletionKind.Method: {
                            MethodInfo method = selectedCompletion.member as MethodInfo;
                            if (method != null && method != methodBase)
                            {
                                ResolveMethodTooltip(method, numOfOverload, contents);
                            }
                            break;
                        }
                        }
                        if (contents.Count > 0)
                        {
                            contents.Add(new GUIContent(""));
                        }
                    }
                    if (methodBase != null)
                    {
                        ResolveMethodTooltip(methodBase, numOfOverload, contents);
                    }

                    if (contents.Count > 0)
                    {
                        GUIContent c = null;
                        for (int i = 0; i < contents.Count; i++)
                        {
                            if (c == null ||
                                uNodeEditorUtility.RemoveHTMLTag(c.text).Length <
                                uNodeEditorUtility.RemoveHTMLTag(contents[i].text).Length)
                            {
                                c = contents[i];
                            }
                        }
                        float width = uNodeGUIUtility.Styles.RichLabel.CalcSize(c).x + 20;
                        if (position.x + position.width + width <= Screen.currentResolution.width)
                        {
                            tooltipWindow = TooltipWindow.Show(new Vector2(position.x + position.width, position.y), contents, width);
                        }
                        else
                        {
                            tooltipWindow = TooltipWindow.Show(new Vector2(position.x - width, position.y), contents, width);
                        }
                        closeTooltip = false;
                    }
                    if (closeTooltip && tooltipWindow != null)
                    {
                        tooltipWindow.Close();
                    }
                }
                uNodeGUIUtility.GetRect(EditorGUIUtility.fieldWidth, 3);
                EditorGUILayout.EndVertical();

                if (Event.current.type == EventType.Repaint && currentHeight != height)
                {
                    currentHeight = height;
                    ShowAsDropDown(new Rect(position.x, position.y, 0, 0), new Vector2(position.width, currentHeight));
                }
            }
            //GUILayout.EndArea();
        }