public void OnLanguageChanged(string inNewLanguage) { if (m_Widget == null) { return; } string font_name = m_FontName; if (m_FontName == "Default") { font_name = "NewFont"; } GUIBase_FontEx newFont = null; if ("English.Old" == inNewLanguage) { newFont = MFFontManager.GetFont(m_FontName, SystemLanguage.English) as GUIBase_FontEx; } else { newFont = MFFontManager.GetFont(font_name) as GUIBase_FontEx; } if (newFont != m_Font) { m_Font = newFont; m_Widget.ChangeMaterial(m_Font.fontMaterial); } SetRegenerationNeeded(); }
static float GetLineWidth(string subString, GUIBase_FontBase inFont) { float lineWidth = 0; GUIBase_FontEx fontEx = inFont as GUIBase_FontEx; GUIBase_Font font = inFont as GUIBase_Font; if (fontEx != null) { for (int i = 0; i < subString.Length; ++i) { lineWidth += fontEx.GetCharWidth((int)subString[i]); } } else { for (int i = 0; i < subString.Length; ++i) { lineWidth += font.GetCharWidth((int)subString[i]); } } return(lineWidth); }
// ================================================================================================== // === Default MoneBehaviour interface ============================================================== #region MoneBehaviourInterface public void Start() { m_Widget = GetComponent <GUIBase_Widget>(); m_Widget.RegisterUpdateDelegate(RegenerateSprites); m_Widget.RegisterCallback(this, (int)E_CallbackType.E_CT_INIT); m_Widget.m_TextScaleFix_HACK = true; if (m_TextID <= 0 && string.IsNullOrEmpty(m_Text)) { m_Text = "This-is-temporary-test-for-testing-TextArea-auto-wrap. This is tooooooo long line. \n\n" + "This is temporary test for testing TextArea auto wrap. This is tooooooo long line. " + "This is temporary test for testing TextArea auto wrap. This is tooooooo long line."; //m_Text = "Test"; } if (m_Font == null) { //m_Font = MFGuiManager.Instance.GetFontForLanguage(SystemLanguage.English); m_Font = MFFontManager.GetFont(m_FontName) as GUIBase_FontEx; } SetRegenerationNeeded(); }
Vector2 GetTextSize() { Vector2 charPos = Vector2.zero; Vector2 charSize = Vector2.zero; Vector2 lineSize = Vector2.zero; Vector2 totalSize = Vector2.zero; int numOfLine = 1; float width = 0; string text = Text; if (string.IsNullOrEmpty(text) == false) { if (m_Uppercase) { text = text.ToUpper(); } for (int i = 0; i < text.Length; ++i) { int character = text[i]; switch (character) { case '\n': totalSize.x = Mathf.Max(totalSize.x, lineSize.x); lineSize.x = 0; numOfLine++; break; default: if (useFontEx == false) { charSize = Vector2.zero; width = 0; GUIBase_Font fontOld = font as GUIBase_Font; if (fontOld.GetCharDscr(character, out width, ref charPos, ref charSize)) { lineSize.x += width; lineSize.y = Mathf.Max(lineSize.y, charSize.y); } } else { Rect screenRect, sourceRect; width = 0; GUIBase_FontEx fontEx = font as GUIBase_FontEx; if (fontEx.GetCharDescription(text[i], out width, out screenRect, out sourceRect, true, false)) { lineSize.x += width; lineSize.y = Mathf.Max(lineSize.y, screenRect.height); } } break; } } } m_LineSize = lineSize; totalSize.x = Mathf.Max(totalSize.x, lineSize.x); m_LineSize.x = totalSize.x; totalSize.y = lineSize.y * numOfLine + (numOfLine - 1) * lineSpace; return(totalSize); }
//--------------------------------------------------------- void RegenerateSprites() { if (IsDataRegenerationNeaded() == false || m_Widget.IsVisible() == false) { return; } // regenerate runtime data for new text... GenerateRunTimeData(); string text = Text; if (string.IsNullOrEmpty(text) == false && m_Uppercase) { text = text.ToUpper(); } // destroy old text if any exist... m_Widget.PrepareSprites(string.IsNullOrEmpty(text) == true ? 0 : text.Length); if (string.IsNullOrEmpty(text) == false && text.Length > 0) { Vector3 scale = Vector3.one; Texture texture = fontTexture; int texWidth = texture ? texture.width : 1; int texHeight = texture ? texture.height : 1; float lineHeight = m_LineSize.y * texHeight; float lineWidth = m_LineSize.x; float widthMult = m_Widget.GetWidth() / lineWidth; // compute down scale if needed if (m_Boundaries.IsEmpty() == false) { Rect clientRect = GetRect(); Rect rect = m_Boundaries.Intersect(clientRect); float scaleX = rect.width / clientRect.width; float scaleY = rect.height / clientRect.height; if (scaleY > scaleX) { scaleY = scaleX; } else { scaleX = scaleY; } scale.x *= scaleX; scale.y *= scaleY; } // setup cursor ... Vector2 cursor = GetLeftUpPos(m_Widget.GetOrigPos(), scale); float lineBegin = cursor.x; cursor.y += lineHeight * scale.y * 0.5f; // textHeight*0.5f; bool multiline = IsMultiline(text); if (multiline == true) { cursor = SetupCursorForTextAlign(cursor, text, 0, m_Alignment, font, lineBegin, lineWidth, widthMult * scale.x); } for (int i = 0; i < text.Length; ++i) { int character = text[i]; switch (character) { case '\n': if (multiline == true) { cursor = SetupCursorForTextAlign(cursor, text, i + 1, m_Alignment, font, lineBegin, lineWidth, widthMult * scale.x); } else { cursor.x = lineBegin; } cursor.y = cursor.y + (lineHeight + lineSpace * texHeight) * scale.y; m_Widget.SetTextureCoords(i, 0.0f, 0.0f, 0.0f, 0.0f); m_Widget.UpdateSpritePosAndSize(i, 0, -Screen.height, 1.0f, 1.0f); m_Widget.ShowSprite(i, false); break; default: { if (useFontEx == false) { Vector2 inTexPos = new Vector2(); Vector2 inTexSize = new Vector2(); float width; GUIBase_Font fontOld = font as GUIBase_Font; fontOld.GetCharDscr(character, out width, ref inTexPos, ref inTexSize); width *= widthMult; int texU = (int)(texWidth * inTexPos.x); int texV = (int)(texHeight * inTexPos.y); int texW = (int)(texWidth * inTexSize.x); int texH = (int)(texHeight * inTexSize.y); cursor.x += 0.5f * width * scale.x; m_Widget.SetTextureCoords(i, texU, texV, texW, texH); m_Widget.UpdateSpritePosAndSize(i, cursor.x, cursor.y, width, lineHeight); cursor.x += 0.5f * width * scale.x; } else { float width; Rect spriteRect, texRect; GUIBase_FontEx fontEx = font as GUIBase_FontEx; if (fontEx.GetCharDescription(text[i], out width, out spriteRect, out texRect, false, false)) { width *= widthMult; Vector2 inCharSize = new Vector2(spriteRect.width * scale.x, spriteRect.height /*****/ * texHeight * scale.y); // TODO :: rewite without texHeight... Vector2 inCharCenter = cursor + new Vector2(spriteRect.center.x * scale.x, 0.0f); m_Widget.SetTextureCoords(i, (int)texRect.x, (int)texRect.y, (int)texRect.width, (int)texRect.height); m_Widget.UpdateSpritePosAndSize(i, inCharCenter.x, inCharCenter.y, inCharSize.x, inCharSize.y); cursor.x += width * scale.x; } } break; } } } } // we have to force widget update. m_RegenerationNeeded = false; m_Widget.SetModify(); }
public static List <TextLine> GetLines(string inText, GUIBase_FontEx inFont, HorizontalTextAlignment inAlignment, float inMaxlineWidth, Vector3 inScale, float inLineSpacePct, bool isForTextField = false) { if (string.IsNullOrEmpty(inText) == true) { return(null); } if (inMaxlineWidth <= 0.0f) { return(null); } if (inScale == Vector3.zero) { return(null); } List <TextLine> lines = new List <TextLine>(); TextLine newLine = null; int lastSpaceIndex = -1; float widthAtLastSpace = 0; float widthOfSpace = inFont.GetCharWidth((int)' ') * inScale.x; float charWidth = 0; float fontHeight = inFont.GetFontHeight(); for (int i = 0; i < inText.Length; ++i) { if (newLine == null) { newLine = new TextLine(); newLine.m_StartIndex = i; newLine.m_SpaceWidth = widthOfSpace; lastSpaceIndex = -1; widthAtLastSpace = 0; } int character = (int)inText[i]; if (!isForTextField && character == ' ') { lastSpaceIndex = i; widthAtLastSpace = newLine.m_Size.x; newLine.m_Size.x += widthOfSpace; newLine.m_NumOfSpaces++; continue; } switch (character) { case '\n': newLine.m_EndIndex = i; newLine.m_EndOfParagraph = true; lines.Add(newLine); newLine = null; break; default: charWidth = inFont.GetCharWidth(character) * inScale.x; if (newLine.m_Size.x + charWidth > inMaxlineWidth) { if (lastSpaceIndex >= 0) { newLine.m_NumOfSpaces--; newLine.m_Size.x = widthAtLastSpace; newLine.m_EndIndex = lastSpaceIndex; i = lastSpaceIndex; } else { newLine.m_EndIndex = i; i = i - 1; } newLine.m_EndOfParagraph = false; if ((newLine.m_EndIndex - newLine.m_StartIndex) < 1) { //this line is invalid. It looks that actual character is too width for inMaxlineWidth // skip it. Debug.LogWarning("Can't generate line for character: " + (char)character); } else { lines.Add(newLine); } newLine = null; } else { newLine.m_Size.x += charWidth; } break; } } if (newLine != null) { newLine.m_EndIndex = inText.Length; newLine.m_EndOfParagraph = true; if ((newLine.m_EndIndex - newLine.m_StartIndex) == 0) { Debug.LogWarning("Empty line"); } else { lines.Add(newLine); } } float yOffset = 0; // Compute x offset of lines by TextAlignment. foreach (TextLine line in lines) { #if !MADFINGER_KEYBOARD_MOUSE // remove spaces from start and end of line... TrimSpaces(inText, line, widthOfSpace); #endif // Check line validity... if ((line.m_EndIndex - line.m_StartIndex) == 0) { //Debug.LogWarning("Empty line"); continue; } // setup line x offset based on TextAlignment... float span = (inMaxlineWidth - line.m_Size.x); switch (inAlignment) { case HorizontalTextAlignment.Left: line.m_Offset.x = 0.0f; break; case HorizontalTextAlignment.Center: line.m_Offset.x = span * 0.5f; break; case HorizontalTextAlignment.Right: line.m_Offset.x = span; break; case HorizontalTextAlignment.Justify: if (line.m_EndOfParagraph == false && line.m_NumOfSpaces > 0) { line.m_SpaceWidth += span / (float)line.m_NumOfSpaces; } break; default: Debug.LogError("Unknown Horizontal text alignment !!!! " + inAlignment); break; } yOffset += 0.5f * fontHeight * inScale.y; line.m_Offset.y = yOffset; yOffset += 0.5f * fontHeight * inScale.y; yOffset += inLineSpacePct * fontHeight * inScale.y; } return(lines); }
//--------------------------------------------------------- void RegenerateSprites() { if (m_RegenerateSprites == false || m_Widget.IsVisible() == false) { return; } if (m_Font == null) { m_Font = MFFontManager.GetFont(m_FontName) as GUIBase_FontEx; if (m_Font == null) { Debug.LogError(gameObject.GetFullName() + " Can't load font with name " + m_FontName); return; } } if (m_TextID > 0) { m_Text = TextDatabase.instance[m_TextID]; } // destroy old text if any exist... int maxSprites = text != null && text.Length > 0 ? Mathf.CeilToInt(text.Length * 0.1f) * 10 : 0; m_Widget.PrepareSprites(maxSprites); if (text != null && text.Length > 0) { Vector3 scale = transform.lossyScale; scale = Vector3.one; // setup cursor ... Vector2 leftUpPos = new Vector2(m_Widget.GetOrigPos().x - m_Widget.GetWidth() * 0.5f * scale.x, m_Widget.GetOrigPos().y - m_Widget.GetHeight() * 0.5f * scale.y); Vector2 cursor = leftUpPos; float maxLineSize = m_Widget.GetWidth() * scale.x; scale.x = m_TextScale.x; scale.y = m_TextScale.y; List <TextLine> textLines = GetLines(text, m_Font, alignment, maxLineSize, scale, lineSpace, IsForTextField); if (textLines == null || textLines.Count <= 0) { return; } float fontHeight = m_Font.GetFontHeight(); m_TextSize = new Vector2(m_Widget.GetWidth(), textLines.Count * fontHeight * scale.y + (textLines.Count - 1) * lineSpace * fontHeight * scale.y); float width; Rect spriteRect, texRect; int spriteIdx = 0; foreach (TextLine line in textLines) { cursor = leftUpPos + line.m_Offset; for (int i = line.m_StartIndex; i < line.m_EndIndex; ++i) { int character = text[i]; if (!IsForTextField && character == ' ') { cursor.x += line.m_SpaceWidth; continue; } switch (character) { case '\n': Debug.LogWarning("function GetLines doesn't work correctly"); break; default: { if (m_Font.GetCharDescription(text[i], out width, out spriteRect, out texRect, false, false, false)) { Vector2 inCharSize = new Vector2(spriteRect.width * scale.x, spriteRect.height * scale.y); Vector2 inCharCenter = cursor + new Vector2(spriteRect.center.x * scale.x, 0.0f); m_Widget.SetTextureCoords(spriteIdx, (int)texRect.x, (int)texRect.y, (int)texRect.width, (int)texRect.height); m_Widget.UpdateSpritePosAndSize(spriteIdx, inCharCenter.x, inCharCenter.y, inCharSize.x, inCharSize.y); m_Widget.ShowSprite(spriteIdx, true); cursor.x += width * scale.x; spriteIdx++; } break; } } } } // hide all unused sprites while (spriteIdx < maxSprites) { m_Widget.SetTextureCoords(spriteIdx, 0.0f, 0.0f, 0.0f, 0.0f); m_Widget.UpdateSpritePosAndSize(spriteIdx, 0.0f, -Screen.height, 1.0f, 1.0f); m_Widget.ShowSprite(spriteIdx, false); spriteIdx++; } } // we have to force widget update. m_RegenerateSprites = false; m_Widget.SetModify(); }