private static ColorInfo ParseColor(UIMarkupToken token, Color defaultColor) { var result = new ColorInfo(Color.white, true); if (token.attributeCount == 1) { string value = token.GetAttribute(0).m_Value.value; result.color = UIMarkupStyle.ParseColor(value, defaultColor); result.overrideColor = true; } return(result); }
public static void CalcTokenRenderSizeNewTags(DynamicFontRenderer renderer, UIMarkupToken token) { if (token.tokenType == UIMarkupTokenType.StartTag && Matches(token, TAG_LINE) && renderer.spriteAtlas != null && token.GetAttribute(0).m_Value.value.Split(',').Length == 3) { float num = 0f; Texture2D texture = renderer.spriteAtlas.texture; float num3 = ((UIDynamicFont)renderer.font).baseline * renderer.textScale * 2.5f; string value = token.GetAttribute(0).m_Value.value.Split(',')[0]; UITextureAtlas.SpriteInfo spriteInfo = renderer.spriteAtlas[value]; if (spriteInfo != null) { float num4 = spriteInfo.region.width * texture.width / (spriteInfo.region.height * texture.height); num = Mathf.CeilToInt((num3 * num4)); } token.height = Mathf.CeilToInt(num3); typeof(UIMarkupToken).GetProperty("width").GetSetMethod(true).Invoke(token, new object[] { Mathf.CeilToInt(num) }); } }
private static Vector2 CalculateTextureSize(UIDynamicFont font, float textScale, ref PoolList <UIMarkupToken> tokens, out int startYPos) { float xAdvance = 0; var yBounds = new Vector2(999999999999999999999999f, -99999999999999999999999999f); for (int i = 0; i < tokens.Count; i++) { UIMarkupToken token = tokens[i]; if (token.tokenType == UIMarkupTokenType.Text) { Vector2 textDimensions = MeasureTextWidth(font, token.value, textScale, out Vector2 yBoundsCalc); xAdvance += textDimensions.x; yBounds.x = Mathf.Min(yBounds.x, yBoundsCalc.x); yBounds.y = Mathf.Max(yBounds.y, yBoundsCalc.y); } else if (token.tokenType == UIMarkupTokenType.Whitespace) { int size2 = Mathf.CeilToInt(font.size * textScale); float num2 = characterSpacing * textScale; float num = 0; font.RequestCharacters(" ", size2, FontStyle.Normal); for (int j = 0; j < token.length; j++) { char c = token[j]; int multiplier = 1; if (c == '\t') { multiplier = 4; } font.baseFont.GetCharacterInfo(' ', out CharacterInfo characterInfo, size2, FontStyle.Normal); num += (characterInfo.advance + num2) * multiplier; } token.height = Mathf.CeilToInt(num); xAdvance += token.height; yBounds.x = Mathf.Min(yBounds.x, 0); yBounds.y = Mathf.Max(yBounds.y, token.height); } else if (token.tokenType == UIMarkupTokenType.StartTag) { if (UIDynamicFontRendererRedirector.Matches(token, "sprite")) { if (token.attributeCount != 1) { tokens.RemoveAt(i); i--; continue; } UITextureAtlas.SpriteInfo spriteInfo = UIView.GetAView().defaultAtlas[token.GetAttribute(0).m_Value.value]; if (spriteInfo == null) { tokens.RemoveAt(i); i--; continue; } float targetScale = font.baseline * textScale / spriteInfo.texture.height; token.height = (int)(targetScale * spriteInfo.texture.height); xAdvance += (int)(targetScale * spriteInfo.texture.width); yBounds.x = Mathf.Min(yBounds.x, 0); yBounds.y = Mathf.Max(yBounds.y, token.height); } else if (UIDynamicFontRendererRedirector.Matches(token, UIDynamicFontRendererRedirector.TAG_LINE)) { if (token.attributeCount != 1) { tokens.RemoveAt(i); i--; continue; } string[] attrs = token.GetAttribute(0).m_Value.value.Split(','); if (attrs.Length != 3 || !Regex.IsMatch(attrs[1], "^[0-9a-fA-F]{6}$")) { tokens.RemoveAt(i); i--; continue; } UITextureAtlas.SpriteInfo spriteInfo = UIView.GetAView().defaultAtlas[attrs[0]]; if (spriteInfo == null) { tokens.RemoveAt(i); i--; continue; } float baseScale = font.baseline * textScale / spriteInfo.texture.height; float targetScale = baseScale * 2; token.height = (int)(targetScale * spriteInfo.texture.height); xAdvance += (int)(targetScale * spriteInfo.texture.width); yBounds.x = Mathf.Min(yBounds.x, -token.height / 3); yBounds.y = Mathf.Max(yBounds.y, token.height / 3 * 2); } } } if (tokens.Count == 0) { startYPos = 0; return(new Vector2(1, 1)); } float ySize = yBounds.y - yBounds.x; startYPos = (int)(-yBounds.x); return(new Vector2(xAdvance, ySize)); }
public static void RenderSpriteLine(DynamicFontRenderer renderer, UIMarkupToken token, Vector3 position, UIRenderData destination) { string[] args = token.GetAttribute(0)?.m_Value?.value?.Split(new char[] { ',' }, 3); if (args == null || args.Length != 3) { LogUtils.DoErrorLog($"INVALID ARGUMENT: {token.GetAttribute(0)?.m_Value?.value ?? "<NULL>"}"); return; } UITextureAtlas.SpriteInfo spriteInfo = renderer.spriteAtlas[args[0]]; if (spriteInfo == null) { CODebugBase <InternalLogChannel> .Warn(InternalLogChannel.UI, "Missing sprite " + args[0] + " in " + renderer.spriteAtlas.name); } else { var midLine = new Vector3(position.x, position.y - (((UIDynamicFont)renderer.font).baseline * renderer.textScale / 2)); Texture2D texture = renderer.spriteAtlas.texture; float calcHeight = ((UIDynamicFont)renderer.font).baseline * renderer.textScale * 2f; float calcProportion = spriteInfo.region.width * texture.width / (spriteInfo.region.height * texture.height); float calcWidth = Mathf.CeilToInt(calcHeight * calcProportion); int height = Mathf.CeilToInt(calcHeight); int width = Mathf.CeilToInt(calcWidth); Color32 bgColor = ColorExtensions.FromRGB(args[1]); Color32 color2 = ApplyOpacity(renderer, bgColor); var size = new Vector3(width, height); Vector2 textDimensions = MeasureTextWidth(renderer, args[2], renderer.textScale, out Vector2 yBounds); float imageProportions = width / spriteInfo.width; float borderWidth = renderer.textScale * 2; float textBoundHeight = height - (spriteInfo.border.vertical * imageProportions) - borderWidth; float textBoundWidth = width - (spriteInfo.border.horizontal * imageProportions) - borderWidth; var textAreaSize = new Vector4((spriteInfo.border.left * imageProportions) + (borderWidth / 2), (-spriteInfo.border.top + spriteInfo.border.bottom) * imageProportions / 2, textBoundWidth, textBoundHeight); float textScale = renderer.textScale; float multipler = Mathf.Min(Mathf.Min(3.5f, textAreaSize.z / textDimensions.x), Mathf.Min(3.5f, textAreaSize.w / textDimensions.y)); if (multipler > 1) { textScale *= 1 + ((multipler - 1) / 2.1f); multipler = 1; textDimensions = MeasureTextWidth(renderer, args[2], textScale, out yBounds); } float midLineOffset = (((UIDynamicFont)renderer.font).baseline / 2 * renderer.textScale); Color contrastColor = KlyteMonoUtils.ContrastColor(bgColor); RenderSprite(renderer.spriteBuffer, new RenderOptions { atlas = renderer.spriteAtlas, color = contrastColor, fillAmount = 1f, flip = UISpriteFlip.None, offset = position - new Vector3(0, -(height / 2) + midLineOffset), pixelsToUnits = renderer.pixelRatio, size = size, spriteInfo = spriteInfo }); RenderSprite(renderer.spriteBuffer, new RenderOptions { atlas = renderer.spriteAtlas, color = color2, fillAmount = 1f, flip = UISpriteFlip.None, offset = position - new Vector3(0, -(height / 2) + midLineOffset) + (new Vector3(borderWidth, -borderWidth) / 2), pixelsToUnits = renderer.pixelRatio, size = size - new Vector3(borderWidth, borderWidth), spriteInfo = spriteInfo }); midLineOffset = ((UIDynamicFont)renderer.font).baseline * textScale; Vector3 targetTextPos = midLine + VectorUtils.XY_(textAreaSize) + (new Vector3(textAreaSize.z - (textDimensions.x * multipler), -textDimensions.y) / 2); RenderText(renderer, args[2], targetTextPos, multipler, destination, textScale, contrastColor, bgColor); } }