Beispiel #1
0
 public PixelText(PixelFont font, string text, Color color)
     : base(false, true)
 {
     Font  = font;
     Text  = text;
     Color = color;
     Text  = text;
     size  = Font.Sizes[0];
     Refresh();
 }
Beispiel #2
0
        public PixelFontSize AddFontSize(string path, XmlElement data, Atlas atlas = null, bool outline = false)
        {
            // check if size already exists
            var size = data["info"].AttrFloat("size");

            foreach (var fs in Sizes)
            {
                if (fs.Size == size)
                {
                    return(fs);
                }
            }

            // get textures
            Textures = new List <MTexture>();
            var pages = data["pages"];

            foreach (XmlElement page in pages)
            {
                var file      = page.Attr("file");
                var atlasPath = Path.GetFileNameWithoutExtension(file);

                if (atlas != null && atlas.Has(atlasPath))
                {
                    Textures.Add(atlas[atlasPath]);
                }
                else
                {
                    var dir = Path.GetDirectoryName(path);
                    dir = dir.Substring(Engine.ContentDirectory.Length + 1);
                    Textures.Add(MTexture.FromFile(Path.Combine(dir, file)));
                }
            }

            // create font size
            var fontSize = new PixelFontSize()
            {
                Textures   = Textures,
                Characters = new Dictionary <int, PixelFontCharacter>(),
                LineHeight = data["common"].AttrInt("lineHeight"),
                Size       = size,
                Outline    = outline
            };

            // get characters
            foreach (XmlElement character in data["chars"])
            {
                int id   = character.AttrInt("id");
                int page = character.AttrInt("page", 0);
                fontSize.Characters.Add(id, new PixelFontCharacter(id, Textures[page], character));
            }

            // get kerning
            if (data["kernings"] != null)
            {
                foreach (XmlElement kerning in data["kernings"])
                {
                    var from = kerning.AttrInt("first");
                    var to   = kerning.AttrInt("second");
                    var push = kerning.AttrInt("amount");

                    PixelFontCharacter c = null;
                    if (fontSize.Characters.TryGetValue(from, out c))
                    {
                        c.Kerning.Add(to, push);
                    }
                }
            }

            // add font size
            Sizes.Add(fontSize);
            Sizes.Sort((a, b) => { return(Math.Sign(a.Size - b.Size)); });

            return(fontSize);
        }