public static void renderNI(this String str, Vector2 position, float scale, tStyle style = tStyle.Normal) { render(str.ToUpper(), position, scale / 2.5f, Color.White, tTextAlignment.Centered, SB.font, 770, 60 * scale / 1.45f, Color.Black, 1.0f, new Vector2(1.5f, 1.5f), style); }
public static void renderNI(this String str, Vector2 position, float scale, tStyle style, Color textColor, Color shadowColor) { render(str.ToUpper(), position, scale / 2.5f, textColor, tTextAlignment.Centered, SB.font, 770, 60 * scale / 1.45f, shadowColor, 1.0f, new Vector2(2.5f, 2.5f), style); }
public static void render(this String str, Vector2 position, float scale, Color color, tTextAlignment alignment, SpriteFont font, float maxWidth, float interLineY, Color shadowColor, float alpha, Vector2 border, tStyle style) { // initialize variables textFont = font; textColor = color; textScale = scale; textAlignment = alignment; textStyle = style; textShadowColor = shadowColor; textAlpha = alpha; textBorder = border; // to center the images in height according the text, get the aproximate height of the font float halfFontHeight = font.MeasureString("A").Y * scale * 0.5f; float spaceWidth = font.MeasureString(" ").X * scale; // current position that depends on how many characters and images have been rendered float sizeX = 0; // to backup the number of elements parsed int elementsParsed = 0; int firstElementInLine = 0; // separate the text and the images and start rendering string[] strToRender = str.Split(' '); // changeline used to tell if \n is parsed bool changeLine = false; bool nextChangeLine = false; for (int i = 0; i < strToRender.Length; i++) { // update de change line changeLine = nextChangeLine; nextChangeLine = false; // if the \n comes from a resource or from a code created string if (strToRender[i].EndsWith("\\n") || strToRender[i].EndsWith("\n")) { nextChangeLine = true; strToRender[i] = strToRender[i].TrimEnd('n'); strToRender[i] = strToRender[i].TrimEnd('\\'); } if (strToRender[i].StartsWith("::")) // transform the format { strToRender[i] = strToRender[i].TrimStart(':'); } else // add a space at the end of each word { strToRender[i] += " "; } // if the substring represents an image... if (images.ContainsKey(strToRender[i])) { ScaledTexture2D st = images[strToRender[i]]; float sizeToAdd = st.getWidth() * scale; if (sizeX + sizeToAdd > maxWidth || changeLine) // change line if it doesnt fit in current line { updateAlignment(alignment, (int)position.X, (int)sizeX, firstElementInLine, i - 1); position.Y += interLineY; sizeX = 0; firstElementInLine = i; } //GraphicsManager.Instance.spriteBatch.Draw(st.texture, new Vector2(position.X + sizeX, position.Y - st.getHeight() * scale * 0.5f + halfFontHeight), null, Color.White, 0, Vector2.Zero, scale * st.scale, SpriteEffects.None, 0); textElements[i].initialize(st.texture, new Vector2(position.X + sizeX, position.Y - st.getHeight() * scale * 0.5f + halfFontHeight), scale * st.scale); sizeX += st.getWidth() * scale + spaceWidth; } else // ...if substring represents text { float sizeToAdd = font.MeasureString(strToRender[i]).X * scale; if (sizeX + sizeToAdd > maxWidth || changeLine) // change line if it doesnt fit in current line { updateAlignment(alignment, (int)position.X, (int)sizeX, firstElementInLine, i - 1); position.Y += interLineY; sizeX = 0; firstElementInLine = i; } //GraphicsManager.Instance.spriteBatch.DrawString(font, strToRender[i], new Vector2(position.X + sizeX, position.Y), color, 0, Vector2.Zero, scale, SpriteEffects.None, 0); textElements[i].initialize(strToRender[i], new Vector2(position.X + sizeX, position.Y)); sizeX += sizeToAdd; } elementsParsed = i; } updateAlignment(alignment, (int)position.X, (int)sizeX, firstElementInLine, elementsParsed); // draw all the text elements for (int i = 0; i <= elementsParsed; i++) { textElements[i].render(); } }