Пример #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)
        {
            var tempPos = Vector2.Zero;
            using (var 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;
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Froms the file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static BitmapFont FromFile(string path)
        {
            var bitmap = new BitmapFont();
            bitmap.Color = Color.White;

            var dir = Path.GetDirectoryName(path);
            var texturePath = Path.Combine(dir, Path.GetFileNameWithoutExtension(path) + ".png");

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path + " not found.");
            }
            if (!File.Exists(texturePath))
            {
                throw new FileNotFoundException(texturePath + " not found.");
            }

            var fontMetric = XElement.Parse(File.ReadAllText(path));

            foreach (var charElement in fontMetric.Elements("character"))
            {
                var code = Convert.ToChar((uint) charElement.Attribute("key"));

                var rect = new Rectangle(
                    (int) charElement.Element("x"), (int) charElement.Element("y"),
                    (int) charElement.Element("width"), (int) charElement.Element("height")
                    );

                bitmap.Characters.Add(code, rect);
            }

            var hgap = fontMetric.Element("hgap");
            if (hgap != null)
            {
                bitmap.HorizontalGap = Convert.ToInt32(hgap.Value);
            }

            var vgap = fontMetric.Element("vgap");
            if (vgap != null)
            {
                bitmap.VerticalGap = Convert.ToInt32(vgap.Value);
            }

            int x = 0, y = 0, w = 0, h = 0;
            var offleft = fontMetric.Element("offleft");
            if (offleft != null)
            {
                x = Convert.ToInt32(offleft.Value);
            }

            var offtop = fontMetric.Element("offtop");
            if (offtop != null)
            {
                y = Convert.ToInt32(offtop.Value);
            }
            var offright = fontMetric.Element("offright");
            if (offright != null)
            {
                w = Convert.ToInt32(offright.Value);
            }

            var offbottom = fontMetric.Element("offbottom");
            if (offbottom != null)
            {
                h = Convert.ToInt32(offbottom.Value);
            }
            bitmap.Offset = new Rectangle(x, y, w, h);

            bitmap.Texture = Manager.Resources.LoadTexture(texturePath);
            return bitmap;
        }
Пример #3
0
 /// <summary>
 ///     OnDraw text on scene
 /// </summary>
 /// <param name="batch">The batch.</param>
 /// <param name="font">The font.</param>
 /// <param name="position">Position to begin drawing</param>
 /// <param name="alignment">The alignment.</param>
 /// <param name="input">A formated string</param>
 /// <param name="args">Optional parameters for the formated string</param>
 public static void DrawFont(this SpriteBatch batch, BitmapFont font, Vector2 position, FontAlignment alignment,
     string input, params object[] args)
 {
     batch.DrawFont(font, position, alignment, font.Color, string.Format(CultureInfo.CurrentCulture, input, args));
 }
Пример #4
0
 /// <summary>
 ///     OnDraw text on scene using default orientation (Left)
 /// </summary>
 /// <param name="batch">The batch.</param>
 /// <param name="font">The font.</param>
 /// <param name="position">Position to begin drawing</param>
 /// <param name="color">The color.</param>
 /// <param name="input">A formated string</param>
 /// <param name="args">Optional parameters for the formated string</param>
 public static void DrawFont(this SpriteBatch batch, BitmapFont font, Vector2 position, Color color, string input,
     params object[] args)
 {
     batch.DrawFont(font, position, FontAlignment.Left, color, input, args);
 }