LostProphet

RPH Graphics Extensions

Jul 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class GraphicsExtensions
  2.     {
  3.         public enum UITextAlignment
  4.         {
  5.             TOP_LEFT,
  6.             TOP_CENTER,
  7.             TOP_RIGHT,
  8.  
  9.             CENTER_LEFT,
  10.             CENTER_CENTER,
  11.             CENTER_RIGHT,
  12.  
  13.             BOTTOM_LEFT,
  14.             BOTTOM_CENTER,
  15.             BOTTOM_RIGHT
  16.         }
  17.  
  18.         public static void DrawText(this Graphics graphics, string text, string font, float fontSize, Color color, UITextAlignment alignment)
  19.         {
  20.             var graphicsSize = Graphics.MeasureText(text, font, fontSize);
  21.             var gw = graphicsSize.Width;
  22.             var gh = graphicsSize.Height;
  23.             var screenSize = Game.Resolution;
  24.             var rw = screenSize.Width;
  25.             var rh = screenSize.Height;
  26.  
  27.             var widthRight = rw - gw;
  28.             var widthHalf = rw / 2.0f - gw / 2.0f;
  29.             var heightHalf = rh / 2.0f - gh / 2.0f;
  30.             var heightFixed = rh - gh * 1.5f;
  31.  
  32.             switch (alignment)
  33.             {
  34.                 case UITextAlignment.TOP_LEFT:
  35.                     graphics.DrawText(text, font, fontSize, new PointF(0, 0), color);
  36.                     break;
  37.                 case UITextAlignment.TOP_CENTER:
  38.                     graphics.DrawText(text, font, fontSize, new PointF(widthHalf, 0), color);
  39.                     break;
  40.                 case UITextAlignment.TOP_RIGHT:
  41.                     graphics.DrawText(text, font, fontSize, new PointF(widthRight, 0), color);
  42.                     break;
  43.                 case UITextAlignment.CENTER_LEFT:
  44.                     graphics.DrawText(text, font, fontSize, new PointF(0, heightHalf), color);
  45.                     break;
  46.                 case UITextAlignment.CENTER_CENTER:
  47.                     graphics.DrawText(text, font, fontSize, new PointF(widthHalf, heightHalf), color);
  48.                     break;
  49.                 case UITextAlignment.CENTER_RIGHT:
  50.                     graphics.DrawText(text, font, fontSize, new PointF(widthRight, heightHalf), color);
  51.                     break;
  52.                 case UITextAlignment.BOTTOM_LEFT:
  53.                     graphics.DrawText(text, font, fontSize, new PointF(0, heightFixed), color);
  54.                     break;
  55.                 case UITextAlignment.BOTTOM_CENTER:
  56.                     graphics.DrawText(text, font, fontSize, new PointF(widthHalf, heightFixed), color);
  57.                     break;
  58.                 case UITextAlignment.BOTTOM_RIGHT:
  59.                     graphics.DrawText(text, font, fontSize, new PointF(widthRight, heightFixed), color);
  60.                     break;
  61.                 default:
  62.                     throw new ArgumentOutOfRangeException(nameof(alignment), alignment, null);
  63.             }
  64.         }
  65.     }
Add Comment
Please, Sign In to add comment