示例#1
0
        /// <summary>Sets the position of the tooltip container.</summary>
        public static void SetPosition(this Tooltip tooltip, UTooltipTrigger trigger, Canvas canvas, Camera camera)
        {
            Vector3[] triggerCorners = new Vector3[4];

            GetWorldCorners(trigger.gameObject, canvas, camera, ref triggerCorners);

            // Set the initial tooltip position.
            tooltip.SetPosition(trigger.tipPosition, trigger.tooltipStyle, triggerCorners, tooltip.BackgroundImage, tooltip.RectTransform, canvas, camera);

            // If overflow protection is disabled, exit.
            if (!UTooltipManager.Instance.overflowProtection)
            {
                return;
            }

            UTooltipOverflow overflow;

            GetOverflow(tooltip.RectTransform, canvas, camera, out overflow);

            // If the tooltip overflows its boundary rectange, reposition it.
            if (overflow.IsAny)
            {
                tooltip.SetPosition(overflow.SuggestNewPosition(trigger.tipPosition), trigger.tooltipStyle,
                                    triggerCorners, tooltip.BackgroundImage, tooltip.RectTransform, canvas, camera);
            }
        }
示例#2
0
        /// <summary>Displays the tooltip.</summary>
        /// <remarks>
        /// This method first waits a couple frames before sizing and positioning the tooltip.
        /// This is necessary in order to get an accurate preferredWidth property of the dynamic text field.
        /// </remarks>
        public IEnumerator Show(UTooltipTrigger trigger)
        {
            if (trigger.tooltipStyle == null)
            {
                Debug.LogWarning("UTooltipTrigger \"" + trigger.name + "\" has no associated TooltipStyle. Cannot show tooltip.");
                yield break;
            }

            Tooltip tooltip       = trigger.Tooltip;
            Image   tooltipBkgImg = tooltip.BackgroundImage;


            // Replace dynamic image placeholders with the correct images.
            if (trigger.dynamicImageFields != null)
            {
                for (int i = 0; i < trigger.dynamicImageFields.Count; i++)
                {
                    for (int j = 0; j < tooltip.ImageFields.Count; j++)
                    {
                        if (tooltip.ImageFields[j].Name == trigger.dynamicImageFields[i].name)
                        {
                            if (trigger.dynamicImageFields[i].replacementSprite == null)
                            {
                                tooltip.ImageFields[j].Image.sprite = tooltip.ImageFields[j].Original;
                            }
                            else
                            {
                                tooltip.ImageFields[j].Image.sprite = trigger.dynamicImageFields[i].replacementSprite;
                            }
                        }
                    }
                }
            }

            // Toggle dynamic sections on or off.
            if (trigger.dynamicSectionFields != null)
            {
                for (int i = 0; i < trigger.dynamicSectionFields.Count; i++)
                {
                    for (int j = 0; j < tooltip.SectionFields.Count; j++)
                    {
                        if (tooltip.SectionFields[j].Name == trigger.dynamicSectionFields[i].name)
                        {
                            tooltip.SectionFields[j].GameObject.SetActive(trigger.dynamicSectionFields[i].isOn);
                        }
                    }
                }
            }

            // Wait for 2 frames so we get an accurate PreferredWidth on the Text component.
            yield return(Utilites.WaitForFrames(2));


            // Set the position of the tooltip.
            tooltip.SetPosition(trigger, TooltipCanvas, UICamera);

            // Set the tint color of the tooltip panel and tips.
            tooltipBkgImg.color = trigger.backgroundTint;

            // If this is a blocking tooltip, assign it as such.
            if (tooltip.IsBlocking)
            {
                BlockingTooltip = tooltip;
            }

            // Display the tooltip.
            tooltip.Display(TooltipFadeDuration);
        }
示例#3
0
        /// <summary>Sets the parameterized text fields on the tooltip.</summary>
        /// <remarks>This is separate from the Show() method because we need to wait a frame or two so the text's RectTransform has time to update its preferredWidth.</remarks>
        public void SetTextAndSize(UTooltipTrigger trigger)
        {
            Tooltip tooltip = trigger.Tooltip;

            if (tooltip == null || trigger.parameterizedTextFields == null)
            {
                return;
            }

            if (tooltip.TextFields == null || tooltip.TextFields.Count == 0)
            {
                return;
            }

            LayoutElement mainTextContainer = tooltip.TooltipStyle.mainTextContainer;

            if (mainTextContainer == null)
            {
                Debug.LogWarning(String.Format("No main text container defined on tooltip style \"{0}\". Note: This LayoutElement is needed in order to resize text appropriately.", trigger.Tooltip.GameObject.name));
            }
            else
            {
                mainTextContainer.preferredWidth = trigger.minTextWidth;
            }

            for (int i = 0; i < tooltip.TextFields.Count; i++)
            {
                Text txt = tooltip.TextFields[i].Text;
                if (txt.text.Length < 3)
                {
                    continue; // text is too short to contain a parameter, so skip it.
                }
                for (int j = 0; j < trigger.parameterizedTextFields.Count; j++)
                {
                    if (!String.IsNullOrEmpty(trigger.parameterizedTextFields[j].value))
                    {
                        string tmpText = txt.text.Replace(trigger.parameterizedTextFields[j].placeholder, trigger.parameterizedTextFields[j].value);
                        if (TooltipMgrSetTextEvent != null)
                        {
                            tmpText = TooltipMgrSetTextEvent.Invoke(trigger.parameterizedTextFields[j].value);
                            trigger.parameterizedTextFields[j].value = tmpText;
                        }

                        txt.text = txt.text.Replace(trigger.parameterizedTextFields[j].placeholder, trigger.parameterizedTextFields[j].value);
                    }
                }

                if (mainTextContainer != null)
                {
                    // if the text would be wider than allowed, constrain the main text container to that limit.
                    if (txt.preferredWidth > trigger.maxTextWidth)
                    {
                        mainTextContainer.preferredWidth = trigger.maxTextWidth;
                    }
                    // otherwise, if it's within the allotted space but bigger than the text container's default width, expand the main text container to accommodate.
                    else if (txt.preferredWidth > trigger.minTextWidth && txt.preferredWidth > mainTextContainer.preferredWidth)
                    {
                        mainTextContainer.preferredWidth = txt.preferredWidth;
                    }
                }
            }
        }
示例#4
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            UTooltipTrigger tooltipTrigger = target as UTooltipTrigger;

            if (tooltipTrigger != null)
            {
                // TOOLTIP STYLE
                GUI.color = tooltipStyle.objectReferenceValue == null ? Color.red : Color.white;
                EditorGUILayout.PropertyField(tooltipStyle, new GUIContent("Tooltip Style"));
                tooltipTrigger.tooltipStyle = tooltipStyle.objectReferenceValue as TooltipStyle;
                GUI.color = Color.white;

                if (tooltipTrigger.parameterizedTextFields == null)
                {
                    tooltipTrigger.parameterizedTextFields = new List <ParameterizedTextField>();
                }
                if (tooltipTrigger.dynamicImageFields == null)
                {
                    tooltipTrigger.dynamicImageFields = new List <DynamicImageField>();
                }
                if (tooltipTrigger.dynamicSectionFields == null)
                {
                    tooltipTrigger.dynamicSectionFields = new List <DynamicSectionField>();
                }

                if (tooltipTrigger.tooltipStyle != null)
                {
                    // Retrieve dynamic text and image fields on the tooltip.
                    Text[]           textFields    = tooltipTrigger.tooltipStyle.GetComponentsInChildren <Text>(true);
                    DynamicImage[]   imageFields   = tooltipTrigger.tooltipStyle.GetComponentsInChildren <DynamicImage>(true);
                    DynamicSection[] sectionFields =
                        tooltipTrigger.tooltipStyle.GetComponentsInChildren <DynamicSection>(true);

                    // Fill and configure the dynamic text and image field collections on the tooltip trigger.
                    textFields.FillParameterizedTextFields(ref tooltipTrigger.parameterizedTextFields, "%");
                    // <= if you want to use a different field delimiter, change it here.
                    imageFields.FillDynamicImageFields(ref tooltipTrigger.dynamicImageFields, "%");
                    // <= if you want to use a different field delimiter, change it here.
                    sectionFields.FillDynamicSectionFields(ref tooltipTrigger.dynamicSectionFields, "%");
                    // <= if you want to use a different field delimiter, change it here.

                    // DYNAMIC TEXT FIELDS
                    if (tooltipTrigger.parameterizedTextFields.Count > 0)
                    {
                        EditorGUILayout.BeginVertical("Box");
                        EditorGUILayout.LabelField("Dynamic Text Fields", EditorStyles.boldLabel);
                        foreach (ParameterizedTextField field in tooltipTrigger.parameterizedTextFields)
                        {
                            EditorGUILayout.LabelField(field.name);
                            EditorStyles.textField.wordWrap = true;
                            field.value = EditorGUILayout.TextArea(field.value);
                        }
                        EditorGUILayout.Space();
                        EditorGUILayout.EndVertical();
                    }

                    // DYNAMIC IMAGE FIELDS
                    if (tooltipTrigger.dynamicImageFields.Count > 0)
                    {
                        EditorGUILayout.BeginVertical("Box");
                        EditorGUILayout.LabelField("Dynamic Image Fields", EditorStyles.boldLabel);
                        foreach (DynamicImageField field in tooltipTrigger.dynamicImageFields)
                        {
                            EditorGUILayout.LabelField(field.name);
                            field.replacementSprite =
                                EditorGUILayout.ObjectField(field.replacementSprite, typeof(Sprite), false) as Sprite;
                        }
                        EditorGUILayout.Space();
                        EditorGUILayout.EndVertical();
                    }

                    // DYNAMIC SECTIONS
                    if (tooltipTrigger.dynamicSectionFields.Count > 0)
                    {
                        EditorGUILayout.BeginVertical("Box");
                        EditorGUILayout.LabelField("Dynamic Sections", EditorStyles.boldLabel);
                        foreach (DynamicSectionField field in tooltipTrigger.dynamicSectionFields)
                        {
                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.LabelField(field.name, GUILayout.Width(166));
                            field.isOn = EditorGUILayout.Toggle(field.isOn, GUILayout.Width(11));
                            EditorGUILayout.LabelField("Visible", GUILayout.Width(40));
                            EditorGUILayout.EndHorizontal();
                        }
                        EditorGUILayout.Space();
                        EditorGUILayout.EndVertical();
                    }
                }
                else
                {
                    // Clear the fields
                    tooltipTrigger.parameterizedTextFields.Clear();
                    tooltipTrigger.dynamicImageFields.Clear();
                    tooltipTrigger.dynamicSectionFields.Clear();
                }

                // STAYS OPEN?
                EditorGUILayout.PropertyField(staysOpen, new GUIContent("Stays Open?"));
                tooltipTrigger.staysOpen = staysOpen.boolValue;

                // IS BLOCKING?
                if (tooltipTrigger.staysOpen)
                {
                    EditorGUILayout.PropertyField(isBlocking, new GUIContent("    Is Blocking?"));
                    tooltipTrigger.isBlocking = isBlocking.boolValue;
                }
                else
                {
                    isBlocking.boolValue      = false;
                    tooltipTrigger.isBlocking = false;
                }
            }

            // Draw the rest of the fields using the default inspector.
            DrawPropertiesExcluding(serializedObject, dontIncludeMe);

            if (GUI.changed)
            {
                EditorUtility.SetDirty(tooltipTrigger);
            }
            serializedObject.ApplyModifiedProperties();
        }