public Image(AtlasAnimation animTexture) : this(animTexture.Texture) { Texture = animTexture.Texture; SourceRegion = animTexture.SourceRegion; AtlasAnimationFrame firstFrame = animTexture["all"][0]; ClippingRegion = firstFrame.ClippingRegion; Origin = firstFrame.OriginalFrame.Position; }
public bool ProcessJson(JObject json, Texture texture, ref Dictionary <string, AtlasSubTexture> subTextures) { // all frames organized as sprite/tag/frame Dictionary <string, Dictionary <string, List <JObject> > > animationsData = new Dictionary <string, Dictionary <string, List <JObject> > >(); JToken sizeToken = json.SelectToken("meta.size"); Rectangle sourceRegion = new Rectangle( sizeToken.Value <int>("w"), sizeToken.Value <int>("h") ); foreach (JProperty prop in json.SelectToken("frames").Children <JProperty>()) { string spriteName, tag; int frameId; Match m = FrameNameRegex.Match(prop.Name); if (m.Success) { spriteName = m.Groups[1].Value; tag = m.Groups[3].Length == 0 ? "none" : m.Groups[3].Value; frameId = m.Groups[2].Length == 0 ? 0 : int.Parse(m.Groups[2].Value); } else { m = FrameNameWithoutTagsRegex.Match(prop.Name); if (m.Success) { spriteName = m.Groups[1].Value; tag = "none"; frameId = int.Parse(m.Groups[2].Value); } else { continue; } } if (!animationsData.ContainsKey(spriteName)) { animationsData.Add(spriteName, new Dictionary <string, List <JObject> >()); animationsData[spriteName].Add("all", new List <JObject>()); } Dictionary <string, List <JObject> > animData = animationsData[spriteName]; if (!animData.ContainsKey(tag)) { animData.Add(tag, new List <JObject>()); } JObject frameObj = prop.Value.ToObject <JObject>(); frameObj.Add("frameId", new JValue(frameId)); animData[tag].Add(frameObj); if (tag != "all") { animData["all"].Add(frameObj); } } // create AtlasSubTextures foreach (KeyValuePair <string, Dictionary <string, List <JObject> > > animationData in animationsData) { string key = animationData.Key.ToLowerInvariant(); if (animationData.Value["all"].Count == 1) { JToken frameRegion = animationData.Value["all"][0]["frame"]; Rectangle clippingRegion = new Rectangle( frameRegion.Value <int>("x"), frameRegion.Value <int>("y"), frameRegion.Value <int>("w"), frameRegion.Value <int>("h") ); subTextures.Add( key, new AtlasSubTexture(texture, sourceRegion, clippingRegion) ); } else { AtlasAnimation animation = new AtlasAnimation(texture, sourceRegion); foreach (KeyValuePair <string, List <JObject> > track in animationData.Value) { foreach (JObject frameData in track.Value) { JToken frameRegion = frameData["frame"]; Rectangle clippingRegion = new Rectangle( frameRegion.Value <int>("x"), frameRegion.Value <int>("y"), frameRegion.Value <int>("w"), frameRegion.Value <int>("h") ); animation.AddFrame(clippingRegion, frameData["duration"].ToObject <int>(), track.Key); } } subTextures.Add(key, animation); } } return(true); }
public Image(AtlasAnimation atlasAnimation, int frameIndex) : this(atlasAnimation, AtlasAnimation.DefaultAllFramesTrackName, frameIndex) { }
public Image(AtlasAnimation atlasAnimation, string tag, int frameIndex) : this(atlasAnimation, atlasAnimation[tag][frameIndex]) { }
public Image(AtlasAnimation atlasAnimation, AtlasAnimationFrame animFrameSubTexture) : this(atlasAnimation.Texture) { SourceRegion = atlasAnimation.SourceRegion; ClippingRegion = animFrameSubTexture.ClippingRegion; Origin = animFrameSubTexture.OriginalFrame.Position; }
public bool ProcessJson(JObject json, Texture texture, ref Dictionary <string, AtlasSubTexture> subTextures) { //JToken texturesToken = json.SelectToken("textures"); //JToken metaToken = json.SelectToken("meta"); JToken animationsToken = json.SelectToken("animations"); IEnumerable <JProperty> animationProperties = animationsToken.Children <JProperty>(); foreach (JProperty animationProperty in animationProperties) { string subTextureName = animationProperty.Name.ToLowerInvariant(); JToken animationToken = animationProperty.Value; // process frames, to easily lookup at tracks processing step List <(Rectangle Source, uint Duration, Rectangle OriginalFrame)> frames = new List <(Rectangle, uint, Rectangle)>(); IJEnumerable <JToken> framesTokens = animationToken["frames"].Children(); foreach (JToken frameToken in framesTokens) { JToken sourceToken = frameToken["source"]; Rectangle source = new Rectangle( sourceToken.Value <float>("x"), sourceToken.Value <float>("y"), sourceToken.Value <float>("w"), sourceToken.Value <float>("h") ); uint duration = frameToken.Value <uint>("duration"); JToken orignalFrameToken = frameToken["original_frame"]; Rectangle originalFrame = new Rectangle( orignalFrameToken.Value <float>("x"), orignalFrameToken.Value <float>("y"), orignalFrameToken.Value <float>("w"), orignalFrameToken.Value <float>("h") ); frames.Add((source, duration, originalFrame)); } // process tracks if (frames.Count == 1) { (Rectangle Source, uint Duration, Rectangle OriginalFrame)frame = frames[0]; AtlasSubTexture subTexture = new AtlasSubTexture( texture, frame.Source, new Rectangle(Vector2.Zero, frame.Source.Size) ) { OriginalFrame = frame.OriginalFrame }; subTextures.Add(subTextureName, subTexture); continue; } AtlasAnimation animation = new AtlasAnimation(texture); // register all frames to "all" track for (uint i = 0U; i < frames.Count; i++) { (Rectangle Source, uint Duration, Rectangle OriginalFrame)frame = frames[(int)i]; animation.AddFrame(frame.Source, (int)frame.Duration, frame.OriginalFrame, "all"); } // process other tracks IJEnumerable <JToken> tracksTokens = animationToken["tracks"].Children(); foreach (JToken trackToken in tracksTokens) { string name = trackToken.Value <string>("name"); uint from = trackToken.Value <uint>("from"); uint to = trackToken.Value <uint>("to"); //string direction = trackToken.Value<string>("direction"); //! unused yet for (uint i = from; i <= to; i++) { (Rectangle Source, uint Duration, Rectangle OriginalFrame)frame = frames[(int)i]; animation.AddFrame(frame.Source, (int)frame.Duration, frame.OriginalFrame, name); } } subTextures.Add(subTextureName, animation); } return(true); }