public CalculatePrintedSize ( string text, bool encoding, SymbolStyle symbolStyle ) : Vector2 | ||
text | string | |
encoding | bool | |
symbolStyle | SymbolStyle | |
return | Vector2 |
void CalculateEmotionSpace(ref string text, out Vector2 LeftSideSpace, out int nNextLineNum, out bool needShow) { nNextLineNum = 0; LeftSideSpace = Vector2.zero; needShow = true; if (null == m_Font) { m_Font = m_ChatText.font; if (m_Font != null) { m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; } } string striptext = NGUITools.StripSymbols(text); int stripEmotionStart = striptext.IndexOf("[em="); LeftSideSpace = m_Font.CalculatePrintedSize(striptext.Substring(0, stripEmotionStart), true, UIFont.SymbolStyle.None); // float fEllipsisWidth = m_Font.CalculatePrintedSize("...", true, UIFont.SymbolStyle.None).x; // if (LeftSideSpace.x + EMOTIONITEM_WIDTH > labelChatText.GetComponent<UIWidget>().width * MaxLines - fEllipsisWidth) // { // needShow = false; // return; // } int nLineStart = 0; for (int i = nLineStart + 1; i <= stripEmotionStart; i++) { float fChatWidth = m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart), true, UIFont.SymbolStyle.None).x; if (i == stripEmotionStart) { if (fChatWidth > m_ChatText.width) { nNextLineNum += 1; LeftSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.Uncolored).x; nLineStart = i - 1; } else if (fChatWidth + SPACE_WIDTH > m_ChatText.width) { nNextLineNum += 1; LeftSideSpace.x = 0; } } else { if (fChatWidth > m_ChatText.width) { nNextLineNum += 1; LeftSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.Uncolored).x; nLineStart = i - 1; } } } }
/// <summary> /// ADD BY XIONGHUI /// Processors the display model text. /// Log编号: 1 /// 修改描述: 多行自动添加"..."逻辑 /// 作者: 袁陈晨 /// 修改日期; 2015-08-18 /// </summary> void ProcessorPartialText(string str) { if (!mPartial) { return; } float reduceWidth = (mFont.CalculatePrintedSize("...", mEncoding, mSymbols).x) * cachedTransform.localScale.x; float screenWidth = mMaxLineWidth * mMaxLineCount - reduceWidth; mPartialText = ""; float realLineWidth = 0; // 当前操作行实际已用长度 float realRemainWidth = screenWidth; // 操作字符串实际剩余可写长度 for (int i = 0; i < str.Length; i++) { char c = str[i]; float charWidth = mFont.CalculatePrintedSize(c.ToString(), mEncoding, mSymbols).x *cachedTransform.localScale.x; if (realRemainWidth < charWidth) { mPartialText += "..."; break; } if (c == '[') { int retVal = NGUITools.ParseSymbol(str, i, null, false); if (retVal > 0) { for (int j = i; j < i + retVal; j++) { c = str[j]; mPartialText += c; } i += retVal - 1; continue; } } realLineWidth += charWidth; // 当前操作行超长,进行换行处理 if (realLineWidth > mMaxLineWidth) { realRemainWidth = realRemainWidth - (mMaxLineWidth + charWidth - realLineWidth); // 删除当前行多余空白长度 realLineWidth = charWidth; } realRemainWidth -= charWidth; mPartialText += c; } }
void Awake() { m_Font = m_ChatText.font; if (m_Font != null) { m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; } EmotionRootPos = m_EmotionRoot.transform.localPosition; }
/// <summary> /// Ctor /// </summary> /// <param name="face">Font name</param> /// <param name="size">Font size</param> /// <param name="bold">Bold flag</param> /// <param name="italic">Italic flag</param> public NGUIFont(string face, int size, bool bold, bool italic) : base(face, size, bold, italic) { // load from resources var uiFontTemplate = Resources.Load("fonts/" + face, typeof(UIFont)) as UIFont; // showing error if font not found if (uiFontTemplate == null) { Debug.LogError("Could not load font: " + face); return; } uiFont = UnityEngine.GameObject.Instantiate(uiFontTemplate) as UIFont; Object.DontDestroyOnLoad(uiFont); var cachedHtmlFontsGo = GameObject.Find("/cachedHtmlFonts"); if (cachedHtmlFontsGo == null) { cachedHtmlFontsGo = new GameObject("cachedHtmlFonts"); UnityEngine.Object.DontDestroyOnLoad(cachedHtmlFontsGo); } uiFont.transform.parent = cachedHtmlFontsGo.transform; uiFont.name = string.Format("{0}{1}{2}{3}", face, size, bold ? "b" : "", italic ? "i" : ""); uiFont.dynamicFontSize = size; if (uiFont.isDynamic) { if (bold && italic) { uiFont.dynamicFontStyle = FontStyle.BoldAndItalic; } else if (bold) { uiFont.dynamicFontStyle = FontStyle.Bold; } else if (italic) { uiFont.dynamicFontStyle = FontStyle.Italic; } else { uiFont.dynamicFontStyle = FontStyle.Normal; } } // calculating whitesize uiFont.Request(" ."); whiteSize = (int)(uiFont.CalculatePrintedSize(" .", true, UIFont.SymbolStyle.None).x); this.whiteSize -= (int)(uiFont.CalculatePrintedSize(".", true, UIFont.SymbolStyle.None).x); }
/// <summary> /// Measuring text width and height /// </summary> /// <param name="text">text to measure</param> /// <returns>width and height of measured text</returns> public override HtSize Measure(string text) { uiFont.Request(text); var r = uiFont.CalculatePrintedSize(text, false, UIFont.SymbolStyle.None); return(new HtSize((int)r.x, (int)r.y)); }
void UpdateEmotionLinkPos(string strChatFull) { if (null == m_Font) { m_Font = m_ChatText.font; if (m_Font != null) { m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; } } float fChatWidth = m_Font.CalculatePrintedSize(strChatFull.Replace(Environment.NewLine, ""), true, UIFont.SymbolStyle.None).x; int moveHeight = Mathf.CeilToInt(fChatWidth / m_ChatText.width) - 1; m_EmotionRoot.transform.localPosition += new Vector3(0, moveHeight * m_ChatTextHeight, 0); m_LinkRoot.transform.localPosition += new Vector3(0, moveHeight * m_ChatTextHeight, 0); }
void Awake() { m_Instance = this; if (m_Font == null) { m_Font = labelChatText2.font; m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; if (m_ChatTextHeight != 0) { MaxLines = (int)(labelChatText2.height / m_ChatTextHeight); } else { MaxLines = 1; } } }
/// <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); }
void Awake() { m_Instance = this; if (m_Font == null) { m_Font = labelChatText2.font; m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; if (m_ChatTextHeight != 0) { MaxLines = (int)(labelChatText2.height / m_ChatTextHeight); } else { MaxLines = 1; } } m_curSelect = 0; InitTextPos(); InitEmotionButtons(); InitAnoymousTabBtn(); }
/// <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); }
/// <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); }
/// <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; } }
// Use this for initialization void Start() { m_FirstChild.SetActive(true); //InvokeRepeating("CheckNotifyData", 1, 1); m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; if (m_ChatTextHeight != 0) { MaxLines = (int)(labelChatText2.height / m_ChatTextHeight); } else { MaxLines = 2; } InitTextPos(); labelChatText = labelChatText2; m_LinkRoot = m_LinkRoot2; m_EmotionRoot = m_EmotionRoot2; // if (null != ClipPanel) // { // m_ClipPanelTransform = ClipPanel.transform; // } m_LocalPos = m_OffsetTrans.localPosition; clearItemList.Add(m_LinkRoot1); clearItemList.Add(m_EmotionRoot1); clearItemList.Add(m_LinkRoot2); clearItemList.Add(m_EmotionRoot2); // InitPlayVoiceBtn(); }
/// <summary> /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels. /// </summary> public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle)); } Vector2 v = Vector2.zero; if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) { if (encoding) { text = NGUITools.StripSymbols(text); } int length = text.Length; int maxX = 0; int x = 0; int y = 0; int prev = 0; int lineHeight = (mFont.charSize + mSpacingY); for (int i = 0; i < length; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) { maxX = x; } x = 0; y += lineHeight; prev = 0; continue; } // Skip invalid characters if (c < ' ') { prev = 0; continue; } // See if there is a symbol matching this text BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, i, length) : null; if (symbol == null) { // Get the glyph for this character BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { x += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance); prev = c; } } else { // Symbol found -- use it x += mSpacingX + symbol.width; i += symbol.length - 1; prev = 0; } } // Convert from pixel coordinates to local coordinates float scale = (mFont.charSize > 0) ? 1f / mFont.charSize : 1f; v.x = scale * ((x > maxX) ? x : maxX); v.y = scale * (y + lineHeight); } return(v); }
public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle)); } Vector2 zero = Vector2.zero; bool isDynamic = this.isDynamic; if (isDynamic || (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))) { if (encoding) { text = NGUITools.StripSymbols(text); } if (isDynamic) { Font.textureRebuilt += OnFontChanged; // mDynamicFont.textureRebuildCallback = OnFontChanged; mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle); // mDynamicFont.textureRebuildCallback = null; Font.textureRebuilt -= OnFontChanged; } int length = text.Length; int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; int size = this.size; int num5 = size + mSpacingY; bool flag = encoding && symbolStyle != 0 && hasSymbols; for (int i = 0; i < length; i++) { char c = text[i]; if (c == '\n') { if (num2 > num) { num = num2; } num2 = 0; num3 += num5; num4 = 0; } else if (c < ' ') { num4 = 0; } else if (!isDynamic) { BMSymbol bMSymbol = (!flag) ? null : MatchSymbol(text, i, length); if (bMSymbol == null) { BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { num2 += mSpacingX + ((num4 == 0) ? glyph.advance : (glyph.advance + glyph.GetKerning(num4))); num4 = c; } } else { num2 += mSpacingX + bMSymbol.width; i += bMSymbol.length - 1; num4 = 0; } } else if (mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle)) { num2 += (int)((float)mSpacingX + mChar.width); } } float num6 = (size <= 0) ? 1f : (1f / (float)size); zero.x = num6 * (float)((num2 <= num) ? num : num2); zero.y = num6 * (float)(num3 + num5); } return(zero); }
string MakeNameLinkEnabled(string fulltext, ChatHistoryItem history) { if (history.SenderGuid != GlobeVar.INVALID_GUID && history.SenderName != "") { GameObject link = null; link = ResourceManager.LoadChatLink(m_LinkRoot); if (link != null) { UInt64 linkguid = history.SenderGuid; string linkname = history.SenderName; string stripText = NGUITools.StripSymbols(fulltext); int linkstart = stripText.IndexOf(linkname) - 1; int linkend = stripText.IndexOf(linkname) + linkname.Length; Vector2 LeftSideSpace = m_Font.CalculatePrintedSize(stripText.Substring(0, linkstart), true, UIFont.SymbolStyle.Uncolored); Vector2 RightSideSpace = m_Font.CalculatePrintedSize(stripText.Substring(0, linkend + 1), true, UIFont.SymbolStyle.Uncolored); link.transform.localPosition = new Vector3((LeftSideSpace.x + RightSideSpace.x) / 2, m_ChatTextHeight / 2, 0); link.GetComponent <BoxCollider>().size = new Vector3(RightSideSpace.x - LeftSideSpace.x, m_ChatTextHeight, 0); link.GetComponent <ChatLinkLogic>().Init_NameLink(linkguid, linkname); fulltext = fulltext.Substring(0, linkstart) + "[FFFF10]" + fulltext.Substring(linkstart, linkname.Length + 2) + TEXT_COLOR + fulltext.Substring(linkend + 1); } } return(fulltext); }
void OnEnable() { m_Space_Height = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; }
/// <summary> /// Get the printed size of the specified string. The returned value is in pixels. /// </summary> ///change by chenzhiwei return value v.z is line count public Vector3 CalculatePrintedSize(string text, int size, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, size, encoding, symbolStyle)); } #if DYNAMIC_FONT if (isDynamic) { return(NGUIText.CalculatePrintedSize(text, mDynamicFont, size, mDynamicFontStyle, encoding)); } #endif Vector3 v = Vector3.zero; if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) { if (encoding) { text = NGUIText.StripSymbols(text); } int length = text.Length; int maxX = 0; int x = 0; int y = 0; int prev = 0; int fs = size; int lineHeight = (fs + mSpacingY); bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols; for (int i = 0; i < length; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) { maxX = x; } x = 0; y += lineHeight; v.z++; prev = 0; continue; } // Skip invalid characters if (c < ' ') { prev = 0; continue; } // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, i, length) : null; if (symbol == null) { // Get the glyph for this character BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { x += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance); prev = c; } } else { // Symbol found -- use it x += mSpacingX + symbol.width; i += symbol.length - 1; prev = 0; } } // Convert from pixel coordinates to local coordinates v.x = ((x > maxX) ? x : maxX); v.y = (y + lineHeight); v.z++; } return(v); }
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); }
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); }
/// <summary> /// 显示MessageBox /// </summary> /// <param name="strInfo">提示信息</param> /// <param name="eType">MessageBox类型</param> /// <param name="funcOKClicked">确定按钮响应函数</param> /// <param name="funcCancelClicked">取消按钮响应函数</param> public void ShowMessageBox(string title, string text, Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE eType) { if (TextHeight < 0) { TextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; } labelTitle.text = title; SetTextAnalyseSymbol(text); m_Type = (int)eType; switch (eType) { case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_OK: { m_MessageBoxOKButton.SetActive(true); m_MessageBoxCancelButton.SetActive(false); //m_MessageBoxOKButton.transform.localPosition = btnCenterPos; m_TypeCost.SetActive(false); m_MessageInfo.SetActive(true); } break; case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_OKCANCEL: { m_MessageBoxOKButton.SetActive(true); m_MessageBoxCancelButton.SetActive(true); //m_MessageBoxOKButton.transform.localPosition = btnLelfPos; //m_MessageBoxCancelButton.transform.localPosition = btnRightPos; m_TypeCost.SetActive(false); m_MessageInfo.SetActive(true); } break; case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_WAIT: { m_MessageBoxOKButton.SetActive(false); m_MessageBoxCancelButton.SetActive(false); m_TypeCost.SetActive(false); m_MessageInfo.SetActive(true); } break; case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_CANCEL: { m_MessageBoxOKButton.SetActive(false); m_MessageBoxCancelButton.SetActive(true); m_TypeCost.SetActive(false); m_MessageInfo.SetActive(true); } break; case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_COST: { m_MessageBoxOKButton.SetActive(true); m_MessageBoxCancelButton.SetActive(true); m_TypeCost.SetActive(true); m_MessageInfo.SetActive(false); m_TypeCostLabel.text = text; m_TypeCostSupplementLabel.text = m_costInfo._textSupplement; m_TypeCostDesc.text = m_costInfo._costDesc; m_TypeCostCount.text = m_costInfo._costCount; m_TypeCostIcon.spriteName = m_costInfo._costIcon; //m_TypeCostIcon.MakePixelPerfect(); } break; case Games.GlobeDefine.GameDefine_Globe.MESSAGEBOX_TYPE.TYPE_INVALID: default: break; } gameObject.SetActive(true); }
/// <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; } }
public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle)); } var zero = Vector2.zero; var isDynamic = this.isDynamic; if (isDynamic || mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) { if (encoding) { text = NGUITools.StripSymbols(text); } if (isDynamic) { mDynamicFont.textureRebuildCallback = OnFontChanged; mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle); mDynamicFont.textureRebuildCallback = null; } var length = text.Length; var num2 = 0; var num3 = 0; var num4 = 0; var previousChar = 0; var size = this.size; var num7 = size + mSpacingY; var flag2 = encoding && symbolStyle != SymbolStyle.None && hasSymbols; for (var i = 0; i < length; i++) { var index = text[i]; if (index == '\n') { if (num3 > num2) { num2 = num3; } num3 = 0; num4 += num7; previousChar = 0; } else if (index < ' ') { previousChar = 0; } else if (!isDynamic) { var symbol = !flag2 ? null : MatchSymbol(text, i, length); if (symbol == null) { var glyph = mFont.GetGlyph(index); if (glyph != null) { num3 += mSpacingX + (previousChar == 0 ? glyph.advance : glyph.advance + glyph.GetKerning(previousChar)); previousChar = index; } } else { num3 += mSpacingX + symbol.width; i += symbol.length - 1; previousChar = 0; } } else if (mDynamicFont.GetCharacterInfo(index, out mChar, mDynamicFontSize, mDynamicFontStyle)) { num3 += mSpacingX + (int)mChar.width; } } var num9 = size <= 0 ? 1f : 1f / size; zero.x = num9 * (num3 <= num2 ? num2 : num3); zero.y = num9 * (num4 + num7); } return(zero); }
/// <summary> /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels. /// </summary> public Vector2 CalculatePrintedSize(string text, bool encoding) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, encoding)); } Vector2 v = Vector2.zero; if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) { if (encoding) { text = NGUITools.StripSymbols(text); } int maxX = 0; int x = 0; int y = 0; int prev = 0; int lineHeight = (mFont.charSize + mSpacingY); for (int i = 0, imax = text.Length; i < imax; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) { maxX = x; } x = 0; y += lineHeight; prev = 0; continue; } // Skip invalid characters if (c < ' ') { prev = 0; continue; } BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { x += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance); prev = c; } } // Convert from pixel coordinates to local coordinates float scale = (mFont.charSize > 0) ? 1f / mFont.charSize : 1f; v.x = scale * ((x > maxX) ? x : maxX); v.y = scale * (y + lineHeight); } return(v); }
/// <summary> /// Get the printed size of the specified string. The returned value is in pixels. /// </summary> public Vector2 CalculatePrintedSize(string text) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text)); } #if DYNAMIC_FONT if (isDynamic) { NGUIText.current.size = mDynamicFontSize; NGUIText.current.style = mDynamicFontStyle; return(NGUIText.CalculatePrintedSize(mDynamicFont, text)); } #endif Vector2 v = Vector2.zero; if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) { if (NGUIText.current.encoding) { text = NGUIText.StripSymbols(text); } int x = 0; int y = 0; int prev = 0; int maxX = 0; int length = text.Length; int lineHeight = NGUIText.current.size + NGUIText.current.spacingY; bool useSymbols = NGUIText.current.encoding && NGUIText.current.symbolStyle != NGUIText.SymbolStyle.None && hasSymbols; for (int i = 0; i < length; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) { maxX = x; } x = 0; y += lineHeight; prev = 0; continue; } // Skip invalid characters if (c < ' ') { prev = 0; continue; } // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, i, length) : null; if (symbol == null) { // Get the glyph for this character BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { x += NGUIText.current.spacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance); prev = c; } } else { // Symbol found -- use it x += NGUIText.current.spacingX + symbol.width; i += symbol.length - 1; prev = 0; } } // Padding is always between characters, so it's one less than the number of characters v.x = ((x > maxX) ? x : maxX); v.y = (y + NGUIText.current.size); } return(v); }
/// <summary> /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels. /// </summary> public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle)); } Vector2 v = Vector2.zero; bool dynamic = isDynamic; #if DYNAMIC_FONT if (dynamic || (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))) #else if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)) #endif { if (encoding) { text = NGUITools.StripSymbols(text); } #if DYNAMIC_FONT if (mDynamicFont != null) { mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize); } #endif int length = text.Length; int maxX = 0; int x = 0; int y = 0; int prev = 0; int fs = size; int lineHeight = (fs + mSpacingY); bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols; for (int i = 0; i < length; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) { maxX = x; } x = 0; y += lineHeight; prev = 0; continue; } // Skip invalid characters if (c < ' ') { prev = 0; continue; } if (!dynamic) { // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, i, length) : null; if (symbol == null) { // Get the glyph for this character BMGlyph glyph = mFont.GetGlyph(c); if (glyph != null) { x += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance); prev = c; } } else { // Symbol found -- use it x += mSpacingX + symbol.width; i += symbol.length - 1; prev = 0; } } #if DYNAMIC_FONT else { if (mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle)) { x += (int)(mSpacingX + mChar.width); } } #endif } // Convert from pixel coordinates to local coordinates float scale = (fs > 0) ? 1f / fs : 1f; v.x = scale * ((x > maxX) ? x : maxX); v.y = scale * (y + lineHeight); } return(v); }
void CalculateLinkSpace(ref string text, int linkstart_whole, int linkstart, int linkend, out Vector2 LeftSideSpace, out Vector2 RightSideSpace, out int nNextLineNum, out bool needShow) { nNextLineNum = 0; LeftSideSpace = Vector2.zero; RightSideSpace = Vector2.zero; needShow = true; if (null == m_Font) { m_Font = m_ChatText.font; if (m_Font != null) { m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y; SPACE_WIDTH = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x; } } string striptext = NGUITools.StripSymbols(text); LeftSideSpace = m_Font.CalculatePrintedSize(striptext.Substring(0, linkstart), true, UIFont.SymbolStyle.None); RightSideSpace = m_Font.CalculatePrintedSize(striptext.Substring(0, linkend), true, UIFont.SymbolStyle.None); float fLinkLength = RightSideSpace.x - LeftSideSpace.x; // float fEllipsisWidth = m_Font.CalculatePrintedSize("...", true, UIFont.SymbolStyle.None).x; int nLeftNextLineNum = 0; int nRightNextLineNum = 0; int nLineStart = 0; for (int i = nLineStart + 1; i <= linkstart; i++) { float fChatWidth = m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart), true, UIFont.SymbolStyle.None).x; if (fChatWidth > m_ChatText.width) { nLeftNextLineNum += 1; LeftSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.None).x; nLineStart = i - 1; } } nLineStart = 0; for (int i = nLineStart + 1; i <= linkend; i++) { float fChatWidth = m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart), true, UIFont.SymbolStyle.None).x; if (fChatWidth > m_ChatText.width) { nRightNextLineNum += 1; RightSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.None).x; nLineStart = i - 1; } } nNextLineNum = nRightNextLineNum; // 是否超出范围 需要让链接另起一行 if (nLeftNextLineNum < nRightNextLineNum) { text = text.Substring(0, linkstart_whole) + Environment.NewLine + text.Substring(linkstart_whole); //RightSideSpace.x = RightSideSpace.x - LeftSideSpace.x; LeftSideSpace.x = 0; RightSideSpace.x = fLinkLength; } }
/// <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); }
public void RefreshText() { if (Font == null) { return; } mMesh.Clear(); var text = Text.Replace("\\n", "\n"); if (string.IsNullOrEmpty(text)) { return; } s_verts.Clear(); s_uvs.Clear(); s_cols.Clear(); Vector3 offsetVec = Font.CalculatePrintedSize(text, true, UIFont.SymbolStyle.Uncolored) * -0.5f; Font.Print(text, TextColor1, TextColor2, UIWidget.E_Direction.Vertical, s_verts, s_uvs, s_cols, false, UIFont.SymbolStyle.Uncolored, UIFont.Alignment.Center, LineWidth, false); int drawCnt = 1; if (EffectStyle == Effect.Outline) { drawCnt = 5; } else if (EffectStyle == Effect.Shadow) { drawCnt = 2; } var uvs = new Vector2[s_uvs.size * drawCnt]; var cols = new Color32[s_cols.size * drawCnt]; Array.Copy(s_uvs.buffer, uvs, s_uvs.size); Array.Copy(s_cols.buffer, cols, s_cols.size); var verts = new Vector3[s_verts.size * drawCnt]; for (int i = 0; i < s_verts.size; ++i) { verts[i] = s_verts[i] + offsetVec; } if (EffectStyle != Effect.None) { float pixel = 1f / Font.size; float fx = pixel * EffectDistance.x; float fy = pixel * EffectDistance.y; if (EffectStyle == Effect.Outline) { ApplyShadow(verts, uvs, cols, s_verts.size * 0, s_verts.size, new Vector3(fx, fy)); ApplyShadow(verts, uvs, cols, s_verts.size * 1, s_verts.size, new Vector3(fx, -fy)); ApplyShadow(verts, uvs, cols, s_verts.size * 2, s_verts.size, new Vector3(-fx, fy)); ApplyShadow(verts, uvs, cols, s_verts.size * 3, s_verts.size, new Vector3(-fx, -fy)); } else if (EffectStyle == Effect.Shadow) { ApplyShadow(verts, uvs, cols, 0, s_verts.size, new Vector3(fx, fy)); } } int rcCount = verts.Length / 4; var triangles = new int[rcCount * 6]; for (int i = 0; i < rcCount; ++i) { triangles[i * 6 + 0] = i * 4 + 0; triangles[i * 6 + 1] = i * 4 + 1; triangles[i * 6 + 2] = i * 4 + 2; triangles[i * 6 + 3] = i * 4 + 2; triangles[i * 6 + 4] = i * 4 + 3; triangles[i * 6 + 5] = i * 4 + 0; } mMesh.vertices = verts; mMesh.triangles = triangles; mMesh.uv = uvs; mMesh.colors32 = cols; }