Пример #1
0
        /// <summary>
        /// Froms the file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static BitmapFont FromFile(string path)
        {
            BitmapFont bitmap = new BitmapFont();
            bitmap.Color = Color.White;

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

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

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

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

                Rectangle 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 = AlmiranteEngine.Resources.LoadTexture(texturePath);
            return bitmap;
        }
Пример #2
0
        /// <summary>
        /// Froms the file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static BitmapFont FromFile(string path)
        {
            BitmapFont bitmap = new BitmapFont();

            bitmap.Color = Color.White;

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

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

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

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

                Rectangle 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 = AlmiranteEngine.Resources.LoadTexture(texturePath);
            return(bitmap);
        }
Пример #3
0
 /// <summary>
 /// Initializes the scene.
 /// </summary>
 protected override void OnInitialize()
 {
     this.BackgroundColor = Color.SlateGray;
     this.texture = AlmiranteEngine.Resources.LoadTexture("Content\\Box.png");
     this.font = BitmapFont.FromFile("Content\\Default.bitmapfont");
 }
Пример #4
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));
 }
Пример #5
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);
 }
Пример #6
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;
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Initializes the scene.
 /// </summary>
 protected override void OnInitialize()
 {
     font = AlmiranteEngine.Resources.DefaultFont;
 }