//page file private void parseImageFileName(string line, string fntFile) { // ////////////////////////////////////////////////////////////////////////// //// line to parse: //// page id=0 file="bitmapFontTest.png" //////////////////////////////////////////////////////////////////////////// // page ID. Sanity check int index = line.IndexOf('=') + 1; int index2 = line.IndexOf(' ', index); string value = line.Substring(index, index2 - index); try { int ivalue = int.Parse(value); } catch (Exception) { throw (new ContentLoadException("Invalid page ID for FNT descriptor. Line=" + line + ", value=" + value + ", indices=" + index + "," + index2)); } // Debug.Assert(Convert.ToInt32(value) == 0, "LabelBMFont file could not be found"); // file index = line.IndexOf('"') + 1; index2 = line.IndexOf('"', index); value = line.Substring(index, index2 - index); AtlasName = value; var directory = string.Empty; if (!CCFileUtils.GetDirectoryName(value, out directory)) { AtlasName = CCFileUtils.FullPathFromRelativeFile(value, fntFile); } }
public CCTexture2D AddImage(string fileimage) { Debug.Assert(!String.IsNullOrEmpty(fileimage), "TextureCache: fileimage MUST not be NULL"); CCTexture2D texture = null; var assetName = fileimage; if (Path.HasExtension(assetName)) { assetName = CCFileUtils.RemoveExtension(assetName); } lock (dictLock) { textures.TryGetValue(assetName, out texture); } if (texture == null) { texture = new CCTexture2D(fileimage); lock (dictLock) { textures[assetName] = texture; } } return(texture); }
public CCNode ReadNodeGraphFromFile(string fileName, object owner, CCSize parentSize) { if (string.IsNullOrEmpty(fileName)) { return(null); } string strCCBFileName = fileName; string strSuffix = ".ccbi"; // Add ccbi suffix if (!CCBReader.EndsWith(strCCBFileName, strSuffix)) { strCCBFileName += strSuffix; } string strPath = CCFileUtils.FullPathFromRelativePath(strCCBFileName); var pBytes = CCFileUtils.GetFileBytes(strPath); byte[] data = pBytes; CCNode ret = ReadNodeGraphFromData(data, owner, parentSize); return(ret); }
void ParseTilesetElement() { string externalTilesetFilename = null; // Tileset source if (currentAttributeDict.TryGetValue(TilesetElementFileSource, out externalTilesetFilename)) { externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename, TileMapFileName); currentFirstGID = short.Parse(currentAttributeDict[TilesetElementFirstGid]); ParseXmlFile(externalTilesetFilename); } else { var tileset = new CCTileSetInfo(); tileset.Name = currentAttributeDict[TilesetElementName]; // First GID if (currentFirstGID == 0) { tileset.FirstGid = short.Parse(currentAttributeDict[TilesetElementFirstGid]); } else { tileset.FirstGid = currentFirstGID; currentFirstGID = 0; } string tileSpacingStr = null; string borderSizeStr = null; // Tilesheet tile spacing if (currentAttributeDict.TryGetValue(TilesetElementTileSpacing, out tileSpacingStr)) { tileset.TileSpacing = int.Parse(tileSpacingStr); } // Tilesheet border width if (currentAttributeDict.TryGetValue(TilesetElementBorderSize, out borderSizeStr)) { tileset.BorderWidth = int.Parse(borderSizeStr); } // Tile texel size CCSize tileTexelSize; tileTexelSize.Width = CCUtils.CCParseFloat(currentAttributeDict[TilesetElementTexelWidth]); tileTexelSize.Height = CCUtils.CCParseFloat(currentAttributeDict[TilesetElementTexelHeight]); tileset.TileTexelSize = tileTexelSize; Tilesets.Add(tileset); } }
public void RemoveTextureForKey(string textureKeyName) { if (String.IsNullOrEmpty(textureKeyName)) { return; } if (Path.HasExtension(textureKeyName)) { textureKeyName = CCFileUtils.RemoveExtension(textureKeyName); } textures.Remove(textureKeyName); }
public void RemoveForKey(string particleSystemKeyName) { if (String.IsNullOrEmpty(particleSystemKeyName)) { return; } if (Path.HasExtension(particleSystemKeyName)) { particleSystemKeyName = CCFileUtils.RemoveExtension(particleSystemKeyName); } configs.Remove(particleSystemKeyName); }
public void AddSpriteFrames(string plistFileName) { PlistDocument document = CCContentManager.SharedContentManager.Load <PlistDocument>(plistFileName); 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, plistFileName); } else { // build texture path by replacing file extension texturePath = plistFileName; // remove .xxx texturePath = CCFileUtils.RemoveExtension(texturePath); // append .png texturePath = texturePath + ".png"; CCLog.Log("CocosSharp: CCSpriteFrameCache: Trying to use file {0} as texture", texturePath); } CCTexture2D pTexture = CCTextureCache.SharedTextureCache.AddImage(texturePath); if (pTexture != null) { AddSpriteFrames(dict, pTexture); } else { CCLog.Log("CocosSharp: CCSpriteFrameCache: Couldn't load texture"); } }
public CCParticleSystemConfig ParticleSystemForKey(string key) { CCParticleSystemConfig config = null; try { if (Path.HasExtension(key)) { key = CCFileUtils.RemoveExtension(key); } configs.TryGetValue(key, out config); } catch (ArgumentNullException) { CCLog.Log("Particle System with key {0} does not exist.", key); } return(config); }
void ParseImageElement() { List <CCTileSetInfo> tilesets = Tilesets; int tilesetCount = tilesets != null ? tilesets.Count : 0; CCTileSetInfo tileset = tilesetCount > 0 ? tilesets[tilesetCount - 1] : null; string imagename = currentAttributeDict[ImageElementTilesheetName]; tileset.TilesheetFilename = imagename; var directory = string.Empty; if (string.IsNullOrEmpty(TileMapFileName)) { tileset.TilesheetFilename = imagename; } else { tileset.TilesheetFilename = CCFileUtils.FullPathFromRelativeFile(imagename, TileMapFileName); } }
public CCTexture2D this[string key] { get { CCTexture2D texture = null; try { if (Path.HasExtension(key)) { key = CCFileUtils.RemoveExtension(key); } textures.TryGetValue(key, out texture); } catch (ArgumentNullException) { CCLog.Log("Texture of key {0} is not exist.", key); } return(texture); } }
CCParticleSystemConfig AddParticleSystem(string fileConfig, string directoryName, bool loadAsync) { Debug.Assert(!String.IsNullOrEmpty(fileConfig), "ParticleSystemConfigCache: fileConfig MUST not be NULL"); CCParticleSystemConfig psConfig = null; var assetName = fileConfig; if (Path.HasExtension(assetName)) { assetName = CCFileUtils.RemoveExtension(assetName); } lock (dictLock) { configs.TryGetValue(assetName, out psConfig); } if (psConfig == null) { try { psConfig = new CCParticleSystemConfig(fileConfig, directoryName, loadAsync); lock (dictLock) { configs[assetName] = psConfig; } } catch (Exception exc) { CCLog.Log("Error creating configuration: {0}", fileConfig); return(null); } } return(psConfig); }
/// <summary> /// Initializes a new instance of the <see cref="FileFontLoader"/> class. /// </summary> /// <param name="factory">The factory.</param> /// <param name="fontFilePath">The relative path and file name.</param> public FileFontLoader(Factory factory, string fontFilePath) { this.factory = factory; var fontBytes = Utilities.ReadStream(CCFileUtils.GetFileStream(fontFilePath)); var stream = new DataStream(fontBytes.Length, true, true); stream.Write(fontBytes, 0, fontBytes.Length); stream.Position = 0; fontStreams.Add(new FileFontFileStream(stream)); // Build a Key storage that stores the index of the font keyStream = new DataStream(sizeof(int) * fontStreams.Count, true, true); for (int i = 0; i < fontStreams.Count; i++) { keyStream.Write((int)i); } keyStream.Position = 0; // Register the factory.RegisterFontFileLoader(this); factory.RegisterFontCollectionLoader(this); }
CCParticleSystemConfig AddParticleSystem(string fileConfig, string directoryName, bool loadAsync) { Debug.Assert(!String.IsNullOrEmpty(fileConfig), "ParticleSystemConfigCache: fileConfig MUST not be NULL"); CCParticleSystemConfig psConfig = null; var assetName = fileConfig; if (Path.HasExtension(assetName)) { assetName = CCFileUtils.RemoveExtension(assetName); } lock (dictLock) { configs.TryGetValue(assetName, out psConfig); } if (psConfig == null) { psConfig = new CCParticleSystemConfig(fileConfig, directoryName, loadAsync); if (psConfig != null) { lock (dictLock) { configs[assetName] = psConfig; } } else { return(null); } } return(psConfig); }
public CCSpriteSheet(string fileName) { PlistDocument document = CCContentManager.SharedContentManager.Load <PlistDocument>(fileName); var dict = document.Root.AsDictionary; var texturePath = string.Empty; plistFilePath = string.Empty; plistType = GetPlistType(dict); if (plistType == PlistType.SpriteKit) { var images = dict.ContainsKey("images") ? dict ["images"].AsArray : null; var imageDict = images [0].AsDictionary; if (imageDict != null) { // try to read texture file name from meta data if (imageDict.ContainsKey("path")) { texturePath = imageDict ["path"].AsString; } } } else { var 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, fileName); } else { // build texture path by replacing file extension texturePath = fileName; // remove .xxx texturePath = CCFileUtils.RemoveExtension(texturePath); CCLog.Log("CocosSharp: CCSpriteFrameCache: Trying to use file {0} as texture", texturePath); } plistFilePath = Path.GetDirectoryName(texturePath); CCTexture2D pTexture = CCTextureCache.SharedTextureCache.AddImage(texturePath); if (pTexture != null) { InitWithDictionary(dict, pTexture); } else { CCLog.Log("CCSpriteSheet: Couldn't load texture"); } }
public CCTMXMapInfo(string tmxFile) : this() { TMXFileName = CCFileUtils.FullPathFromRelativePath(tmxFile); ParseXmlFile(TMXFileName); }
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 { // Fix for SpriteKit texture atlases // Backgrounds.1.png // Example is that after stripping the extension when passing the value in // the ".1" is considered an extension so it is stripped as well resulting // in the texture not being found. So we try with the texture key first and // only if it is not found then we check for extension and continue with normal // processing. if (Path.HasExtension(path)) { var extension = Path.GetExtension(path); var sequence = 0; if (Int32.TryParse(extension.Substring(1), out sequence)) { path = imageDict ["path"].AsString; path = Path.Combine(plistFilePath, path); } } 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 frameRect.Origin += spriteOffset; // create frame spriteFrame = new CCSpriteFrame( spriteSourceSize, texture, frameRect, textureRotated ); spriteFrame.TextureFilename = name; spriteFrames [name] = spriteFrame; } } AutoCreateAliasList(); }
public CCLabelAtlas(string label, string fntFile) : this(label, new PlistDocument(CCFileUtils.GetFileData(fntFile)).Root as PlistDictionary) { }
public CCTileMapAtlas(string tile, string mapFile, int tileWidth, int tileHeight) : this(tile, mapFile, tileWidth, tileHeight, new CCImageTGA(CCFileUtils.FullPathFromRelativePath(mapFile))) { }
public CCTileMapInfo(string tileMapFile) : this() { TileMapFileName = CCFileUtils.FullPathFromRelativePath(tileMapFile); ParseXmlFile(TileMapFileName); }
private CCBKeyframe ReadKeyframe(CCBPropertyType type) { var keyframe = new CCBKeyframe(); keyframe.Time = ReadFloat(); var easingType = (CCBEasingType)ReadInt(false); float easingOpt = 0; object value = null; if (easingType == CCBEasingType.CubicIn || easingType == CCBEasingType.CubicOut || easingType == CCBEasingType.CubicInOut || easingType == CCBEasingType.ElasticIn || easingType == CCBEasingType.ElasticOut || easingType == CCBEasingType.ElasticInOut) { easingOpt = ReadFloat(); } keyframe.EasingType = easingType; keyframe.EasingOpt = easingOpt; if (type == CCBPropertyType.Check) { value = new CCBValue(ReadBool()); } else if (type == CCBPropertyType.Byte) { value = new CCBValue(ReadByte()); } else if (type == CCBPropertyType.Color3) { byte r = ReadByte(); byte g = ReadByte(); byte b = ReadByte(); var c = new CCColor3B(r, g, b); value = new CCColor3BWapper(c); } else if (type == CCBPropertyType.Degrees) { value = new CCBValue(ReadFloat()); } else if (type == CCBPropertyType.ScaleLock || type == CCBPropertyType.Position || type == CCBPropertyType.FloatXY) { float a = ReadFloat(); float b = ReadFloat(); value = new List <CCBValue> { new CCBValue(a), new CCBValue(b) }; } else if (type == CCBPropertyType.SpriteFrame) { string spriteSheet = ReadCachedString(); string spriteFile = ReadCachedString(); CCSpriteFrame spriteFrame; if (String.IsNullOrEmpty(spriteSheet)) { spriteFile = _CCBRootPath + spriteFile; CCTexture2D texture = CCTextureCache.SharedTextureCache.AddImage(CCFileUtils.RemoveExtension(spriteFile)); var bounds = new CCRect(0, 0, texture.ContentSizeInPixels.Width, texture.ContentSizeInPixels.Height); spriteFrame = new CCSpriteFrame(texture, bounds); } else { spriteSheet = _CCBRootPath + spriteSheet; CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache; // Load the sprite sheet only if it is not loaded if (!_loadedSpriteSheets.Contains(spriteSheet)) { frameCache.AddSpriteFrames(spriteSheet); _loadedSpriteSheets.Add(spriteSheet); } spriteFrame = frameCache[spriteFile]; } value = spriteFrame; } keyframe.Value = value; return(keyframe); }
public void StartElement(object ctx, string name, string[] atts) { CCTMXMapInfo pTMXMapInfo = this; string elementName = name; var attributeDict = new Dictionary <string, string>(); if (atts != null && atts[0] != null) { for (int i = 0; i + 1 < atts.Length; i += 2) { string key = atts[i]; string value = atts[i + 1]; attributeDict.Add(key, value); } } if (elementName == "map") { string version = attributeDict["version"]; if (version != "1.0") { CCLog.Log("CocosSharp: TMXFormat: Unsupported TMX version: {0}", version); } string orientationStr = attributeDict["orientation"]; if (orientationStr == "orthogonal") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Ortho); } else if (orientationStr == "isometric") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Iso); } else if (orientationStr == "hexagonal") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Hex); } else { CCLog.Log("CocosSharp: TMXFomat: Unsupported orientation: {0}", pTMXMapInfo.Orientation); } CCSize sMapSize; sMapSize.Width = CCUtils.CCParseFloat(attributeDict["width"]); sMapSize.Height = CCUtils.CCParseFloat(attributeDict["height"]); pTMXMapInfo.MapSize = sMapSize; CCSize sTileSize; sTileSize.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); sTileSize.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); pTMXMapInfo.TileSize = sTileSize; // The parent element is now "map" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Map; } else if (elementName == "tileset") { // If this is an external tileset then start parsing that if (attributeDict.ContainsKey("source")) { string externalTilesetFilename = attributeDict["source"]; externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename, pTMXMapInfo.TMXFileName); currentFirstGID = uint.Parse(attributeDict["firstgid"]); pTMXMapInfo.ParseXmlFile(externalTilesetFilename); } else { var tileset = new CCTMXTilesetInfo(); tileset.Name = attributeDict["name"]; if (currentFirstGID == 0) { tileset.FirstGid = uint.Parse(attributeDict["firstgid"]); } else { tileset.FirstGid = currentFirstGID; currentFirstGID = 0; } if (attributeDict.ContainsKey("spacing")) { tileset.Spacing = int.Parse(attributeDict["spacing"]); } if (attributeDict.ContainsKey("margin")) { tileset.Margin = int.Parse(attributeDict["margin"]); } CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); s.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); tileset.TileSize = s; pTMXMapInfo.Tilesets.Add(tileset); } } else if (elementName == "tile") { List <CCTMXTilesetInfo> tilesets = pTMXMapInfo.Tilesets; int tilesetCount = tilesets != null ? tilesets.Count : 0; CCTMXTilesetInfo info = tilesetCount > 0 ? tilesets[tilesetCount - 1] : null; var dict = new Dictionary <string, string>(); pTMXMapInfo.ParentGID = (info.FirstGid + uint.Parse(attributeDict["id"])); pTMXMapInfo.TileProperties.Add(pTMXMapInfo.ParentGID, dict); pTMXMapInfo.ParentElement = (int)CCTMXProperty.Tile; } else if (elementName == "layer") { var layer = new CCTMXLayerInfo(); layer.Name = attributeDict["name"]; CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["width"]); s.Height = CCUtils.CCParseFloat(attributeDict["height"]); layer.LayerSize = s; layer.Tiles = new uint[(int)s.Width * (int)s.Height]; if (attributeDict.ContainsKey("visible")) { string visible = attributeDict["visible"]; layer.Visible = !(visible == "0"); } else { layer.Visible = true; } if (attributeDict.ContainsKey("opacity")) { string opacity = attributeDict["opacity"]; layer.Opacity = (byte)(255 * CCUtils.CCParseFloat(opacity)); } else { layer.Opacity = 255; } float x = attributeDict.ContainsKey("x") ? CCUtils.CCParseFloat(attributeDict["x"]) : 0; float y = attributeDict.ContainsKey("y") ? CCUtils.CCParseFloat(attributeDict["y"]) : 0; layer.Offset = new CCPoint(x, y); pTMXMapInfo.Layers.Add(layer); // The parent element is now "layer" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Layer; } else if (elementName == "objectgroup") { var objectGroup = new CCTMXObjectGroup(); objectGroup.GroupName = attributeDict["name"]; CCPoint positionOffset = CCPoint.Zero; if (attributeDict.ContainsKey("x")) { positionOffset.X = CCUtils.CCParseFloat(attributeDict["x"]) * pTMXMapInfo.TileSize.Width; } if (attributeDict.ContainsKey("y")) { positionOffset.Y = CCUtils.CCParseFloat(attributeDict["y"]) * pTMXMapInfo.TileSize.Height; } objectGroup.PositionOffset = positionOffset; pTMXMapInfo.ObjectGroups.Add(objectGroup); // The parent element is now "objectgroup" pTMXMapInfo.ParentElement = (int)CCTMXProperty.ObjectGroup; } else if (elementName == "image") { List <CCTMXTilesetInfo> tilesets = pTMXMapInfo.Tilesets; int tilesetCount = tilesets != null ? tilesets.Count : 0; CCTMXTilesetInfo tileset = tilesetCount > 0 ? tilesets[tilesetCount - 1] : null; // build full path string imagename = attributeDict["source"]; tileset.SourceImage = CCFileUtils.FullPathFromRelativeFile(imagename, pTMXMapInfo.TMXFileName); } else if (elementName == "data") { string encoding = attributeDict.ContainsKey("encoding") ? attributeDict["encoding"] : ""; string compression = attributeDict.ContainsKey("compression") ? attributeDict["compression"] : ""; if (encoding == "base64") { int layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Base64; pTMXMapInfo.StoringCharacters = true; if (compression == "gzip") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Gzip; } else if (compression == "zlib") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Zlib; } Debug.Assert(compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method"); } Debug.Assert(pTMXMapInfo.LayerAttribs != (int)CCTMXLayerAttrib.None, "TMX tile map: Only base64 and/or gzip/zlib maps are supported"); } else if (elementName == "object") { List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups; int objectGroupCount = objectGroups != null ? objectGroups.Count : 0; CCTMXObjectGroup objectGroup = objectGroupCount > 0 ? objectGroups[objectGroupCount - 1] : null; // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary <string, string>(); var pArray = new[] { "name", "type", "width", "height", "gid" }; for (int i = 0; i < pArray.Length; i++) { string key = pArray[i]; if (attributeDict.ContainsKey(key)) { dict.Add(key, attributeDict[key]); } } // But X and Y since they need special treatment // X int x = int.Parse(attributeDict["x"]) + (int)objectGroup.PositionOffset.X; dict.Add("x", x.ToString()); int y = int.Parse(attributeDict["y"]) + (int)objectGroup.PositionOffset.Y; // Correct y position. (Tiled uses Flipped, cocos2d uses Standard) y = (int)(pTMXMapInfo.MapSize.Height * pTMXMapInfo.TileSize.Height) - y - (attributeDict.ContainsKey("height") ? int.Parse(attributeDict["height"]) : 0); dict.Add("y", y.ToString()); // Add the object to the objectGroup objectGroup.Objects.Add(dict); // The parent element is now "object" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Object; } else if (elementName == "property") { if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.None) { CCLog.Log("TMX tile map: Parent element is unsupported. Cannot add property named '{0}' with value '{1}'", attributeDict["name"], attributeDict["value"]); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Map) { // The parent element is the map string value = attributeDict["value"]; string key = attributeDict["name"]; pTMXMapInfo.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Layer) { // The parent element is the last layer List <CCTMXLayerInfo> layers = pTMXMapInfo.Layers; int layersCount = layers != null ? layers.Count : 0; CCTMXLayerInfo layer = layersCount > 0 ? layers[layersCount - 1] : null; string value = attributeDict["value"]; string key = attributeDict["name"]; // Add the property to the layer layer.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.ObjectGroup) { // The parent element is the last object group List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups; int objGroupsCount = objectGroups != null ? objectGroups.Count : 0; CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? objectGroups[objGroupsCount - 1] : null; string value = attributeDict["value"]; string key = attributeDict["name"]; objectGroup.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Object) { // The parent element is the last object List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups; int objGroupsCount = objectGroups != null ? objectGroups.Count : 0; CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? objectGroups[objGroupsCount - 1] : null; List <Dictionary <string, string> > objects = objectGroup.Objects; int objCount = objects != null ? objects.Count : 0; Dictionary <string, string> dict = objCount > 0 ? objects[objCount - 1] : null; string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Tile) { Dictionary <string, string> dict = pTMXMapInfo.TileProperties[pTMXMapInfo.ParentGID]; string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } } else if (elementName == "polygon") { // find parent object's dict and add polygon-points to it int objGroupsCount = ObjectGroups != null ? ObjectGroups.Count : 0; CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? ObjectGroups[objGroupsCount - 1] : null; List <Dictionary <string, string> > objects = objectGroup.Objects; int objCount = objects != null ? objects.Count : 0; Dictionary <string, string> dict = objCount > 0 ? objects[objCount - 1] : null; // get points value string var value = attributeDict["points"]; if (!String.IsNullOrEmpty(value)) { var pPointsArray = new List <CCPoint>(); var pointPairs = value.Split(' '); foreach (var pontPair in pointPairs) { //TODO: Parse points //CCPoint point; //point.X = x + objectGroup.PositionOffset.X; //point.Y = y + objectGroup.PositionOffset.Y; //pPointsArray.Add(point); } //dict.Add("points", pPointsArray); } } else if (elementName == "polyline") { // find parent object's dict and add polyline-points to it // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)ObjectGroups->lastObject(); // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject(); // TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"]; } }
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 frameRect.Origin += spriteOffset; // create frame spriteFrame = new CCSpriteFrame( spriteSourceSize, texture, frameRect, textureRotated ); spriteFrame.TextureFilename = name; spriteFrames [name] = spriteFrame; } } AutoCreateAliasList(); }