예제 #1
0
    public static List <TextLine> GetLines(string inText,
                                           GUIBase_FontEx inFont,
                                           HorizontalTextAlignment inAlignment,
                                           float inMaxlineWidth,
                                           Vector3 inScale,
                                           float inLineSpacePct,
                                           bool isForTextField = false)
    {
        if (string.IsNullOrEmpty(inText) == true)
        {
            return(null);
        }
        if (inMaxlineWidth <= 0.0f)
        {
            return(null);
        }
        if (inScale == Vector3.zero)
        {
            return(null);
        }

        List <TextLine> lines            = new List <TextLine>();
        TextLine        newLine          = null;
        int             lastSpaceIndex   = -1;
        float           widthAtLastSpace = 0;
        float           widthOfSpace     = inFont.GetCharWidth((int)' ') * inScale.x;
        float           charWidth        = 0;
        float           fontHeight       = inFont.GetFontHeight();

        for (int i = 0; i < inText.Length; ++i)
        {
            if (newLine == null)
            {
                newLine = new TextLine();
                newLine.m_StartIndex = i;
                newLine.m_SpaceWidth = widthOfSpace;
                lastSpaceIndex       = -1;
                widthAtLastSpace     = 0;
            }

            int character = (int)inText[i];

            if (!isForTextField && character == ' ')
            {
                lastSpaceIndex    = i;
                widthAtLastSpace  = newLine.m_Size.x;
                newLine.m_Size.x += widthOfSpace;
                newLine.m_NumOfSpaces++;
                continue;
            }
            switch (character)
            {
            case '\n':
                newLine.m_EndIndex       = i;
                newLine.m_EndOfParagraph = true;
                lines.Add(newLine);
                newLine = null;
                break;

            default:
                charWidth = inFont.GetCharWidth(character) * inScale.x;
                if (newLine.m_Size.x + charWidth > inMaxlineWidth)
                {
                    if (lastSpaceIndex >= 0)
                    {
                        newLine.m_NumOfSpaces--;
                        newLine.m_Size.x   = widthAtLastSpace;
                        newLine.m_EndIndex = lastSpaceIndex;
                        i = lastSpaceIndex;
                    }
                    else
                    {
                        newLine.m_EndIndex = i;
                        i = i - 1;
                    }

                    newLine.m_EndOfParagraph = false;
                    if ((newLine.m_EndIndex - newLine.m_StartIndex) < 1)
                    {
                        //this line is invalid. It looks that actual character is too width for inMaxlineWidth
                        // skip it.
                        Debug.LogWarning("Can't generate line for character: " + (char)character);
                    }
                    else
                    {
                        lines.Add(newLine);
                    }

                    newLine = null;
                }
                else
                {
                    newLine.m_Size.x += charWidth;
                }

                break;
            }
        }

        if (newLine != null)
        {
            newLine.m_EndIndex       = inText.Length;
            newLine.m_EndOfParagraph = true;

            if ((newLine.m_EndIndex - newLine.m_StartIndex) == 0)
            {
                Debug.LogWarning("Empty line");
            }
            else
            {
                lines.Add(newLine);
            }
        }

        float yOffset = 0;

        // Compute x offset of lines by TextAlignment.
        foreach (TextLine line in lines)
        {
#if !MADFINGER_KEYBOARD_MOUSE
            // remove spaces from start and end of line...
            TrimSpaces(inText, line, widthOfSpace);
#endif

            // Check line validity...
            if ((line.m_EndIndex - line.m_StartIndex) == 0)
            {
                //Debug.LogWarning("Empty line");
                continue;
            }

            // setup line x offset based on TextAlignment...
            float span = (inMaxlineWidth - line.m_Size.x);

            switch (inAlignment)
            {
            case HorizontalTextAlignment.Left:
                line.m_Offset.x = 0.0f;
                break;

            case HorizontalTextAlignment.Center:
                line.m_Offset.x = span * 0.5f;
                break;

            case HorizontalTextAlignment.Right:
                line.m_Offset.x = span;
                break;

            case HorizontalTextAlignment.Justify:
                if (line.m_EndOfParagraph == false && line.m_NumOfSpaces > 0)
                {
                    line.m_SpaceWidth += span / (float)line.m_NumOfSpaces;
                }
                break;

            default:
                Debug.LogError("Unknown Horizontal text alignment !!!! " + inAlignment);
                break;
            }

            yOffset        += 0.5f * fontHeight * inScale.y;
            line.m_Offset.y = yOffset;
            yOffset        += 0.5f * fontHeight * inScale.y;
            yOffset        += inLineSpacePct * fontHeight * inScale.y;
        }

        return(lines);
    }
예제 #2
0
    //---------------------------------------------------------
    void RegenerateSprites()
    {
        if (m_RegenerateSprites == false || m_Widget.IsVisible() == false)
        {
            return;
        }

        if (m_Font == null)
        {
            m_Font = MFFontManager.GetFont(m_FontName) as GUIBase_FontEx;
            if (m_Font == null)
            {
                Debug.LogError(gameObject.GetFullName() + " Can't load font with name " + m_FontName);
                return;
            }
        }

        if (m_TextID > 0)
        {
            m_Text = TextDatabase.instance[m_TextID];
        }

        // destroy old text if any exist...
        int maxSprites = text != null && text.Length > 0 ? Mathf.CeilToInt(text.Length * 0.1f) * 10 : 0;

        m_Widget.PrepareSprites(maxSprites);

        if (text != null && text.Length > 0)
        {
            Vector3 scale = transform.lossyScale;
            scale = Vector3.one;

            // setup cursor ...
            Vector2 leftUpPos = new Vector2(m_Widget.GetOrigPos().x - m_Widget.GetWidth() * 0.5f * scale.x,
                                            m_Widget.GetOrigPos().y - m_Widget.GetHeight() * 0.5f * scale.y);
            Vector2 cursor = leftUpPos;

            float maxLineSize = m_Widget.GetWidth() * scale.x;

            scale.x = m_TextScale.x;
            scale.y = m_TextScale.y;

            List <TextLine> textLines = GetLines(text, m_Font, alignment, maxLineSize, scale, lineSpace, IsForTextField);
            if (textLines == null || textLines.Count <= 0)
            {
                return;
            }

            float fontHeight = m_Font.GetFontHeight();
            m_TextSize = new Vector2(m_Widget.GetWidth(), textLines.Count * fontHeight * scale.y + (textLines.Count - 1) * lineSpace * fontHeight * scale.y);

            float width;
            Rect  spriteRect, texRect;

            int spriteIdx = 0;
            foreach (TextLine line in textLines)
            {
                cursor = leftUpPos + line.m_Offset;

                for (int i = line.m_StartIndex; i < line.m_EndIndex; ++i)
                {
                    int character = text[i];

                    if (!IsForTextField && character == ' ')
                    {
                        cursor.x += line.m_SpaceWidth;
                        continue;
                    }
                    switch (character)
                    {
                    case '\n':
                        Debug.LogWarning("function GetLines doesn't work correctly");
                        break;

                    default:
                    {
                        if (m_Font.GetCharDescription(text[i], out width, out spriteRect, out texRect, false, false, false))
                        {
                            Vector2 inCharSize   = new Vector2(spriteRect.width * scale.x, spriteRect.height * scale.y);
                            Vector2 inCharCenter = cursor + new Vector2(spriteRect.center.x * scale.x, 0.0f);

                            m_Widget.SetTextureCoords(spriteIdx, (int)texRect.x, (int)texRect.y, (int)texRect.width, (int)texRect.height);
                            m_Widget.UpdateSpritePosAndSize(spriteIdx, inCharCenter.x, inCharCenter.y, inCharSize.x, inCharSize.y);
                            m_Widget.ShowSprite(spriteIdx, true);
                            cursor.x += width * scale.x;

                            spriteIdx++;
                        }
                        break;
                    }
                    }
                }
            }

            // hide all unused sprites
            while (spriteIdx < maxSprites)
            {
                m_Widget.SetTextureCoords(spriteIdx, 0.0f, 0.0f, 0.0f, 0.0f);
                m_Widget.UpdateSpritePosAndSize(spriteIdx, 0.0f, -Screen.height, 1.0f, 1.0f);
                m_Widget.ShowSprite(spriteIdx, false);
                spriteIdx++;
            }
        }

        // we have to force widget update.
        m_RegenerateSprites = false;
        m_Widget.SetModify();
    }