Beispiel #1
0
        /// <summary>
        /// OnDraw text on scene
        /// </summary>
        /// <param name="batch">The batch.</param>
        /// <param name="font">The font.</param>
        /// <param name="position">The position.</param>
        /// <param name="alignment">The alignment.</param>
        /// <param name="color">The color.</param>
        /// <param name="input">The input.</param>
        public static void DrawFont(this SpriteBatch batch, BitmapFont font, Vector2 position, FontAlignment alignment, Color color, string input)
        {
            Vector2 tempPos = Vector2.Zero;

            using (StringReader reader = new StringReader(input))
            {
                tempPos.X = position.X;
                tempPos.Y = position.Y + font.Offset.Y;// -(FontHeight / 2);
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    switch (alignment)
                    {
                    case FontAlignment.Left:
                        tempPos.X += font.Offset.X;
                        break;

                    case FontAlignment.Center:
                    {
                        var size = font.MeasureString(line);
                        tempPos.X -= (int)(size.X / 2) - font.Offset.X;
                        break;
                    }

                    case FontAlignment.Right:
                    {
                        var size = font.MeasureString(line);
                        tempPos.X -= size.X - font.Offset.Width;
                        break;
                    }

                    default:
                        // do the defalut action
                        break;
                    }
                    font.DrawLine(batch, tempPos, color, line);
                    tempPos.X  = position.X;
                    tempPos.Y += font.FontHeight + font.VerticalGap;
                }
            }
        }