/// <summary> /// /// </summary> /// <param name="pszPlist"></param> /// <returns>The scope parameter derived from the pszPlist parameter.</returns> public string AddSpriteFramesWithFile(string pszPlist) { string pszPath = CCFileUtils.FullPathFromRelativePath(pszPlist); //Dictionary<string, Object> dict = CCFileUtils.dictionaryWithContentsOfFile(pszPath); PlistDictionary dict = CCApplication.SharedApplication.Content.Load <PlistDocument>(pszPlist).Root.AsDictionary; string texturePath = ""; PlistDictionary metadataDict = dict.ContainsKey("metadata") ? dict["metadata"].AsDictionary : null; string framePrefix = string.Empty; if (metadataDict != null) { // try to read texture file name from meta data if (metadataDict.ContainsKey("textureFileName")) { texturePath = metadataDict["textureFileName"].AsString; framePrefix = ExtractPrefix(texturePath); } } if (!string.IsNullOrEmpty(texturePath)) { // build texture path relative to plist file texturePath = CCFileUtils.FullPathFromRelativeFile(texturePath, pszPath); } else { // build texture path by replacing file extension texturePath = pszPath; // remove .xxx texturePath = CCFileUtils.RemoveExtension(texturePath); CCLog.Log("cocos2d: CCSpriteFrameCache: Trying to use file {0} as texture", texturePath); } CCTexture2D pTexture = CCTextureCache.SharedTextureCache.AddImage(texturePath); if (pTexture != null) { AddSpriteFramesWithDictionary(dict, pTexture, framePrefix); } else { CCLog.Log("cocos2d: CCSpriteFrameCache: Couldn't load texture"); } return(framePrefix); }
public void AddSpriteFramesWithFile(string pszPlist) { PlistDocument document = CCContentManager.SharedContentManager.Load <PlistDocument>(pszPlist); PlistDictionary dict = document.Root.AsDictionary; string texturePath = ""; PlistDictionary metadataDict = dict.ContainsKey("metadata") ? dict["metadata"].AsDictionary : null; if (metadataDict != null) { // try to read texture file name from meta data if (metadataDict.ContainsKey("textureFileName")) { texturePath = metadataDict["textureFileName"].AsString; } } if (!string.IsNullOrEmpty(texturePath)) { // build texture path relative to plist file texturePath = CCFileUtils.FullPathFromRelativeFile(texturePath, pszPlist); } else { // build texture path by replacing file extension texturePath = pszPlist; // remove .xxx texturePath = CCFileUtils.RemoveExtension(texturePath); // append .png texturePath = texturePath + ".png"; CCLog.Log("cocos2d: CCSpriteFrameCache: Trying to use file {0} as texture", texturePath); } CCTexture2D pTexture = CCTextureCache.SharedTextureCache.AddImage(texturePath); if (pTexture != null) { AddSpriteFramesWithDictionary(dict, pTexture); } else { CCLog.Log("cocos2d: CCSpriteFrameCache: Couldn't load texture"); } }
private PlistType GetPlistType(PlistDictionary dict) { var isSpriteKit = dict.ContainsKey("format") ? dict ["format"].AsString == "APPL" : false; return(isSpriteKit ? PlistType.SpriteKit : PlistType.Cocos2D); }
private void LoadCocos2DDictionary(PlistDictionary dict, CCTexture2D texture) { PlistDictionary metadataDict = null; if (dict.ContainsKey("metadata")) { metadataDict = dict["metadata"].AsDictionary; } PlistDictionary framesDict = null; if (dict.ContainsKey("frames")) { framesDict = dict["frames"].AsDictionary; } // get the format int format = 0; if (metadataDict != null) { format = metadataDict["format"].AsInt; } // check the format if (format < 0 || format > 3) { throw (new NotSupportedException("PList format " + format + " is not supported.")); } foreach (var pair in framesDict) { PlistDictionary frameDict = pair.Value.AsDictionary; CCSpriteFrame spriteFrame = null; if (format == 0) { float x = 0f, y = 0f, w = 0f, h = 0f; x = frameDict["x"].AsFloat; y = frameDict["y"].AsFloat; w = frameDict["width"].AsFloat; h = frameDict["height"].AsFloat; float ox = 0f, oy = 0f; ox = frameDict["offsetX"].AsFloat; oy = frameDict["offsetY"].AsFloat; int ow = 0, oh = 0; ow = frameDict["originalWidth"].AsInt; oh = frameDict["originalHeight"].AsInt; // check ow/oh if (ow == 0 || oh == 0) { CCLog.Log( "cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist or check the 'format' metatag"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // create frame spriteFrame = new CCSpriteFrame( texture, new CCRect(x, y, w, h), false, new CCPoint(ox, oy), new CCSize(ow, oh) ); } else if (format == 1 || format == 2) { var frame = CCRect.Parse(frameDict["frame"].AsString); bool rotated = false; // rotation if (format == 2) { if (frameDict.ContainsKey("rotated")) { rotated = frameDict["rotated"].AsBool; } } var offset = CCPoint.Parse(frameDict["offset"].AsString); var sourceSize = CCSize.Parse(frameDict["sourceSize"].AsString); // create frame spriteFrame = new CCSpriteFrame(texture, frame, rotated, offset, sourceSize); } else if (format == 3) { var spriteSize = CCSize.Parse(frameDict["spriteSize"].AsString); var spriteOffset = CCPoint.Parse(frameDict["spriteOffset"].AsString); var spriteSourceSize = CCSize.Parse(frameDict["spriteSourceSize"].AsString); var textureRect = CCRect.Parse(frameDict["textureRect"].AsString); bool textureRotated = false; if (frameDict.ContainsKey("textureRotated")) { textureRotated = frameDict["textureRotated"].AsBool; } // get aliases var aliases = frameDict["aliases"].AsArray; for (int i = 0; i < aliases.Count; i++) { string oneAlias = aliases[i].AsString; if (_spriteFramesAliases.ContainsKey(oneAlias)) { if (_spriteFramesAliases[oneAlias] != null) { CCLog.Log("cocos2d: WARNING: an alias with name {0} already exists", oneAlias); } } if (!_spriteFramesAliases.ContainsKey(oneAlias)) { _spriteFramesAliases.Add(oneAlias, pair.Key); } } // create frame spriteFrame = new CCSpriteFrame( texture, new CCRect(textureRect.Origin.X, textureRect.Origin.Y, spriteSize.Width, spriteSize.Height), textureRotated, spriteOffset, spriteSourceSize ); } _spriteFrames[pair.Key] = spriteFrame; } AutoCreateAliasList(); }
private void LoadAppleDictionary(PlistDictionary dict, CCTexture2D texture) { var version = dict.ContainsKey("version") ? dict ["version"].AsInt : 0; if (version != 1) { throw (new NotSupportedException("Binary PList version " + version + " is not supported.")); } var images = dict.ContainsKey("images") ? dict ["images"].AsArray : null; foreach (var imageEntry in images) { // we only support one image for now var imageDict = imageEntry.AsDictionary; var path = imageDict ["path"].AsString; path = Path.Combine(plistFilePath, CCFileUtils.RemoveExtension(path)); if (!CCTextureCache.SharedTextureCache.Contains(path)) { texture = CCTextureCache.SharedTextureCache.AddImage(path); } else { texture = CCTextureCache.SharedTextureCache[path]; } // size not used right now //var size = CCSize.Parse(imageDict ["size"].AsString); var subImages = imageDict ["subimages"].AsArray; foreach (var subImage in subImages) { CCSpriteFrame spriteFrame = null; var subImageDict = subImage.AsDictionary; var name = subImageDict ["name"].AsString; var alias = subImageDict ["alias"].AsString; var isFullyOpaque = true; if (subImageDict.ContainsKey("isFullyOpaque")) { isFullyOpaque = subImageDict ["isFullyOpaque"].AsBool; } var textureRect = CCRect.Parse(subImageDict ["textureRect"].AsString); var spriteOffset = CCPoint.Parse(subImageDict ["spriteOffset"].AsString); // We are going to override the sprite offset for now to be 0,0 // It seems the offset is calculated off of the original size but if // we pass this offset it throws our center position calculations off. spriteOffset = CCPoint.Zero; var textureRotated = false; if (subImageDict.ContainsKey("textureRotated")) { textureRotated = subImageDict ["textureRotated"].AsBool; } var spriteSourceSize = CCSize.Parse(subImageDict ["spriteSourceSize"].AsString); var frameRect = textureRect; if (textureRotated) { frameRect = new CCRect(textureRect.Origin.X, textureRect.Origin.Y, textureRect.Size.Height, textureRect.Size.Width); } #if DEBUG CCLog.Log("texture {0} rect {1} rotated {2} offset {3}, sourcesize {4}", name, textureRect, textureRotated, spriteOffset, spriteSourceSize); #endif // create frame spriteFrame = new CCSpriteFrame( texture, frameRect, textureRotated, spriteOffset, spriteSourceSize ); _spriteFrames [name] = spriteFrame; } } AutoCreateAliasList(); }
public void AddSpriteFramesWithDictionary(PlistDictionary pobDictionary, CCTexture2D pobTexture, string framePrefix) { /* * Supported Zwoptex Formats: * * ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version * ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b * ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1 * ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+ */ PlistDictionary metadataDict = null; if (pobDictionary.ContainsKey("metadata")) { metadataDict = pobDictionary["metadata"].AsDictionary; } PlistDictionary framesDict = null; if (pobDictionary.ContainsKey("frames")) { framesDict = pobDictionary["frames"].AsDictionary; } int format = 0; // get the format if (metadataDict != null) { format = metadataDict["format"].AsInt; } // check the format if (format < 0 || format > 3) { throw (new NotSupportedException("PList format " + format + " is not supported.")); } foreach (var pair in framesDict) { PlistDictionary frameDict = pair.Value.AsDictionary; CCSpriteFrame spriteFrame = null; if (format == 0) { float x = 0f, y = 0f, w = 0f, h = 0f; x = frameDict["x"].AsFloat; y = frameDict["y"].AsFloat; w = frameDict["width"].AsFloat; h = frameDict["height"].AsFloat; float ox = 0f, oy = 0f; ox = frameDict["offsetX"].AsFloat; oy = frameDict["offsetY"].AsFloat; int ow = 0, oh = 0; ow = frameDict["originalWidth"].AsInt; oh = frameDict["originalHeight"].AsInt; // check ow/oh if (ow == 0 || oh == 0) { CCLog.Log( "cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist or check the 'format' metatag"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // create frame spriteFrame = new CCSpriteFrame(pobTexture, new CCRect(x, y, w, h), false, new CCPoint(ox, oy), new CCSize(ow, oh) ); } else if (format == 1 || format == 2) { CCRect frame = CCRect.Parse(frameDict["frame"].AsString); bool rotated = false; // rotation if (format == 2) { if (frameDict.ContainsKey("rotated")) { rotated = frameDict["rotated"].AsBool; } } CCPoint offset = CCPoint.Parse(frameDict["offset"].AsString); CCSize sourceSize = CCSize.Parse(frameDict["sourceSize"].AsString); // create frame spriteFrame = new CCSpriteFrame(pobTexture, frame, rotated, offset, sourceSize ); } else if (format == 3) { // get values CCSize spriteSize = CCSize.Parse(frameDict["spriteSize"].AsString); CCPoint spriteOffset = CCPoint.Parse(frameDict["spriteOffset"].AsString); CCSize spriteSourceSize = CCSize.Parse(frameDict["spriteSourceSize"].AsString); CCRect textureRect = CCRect.Parse(frameDict["textureRect"].AsString); bool textureRotated = false; if (frameDict.ContainsKey("textureRotated")) { textureRotated = frameDict["textureRotated"].AsBool; } // get aliases PlistArray aliases = frameDict["aliases"].AsArray; string frameKey = pair.Key; foreach (PlistObjectBase item2 in aliases) { string oneAlias = item2.AsString; if (m_pSpriteFramesAliases.Keys.Contains(oneAlias)) { if (m_pSpriteFramesAliases[oneAlias] != null) { CCLog.Log("cocos2d: WARNING: an alias with name {0} already exists", oneAlias); } } if (!m_pSpriteFramesAliases.Keys.Contains(oneAlias)) { m_pSpriteFramesAliases.Add(oneAlias, frameKey); } } // create frame spriteFrame = new CCSpriteFrame(pobTexture, new CCRect(textureRect.Origin.X, textureRect.Origin.Y, spriteSize.Width, spriteSize.Height), textureRotated, spriteOffset, spriteSourceSize); } // add sprite frame string key = framePrefix + pair.Key; if (!_AllowFrameOverwrite && m_pSpriteFrames.ContainsKey(key)) { CCLog.Log("Frame named " + key + " already exists in the animation cache. Not overwriting existing record."); } else if (_AllowFrameOverwrite || !m_pSpriteFrames.ContainsKey(key)) { m_pSpriteFrames[key] = spriteFrame; } } }
/// <summary> /// /// </summary> /// <param name="pszPlist"></param> /// <returns>The scope parameter derived from the pszPlist parameter.</returns> public string AddSpriteFramesWithFile(string pszPlist) { string path = CCFileUtils.FullPathFromRelativePath(pszPlist); //Dictionary<string, Object> dict = CCFileUtils.dictionaryWithContentsOfFile(pszPath); PlistDocument document = null; try { document = CCApplication.SharedApplication.Content.Load <PlistDocument>(path); } catch (System.Exception) { string xml = CCContent.LoadContentFile(path); if (xml != null) { document = new PlistDocument(xml); } } if (document == null) { throw (new Microsoft.Xna.Framework.Content.ContentLoadException("Failed to load the particle definition file from " + path)); } PlistDictionary dict = document.Root.AsDictionary; string texturePath = ""; PlistDictionary metadataDict = dict.ContainsKey("metadata") ? dict["metadata"].AsDictionary : null; string framePrefix = string.Empty; if (metadataDict != null) { // try to read texture file name from meta data if (metadataDict.ContainsKey("textureFileName")) { texturePath = metadataDict["textureFileName"].AsString; framePrefix = ExtractPrefix(texturePath); } } if (!string.IsNullOrEmpty(texturePath)) { // build texture path relative to plist file texturePath = CCFileUtils.FullPathFromRelativeFile(texturePath, path); } else { // build texture path by replacing file extension texturePath = path; // remove .xxx texturePath = CCFileUtils.RemoveExtension(texturePath); CCLog.Log("cocos2d: CCSpriteFrameCache: Trying to use file {0} as texture", texturePath); } CCTexture2D pTexture = CCTextureCache.SharedTextureCache.AddImage(texturePath); if (pTexture != null) { AddSpriteFramesWithDictionary(dict, pTexture, framePrefix); } else { CCLog.Log("cocos2d: CCSpriteFrameCache: Couldn't load texture"); } return(framePrefix); }
private void LoadAppleDictionary(PlistDictionary dict, CCTexture2D texture) { var version = dict.ContainsKey("version") ? dict ["version"].AsInt : 0; if (version != 1) { throw (new NotSupportedException("Binary PList version " + version + " is not supported.")); } var images = dict.ContainsKey("images") ? dict ["images"].AsArray : null; // we only support one image for now var imageDict = images [0].AsDictionary; var path = imageDict ["path"].AsString; // size not used right now //var size = CCSize.Parse(imageDict ["size"].AsString); var subImages = imageDict ["subimages"].AsArray; foreach (var subImage in subImages) { CCSpriteFrame spriteFrame = null; var subImageDict = subImage.AsDictionary; var name = subImageDict ["name"].AsString; var alias = subImageDict ["alias"].AsString; var isFullyOpaque = true; if (subImageDict.ContainsKey("isFullyOpaque")) { isFullyOpaque = subImageDict ["isFullyOpaque"].AsBool; } var textureRect = CCRect.Parse(subImageDict ["textureRect"].AsString); var spriteOffset = CCPoint.Parse(subImageDict ["spriteOffset"].AsString); var textureRotated = false; if (subImageDict.ContainsKey("textureRotated")) { textureRotated = subImageDict ["textureRotated"].AsBool; } var spriteSourceSize = CCSize.Parse(subImageDict ["spriteSourceSize"].AsString); var frameRect = textureRect; if (textureRotated) { frameRect = new CCRect(textureRect.Origin.X, textureRect.Origin.Y, textureRect.Size.Height, textureRect.Size.Width); } #if DEBUG CCLog.Log("texture {0} rect {1} rotated {2} offset {3}, sourcesize {4}", name, textureRect, textureRotated, spriteOffset, spriteSourceSize); #endif // create frame spriteFrame = new CCSpriteFrame( texture, frameRect, textureRotated, spriteOffset, spriteSourceSize ); _spriteFrames[name] = spriteFrame; } }