Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class GraphicsExtensions
- {
- public enum UITextAlignment
- {
- TOP_LEFT,
- TOP_CENTER,
- TOP_RIGHT,
- CENTER_LEFT,
- CENTER_CENTER,
- CENTER_RIGHT,
- BOTTOM_LEFT,
- BOTTOM_CENTER,
- BOTTOM_RIGHT
- }
- public static void DrawText(this Graphics graphics, string text, string font, float fontSize, Color color, UITextAlignment alignment)
- {
- var graphicsSize = Graphics.MeasureText(text, font, fontSize);
- var gw = graphicsSize.Width;
- var gh = graphicsSize.Height;
- var screenSize = Game.Resolution;
- var rw = screenSize.Width;
- var rh = screenSize.Height;
- var widthRight = rw - gw;
- var widthHalf = rw / 2.0f - gw / 2.0f;
- var heightHalf = rh / 2.0f - gh / 2.0f;
- var heightFixed = rh - gh * 1.5f;
- switch (alignment)
- {
- case UITextAlignment.TOP_LEFT:
- graphics.DrawText(text, font, fontSize, new PointF(0, 0), color);
- break;
- case UITextAlignment.TOP_CENTER:
- graphics.DrawText(text, font, fontSize, new PointF(widthHalf, 0), color);
- break;
- case UITextAlignment.TOP_RIGHT:
- graphics.DrawText(text, font, fontSize, new PointF(widthRight, 0), color);
- break;
- case UITextAlignment.CENTER_LEFT:
- graphics.DrawText(text, font, fontSize, new PointF(0, heightHalf), color);
- break;
- case UITextAlignment.CENTER_CENTER:
- graphics.DrawText(text, font, fontSize, new PointF(widthHalf, heightHalf), color);
- break;
- case UITextAlignment.CENTER_RIGHT:
- graphics.DrawText(text, font, fontSize, new PointF(widthRight, heightHalf), color);
- break;
- case UITextAlignment.BOTTOM_LEFT:
- graphics.DrawText(text, font, fontSize, new PointF(0, heightFixed), color);
- break;
- case UITextAlignment.BOTTOM_CENTER:
- graphics.DrawText(text, font, fontSize, new PointF(widthHalf, heightFixed), color);
- break;
- case UITextAlignment.BOTTOM_RIGHT:
- graphics.DrawText(text, font, fontSize, new PointF(widthRight, heightFixed), color);
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(alignment), alignment, null);
- }
- }
- }
Add Comment
Please, Sign In to add comment