/// <summary> /// Draw a string with a shading color, a different font, a rotation and some alignment. /// </summary> /// <param name="spriteFont">The font.</param> /// <param name="text">The string.</param> /// <param name="position">The position to draw on the screen in world coordinates.</param> /// <param name="color">Shading color to use.</param> /// <param name="fRot">Rotation of the text.</param> /// <param name="alignH">Horizontal text alignment.</param> /// <param name="alignV">Vertical text alignment.</param> public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float fRot, TextRenderAlignmentH alignH, TextRenderAlignmentV alignV) { Vector2 drawPos = Vector2.Transform(new Vector2((int)position.X, (int)position.Y), GetTransformation()); if (Camera.AllowRotating && Camera.AllowTransforming) { fRot += Camera.Rotation; } float fScale = 1.0f; if (Camera.AllowScaling && Camera.AllowTransforming) { fScale = Camera.Scale; } Vector2 vTextSize = spriteFont.MeasureString(text); if (alignH == TextRenderAlignmentH.Middle) { drawPos.X -= vTextSize.X / 2f; } else if (alignH == TextRenderAlignmentH.Right) { drawPos.X -= vTextSize.X; } if (alignV == TextRenderAlignmentV.Middle) { drawPos.Y -= vTextSize.Y / 2f; } else if (alignV == TextRenderAlignmentV.Bottom) { drawPos.Y -= vTextSize.Y; } this.spriteBatch.DrawString(spriteFont, text, drawPos, color, fRot, Vector2.Zero, fScale, SpriteEffects.None, 0); }
/// <summary> /// Draw a string with a shading color and some alignment. /// </summary> /// <param name="text">The string.</param> /// <param name="position">The position to draw on the screen in world coordinates.</param> /// <param name="color">Shading color to use.</param> /// <param name="alignH">Horizontal text alignment.</param> /// <param name="alignV">Vertical text alignment.</param> public void DrawString(string text, Vector2 position, Color color, TextRenderAlignmentH alignH, TextRenderAlignmentV alignV) { Font.Render(this, text, position, color, alignH, alignV); }
/// <summary> /// Draw a string with a shading color, a different font and some alignment. /// </summary> /// <param name="spriteFont">The font.</param> /// <param name="text">The string.</param> /// <param name="position">The position to draw on the screen in world coordinates.</param> /// <param name="color">Shading color to use.</param> /// <param name="alignH">Horizontal text alignment.</param> /// <param name="alignV">Vertical text alignment.</param> public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, TextRenderAlignmentH alignH, TextRenderAlignmentV alignV) { this.DrawString(spriteFont, text, position, color, 0, TextRenderAlignmentH.Left, TextRenderAlignmentV.Top); }
/// <summary> /// Render some text directly to the spritebatch with the given alignment. /// </summary> /// <param name="sb">The spritebatch to draw with.</param> /// <param name="str">The string.</param> /// <param name="pos">The top-left position to draw the text at.</param> /// <param name="color">The color to draw the text with.</param> /// <param name="alignH">Horizontal alignment.</param> /// <param name="alignV">Vertical alignment.</param> public void Render(CustomSpriteBatch sb, string str, Vector2 pos, Color color, TextRenderAlignmentH alignH = TextRenderAlignmentH.Left, TextRenderAlignmentV alignV = TextRenderAlignmentV.Top) { var size = Measure(str); if (alignH == TextRenderAlignmentH.Middle) { pos.X += size.X / 2.0f; } else if (alignH == TextRenderAlignmentH.Right) { pos.X += size.X; } if (alignV == TextRenderAlignmentV.Middle) { pos.Y += size.Y / 2.0f; } else if (alignV == TextRenderAlignmentV.Bottom) { pos.Y += size.Y; } Render(sb, str, pos, color); }