public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Rectangle?sourceRectangle, Color color)
 {
     spriteBatch.Draw(texture,
                      destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
                      sourceRectangle,
                      color * ctrl.AbsoluteOpacity());
 }
 public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, Texture2D texture, Rectangle destinationRectangle, Rectangle?sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects = SpriteEffects.None)
 {
     spriteBatch.Draw(texture,
                      destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
                      sourceRectangle,
                      color * ctrl.AbsoluteOpacity(),
                      rotation,
                      origin,
                      effects,
                      0);
 }
 public static void DrawOnCtrl(this SpriteBatch spriteBatch, Control ctrl, TextureRegion2D texture, Rectangle destinationRectangle)
 {
     spriteBatch.Draw(texture,
                      destinationRectangle.ToBounds(ctrl.AbsoluteBounds),
                      Color.White * ctrl.AbsoluteOpacity());
 }
        public static void DrawStringOnCtrl(this SpriteBatch spriteBatch,
                                            Control ctrl,
                                            string text,
                                            BitmapFont font,
                                            Rectangle destinationRectangle,
                                            Color color,
                                            bool wrap,
                                            bool stroke,
                                            int strokeDistance = 1,
                                            HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
                                            VerticalAlignment verticalAlignment     = VerticalAlignment.Middle)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            text = wrap ? DrawUtil.WrapText(font, text, destinationRectangle.Width) : text;

            // TODO: This does not account for vertical alignment
            if (horizontalAlignment != HorizontalAlignment.Left && (wrap || text.Contains("\n")))
            {
                using (StringReader reader = new StringReader(text)) {
                    string line;

                    int lineHeightDiff = 0;

                    while (destinationRectangle.Height - lineHeightDiff > 0 && (line = reader.ReadLine()) != null)
                    {
                        DrawStringOnCtrl(spriteBatch, ctrl, line, font, destinationRectangle.Add(0, lineHeightDiff, 0, -0), color, wrap, stroke, strokeDistance, horizontalAlignment, verticalAlignment);

                        lineHeightDiff += font.LineHeight;
                    }
                }

                return;
            }

            Vector2 textSize = font.MeasureString(text);

            destinationRectangle = destinationRectangle.ToBounds(ctrl.AbsoluteBounds);

            int xPos = destinationRectangle.X;
            int yPos = destinationRectangle.Y;

            switch (horizontalAlignment)
            {
            case HorizontalAlignment.Center:
                xPos += destinationRectangle.Width / 2 - (int)textSize.X / 2;
                break;

            case HorizontalAlignment.Right:
                xPos += destinationRectangle.Width - (int)textSize.X;
                break;
            }

            switch (verticalAlignment)
            {
            case VerticalAlignment.Middle:
                yPos += destinationRectangle.Height / 2 - (int)textSize.Y / 2;
                break;

            case VerticalAlignment.Bottom:
                yPos += destinationRectangle.Height - (int)textSize.Y;
                break;
            }

            var textPos = new Vector2(xPos, yPos);

            if (stroke)
            {
                spriteBatch.DrawString(font, text, textPos.OffsetBy(0, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, 0), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(0, strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, 0), Color.Black * ctrl.AbsoluteOpacity());
                spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity());
            }

            spriteBatch.DrawString(font, text, textPos, color * ctrl.AbsoluteOpacity());
        }