private static void updateAlignment(tTextAlignment alignment, int positionX, int sizeX, int first, int last) { int xOffset = 0; switch (alignment) { case tTextAlignment.NoAlignment: return; case tTextAlignment.Centered: xOffset -= sizeX / 2; break; case tTextAlignment.Left: break; case tTextAlignment.Right: xOffset -= sizeX; break; } for (int i = first; i <= last; i++) { textElements[i].position.X += xOffset; } }
public static void renderSC(this String str, Vector2 position, float scale, Color color, Color shadowColor, tTextAlignment alignment) { render(str, position, scale, color, alignment, SB.font, 1000, 30 * scale, shadowColor, 1.0f, new Vector2(1,1), tStyle.Border); }
public static void render(this String str, Vector2 position, float scale, Color color, tTextAlignment alignment, SpriteFont font) { render(str, position, scale, color, alignment, SB.font, 550, 40 * scale, Color.Black, 1.0f, Vector2.Zero, tStyle.Normal); }
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(); } }
public static void render(this String str, Vector2 position, float scale, Color color, tTextAlignment alignment) { render(str, position, scale, color, alignment, SB.font); }