All pastes #986593 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-04-15 22:07 UTC
rendered paste body
 public void DrawText(SpriteBatch batch, SpriteFont font, string text, Rectangle dest, HorizontalAlignment horizontal, VerticalAlignment vertical, Color color)
        {
            if (text == "")
                return;

            if (font == null)
            {
                throw new NullReferenceException("font");
            }

            text = Utility.StringReplaceTabs(text);
            string[] lines = Utility.StringToLines(text);
            ScreenText[] st = new ScreenText[lines.Length];

            if (horizontal == HorizontalAlignment.Left)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    st[i] = new ScreenText();
                    st[i].Position = new Vector2(dest.X, dest.Y + i * font.LineSpacing);
                    st[i].Text = lines[i];
                    st[i].Color = color;
                }
            }
            else if (horizontal == HorizontalAlignment.Center)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    Vector2 dim = font.MeasureString(lines[i]);

                    st[i] = new ScreenText();
                    st[i].Position = new Vector2((dest.X + (dest.Width / 2)) - (int)(dim.X / 2),
                                                dest.Y + i * font.LineSpacing);
                    st[i].Text = lines[i];
                    st[i].Color = color;
                }
            }
            else if (horizontal == HorizontalAlignment.Right)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    Vector2 dim = font.MeasureString(lines[i]);

                    st[i] = new ScreenText();
                    st[i].Position = new Vector2(dest.Right - (int)dim.X,
                                               dest.Y + i * font.LineSpacing);
                    st[i].Text = lines[i];
                    st[i].Color = color;
                }
            }

            if (vertical == VerticalAlignment.Center)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    Vector2 dim = font.MeasureString(lines[i]);
                    st[i].Position.Y = dest.Y + (dest.Height / 2) - (lines.Length * font.LineSpacing / 2) + (i * font.LineSpacing);
                }
            }
            else if (vertical == VerticalAlignment.Bottom)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    Vector2 dim = font.MeasureString(lines[i]);
                    st[i].Position.Y = dest.Bottom - (lines.Length - i) * font.LineSpacing;
                }
            }

            for (int i = 0; i < st.Length; i++)
            {
                batch.DrawString(font, st[i].Text, st[i].Position, st[i].Color);
            }
        }