private void Load()
        {
            Spriter = LoadContent <Spriter>(scmlPath);
            if (Spriter.Atlases == null || Spriter.Atlases.Length == 0)
            {
                return;
            }
            atlases = new Dictionary <int, SpriterAtlas>();
            infos   = new Dictionary <SpriterAtlas, Dictionary <string, ImageInfo> >();

            foreach (var atlasRef in Spriter.Atlases)
            {
                String       path  = FormatPath(atlasRef.Name);
                SpriterAtlas atlas = content.Load <SpriterAtlas>(path);
                atlases[atlasRef.Id] = atlas;

                Dictionary <string, ImageInfo> imageInfos = new Dictionary <string, ImageInfo>();
                infos[atlas] = imageInfos;

                foreach (ImageInfo info in atlas.ImageInfos)
                {
                    imageInfos[info.Name] = info;
                }
            }
        }
        public void Fill(SpriterAtlas atlas)
        {
            atlas.Meta       = Meta;
            atlas.ImageInfos = new List <ImageInfo>();

            foreach (var entry in Frames.ImageInfos)
            {
                ImageInfo info = entry.Value;
                info.Name = entry.Key;
                atlas.ImageInfos.Add(info);
            }
        }
        private void AddAtlasFolder(SpriterFolder folder, DefaultProviderFactory <Sprite, SoundEffect> factory)
        {
            SpriterAtlas atlas   = atlases[folder.AtlasId];
            Texture2D    texture = content.Load <Texture2D>(FormatPath(atlas.Meta.Image));
            Dictionary <string, ImageInfo> imageInfos = infos[atlas];

            foreach (SpriterFile file in folder.Files)
            {
                ImageInfo info = imageInfos[file.Name];

                // "x", "y" = location in spritesheet, "w", "h" = trimmed unrotated image size
                Size frame = info.Frame;

                // "w", "h" = original image size
                Size source = info.SourceSize;

                // "x", "y" = trimmed offset - pixels trimmed from the top and left
                Size spriteSource = info.SpriteSourceSize;

                Sprite sprite = new Sprite
                {
                    Texture = texture,
                    Width   = source.W,
                    Height  = source.H
                };

                sprite.TrimLeft   = spriteSource.X;
                sprite.TrimRight  = source.W - frame.W - spriteSource.X;
                sprite.TrimTop    = spriteSource.Y;
                sprite.TrimBottom = source.H - frame.H - spriteSource.Y;

                if (info.Rotated)
                {
                    sprite.SourceRectangle = new Rectangle(frame.X, frame.Y, frame.H, frame.W);
                    sprite.Rotated         = true;
                }
                else
                {
                    sprite.SourceRectangle = new Rectangle(frame.X, frame.Y, frame.W, frame.H);
                }

                factory.SetSprite(Spriter, folder, file, sprite);
            }
        }
        public override SpriterAtlasWrapper Import(string filename, ContentImporterContext context)
        {
            context.Logger.LogMessage("Importing Spriter Atlas file: {0}", filename);
            string jsonData = File.ReadAllText(filename);

            SpriterAtlasWrapper ret = new SpriterAtlasWrapper();

            SpriterAtlasJson atlasJson = JsonConvert.DeserializeObject <SpriterAtlasJson>(jsonData, new RectangleConverter());
            SpriterAtlas     atlas     = new SpriterAtlas();

            atlasJson.Fill(atlas);

            XmlSerializer serializer = new XmlSerializer(typeof(SpriterAtlas));

            using (StringWriter sww = new StringWriter())
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    serializer.Serialize(writer, atlas);
                    ret.AtlasData = sww.ToString();
                }

            return(ret);
        }