コード例 #1
0
ファイル: Gdx.cs プロジェクト: janfokke/Myra
        public static TextureRegionAtlas FromGDX(string data, Func <string, Texture2D> textureLoader)
        {
            GDXPageData pageData    = null;
            var         spriteDatas = new Dictionary <string, GDXSpriteData>();

            using (var textReader = new StringReader(data))
            {
                GDXSpriteData spriteData = null;
                while (true)
                {
                    var s = textReader.ReadLine();
                    if (s == null)
                    {
                        break;
                    }

                    s = s.Trim();
                    if (string.IsNullOrEmpty(s))
                    {
                        // New PageData
                        pageData = null;
                        continue;
                    }

                    if (pageData == null)
                    {
                        var texture = textureLoader(s);
                        if (texture == null)
                        {
                            throw new Exception(string.Format("Unable to resolve texture {0}", s));
                        }

                        pageData = new GDXPageData {
                            Texture = texture
                        };
                        continue;
                    }

                    if (!s.Contains(":"))
                    {
                        spriteData = new GDXSpriteData
                        {
                            PageData = pageData,
                            Name     = s
                        };

                        spriteDatas[s] = spriteData;
                        continue;
                    }

                    var parts = s.Split(':');

                    var key   = parts[0].Trim().ToLower();
                    var value = parts[1].Trim();

                    switch (key)
                    {
                    case "format":
                        SurfaceFormat format;
                        if (!Enum.TryParse(value, out format))
                        {
#if !XENKO
                            format = SurfaceFormat.Color;
#else
                            format = SurfaceFormat.R8G8B8A8_SNorm;
#endif
                        }

                        pageData.Format = format;
                        break;

                    case "filter":
                        parts = value.Split(',');
                        var minFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[0].Trim());
                        var magFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[1].Trim());

                        pageData.Filter = FromGDXFilters(minFilter, magFilter);
                        break;

                    case "repeat":
                        if (value == "x")
                        {
                            pageData.UWrap = TextureAddressMode.Wrap;
                        }
                        else if (value == "y")
                        {
                            pageData.VWrap = TextureAddressMode.Wrap;
                        }
                        else if (value == "xy")
                        {
                            pageData.UWrap = TextureAddressMode.Wrap;
                            pageData.VWrap = TextureAddressMode.Wrap;
                        }

                        break;

                    case "rotate":
                        bool isRotated;
                        bool.TryParse(value, out isRotated);
                        spriteData.IsRotated = isRotated;
                        break;

                    case "xy":
                        parts = value.Split(',');
                        spriteData.SourceRectangle.X = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Y = int.Parse(parts[1].Trim());

                        break;

                    case "size":
                        if (spriteData == null)
                        {
                            continue;
                        }

                        parts = value.Split(',');
                        spriteData.SourceRectangle.Width  = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Height = int.Parse(parts[1].Trim());

                        break;

                    case "orig":
                        parts = value.Split(',');
                        spriteData.OriginalSize = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));

                        break;

                    case "offset":
                        parts             = value.Split(',');
                        spriteData.Offset = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
                        break;

                    case "split":
                        parts = value.Split(',');
                        var split = new Thickness
                        {
                            Left   = int.Parse(parts[0].Trim()),
                            Right  = int.Parse(parts[1].Trim()),
                            Top    = int.Parse(parts[2].Trim()),
                            Bottom = int.Parse(parts[3].Trim())
                        };

                        spriteData.Split = split;
                        break;
                    }
                }
            }

            var result  = new TextureRegionAtlas();
            var regions = result.Regions;
            foreach (var sd in spriteDatas)
            {
                var texture = sd.Value.PageData.Texture;
                var bounds  = sd.Value.SourceRectangle;

                TextureRegion IRenderable;
                if (!sd.Value.Split.HasValue)
                {
                    IRenderable = new TextureRegion(texture, bounds);
                }
                else
                {
                    IRenderable = new NinePatchRegion(texture, bounds, sd.Value.Split.Value);
                }

                regions[sd.Key] = IRenderable;
            }

            return(result);
        }
コード例 #2
0
        public static TextureAtlasContent Load(string data)
        {
            var mode = GDXMode.PageHeader;

            GDXPageData pageData    = null;
            var         spriteDatas = new Dictionary <string, GDXSpriteData>();

            using (var textReader = new StringReader(data))
            {
                GDXSpriteData spriteData = null;
                while (true)
                {
                    var s = textReader.ReadLine();
                    if (s == null)
                    {
                        break;
                    }

                    s = s.Trim();
                    if (string.IsNullOrEmpty(s))
                    {
                        // New PageData
                        pageData = null;
                        continue;
                    }

                    if (pageData == null)
                    {
                        var texture = s;
                        pageData = new GDXPageData {
                            Texture = texture
                        };
                        mode = GDXMode.PageHeader;
                        continue;
                    }

                    if (!s.Contains(":"))
                    {
                        spriteData = new GDXSpriteData
                        {
                            PageData = pageData,
                            Name     = s
                        };

                        spriteDatas[s] = spriteData;
                        mode           = GDXMode.Sprite;
                        continue;
                    }

                    var parts = s.Split(':');

                    var key   = parts[0].Trim().ToLower();
                    var value = parts[1].Trim();

                    switch (key)
                    {
                    case "format":
                        SurfaceFormat format;
                        if (!Enum.TryParse(value, out format))
                        {
                            format = SurfaceFormat.Color;
                        }
                        pageData.Format = format;
                        break;

                    case "filter":
                        parts = value.Split(',');
                        var minFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[0].Trim());
                        var magFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[1].Trim());

                        pageData.Filter = FromGDXFilters(minFilter, magFilter);
                        break;

                    case "repeat":
                        if (value == "x")
                        {
                            pageData.UWrap = TextureAddressMode.Wrap;
                        }
                        else if (value == "y")
                        {
                            pageData.VWrap = TextureAddressMode.Wrap;
                        }
                        else if (value == "xy")
                        {
                            pageData.UWrap = TextureAddressMode.Wrap;
                            pageData.VWrap = TextureAddressMode.Wrap;
                        }
                        break;

                    case "rotate":
                        bool isRotated;
                        bool.TryParse(value, out isRotated);
                        spriteData.IsRotated = isRotated;
                        break;

                    case "xy":
                        parts = value.Split(',');
                        spriteData.SourceRectangle.X = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Y = int.Parse(parts[1].Trim());

                        break;

                    case "size":
                        if (spriteData == null)
                        {
                            continue;
                        }
                        parts = value.Split(',');
                        spriteData.SourceRectangle.Width  = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Height = int.Parse(parts[1].Trim());

                        break;

                    case "orig":
                        parts = value.Split(',');
                        spriteData.OriginalSize = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));

                        break;

                    case "offset":
                        parts             = value.Split(',');
                        spriteData.Offset = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
                        break;

                    case "split":
                        parts = value.Split(',');
                        var split = new Thickness
                        {
                            Left   = int.Parse(parts[0].Trim()),
                            Right  = int.Parse(parts[1].Trim()),
                            Top    = int.Parse(parts[2].Trim()),
                            Bottom = int.Parse(parts[3].Trim())
                        };

                        spriteData.Split = split;
                        break;
                    }
                }
            }

            var result = new TextureAtlasContent
            {
                Name = pageData.Texture
            };


            var regions = new List <TextureAtlasRegionContent>();

            foreach (var pair in spriteDatas)
            {
                var sd     = pair.Value;
                var bounds = sd.SourceRectangle;

                var texturePackerRegion = new TextureAtlasRegionContent
                {
                    Name          = sd.Name,
                    Bounds        = bounds,
                    NinePatchInfo = sd.Split
                };
                regions.Add(texturePackerRegion);
            }

            result.Regions = regions.ToArray();

            return(result);
        }
コード例 #3
0
        public static TextureRegionAtlas FromGDX(string data)
        {
            GDXPageData pageData    = null;
            var         spriteDatas = new Dictionary <string, GDXSpriteData>();

            using (var textReader = new StringReader(data))
            {
                GDXSpriteData spriteData = null;
                while (true)
                {
                    var s = textReader.ReadLine();
                    if (s == null)
                    {
                        break;
                    }

                    s = s.Trim();
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }

                    if (pageData == null)
                    {
                        pageData = new GDXPageData {
                            ImageFile = s
                        };
                        continue;
                    }

                    if (!s.Contains(":"))
                    {
                        spriteData = new GDXSpriteData
                        {
                            Name = s
                        };

                        spriteDatas[s] = spriteData;
                        continue;
                    }

                    var parts = s.Split(':');

                    var key   = parts[0].Trim().ToLower();
                    var value = parts[1].Trim();

                    switch (key)
                    {
                    case "format":
                        break;

                    case "filter":
                        break;

                    case "repeat":
                        break;

                    case "rotate":
                        bool isRotated;
                        bool.TryParse(value, out isRotated);
                        spriteData.IsRotated = isRotated;
                        break;

                    case "xy":
                        parts = value.Split(',');
                        spriteData.SourceRectangle.X = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Y = int.Parse(parts[1].Trim());

                        break;

                    case "size":
                        if (spriteData == null)
                        {
                            continue;
                        }

                        parts = value.Split(',');
                        spriteData.SourceRectangle.Width  = int.Parse(parts[0].Trim());
                        spriteData.SourceRectangle.Height = int.Parse(parts[1].Trim());

                        break;

                    case "orig":
                        parts = value.Split(',');
                        spriteData.OriginalSize = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));

                        break;

                    case "offset":
                        parts             = value.Split(',');
                        spriteData.Offset = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
                        break;

                    case "split":
                        parts = value.Split(',');
                        var split = new Thickness
                        {
                            Left   = int.Parse(parts[0].Trim()),
                            Right  = int.Parse(parts[1].Trim()),
                            Top    = int.Parse(parts[2].Trim()),
                            Bottom = int.Parse(parts[3].Trim())
                        };

                        spriteData.Split = split;
                        break;
                    }
                }
            }

            if (pageData == null)
            {
                throw new Exception("Could not parse header");
            }

            var result = new TextureRegionAtlas
            {
                ImageFile = pageData.ImageFile
            };

            var regions = result.Regions;

            foreach (var sd in spriteDatas)
            {
                var bounds = sd.Value.SourceRectangle;

                TextureRegion region;
                if (!sd.Value.Split.HasValue)
                {
                    region = new TextureRegion
                    {
                        Bounds = bounds
                    };
                }
                else
                {
                    region = new NinePatchRegion
                    {
                        Bounds = bounds,
                        Info   = sd.Value.Split.Value
                    };
                }

                regions[sd.Key] = region;
            }

            return(result);
        }