WrapText() 공개 메소드

Text wrapping functionality. Legacy compatibility function.
public WrapText ( string text, float maxWidth, int maxLineCount ) : string
text string
maxWidth float
maxLineCount int
리턴 string
예제 #1
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText()
    {
        mChanged   = true;
        hasChanged = false;
        mLastText  = mText;

        mProcessedText = mText;
        if (TranslateReturn)
        {
            mProcessedText = mText.Replace("\\n", "\n");
        }

        if (mPassword)
        {
            string hidden = "";

            if (mShowLastChar)
            {
                for (int i = 0, imax = mProcessedText.Length - 1; i < imax; ++i)
                {
                    hidden += "*";
                }
                if (mProcessedText.Length > 0)
                {
                    hidden += mProcessedText[mProcessedText.Length - 1];
                }
            }
            else
            {
                for (int i = 0, imax = mProcessedText.Length; i < imax; ++i)
                {
                    hidden += "*";
                }
            }
            mProcessedText = mFont.WrapText(hidden, mMaxLineWidth / cachedTransform.localScale.x,
                                            mMaxLineCount, false, UIFont.SymbolStyle.None);
        }
        else if (mMaxLineWidth > 0)
        {
            mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / cachedTransform.localScale.x, mMaxLineCount, mEncoding, mSymbols, mSpacingX);
        }
        else if (mMaxLineCount > 0)
        {
            mProcessedText = mFont.WrapText(mProcessedText, 100000f, mMaxLineCount, mEncoding, mSymbols, mSpacingX);
        }

        mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols, mSpacingX, mSpacingY, TranslateReturn) : Vector2.one;
        float scale = cachedTransform.localScale.x;

        mSize.x = Mathf.Max(mSize.x, (mFont != null && scale > 1f) ? lineWidth / scale : 1f);
        mSize.y = Mathf.Max(mSize.y, 1f);
    }
예제 #2
0
파일: UILabel.cs 프로젝트: yazici/FRONTIERS
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText()
    {
        mChanged       = true;
        hasChanged     = false;
        mLastText      = mText;
        mProcessedText = mText.Replace("\\n", "\n");

        if (mPassword)
        {
            mProcessedText = mFont.WrapText(mProcessedText, 100000f, false, false);

            string hidden = "";

            if (mShowLastChar)
            {
                for (int i = 1, imax = mProcessedText.Length; i < imax; ++i)
                {
                    hidden += "*";
                }
                if (mProcessedText.Length > 0)
                {
                    hidden += mProcessedText[mProcessedText.Length - 1];
                }
            }
            else
            {
                for (int i = 0, imax = mProcessedText.Length; i < imax; ++i)
                {
                    hidden += "*";
                }
            }
            mProcessedText = hidden;
        }
        else if (mMaxLineWidth > 0)
        {
            mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / cachedTransform.localScale.x, mMultiline, mEncoding);
        }
        else if (!mMultiline)
        {
            mProcessedText = mFont.WrapText(mProcessedText, 100000f, false, mEncoding);
        }

        mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding) : Vector2.one;
        float scale = cachedTransform.localScale.x;

        mSize.x = Mathf.Max(mSize.x, (mFont != null && scale > 1f) ? lineWidth / scale : 1f);
        mSize.y = Mathf.Max(mSize.y, 1f);
    }
예제 #3
0
    protected void Add(string text, bool updateVisible)
    {
        Paragraph paragraph = null;

        if (mParagraphs.Count < maxEntries)
        {
            paragraph = new Paragraph();
        }
        else
        {
            paragraph = mParagraphs[0];
            mParagraphs.RemoveAt(0);
        }
        paragraph.text = text;
        mParagraphs.Add(paragraph);
        if (textLabel != null && textLabel.font != null)
        {
            Paragraph paragraph2 = paragraph;
            UIFont    font       = textLabel.font;
            string    text2      = paragraph.text;
            float     num        = maxWidth;
            Vector3   localScale = textLabel.transform.localScale;
            paragraph2.lines = font.WrapText(text2, num / localScale.y, textLabel.maxLineCount, textLabel.supportEncoding, textLabel.symbolStyle).Split(mSeparator);
            mTotalLines      = 0;
            int i = 0;
            for (int count = mParagraphs.Count; i < count; i++)
            {
                mTotalLines += mParagraphs[i].lines.Length;
            }
        }
        if (updateVisible)
        {
            UpdateVisibleText();
        }
    }
예제 #4
0
 private void Update()
 {
     if (mLabel == null)
     {
         mLabel = GetComponent <UILabel>();
         mLabel.supportEncoding = false;
         mLabel.symbolStyle     = UIFont.SymbolStyle.None;
         UIFont  font       = mLabel.font;
         string  text       = mLabel.text;
         float   num        = mLabel.lineWidth;
         Vector3 localScale = mLabel.cachedTransform.localScale;
         mText = font.WrapText(text, num / localScale.x, mLabel.maxLineCount, encoding: false, UIFont.SymbolStyle.None);
     }
     if (mOffset < mText.Length)
     {
         if (mNextChar <= Time.time)
         {
             charsPerSecond = Mathf.Max(1, charsPerSecond);
             float num2 = 1f / (float)charsPerSecond;
             char  c    = mText[mOffset];
             if (c == '.' || c == '\n' || c == '!' || c == '?')
             {
                 num2 *= 4f;
             }
             mNextChar   = Time.time + num2;
             mLabel.text = mText.Substring(0, ++mOffset);
         }
     }
     else
     {
         Object.Destroy(this);
     }
 }
예제 #5
0
    /// <summary>
    /// Rebuild the visible text.
    /// </summary>

    protected void Rebuild()
    {
        if (isValid)
        {
            // Although we could simply use UILabel.Wrap, it would mean setting the same data
            // over and over every paragraph, which is not ideal. It's faster to only do it once
            // and then do wrapping ourselves in the 'for' loop below.
            textLabel.UpdateNGUIText();
            NGUIText.current.lineHeight = 1000000;

            UIFont bitmapFont = textLabel.bitmapFont;
            mTotalLines = 0;

            for (int i = 0; i < mParagraphs.size; ++i)
            {
                string    final;
                Paragraph p = mParagraphs.buffer[i];

                if (bitmapFont != null)
                {
                    if (bitmapFont.WrapText(p.text, out final))
                    {
                        p.lines      = final.Split('\n');
                        mTotalLines += p.lines.Length;
                    }
                }
#if DYNAMIC_FONT
                else if (NGUIText.WrapText(textLabel.trueTypeFont, p.text, out final))
                {
                    p.lines      = final.Split('\n');
                    mTotalLines += p.lines.Length;
                }
#endif
            }

            // Recalculate the total number of lines
            mTotalLines = 0;
            for (int i = 0, imax = mParagraphs.size; i < imax; ++i)
            {
                mTotalLines += mParagraphs.buffer[i].lines.Length;
            }

            // Update the bar's size
            if (scrollBar != null)
            {
                UIScrollBar sb = scrollBar as UIScrollBar;
                if (sb != null)
                {
                    sb.barSize = 1f - (float)scrollHeight / mTotalLines;
                }
            }

            // Update the visible text
            UpdateVisibleText();
        }
    }
예제 #6
0
    internal void UpdateLabel()
    {
        string str;

        if (this.maxChars > 0 && this.mText.Length > this.maxChars)
        {
            this.mText = this.mText.Substring(0, this.maxChars);
        }
        if (this.label.font != null)
        {
            str = (!this.selected ? this.mText : string.Concat(this.mText, this.mLastIME));
            this.label.supportEncoding = false;
            UIFont uIFont = this.label.font;
            List <UITextMarkup> uITextMarkups = this.markups;
            float   single  = (float)this.label.lineWidth;
            Vector3 vector3 = this.label.cachedTransform.localScale;
            str = uIFont.WrapText(uITextMarkups, str, single / vector3.x, 0, false, UIFont.SymbolStyle.None);
            this.markups.SortMarkup();
            this.label.text = str;
            this.label.showLastPasswordChar = this.selected;
        }
    }
예제 #7
0
 protected void Add(string text, bool updateVisible)
 {
     UITextList.Paragraph item = null;
     if (this.mParagraphs.Count >= this.maxEntries)
     {
         item = this.mParagraphs[0];
         this.mParagraphs.RemoveAt(0);
     }
     else
     {
         item = new UITextList.Paragraph();
     }
     item.text = text;
     this.mParagraphs.Add(item);
     if (this.textLabel != null && this.textLabel.font != null)
     {
         UIFont uIFont = this.textLabel.font;
         List <UITextMarkup> uITextMarkups = UIFont.tempMarkup;
         string  str     = item.text;
         float   single  = this.maxWidth;
         Vector3 vector3 = this.textLabel.transform.localScale;
         item.lines       = uIFont.WrapText(uITextMarkups, str, single / vector3.y, this.textLabel.maxLineCount, this.textLabel.supportEncoding, this.textLabel.symbolStyle).Split(this.mSeparator);
         this.mTotalLines = 0;
         int num   = 0;
         int count = this.mParagraphs.Count;
         while (num < count)
         {
             UITextList length = this;
             length.mTotalLines = length.mTotalLines + (int)this.mParagraphs[num].lines.Length;
             num++;
         }
     }
     if (updateVisible)
     {
         this.UpdateVisibleText();
     }
 }
예제 #8
0
    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }

        var num = Mathf.RoundToInt(maxWidth * size);

        if (num < 1)
        {
            return(text);
        }

        var s            = new StringBuilder();
        var length       = text.Length;
        var num3         = num;
        var previousChar = 0;
        var startIndex   = 0;
        var offset       = 0;
        var flag         = true;
        var flag2        = maxLineCount != 1;
        var num7         = 1;
        var flag3        = encoding && symbolStyle != SymbolStyle.None && hasSymbols;
        var isDynamic    = this.isDynamic;

        if (isDynamic)
        {
            mDynamicFont.textureRebuildCallback = OnFontChanged;
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
            mDynamicFont.textureRebuildCallback = null;
        }

        while (offset < length)
        {
            var ch = text[offset];
            if (ch == '\n')
            {
                if (!flag2 || num7 == maxLineCount)
                {
                    break;
                }

                num3 = num;
                if (startIndex < offset)
                {
                    s.Append(text.Substring(startIndex, offset - startIndex + 1));
                }
                else
                {
                    s.Append(ch);
                }

                flag = true;
                num7++;
                startIndex   = offset + 1;
                previousChar = 0;
                goto Label_03E7;
            }

            if (ch == ' ' && previousChar != 32 && startIndex < offset)
            {
                s.Append(text.Substring(startIndex, offset - startIndex + 1));
                flag         = false;
                startIndex   = offset + 1;
                previousChar = ch;
            }

            if (encoding && ch == '[' && offset + 2 < length)
            {
                if (text[offset + 1] == '-' && text[offset + 2] == ']')
                {
                    offset += 2;
                    goto Label_03E7;
                }

                if (offset + 7 < length && text[offset + 7] == ']' && NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                {
                    offset += 7;
                    goto Label_03E7;
                }
            }

            var symbol    = !flag3 ? null : MatchSymbol(text, offset, length);
            var mSpacingX = this.mSpacingX;
            if (!isDynamic)
            {
                if (symbol != null)
                {
                    mSpacingX += symbol.advance;
                }
                else
                {
                    var glyph = symbol != null ? null : mFont.GetGlyph(ch);
                    if (glyph == null)
                    {
                        goto Label_03E7;
                    }

                    mSpacingX += previousChar == 0 ? glyph.advance : glyph.advance + glyph.GetKerning(previousChar);
                }
            }
            else if (mDynamicFont.GetCharacterInfo(ch, out mChar, mDynamicFontSize, mDynamicFontStyle))
            {
                mSpacingX += Mathf.RoundToInt(mChar.width);
            }

            num3 -= mSpacingX;
            if (num3 < 0)
            {
                if (flag || !flag2 || num7 == maxLineCount)
                {
                    s.Append(text.Substring(startIndex, Mathf.Max(0, offset - startIndex)));
                    if (!flag2 || num7 == maxLineCount)
                    {
                        startIndex = offset;
                        break;
                    }

                    EndLine(ref s);
                    flag = true;
                    num7++;
                    if (ch == ' ')
                    {
                        startIndex = offset + 1;
                        num3       = num;
                    }
                    else
                    {
                        startIndex = offset;
                        num3       = num - mSpacingX;
                    }

                    previousChar = 0;
                    goto Label_03C8;
                }

                while (startIndex < length && text[startIndex] == ' ')
                {
                    startIndex++;
                }

                flag         = true;
                num3         = num;
                offset       = startIndex - 1;
                previousChar = 0;
                if (!flag2 || num7 == maxLineCount)
                {
                    break;
                }

                num7++;
                EndLine(ref s);
                goto Label_03E7;
            }

            previousChar = ch;
Label_03C8:
            if (!isDynamic && symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }

Label_03E7:
            offset++;
        }

        if (startIndex < offset)
        {
            s.Append(text.Substring(startIndex, offset - startIndex));
        }

        return(s.ToString());
    }
예제 #9
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText(bool legacyMode)
    {
        if (!isValid)
        {
            return;
        }

        mChanged   = true;
        hasChanged = false;

        int   fs      = fontSize;
        float invFS   = 1f / fs;
        float ps      = pixelSize;
        float invSize = 1f / ps;
        float lw      = legacyMode ? (mMaxLineWidth != 0 ? mMaxLineWidth * invSize : 1000000) : width * invSize;
        float lh      = legacyMode ? (mMaxLineHeight != 0 ? mMaxLineHeight * invSize : 1000000) : height * invSize;

        mScale       = 1f;
        mPrintedSize = Mathf.Abs(legacyMode ? Mathf.RoundToInt(cachedTransform.localScale.x) : fs);

        NGUIText.current.size = fs;
        UpdateNGUIText();

        if (mPrintedSize > 0)
        {
            for (;;)
            {
                mScale = mPrintedSize * invFS;

                bool fits = true;

                NGUIText.current.lineWidth  = (mOverflow == Overflow.ResizeFreely) ? 1000000 : Mathf.RoundToInt(lw / mScale);
                NGUIText.current.lineHeight = (mOverflow == Overflow.ResizeFreely || mOverflow == Overflow.ResizeHeight) ?
                                              1000000 : Mathf.RoundToInt(lh / mScale);

                if (lw > 0f || lh > 0f)
                {
                    if (mFont != null)
                    {
                        fits = mFont.WrapText(mText, out mProcessedText);
                    }
#if DYNAMIC_FONT
                    else
                    {
                        fits = NGUIText.WrapText(mTrueTypeFont, mText, out mProcessedText);
                    }
#endif
                }
                else
                {
                    mProcessedText = mText;
                }

                // Remember the final printed size
                if (!string.IsNullOrEmpty(mProcessedText))
                {
                    if (mFont != null)
                    {
                        mCalculatedSize = mFont.CalculatePrintedSize(mProcessedText);
                    }
#if DYNAMIC_FONT
                    else
                    {
                        mCalculatedSize = NGUIText.CalculatePrintedSize(mTrueTypeFont, mProcessedText);
                    }
#endif
                }
                else
                {
                    mCalculatedSize = Vector2.zero;
                }

                if (mOverflow == Overflow.ResizeFreely)
                {
                    mWidth  = Mathf.RoundToInt(mCalculatedSize.x * ps);
                    mHeight = Mathf.RoundToInt(mCalculatedSize.y * ps);
                }
                else if (mOverflow == Overflow.ResizeHeight)
                {
                    mHeight = Mathf.RoundToInt(mCalculatedSize.y * ps);
                }
                else if (mOverflow == Overflow.ShrinkContent && !fits)
                {
                    if (--mPrintedSize > 1)
                    {
                        continue;
                    }
                }

                // Upgrade to the new system
                if (legacyMode)
                {
                    width  = Mathf.RoundToInt(mCalculatedSize.x * ps);
                    height = Mathf.RoundToInt(mCalculatedSize.y * ps);
                    cachedTransform.localScale = Vector3.one;
                }
                break;
            }
        }
        else
        {
            cachedTransform.localScale = Vector3.one;
            mProcessedText             = "";
            mScale = 1f;
        }
    }
예제 #10
0
    private void UpdateLabel()
    {
        if (mDoInit)
        {
            initMain();
        }
        if (maxChars > 0 && mText.Length > maxChars)
        {
            mText = mText.Substring(0, maxChars);
        }
        if (label.font == null)
        {
            return;
        }
        string str;

        if (isPassword && selected)
        {
            str = string.Empty;
            int i = 0;
            for (int length = mText.Length; i < length; i++)
            {
                str += "*";
            }
            str = str + Input.compositionString + caratChar;
        }
        else
        {
            str = ((!selected) ? mText : (mText + Input.compositionString + caratChar));
        }
        label.supportEncoding = false;
        if (!label.shrinkToFit)
        {
            if (label.multiLine)
            {
                UIFont  font       = label.font;
                string  text       = str;
                float   num        = label.lineWidth;
                Vector3 localScale = label.cachedTransform.localScale;
                str = font.WrapText(text, num / localScale.x, 0, encoding: false, UIFont.SymbolStyle.None);
            }
            else
            {
                UIFont  font2             = label.font;
                string  text2             = str;
                float   num2              = label.lineWidth;
                Vector3 localScale2       = label.cachedTransform.localScale;
                string  endOfLineThatFits = font2.GetEndOfLineThatFits(text2, num2 / localScale2.x, encoding: false, UIFont.SymbolStyle.None);
                if (endOfLineThatFits != str)
                {
                    str = endOfLineThatFits;
                    Vector3 localPosition = label.cachedTransform.localPosition;
                    localPosition.x = mPosition + (float)label.lineWidth;
                    if (mPivot == UIWidget.Pivot.Left)
                    {
                        label.pivot = UIWidget.Pivot.Right;
                    }
                    else if (mPivot == UIWidget.Pivot.TopLeft)
                    {
                        label.pivot = UIWidget.Pivot.TopRight;
                    }
                    else if (mPivot == UIWidget.Pivot.BottomLeft)
                    {
                        label.pivot = UIWidget.Pivot.BottomRight;
                    }
                    label.cachedTransform.localPosition = localPosition;
                }
                else
                {
                    RestoreLabel();
                }
            }
        }
        label.text = str;
        label.showLastPasswordChar = selected;
    }
예제 #11
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText(bool legacyMode)
    {
        if (mFont == null)
        {
            return;
        }

        mChanged   = true;
        hasChanged = false;

        float invSize   = 1f / mFont.pixelSize;
        float printSize = Mathf.Abs(legacyMode ? cachedTransform.localScale.x : mFont.size);
        float lw        = legacyMode ? (mMaxLineWidth != 0 ? mMaxLineWidth * invSize : 1000000) : width * invSize;
        float lh        = legacyMode ? (mMaxLineHeight != 0 ? mMaxLineHeight * invSize : 1000000) : height * invSize;

        if (printSize > 0f)
        {
            for (;;)
            {
                mScale = printSize / mFont.size;

                bool fits = true;

                int pw = (mOverflow == Overflow.ResizeFreely) ? 100000 : Mathf.RoundToInt(lw / mScale);
                int ph = (mOverflow == Overflow.ResizeFreely || mOverflow == Overflow.ResizeHeight) ?
                         100000 : Mathf.RoundToInt(lh / mScale);

                if (mPassword)
                {
                    mProcessedText = "";

                    if (mShowLastChar)
                    {
                        for (int i = 0, imax = mText.Length - 1; i < imax; ++i)
                        {
                            mProcessedText += "*";
                        }
                        if (mText.Length > 0)
                        {
                            mProcessedText += mText[mText.Length - 1];
                        }
                    }
                    else
                    {
                        for (int i = 0, imax = mText.Length; i < imax; ++i)
                        {
                            mProcessedText += "*";
                        }
                    }

                    fits = mFont.WrapText(mProcessedText, out mProcessedText, pw, ph, mMaxLineCount, false, UIFont.SymbolStyle.None);
                }
                else if (lw > 0f || lh > 0f)
                {
                    fits = mFont.WrapText(mText, out mProcessedText, pw, ph, mMaxLineCount, mEncoding, mSymbols);
                }
                else
                {
                    mProcessedText = mText;
                }

                // Remember the final printed size
                mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols) : Vector2.zero;

                if (mOverflow == Overflow.ResizeFreely)
                {
                    mWidth  = Mathf.RoundToInt(mSize.x * mFont.pixelSize);
                    mHeight = Mathf.RoundToInt(mSize.y * mFont.pixelSize);
                }
                else if (mOverflow == Overflow.ResizeHeight)
                {
                    mHeight = Mathf.RoundToInt(mSize.y * mFont.pixelSize);
                }
                else if (mOverflow == Overflow.ShrinkContent && !fits)
                {
                    printSize = Mathf.Round(printSize - 1f);
                    if (printSize > 1f)
                    {
                        continue;
                    }
                }

                // Upgrade to the new system
                if (legacyMode)
                {
                    width  = Mathf.RoundToInt(mSize.x * mFont.pixelSize);
                    height = Mathf.RoundToInt(mSize.y * mFont.pixelSize);
                    cachedTransform.localScale = Vector3.one;
                }
                break;
            }
        }
        else
        {
            cachedTransform.localScale = Vector3.one;
            mProcessedText             = "";
            mScale = 1f;
        }
    }
예제 #12
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText()
    {
        mChanged       = true;
        hasChanged     = false;
        mLastText      = mText;
        mProcessedText = mText;

        if (mPassword)
        {
            string hidden = "";

            if (mShowLastChar)
            {
                for (int i = 0, imax = mProcessedText.Length - 1; i < imax; ++i)
                {
                    hidden += "*";
                }
                if (mProcessedText.Length > 0)
                {
                    hidden += mProcessedText[mProcessedText.Length - 1];
                }
            }
            else
            {
                for (int i = 0, imax = mProcessedText.Length; i < imax; ++i)
                {
                    hidden += "*";
                }
            }
            mProcessedText = mFont.WrapText(hidden, mMaxLineWidth / cachedTransform.localScale.x,
                                            mMaxLineCount, false, UIFont.SymbolStyle.None);
        }
        else if (!mShrinkToFit)
        {
            if (mMaxLineWidth > 0)
            {
                mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / cachedTransform.localScale.x, mMaxLineCount, mEncoding, mSymbols);
            }
            else if (mMaxLineCount > 0)
            {
                mProcessedText = mFont.WrapText(mProcessedText, 100000f, mMaxLineCount, mEncoding, mSymbols);
            }
        }

        float scale = Mathf.Abs(cachedTransform.localScale.x);

        mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols) : Vector2.one;

        if (mShrinkToFit && mMaxLineWidth > 0)
        {
            // We want to shrink the label (when it doesn't fit)
            if (scale > 0f)
            {
                float maxSize = (float)mMaxLineWidth / mFont.size;
                scale = (mSize.x > maxSize) ? (maxSize / mSize.x) * mFont.size : mFont.size;
                cachedTransform.localScale = new Vector3(scale, scale, 1f);
            }
            else
            {
                mSize.x = 1f;
                cachedTransform.localScale = new Vector3(mFont.size, mFont.size, 1f);
            }
        }
        else
        {
            mSize.x = Mathf.Max(mSize.x, (scale > 0f) ? lineWidth / scale : 1f);
        }

        mSize.y = Mathf.Max(mSize.y, 1f);
    }
예제 #13
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }

        // Width of the line in pixels
        int lineWidth = Mathf.RoundToInt(maxWidth * size);

        if (lineWidth < 1)
        {
            return(text);
        }

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (maxLineCount != 1);
        int           lineCount      = 1;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        if (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                        {
                            offset += 7;
                            continue;
                        }
                    }
                }
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, offset, textLength) : null;

            // Find the glyph for this character
            BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(ch) : null;

            // Calculate how wide this symbol or character is going to be
            int glyphWidth = mSpacingX;

            if (symbol != null)
            {
                glyphWidth += symbol.width;
            }
            else if (glyph != null)
            {
                glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
            }
            else
            {
                continue;
            }

            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = lineWidth;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = lineWidth - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = lineWidth;
                    offset         = start - 1;
                    previousChar   = 0;
                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        return(sb.ToString());
    }
예제 #14
0
    private void ProcessText()
    {
        mChanged   = true;
        hasChanged = false;
        mLastText  = mText;
        Vector3 localScale = base.cachedTransform.localScale;
        float   num        = Mathf.Abs(localScale.x);
        float   num2       = mFont.size * mMaxLineCount;

        if (num > 0f)
        {
            while (true)
            {
                if (mPassword)
                {
                    mProcessedText = string.Empty;
                    if (mShowLastChar)
                    {
                        int i = 0;
                        for (int num3 = mText.Length - 1; i < num3; i++)
                        {
                            mProcessedText += "*";
                        }
                        if (mText.Length > 0)
                        {
                            mProcessedText += mText[mText.Length - 1];
                        }
                    }
                    else
                    {
                        int j = 0;
                        for (int length = mText.Length; j < length; j++)
                        {
                            mProcessedText += "*";
                        }
                    }
                    mProcessedText = mFont.WrapText(mProcessedText, (float)mMaxLineWidth / num, mMaxLineCount, encoding: false, UIFont.SymbolStyle.None);
                }
                else if (mMaxLineWidth > 0)
                {
                    mProcessedText = mFont.WrapText(mText, (float)mMaxLineWidth / num, (!mShrinkToFit) ? mMaxLineCount : 0, mEncoding, mSymbols);
                }
                else if (!mShrinkToFit && mMaxLineCount > 0)
                {
                    mProcessedText = mFont.WrapText(mText, 100000f, mMaxLineCount, mEncoding, mSymbols);
                }
                else
                {
                    mProcessedText = mText;
                }
                mSize = (string.IsNullOrEmpty(mProcessedText) ? Vector2.one : mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols));
                if (!mShrinkToFit)
                {
                    break;
                }
                if (mMaxLineCount > 0 && mSize.y * num > num2)
                {
                    num = Mathf.Round(num - 1f);
                    if (num > 1f)
                    {
                        continue;
                    }
                }
                if (mMaxLineWidth > 0)
                {
                    float num4 = (float)mMaxLineWidth / num;
                    float a    = (!(mSize.x * num > num4)) ? num : (num4 / mSize.x * num);
                    num = Mathf.Min(a, num);
                }
                num = Mathf.Round(num);
                base.cachedTransform.localScale = new Vector3(num, num, 1f);
                break;
            }
            mSize.x = Mathf.Max(mSize.x, (!(num > 0f)) ? 1f : ((float)lineWidth / num));
        }
        else
        {
            mSize.x = 1f;
            num     = mFont.size;
            base.cachedTransform.localScale = new Vector3(0.01f, 0.01f, 1f);
            mProcessedText = string.Empty;
        }
        mSize.y = Mathf.Max(mSize.y, 1f);
    }
예제 #15
0
    /// <summary>
    /// Text wrapping functionality. The 'width' and 'height' should be in pixels.
    /// </summary>

    public bool WrapText(string text, int size, out string finalText, int width, int height, int maxLines, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, size, out finalText, width, height, maxLines, encoding, symbolStyle));
        }

#if DYNAMIC_FONT
        if (isDynamic)
        {
            return(NGUIText.WrapText(text, mDynamicFont, size, mDynamicFontStyle, width, height, maxLines, encoding, out finalText));
        }
#endif
        if (width < 1 || height < 1)
        {
            finalText = "";
            return(false);
        }

        int maxLineCount = (maxLines > 0) ? maxLines : 999999;
        maxLineCount = Mathf.Min(maxLineCount, height / size);

        if (maxLineCount == 0)
        {
            finalText = "";
            return(false);
        }

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = width;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (maxLines != 1);
        int           lineCount      = 1;
        bool          useSymbols     = encoding && symbolStyle != SymbolStyle.None && hasSymbols;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = width;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (NGUIText.ParseSymbol(text, ref offset))
            {
                --offset; continue;
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, textLength) : null;

            // Calculate how wide this symbol or character is going to be
            int glyphWidth = mSpacingX;

            if (symbol != null)
            {
                glyphWidth += symbol.advance;
            }
            else
            {
                // Find the glyph for this character
                BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(ch) : null;

                if (glyph != null)
                {
                    glyphWidth += (previousChar != 0) ?
                                  glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
                }

                else
                {
                    continue;
                }
            }

            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    NGUIText.EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = width;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = width - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = width;
                    offset         = start - 1;
                    previousChar   = 0;

                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    NGUIText.EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        finalText = sb.ToString();
        return(!multiline || offset == textLength || (maxLines > 0 && lineCount <= maxLines));
    }
예제 #16
0
    private void ProcessText()
    {
        float single;

        base.ChangedAuto();
        this.mLastText = this.mText;
        this.markups.Clear();
        string str = this.mProcessedText;

        this.mProcessedText = this.mText;
        if (this.mPassword)
        {
            this.mProcessedText = this.mFont.WrapText(this.markups, this.mProcessedText, 100000f, 1, false, UIFont.SymbolStyle.None);
            string empty = string.Empty;
            if (!this.mShowLastChar)
            {
                int num    = 0;
                int length = this.mProcessedText.Length;
                while (num < length)
                {
                    empty = string.Concat(empty, "*");
                    num++;
                }
            }
            else
            {
                int num1    = 1;
                int length1 = this.mProcessedText.Length;
                while (num1 < length1)
                {
                    empty = string.Concat(empty, "*");
                    num1++;
                }
                if (this.mProcessedText.Length > 0)
                {
                    empty = string.Concat(empty, this.mProcessedText[this.mProcessedText.Length - 1]);
                }
            }
            this.mProcessedText = empty;
        }
        else if (this.mMaxLineWidth > 0)
        {
            UIFont uIFont = this.mFont;
            List <UITextMarkup> uITextMarkups = this.markups;
            string  str1    = this.mProcessedText;
            float   single1 = (float)this.mMaxLineWidth;
            Vector3 vector3 = base.cachedTransform.localScale;
            this.mProcessedText = uIFont.WrapText(uITextMarkups, str1, single1 / vector3.x, this.mMaxLineCount, this.mEncoding, this.mSymbols);
        }
        else if (this.mMaxLineCount > 0)
        {
            this.mProcessedText = this.mFont.WrapText(this.markups, this.mProcessedText, 100000f, this.mMaxLineCount, this.mEncoding, this.mSymbols);
        }
        this.mSize = (string.IsNullOrEmpty(this.mProcessedText) ? Vector2.one : this.mFont.CalculatePrintedSize(this.mProcessedText, this.mEncoding, this.mSymbols));
        float single2 = base.cachedTransform.localScale.x;
        float single3 = this.mSize.x;

        single       = (!(this.mFont != null) || single2 <= 1f ? 1f : (float)this.lineWidth / single2);
        this.mSize.x = Mathf.Max(single3, single);
        this.mSize.y = Mathf.Max(this.mSize.y, 1f);
        if (str != this.mProcessedText)
        {
            this.mSelection = new UITextSelection();
        }
        this.ApplyChanges();
    }
예제 #17
0
    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * (float)size);

        if (num < 1)
        {
            return(text);
        }
        StringBuilder s         = new StringBuilder();
        int           length    = text.Length;
        int           num2      = num;
        int           num3      = 0;
        int           i         = 0;
        int           j         = 0;
        bool          flag      = true;
        bool          flag2     = maxLineCount != 1;
        int           num4      = 1;
        bool          flag3     = encoding && symbolStyle != 0 && hasSymbols;
        bool          isDynamic = this.isDynamic;

        if (isDynamic)
        {
            Font.textureRebuilt += OnFontChanged;
            // mDynamicFont.textureRebuildCallback = OnFontChanged;
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
            // mDynamicFont.textureRebuildCallback = null;
            Font.textureRebuilt -= OnFontChanged;
        }
        for (; j < length; j++)
        {
            char c = text[j];
            if (c == '\n')
            {
                if (!flag2 || num4 == maxLineCount)
                {
                    break;
                }
                num2 = num;
                if (i < j)
                {
                    s.Append(text.Substring(i, j - i + 1));
                }
                else
                {
                    s.Append(c);
                }
                flag = true;
                num4++;
                i    = j + 1;
                num3 = 0;
                continue;
            }
            if (c == ' ' && num3 != 32 && i < j)
            {
                s.Append(text.Substring(i, j - i + 1));
                flag = false;
                i    = j + 1;
                num3 = c;
            }
            if (encoding && c == '[' && j + 2 < length)
            {
                if (text[j + 1] == '-' && text[j + 2] == ']')
                {
                    j += 2;
                    continue;
                }
                if (j + 7 < length && text[j + 7] == ']' && NGUITools.EncodeColor(NGUITools.ParseColor(text, j + 1)) == text.Substring(j + 1, 6).ToUpper())
                {
                    j += 7;
                    continue;
                }
            }
            BMSymbol bMSymbol = (!flag3) ? null : MatchSymbol(text, j, length);
            int      num5     = mSpacingX;
            if (!isDynamic)
            {
                if (bMSymbol != null)
                {
                    num5 += bMSymbol.advance;
                }
                else
                {
                    BMGlyph bMGlyph = (bMSymbol != null) ? null : mFont.GetGlyph(c);
                    if (bMGlyph == null)
                    {
                        continue;
                    }
                    num5 += ((num3 == 0) ? bMGlyph.advance : (bMGlyph.advance + bMGlyph.GetKerning(num3)));
                }
            }
            else if (mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle))
            {
                num5 += Mathf.RoundToInt(mChar.width);
            }
            num2 -= num5;
            if (num2 < 0)
            {
                if (!flag && flag2 && num4 != maxLineCount)
                {
                    for (; i < length && text[i] == ' '; i++)
                    {
                    }
                    flag = true;
                    num2 = num;
                    j    = i - 1;
                    num3 = 0;
                    if (!flag2 || num4 == maxLineCount)
                    {
                        break;
                    }
                    num4++;
                    EndLine(ref s);
                    continue;
                }
                s.Append(text.Substring(i, Mathf.Max(0, j - i)));
                if (!flag2 || num4 == maxLineCount)
                {
                    i = j;
                    break;
                }
                EndLine(ref s);
                flag = true;
                num4++;
                if (c == ' ')
                {
                    i    = j + 1;
                    num2 = num;
                }
                else
                {
                    i    = j;
                    num2 = num - num5;
                }
                num3 = 0;
            }
            else
            {
                num3 = c;
            }
            if (!isDynamic && bMSymbol != null)
            {
                j   += bMSymbol.length - 1;
                num3 = 0;
            }
        }
        if (i < j)
        {
            s.Append(text.Substring(i, j - i));
        }
        return(s.ToString());
    }
예제 #18
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public bool WrapText(string text, out string finalText, float width, float height, int lines, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, out finalText, width, height, lines, encoding, symbolStyle));
        }

        // Zero means unlimited
        if (width == 0f)
        {
            width = 100000f;
        }
        if (height == 0f)
        {
            height = 100000f;
        }

        // Width and height of the line in pixels
        int lineWidth  = Mathf.FloorToInt(width * size);
        int lineHeight = Mathf.FloorToInt(height * size);

        if (lineWidth < 1 || lineHeight < 1)
        {
            finalText = "";
            return(false);
        }

        int maxLineCount = (lines > 0) ? lines : 999999;

        if (height != 0f)
        {
            maxLineCount = Mathf.Min(maxLineCount, Mathf.FloorToInt(height));

            if (maxLineCount == 0)
            {
                finalText = "";
                return(false);
            }
        }

#if DYNAMIC_FONT
        if (mDynamicFont != null)
        {
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize);
        }
#endif
        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (lines != 1);
        int           lineCount      = 1;
        bool          useSymbols     = encoding && symbolStyle != SymbolStyle.None && hasSymbols;
        bool          dynamic        = isDynamic;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        if (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                        {
                            offset += 7;
                            continue;
                        }
                    }
                }
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, textLength) : null;

            // Calculate how wide this symbol or character is going to be
            int glyphWidth = mSpacingX;

            if (!dynamic)
            {
                if (symbol != null)
                {
                    glyphWidth += symbol.advance;
                }
                else
                {
                    // Find the glyph for this character
                    BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(ch) : null;

                    if (glyph != null)
                    {
                        glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
#if DYNAMIC_FONT
            else
            {
                if (mDynamicFont.GetCharacterInfo(ch, out mChar, mDynamicFontSize, mDynamicFontStyle))
                {
                    glyphWidth += Mathf.RoundToInt(mChar.width);
                }
            }
#endif
            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = lineWidth;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = lineWidth - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = lineWidth;
                    offset         = start - 1;
                    previousChar   = 0;

                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (!dynamic && symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        finalText = sb.ToString();
        return(!multiline || offset == textLength || (lines > 0 && lineCount <= lines));
    }
예제 #19
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public string WrapText(string text, float maxWidth, bool multiline, bool encoding)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, multiline, encoding));
        }

        // Width of the line in pixels
        int lineWidth = Mathf.RoundToInt(maxWidth * size);

        if (lineWidth < 1)
        {
            return(text);
        }

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty  = true;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        offset += 7;
                        continue;
                    }
                }
            }

            BMGlyph glyph = mFont.GetGlyph(ch);

            if (glyph != null)
            {
                int charSize = mSpacingX + ((previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance);

                remainingWidth -= charSize;

                // Doesn't fit?
                if (remainingWidth < 0)
                {
                    if (lineIsEmpty || !multiline)
                    {
                        // This is the first word on the line -- add it up to the character that fits
                        sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                        if (!multiline)
                        {
                            start = offset;
                            break;
                        }
                        EndLine(ref sb);

                        // Start a brand-new line
                        lineIsEmpty = true;

                        if (ch == ' ')
                        {
                            start          = offset + 1;
                            remainingWidth = lineWidth;
                        }
                        else
                        {
                            start          = offset;
                            remainingWidth = lineWidth - charSize;
                        }
                        previousChar = 0;
                    }
                    else
                    {
                        // Skip all spaces before the word
                        while (start < textLength && text[start] == ' ')
                        {
                            ++start;
                        }

                        // Revert the position to the beginning of the word and reset the line
                        lineIsEmpty    = true;
                        remainingWidth = lineWidth;
                        offset         = start - 1;
                        previousChar   = 0;
                        if (!multiline)
                        {
                            break;
                        }
                        EndLine(ref sb);
                        continue;
                    }
                }
                else
                {
                    previousChar = ch;
                }
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        return(sb.ToString());
    }
예제 #20
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText()
    {
        mChanged   = true;
        hasChanged = false;
        mLastText  = mText;
        mText      = mText.Replace("\\n", "\n");

        float scale = Mathf.Abs(cachedTransform.localScale.x);
        float maxY  = mFont.size * mMaxLineCount;

        if (scale > 0f)
        {
            for (;;)
            {
                if (mPassword)
                {
                    mProcessedText = "";

                    if (mShowLastChar)
                    {
                        for (int i = 0, imax = mText.Length - 1; i < imax; ++i)
                        {
                            mProcessedText += "*";
                        }
                        if (mText.Length > 0)
                        {
                            mProcessedText += mText[mText.Length - 1];
                        }
                    }
                    else
                    {
                        for (int i = 0, imax = mText.Length; i < imax; ++i)
                        {
                            mProcessedText += "*";
                        }
                    }
                    mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / scale, mMaxLineCount, false, UIFont.SymbolStyle.None);
                }
                else if (mMaxLineWidth > 0)
                {
                    mProcessedText = mFont.WrapText(mText, mMaxLineWidth / scale, mShrinkToFit ? 0 : mMaxLineCount, mEncoding, mSymbols);
                }
                else if (!mShrinkToFit && mMaxLineCount > 0)
                {
                    mProcessedText = mFont.WrapText(mText, 100000f, mMaxLineCount, mEncoding, mSymbols);
                }
                else
                {
                    mProcessedText = mText;
                }

                mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols) : Vector2.one;

                if (mShrinkToFit)
                {
                    // We want to shrink the label (when it doesn't fit)
                    if (mMaxLineCount > 0 && mSize.y * scale > maxY)
                    {
                        scale = Mathf.Round(scale - 1f);
                        if (scale > 1f)
                        {
                            continue;
                        }
                    }

                    if (mMaxLineWidth > 0)
                    {
                        float maxX = (float)mMaxLineWidth / scale;
                        float x    = (mSize.x * scale > maxX) ? (maxX / mSize.x) * scale : scale;
                        scale = Mathf.Min(x, scale);
                    }

                    scale = Mathf.Round(scale);
                    cachedTransform.localScale = new Vector3(scale, scale, 1f);
                }
                break;
            }
            mSize.x = Mathf.Max(mSize.x, (scale > 0f) ? lineWidth / scale : 1f);
        }
        else
        {
            // This should never happen (label should never have a scale of 0) -- but just in case.
            mSize.x = 1f;
            scale   = mFont.size;
            cachedTransform.localScale = new Vector3(0.01f, 0.01f, 1f);
            mProcessedText             = "";
        }
        mSize.y = Mathf.Max(mSize.y, 1f);
    }
예제 #21
0
    private void ProcessText()
    {
        mChanged   = true;
        hasChanged = false;
        mLastText  = mText;
        var   b    = Mathf.Abs(cachedTransform.localScale.x);
        float num2 = mFont.size * mMaxLineCount;

        if (b <= 0f)
        {
            mSize.x = 1f;
            b       = mFont.size;
            cachedTransform.localScale = new Vector3(0.01f, 0.01f, 1f);
            mProcessedText             = string.Empty;
            goto Label_037C;
        }

Label_0057:
        if (mPassword)
        {
            mProcessedText = string.Empty;
            if (mShowLastChar)
            {
                var num3 = 0;
                var num4 = mText.Length - 1;
                while (num3 < num4)
                {
                    mProcessedText = mProcessedText + "*";
                    num3++;
                }

                if (mText.Length > 0)
                {
                    mProcessedText = mProcessedText + mText[mText.Length - 1];
                }
            }
            else
            {
                var num5   = 0;
                var length = mText.Length;
                while (num5 < length)
                {
                    mProcessedText = mProcessedText + "*";
                    num5++;
                }
            }

            mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / b, mMaxLineCount, false, UIFont.SymbolStyle.None);
        }
        else if (mMaxLineWidth > 0)
        {
            mProcessedText = mFont.WrapText(mText, mMaxLineWidth / b, !mShrinkToFit ? mMaxLineCount : 0, mEncoding, mSymbols);
        }
        else if (!mShrinkToFit && mMaxLineCount > 0)
        {
            mProcessedText = mFont.WrapText(mText, 100000f, mMaxLineCount, mEncoding, mSymbols);
        }
        else
        {
            mProcessedText = mText;
        }

        mSize = string.IsNullOrEmpty(mProcessedText) ? Vector2.one : mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols);
        if (mShrinkToFit)
        {
            if (mMaxLineCount > 0 && mSize.y * b > num2)
            {
                b = Mathf.Round(b - 1f);
                if (b > 1f)
                {
                    goto Label_0057;
                }
            }

            if (mMaxLineWidth > 0)
            {
                var num7 = mMaxLineWidth / b;
                var a    = mSize.x * b <= num7 ? b : num7 / mSize.x * b;
                b = Mathf.Min(a, b);
            }

            b = Mathf.Round(b);
            cachedTransform.localScale = new Vector3(b, b, 1f);
        }

        mSize.x = Mathf.Max(mSize.x, b <= 0f ? 1f : lineWidth / b);
Label_037C:
        mSize.y = Mathf.Max(mSize.y, 1f);
    }