void parseVersion1(NSDictionary animations) { CCSpriteFrameCache frameCache = CCSpriteFrameCache.sharedSpriteFrameCache; var enumerator = animations.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; string name = (string)kv.Key; NSDictionary animationDict = (NSDictionary)kv.Value; ArrayList frameNames = (ArrayList)animationDict["frames"]; float delay = (float)animationDict["delay"]; CCAnimation animation = null; if (frameNames == null) { CCDebug.Log("cocos2d: CCAnimationCache: Animation '{0}' found in dictionary without any frames - cannot add to animation cache.", name); continue; } List <CCAnimationFrame> frames = new List <CCAnimationFrame>(frameNames.Count); var framesEnumerator = frameNames.GetEnumerator(); while (framesEnumerator.MoveNext()) { string frameName = (string)framesEnumerator.Current; CCSpriteFrame spriteFrame = frameCache.spriteFrameByName(frameName); if (spriteFrame == null) { CCDebug.Log("cocos2d: CCAnimationCache: Animation '{0}' refers to frame '{1}' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", name, frameName); continue; } CCAnimationFrame animFrame = new CCAnimationFrame(spriteFrame, 1, null); frames.Add(animFrame); } if (frames.Count == 0) { CCDebug.Log("cocos2d: CCAnimationCache: None of the frames for animation '{0}' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache.", name); continue; } else if (frames.Count != frameNames.Count) { CCDebug.Log("cocos2d: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '{0}' may be missing.", name); } animation = new CCAnimation(frames, delay, 1); CCAnimationCache.sharedAnimationCache.addAnimation(animation, name); } }
public static NSDictionary DictionaryWithDictionary(NSDictionary dictionary) { NSDictionary dict = new NSDictionary(); var enumerator = dictionary.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; dict.Add(kv.Key, kv.Value); } return(dict); }
public void removeSpriteFramesFromDictionary(NSDictionary dictionary) { NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // List<string> keysToRemove = new List<string> (); var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; string frameDictKey = (string)kv.Key; _spriteFrames.Remove(frameDictKey); } }
static void WriteDictionaryValues(NSDictionary dictionary, XmlWriter writer) { writer.WriteStartElement("dict"); var enumerator = dictionary.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; object value = kv.Value; if (value != null) { writer.WriteElementString("key", kv.Key.ToString()); Compose(value, writer); } } writer.WriteEndElement(); }
void parseVersion2(NSDictionary animations) { CCSpriteFrameCache frameCache = CCSpriteFrameCache.sharedSpriteFrameCache; var enumerator = animations.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; string name = (string)kv.Key; NSDictionary animationDict = (NSDictionary)kv.Value; int loops = 0; object loopsObj = loops; if (!animationDict.TryGetValue("loops", out loopsObj)) { loops = 1; } else { loops = (int)loopsObj; } bool restoreOriginalFrame = (bool)animationDict["restoreOriginalFrame"]; NSArray frameArray = (NSArray)animationDict["frames"]; if (frameArray == null) { CCDebug.Log(@"cocos2d: CCAnimationCache: Animation '%@' found in dictionary without any frames - cannot add to animation cache.", name); continue; } // Array of AnimationFrames List <CCAnimationFrame> array = new List <CCAnimationFrame>(frameArray.Count); var frameArrayEnumerator = frameArray.GetEnumerator(); while (frameArrayEnumerator.MoveNext()) { NSDictionary entry = (NSDictionary)frameArrayEnumerator.Current; string spriteFrameName = (string)entry["spriteframe"]; CCSpriteFrame spriteFrame = frameCache.spriteFrameByName(spriteFrameName); if (spriteFrame == null) { CCDebug.Log("cocos2d: CCAnimationCache: Animation '{0}' refers to frame '{1}' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", name, spriteFrameName); continue; } float delayUnits = float.Parse(entry["delayUnits"].ToString()); NSDictionary userInfo = entry.objectForKey <NSDictionary>("notification"); CCAnimationFrame animFrame = new CCAnimationFrame(spriteFrame, delayUnits, userInfo); array.Add(animFrame); } float delayPerUnit = (float)animationDict["delayPerUnit"]; CCAnimation animation = new CCAnimation(array, delayPerUnit, (uint)loops); animation.restoreOriginalFrame = restoreOriginalFrame; CCAnimationCache.sharedAnimationCache.addAnimation(animation, name); } }
void addSpriteFrames(NSDictionary dictionary, Texture2D texture, string textureFileName) { NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // get the format int format = 0; if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // get texture size Vector2 textureSize = new Vector2(texture.width, texture.height); // check the format NSUtils.Assert(format >= 0 && format <= 3, @"cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:"); // SpriteFrame info Rect rect = new Rect(); bool textureRotated = false; Vector2 spriteOffset = Vector2.zero; Vector2 originalSize = Vector2.zero; bool semi = false; var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; if (format == 0) { float x = frameDict.objectForKey <float>("x"); float y = frameDict.objectForKey <float>("y"); float w = frameDict.objectForKey <float>("width"); float h = frameDict.objectForKey <float>("height"); float ox = frameDict.objectForKey <float>("offsetX"); float oy = frameDict.objectForKey <float>("offsetY"); int ow = frameDict.objectForKey <int>("originalWidth"); int oh = frameDict.objectForKey <int>("originalHeight"); // check ow/oh if (ow == 0 || oh == 0) { CCDebug.Warning("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // set frame info rect = new Rect(x, y, w, h); textureRotated = false; spriteOffset = new Vector2(ox, oy); originalSize = new Vector2(ow, oh); semi = frameDict.objectForKey <bool>("semi"); } else if (format == 1 || format == 2) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("frame")); bool rotated = false; // rotation if (format == 2) { rotated = frameDict.objectForKey <bool>("rotated"); } Vector2 offset = ccUtils.PointFromString(frameDict.objectForKey <string>("offset")); Vector2 sourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("sourceSize")); // set frame info rect = frame; textureRotated = rotated; spriteOffset = offset; originalSize = sourceSize; semi = frameDict.objectForKey <bool>("semi"); } else if (format == 3) { // get values Vector2 spriteSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSize")); Vector2 spriteOffset_ = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteOffset")); Vector2 spriteSourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSourceSize")); Rect textureRect = ccUtils.RectFromString(frameDict.objectForKey <string>("textureRect")); bool textureRotated_ = frameDict.objectForKey <bool>("textureRotated"); // get aliases NSArray aliases = frameDict.objectForKey <NSArray>("aliases"); var aliasesEnumerator = aliases.GetEnumerator(); while (aliasesEnumerator.MoveNext()) { string alias = (string)aliasesEnumerator.Current; if (_spriteFramesAliases.ContainsKey(alias)) { CCDebug.Warning("cocos2d: WARNING: an alias with name {0} already exists", alias); } _spriteFramesAliases[alias] = frameDictKey; } // set frame info rect = new Rect(textureRect.position.x, textureRect.position.y, spriteSize.x, spriteSize.y); textureRotated = textureRotated_; spriteOffset = spriteOffset_; originalSize = spriteSourceSize; semi = frameDict.objectForKey <bool>("semi"); } if (textureRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } rect.y = textureSize.y - rect.y - rect.height; // add sprite frame CCSpriteFrame spriteFrame = new CCSpriteFrame(texture, rect, textureRotated, spriteOffset, originalSize, semi); spriteFrame.frameFileName = frameDictKey; spriteFrame.textureFilename = textureFileName; _spriteFrames [frameDictKey] = spriteFrame; } }
static void CheckSemiTransparentSprite(NSDictionary dictionary, Texture2D texture) { NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // get the format int format = 0; if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // get texture size Vector2 textureSize = new Vector2(texture.width, texture.height); // check the format NSUtils.Assert(format >= 0 && format <= 3, @"cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:"); // SpriteFrame info Rect rect = new Rect(); bool textureRotated = false; // add real frames var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; if (format == 0) { float x = frameDict.objectForKey <float>("x"); float y = frameDict.objectForKey <float>("y"); float w = frameDict.objectForKey <float>("width"); float h = frameDict.objectForKey <float>("height"); int ow = frameDict.objectForKey <int>("originalWidth"); int oh = frameDict.objectForKey <int>("originalHeight"); // check ow/oh if (ow == 0 || oh == 0) { CCDebug.Warning("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // set frame info rect = new Rect(x, y, w, h); textureRotated = false; } else if (format == 1 || format == 2) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("frame")); bool rotated = false; // rotation if (format == 2) { rotated = frameDict.objectForKey <bool>("rotated"); } // set frame info rect = frame; textureRotated = rotated; } else if (format == 3) { // get values Vector2 spriteSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSize")); Rect textureRect = ccUtils.RectFromString(frameDict.objectForKey <string>("textureRect")); bool textureRotated_ = frameDict.objectForKey <bool>("textureRotated"); // set frame info rect = new Rect(textureRect.position.x, textureRect.position.y, spriteSize.x, spriteSize.y); textureRotated = textureRotated_; } if (textureRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } rect.y = textureSize.y - rect.y - rect.height; // add sprite frame int rectX = Mathf.RoundToInt(rect.xMin); int rectY = Mathf.RoundToInt(rect.yMin); int rectW = Mathf.RoundToInt(rect.width); int rectH = Mathf.RoundToInt(rect.height); bool isSemi = false; for (int x = 0; x < rectW; x++) { for (int y = 0; y < rectH; y++) { Color color = texture.GetPixel(rectX + x, rectY + y); if (FloatUtils.Big(color.a, 0) && FloatUtils.Small(color.a, 1)) { isSemi = true; break; } } if (isSemi) { break; } } frameDict.Add("semi", isSemi); } }
static void ParseImagePlist(int firstGid, int tileWidth, int tileHeight, string filename, Dictionary <int, string> gidToFiles, NSDictionary tilesetCaches) { string path = filename + ".txt"; NSDictionary plist = null; if (tilesetCaches != null) { plist = tilesetCaches.objectForKey <NSDictionary>(path); } if (plist == null) { plist = NSDictionary.DictionaryWithContentsOfFileFromResources(filename + ".txt"); } NSDictionary metaDataDict = plist.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = plist.objectForKey <NSDictionary>("frames"); int format = 0; if (metaDataDict != null) { format = metaDataDict.objectForKey <int> ("format"); } int width = 0; if (metaDataDict != null) { Vector2 size = ccUtils.PointFromString((string)metaDataDict ["size"]); width = Mathf.RoundToInt(size.x); } else { NSDictionary texture = metaDataDict.objectForKey <NSDictionary>("texture"); width = texture.objectForKey <int>("width"); } var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; int x = 0, y = 0, w = 0, h = 0; if (format == 0) { float ox = frameDict.objectForKey <float>("x"); float oy = frameDict.objectForKey <float>("y"); float ow = frameDict.objectForKey <float>("width"); float oh = frameDict.objectForKey <float>("height"); x = Mathf.RoundToInt(ox); y = Mathf.RoundToInt(oy); w = Mathf.RoundToInt(ow); h = Mathf.RoundToInt(oh); } else if (format == 1 || format == 2) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("frame")); x = Mathf.RoundToInt(frame.x); y = Mathf.RoundToInt(frame.y); w = Mathf.RoundToInt(frame.width); h = Mathf.RoundToInt(frame.height); } else if (format == 3) { Rect frame = ccUtils.RectFromString(frameDict.objectForKey <string>("textureRect")); x = Mathf.RoundToInt(frame.x); y = Mathf.RoundToInt(frame.y); w = Mathf.RoundToInt(frame.width); h = Mathf.RoundToInt(frame.height); } else { NSUtils.Assert(false, "cocos2d:CCTMXMap: Uknown TexturePack format."); } NSUtils.Assert(w == tileWidth && h == tileHeight, "cocos2d:CCTMXMap: Frame size of tileset file must be same with tmx tilesize."); int col = Mathf.RoundToInt(x / tileWidth); int row = Mathf.RoundToInt(y / tileHeight); int cols = Mathf.RoundToInt(width / tileWidth); int gid = firstGid + col + row * cols; gidToFiles[gid] = frameDictKey; } }