Guest User

Untitled

a guest
May 3rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1.         // smart text drawing
  2.         // text        = the text to draw
  3.         // bounds      = the box where the text will be drawn
  4.         // spritebatch = the spritebatch
  5.         // font        = the font
  6.         // cutoff      = the point (character) where the text stops drawing (doesn't effect allignment)
  7.         // allignmentX = the allignment in which the text will be drawn (X
  8.         // allignmentY = the allignment in which the text will be drawn (Y)
  9.         // paddingX    = padding between lines and the edge of the bounds (X)
  10.         // paddingY    = padding between lines and the edge of the bounds (Y)
  11.         // measureOnly = whether or not to actually draw text or just measure the size the text would take (easier not to enable manually, just use helper function below)
  12.  
  13.         // TODO character / word that forces new line or to stop displaying until interacted with
  14.  
  15.         public static Point smartDrawText(string text, Rectangle bounds, SpriteBatch spriteBatch, SpriteFont font, Color color, int cutoff = 0, allignX allignmentX = allignX.LEFT, allignY allignmentY = allignY.TOP, int paddingX = 2, int paddingY = 2, bool measureOnly = false) {
  16.             string[] words = text.Split(' '); // array of the words in text
  17.             float textHeight = font.MeasureString(text).Y;
  18.             float yProgress = 0;
  19.  
  20.             // calculate cutoff point
  21.             int tempCounter = 0;
  22.             int displayedWords = 0;
  23.             int finalWordCutoff = 0;
  24.             foreach (string w in words) {
  25.                 if (tempCounter + w.Length + 1 <= cutoff) {
  26.                     tempCounter += w.Length + 1;
  27.                 }
  28.                 else if (tempCounter + w.Length <= cutoff) {
  29.                     tempCounter += w.Length;
  30.                 }
  31.                 else {
  32.                     finalWordCutoff = cutoff - tempCounter;
  33.                     break;
  34.                 }
  35.                 displayedWords++;
  36.             }
  37.  
  38.             // store lines
  39.             List<string> lines = new List<string>();
  40.             List<Vector2> lineLocations = new List<Vector2>();
  41.  
  42.             // loop through lines
  43.             int word = 0;
  44.             while (word < words.Count()) {
  45.  
  46.                 // construct text for each line
  47.                 string line = "";
  48.                 float lineWidth = paddingX;
  49.                 while (lineWidth <= bounds.Width - paddingX & word < words.Count()) {
  50.                     float newLineWidth = lineWidth + font.MeasureString(words[word] + " ").X;
  51.                     if (newLineWidth <= bounds.Width) {
  52.  
  53.                         if (word < displayedWords || cutoff == 0) {
  54.                             line = line + words[word] + ' ';
  55.                         }
  56.                         else if (word == displayedWords) {
  57.                             line = line + words[word].Substring(0, finalWordCutoff);
  58.                         }
  59.                         lineWidth = newLineWidth + 1;
  60.                         word++;
  61.                     }
  62.                     else {
  63.                         break;
  64.                     }
  65.                 }
  66.  
  67.                 // add x allignment
  68.                 Vector2 offsetX = new Vector2(paddingX, yProgress);
  69.                 float spaceLeftX = bounds.Width - lineWidth - paddingX * 2;
  70.                 switch (allignmentX) {
  71.                     case allignX.LEFT:
  72.                         break;
  73.                     case allignX.CENTER:
  74.                         offsetX = new Vector2(spaceLeftX / 2, offsetX.Y);
  75.                         break;
  76.                     case allignX.RIGHT:
  77.                         offsetX = new Vector2(spaceLeftX, offsetX.Y);
  78.                         break;
  79.                 }
  80.  
  81.                 lines.Add(line); lineLocations.Add(new Vector2(bounds.X + offsetX.X, bounds.Y + offsetX.Y));
  82.  
  83.                 // add to progress on y axis
  84.                 yProgress += textHeight + 2;
  85.             }
  86.  
  87.             // get size of "text chunk"
  88.             // if only measuring, just return size
  89.             Point size = new Point(bounds.Width, (int)yProgress);
  90.             if (measureOnly) return size;
  91.  
  92.             // add y allignment
  93.             // yProgress now represents the height of the "chunk of text"
  94.             float spaceLeftY = bounds.Height - yProgress - paddingY * 4;
  95.             Vector2 offsetY = new Vector2(0, spaceLeftY + paddingY);
  96.             switch (allignmentY) {
  97.                 case allignY.TOP:
  98.                     break;
  99.                 case allignY.CENTER:
  100.                     offsetY = new Vector2(0, offsetY.Y / 2);
  101.                     break;
  102.                 case allignY.BOTTOM:
  103.                     offsetY = new Vector2(0, offsetY.Y);
  104.                     break;
  105.             }
  106.  
  107.             // finally draw all lines with proper x and y allignment options applied
  108.             for (int counter = 0; counter < lines.Count; counter++) {
  109.                 spriteBatch.DrawString(font, lines.ElementAt(counter), new Vector2(lineLocations.ElementAt(counter).X, lineLocations.ElementAt(counter).Y + offsetY.Y), color, 0, new Vector2(), 1, SpriteEffects.None, 0.0009F);
  110.                 spriteBatch.DrawString(font, lines.ElementAt(counter), new Vector2(lineLocations.ElementAt(counter).X + 2, lineLocations.ElementAt(counter).Y + offsetY.Y + 2), Color.Black * 0.8F, 0, new Vector2(), 1, SpriteEffects.None, 0.00091F);
  111.             }
  112.  
  113.             return size;
  114.         }
  115.         public static Point measureText(string text, Rectangle bounds, SpriteFont font) {
  116.             return smartDrawText(text, bounds, null, font, Color.White, 0, allignX.LEFT, allignY.TOP, 2, 2, true);
  117.         }
Advertisement
Add Comment
Please, Sign In to add comment