//--------------------------------------------------------- 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(); }
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 (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(); }