protected override List <TextureAtlas> Read(ContentReader reader, List <TextureAtlas> existingInstance) { String assetName; Texture2D texture; List <TextureAtlas> atlasList = new List <TextureAtlas>(); int textureCount = 0; int subTextCount = 0; TextureAtlas curAtlas; int textWidth; int textHeight; SurfaceFormat format = SurfaceFormat.Bgr32; Microsoft.Xna.Framework.Graphics.TextureFilter minFilter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic; Microsoft.Xna.Framework.Graphics.TextureFilter maxFilter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic; LibGDXAtlasExtender.Model.KeyEnums.Repeat repeat = LibGDXAtlasExtender.Model.KeyEnums.Repeat.none; String buffStr; textureCount = reader.ReadInt32(); for (var i = 0; i < textureCount; i++) { assetName = reader.GetRelativeAssetPath(reader.ReadString()); textWidth = reader.ReadInt32(); textHeight = reader.ReadInt32(); buffStr = reader.ReadString(); KeyTypeUtilities.Parse <SurfaceFormat>(buffStr, ref format); buffStr = reader.ReadString(); KeyTypeUtilities.Parse <Microsoft.Xna.Framework.Graphics.TextureFilter>(buffStr, ref minFilter); buffStr = reader.ReadString(); KeyTypeUtilities.Parse <Microsoft.Xna.Framework.Graphics.TextureFilter>(buffStr, ref maxFilter); buffStr = reader.ReadString(); KeyTypeUtilities.Parse <LibGDXAtlasExtender.Model.KeyEnums.Repeat>(buffStr, ref repeat); Console.WriteLine("Assset Name: " + assetName); texture = reader.ContentManager.Load <Texture2D>(assetName); curAtlas = new TextureAtlas(texture, textWidth, textHeight, format, minFilter, maxFilter, repeat); subTextCount = reader.ReadInt32(); for (int s = 0; s < subTextCount; s++) { curAtlas.CreateRegion(reader.ReadString(), reader.ReadBoolean(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()); } Console.WriteLine("Width: {0} Height: {1}", curAtlas.GetRegion(0).Width, curAtlas.GetRegion(0).Height); atlasList.Add(curAtlas); } return(atlasList); }
/* * <summary> * Imports <see cref="TextureAtlasFile"/> from a json.atlas file to * a <see cref="TextureAtlasFile"/>. * </summary> * <param name="filename"> * A json.atlas file contains texture regions and attributes. * </param> */ public static TextureAtlasFile ImportFromFile(string filename) { FileParser lParser = new FileParser(); SectionTreeCollection tree; TextureAtlasFile textFile = new TextureAtlasFile(); TextureInfo textInfo; SubTextureInfo subtextInfo; List <string> values; tree = lParser.ReadFile(filename); foreach (SectionTreeCollection textureTree in tree.GetChildren()) { textInfo = new TextureInfo(textureTree.SectionName); // Set Texture the Properties values = textureTree.Keys["size"]; if (values != null && values.Count == 2) { try { textInfo.Width = int.Parse(values[0]); textInfo.Height = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); textInfo.Width = 0; textInfo.Height = 0; } } values = textureTree.Keys["filter"]; if (values != null && values.Count == 2) { TextureFilter filter = TextureFilter.Linear; KeyTypeUtilities.Parse <TextureFilter>(values[0], ref filter); textInfo.FilterMin = filter; KeyTypeUtilities.Parse <TextureFilter>(values[1], ref filter); textInfo.FilterMax = filter; } values = textureTree.Keys["format"]; if (values != null && values.Count == 1) { Format format = Format.Alpha; KeyTypeUtilities.Parse <Format>(values[0], ref format); textInfo.Format = format; } values = textureTree.Keys["repeat"]; if (values != null && values.Count == 1) { Repeat repeat = Repeat.xy; KeyTypeUtilities.Parse <Repeat>(values[0], ref repeat); textInfo.Repeat = repeat; } foreach (SectionTreeCollection subTextureTree in textureTree.GetChildren()) { subtextInfo = new SubTextureInfo(subTextureTree.SectionName); // Set Subtexture the Properties values = subTextureTree.Keys["xy"]; if (values != null && values.Count == 2) { try { subtextInfo.X = int.Parse(values[0]); subtextInfo.Y = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.X = 0; subtextInfo.Y = 0; } } values = subTextureTree.Keys["size"]; if (values != null && values.Count == 2) { try { subtextInfo.Width = int.Parse(values[0]); subtextInfo.Height = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.Width = 0; subtextInfo.Height = 0; } } values = subTextureTree.Keys["orig"]; if (values != null && values.Count == 2) { try { subtextInfo.OrigWidth = int.Parse(values[0]); subtextInfo.OrigHeight = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OrigWidth = 0; subtextInfo.OrigHeight = 0; } } values = subTextureTree.Keys["offset"]; if (values != null && values.Count == 2) { try { subtextInfo.OffsetWidth = int.Parse(values[0]); subtextInfo.OffsetHeight = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OffsetWidth = 0; subtextInfo.OffsetHeight = 0; } } values = subTextureTree.Keys["index"]; if (values != null && values.Count == 1) { try { subtextInfo.Index = int.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.Index = -1; } } values = subTextureTree.Keys["rotate"]; if (values != null && values.Count == 1) { try { subtextInfo.Rotate = bool.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.Rotate = false; } } values = subTextureTree.Keys["fps"]; if (values != null && values.Count == 1) { try { subtextInfo.FPS = float.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.FPS = null; } } values = subTextureTree.Keys["duration"]; if (values != null && values.Count == 1) { try { subtextInfo.Duration = int.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.Duration = null; } } textInfo.Subtexture.Add(subtextInfo); } textFile.Textures.Add(textInfo); } return(textFile); }