Example #1
0
    public bool TestForHit(Vector2 screenPoint, Camera eventCamera, Action <string, int> block)
    {
        TMP_TextInfo textInfo = null;
        TMP_Text     text     = null;

        if (entity.text != null)
        {
            text     = entity.text;
            textInfo = entity.text.textInfo;
        }
        if (entity.textGUI != null)
        {
            text     = entity.textGUI;
            textInfo = entity.textGUI.textInfo;
        }

        if (textInfo != null)
        {
            // find the closest link to our touch point
            float        minDistance = 999999;
            TMP_LinkInfo minLinkInfo = new TMP_LinkInfo();

            Transform rectTransform = text.transform;
            Vector3   position      = screenPoint;

            // Convert position into Worldspace coordinates
            TMP_TextUtilities.ScreenPointToWorldPointInRectangle(rectTransform, position, eventCamera, out position);

            for (int i = 0; i < text.textInfo.linkCount; i++)
            {
                TMP_LinkInfo linkInfo = text.textInfo.linkInfo [i];

                bool isBeginRegion = false;

                Vector3 bl = Vector3.zero;
                Vector3 tl = Vector3.zero;
                Vector3 br = Vector3.zero;
                Vector3 tr = Vector3.zero;

                // Iterate through each character of the word
                for (int j = 0; j < linkInfo.linkTextLength; j++)
                {
                    int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
                    TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo [characterIndex];
                    int currentLine = currentCharInfo.lineNumber;

                    // Check if Link characters are on the current page
                    if (text.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay)
                    {
                        continue;
                    }

                    if (isBeginRegion == false)
                    {
                        isBeginRegion = true;

                        bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
                        tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));

                        // If Word is one character
                        if (linkInfo.linkTextLength == 1)
                        {
                            isBeginRegion = false;

                            br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
                            tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));

                            // Check for Intersection
                            IsCloserToPoint(position, bl, linkInfo, ref minDistance, ref minLinkInfo);
                            IsCloserToPoint(position, tl, linkInfo, ref minDistance, ref minLinkInfo);
                            IsCloserToPoint(position, tr, linkInfo, ref minDistance, ref minLinkInfo);
                            IsCloserToPoint(position, br, linkInfo, ref minDistance, ref minLinkInfo);
                            IsCloserToPoint(position, (bl + br + tl + tr) * 0.25f, linkInfo, ref minDistance, ref minLinkInfo);
                        }
                    }

                    // Last Character of Word
                    if (isBeginRegion && j == linkInfo.linkTextLength - 1)
                    {
                        isBeginRegion = false;

                        br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
                        tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));

                        IsCloserToPoint(position, bl, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, tl, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, tr, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, br, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, (bl + br + tl + tr) * 0.25f, linkInfo, ref minDistance, ref minLinkInfo);
                    }
                    // If Word is split on more than one line.
                    else if (isBeginRegion && currentLine != text.textInfo.characterInfo [characterIndex + 1].lineNumber)
                    {
                        isBeginRegion = false;

                        br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
                        tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));

                        IsCloserToPoint(position, bl, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, tl, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, tr, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, br, linkInfo, ref minDistance, ref minLinkInfo);
                        IsCloserToPoint(position, (bl + br + tl + tr) * 0.25f, linkInfo, ref minDistance, ref minLinkInfo);
                    }
                }
            }

            int linkIdx = Array.IndexOf(text.textInfo.linkInfo, minLinkInfo);
            if (linkIdx >= 0)
            {
                if (block != null)
                {
                    block(minLinkInfo.GetLinkText(), linkIdx);
                }
                return(true);
            }
        }

        return(false);
    }