Advertisement
alikimoko

XNA games | DrawHelper class for drawing centered text

Oct 21st, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.28 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. class DrawHelper
  6. {
  7.     /// <summary>Draw a centered text line.</summary>
  8.     /// <param name="font">The font to use.</param>
  9.     /// <param name="fontColor">The color to draw the text in.</param>
  10.     /// <param name="text">The text to draw.</param>
  11.     /// <param name="YStart">The y cordinate to start drawing the first line.</param>
  12.     /// <param name="centre">The x cordinate to centre the text arround.</param>
  13.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  14.     /// <returns>The total height of the text.</returns>
  15.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string text, float YStart, float Xcentre, int lineSpacing = -1)
  16.     {
  17.         if (!text.Contains("\n"))
  18.         {
  19.             // 1 line of text
  20.             // temporary spacing
  21.             int originalLineSpacing = font.LineSpacing;
  22.             if (lineSpacing != -1) { font.LineSpacing = lineSpacing; }
  23.  
  24.             // string drawing
  25.             Vector2 stringSize = font.MeasureString(text);
  26.             spriteBatch.DrawString(font, text, new Vector2(Xcentre, YStart), fontColor, 0, new Vector2(stringSize.X / 2, 0), 1, SpriteEffects.None, 0);
  27.  
  28.             // return to normal spacing
  29.             if (lineSpacing != -1) { font.LineSpacing = originalLineSpacing; }
  30.             return (int)stringSize.Y;
  31.         }
  32.         else
  33.         {
  34.             // multiple lines of text
  35.             return drawCenteredString(spriteBatch, font, fontColor, text.Split(new string[] { "\n" }, StringSplitOptions.None), YStart, Xcentre, lineSpacing);
  36.         }
  37.     }
  38.  
  39.     /// <summary>Draw a centered text line.</summary>
  40.     /// <param name="font">The font to use.</param>
  41.     /// <param name="fontColor">The color to draw the text in.</param>
  42.     /// <param name="text">The text to draw.</param>
  43.     /// <param name="textBox">A rectangle specifying the place the text should be drawn. (The text may go outside this box if it doesn't fit.)</param>
  44.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  45.     /// <returns>The total height of the text.</returns>
  46.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string text, Rectangle textBox, int lineSpacing = -1)
  47.     {
  48.         return drawCenteredString(spriteBatch, font, fontColor, text, textBox.Top, textBox.Left + textBox.Width / 2, lineSpacing);
  49.     }
  50.  
  51.     /// <summary>Draw a centered text line.</summary>
  52.     /// <param name="font">The font to use.</param>
  53.     /// <param name="fontColor">The color to draw the text in.</param>
  54.     /// <param name="text">The text to draw.</param>
  55.     /// <param name="YStart">The y cordinate to start drawing the first line.</param>
  56.     /// <param name="leftBound">The left bound of the intended text space. (Text may go outside if it's too big.)</param>
  57.     /// <param name="leftBound">The right bound of the intended text space. (Text may go outside if it's too big.)</param>
  58.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  59.     /// <returns>The total height of the text.</returns>
  60.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string text, float YStart, float leftBound, float rightBound, int lineSpacing = -1)
  61.     {
  62.         return drawCenteredString(spriteBatch, font, fontColor, text, YStart, (rightBound - leftBound) / 2, lineSpacing);
  63.     }
  64.  
  65.     /// <summary>Draw a centered text line.</summary>
  66.     /// <param name="font">The font to use.</param>
  67.     /// <param name="fontColor">The color to draw the text in.</param>
  68.     /// <param name="text">The text to draw.</param>
  69.     /// <param name="centre">The top centre of where the string should be drawn.</param>
  70.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  71.     /// <returns>The total height of the text.</returns>
  72.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color color, string text, Vector2 centre, int lineSpacing = -1)
  73.     {
  74.         return drawCenteredString(spriteBatch, font, color, text, centre.Y, centre.X, lineSpacing);
  75.     }
  76.  
  77.     /// <summary>Draw a centered text line.</summary>
  78.     /// <param name="font">The font to use.</param>
  79.     /// <param name="fontColor">The color to draw the text in.</param>
  80.     /// <param name="text">An array with the lines to draw.</param>
  81.     /// <param name="YStart">The y cordinate to start drawing the first line.</param>
  82.     /// <param name="Xcentre">The x cordinate to centre the text arround.</param>
  83.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  84.     /// <returns>The total height of the text.</returns>
  85.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string[] text, float YStart, float Xcentre, int lineSpacing = -1)
  86.     {
  87.         // temporary spacing
  88.         int originalLineSpacing = font.LineSpacing;
  89.         if (lineSpacing != -1) { font.LineSpacing = lineSpacing; }
  90.  
  91.         // string drawing
  92.         for (int i = 0; i < text.Length; i++)
  93.         {
  94.             Vector2 stringSize = font.MeasureString(text[i]);
  95.             spriteBatch.DrawString(font, text[i], new Vector2(Xcentre, YStart + i * font.LineSpacing), fontColor, 0, new Vector2(stringSize.X / 2, 0), 1, SpriteEffects.None, 0);
  96.         }
  97.  
  98.         // return to normal spacing
  99.         if (lineSpacing != -1) { font.LineSpacing = originalLineSpacing; }
  100.         return (lineSpacing != -1 ? lineSpacing : originalLineSpacing) * text.Length;
  101.     }
  102.  
  103.     /// <summary>Draw a centered text line.</summary>
  104.     /// <param name="font">The font to use.</param>
  105.     /// <param name="fontColor">The color to draw the text in.</param>
  106.     /// <param name="text">An array with the lines to draw.</param>
  107.     /// <param name="textBox">A rectangle specifying the place the text should be drawn. (The text may go outside this box if it doesn't fit.)</param>
  108.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  109.     /// <returns>The total height of the text.</returns>
  110.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string[] text, Rectangle textBox, int lineSpacing = -1)
  111.     {
  112.         return drawCenteredString(spriteBatch, font, fontColor, text, textBox.Top, textBox.Left + textBox.Width / 2, lineSpacing);
  113.     }
  114.  
  115.     /// <summary>Draw a centered text line.</summary>
  116.     /// <param name="font">The font to use.</param>
  117.     /// <param name="fontColor">The color to draw the text in.</param>
  118.     /// <param name="text">An array with the lines to draw.</param>
  119.     /// <param name="YStart">The y cordinate to start drawing the first line.</param>
  120.     /// <param name="leftBound">The left bound of the intended text space. (Text may go outside if it's too big.)</param>
  121.     /// <param name="leftBound">The right bound of the intended text space. (Text may go outside if it's too big.)</param>
  122.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  123.     /// <returns>The total height of the text.</returns>
  124.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color fontColor, string[] text, float YStart, float leftBound, float rightBound, int lineSpacing = -1)
  125.     {
  126.         return drawCenteredString(spriteBatch, font, fontColor, text, YStart, (rightBound - leftBound) / 2, lineSpacing);
  127.     }
  128.  
  129.     /// <summary>Draw a centered text line.</summary>
  130.     /// <param name="font">The font to use.</param>
  131.     /// <param name="fontColor">The color to draw the text in.</param>
  132.     /// <param name="text">An array with the lines to draw.</param>
  133.     /// <param name="centre">The top centre of where the string should be drawn.</param>
  134.     /// <param name="lineSpacing">The hight of a line. -1 uses default font height</param>
  135.     /// <returns>The total height of the text.</returns>
  136.     public static int drawCenteredString(SpriteBatch spriteBatch, SpriteFont font, Color color, string[] text, Vector2 centre, int lineSpacing = -1)
  137.     {
  138.         return drawCenteredString(spriteBatch, font, color, text, centre.Y, centre.X, lineSpacing);
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement