示例#1
0
		private PlistType GetPlistType(PlistDictionary dict)
		{
			var isSpriteKit = dict.ContainsKey ("format") ? dict ["format"].AsString == "APPL" : false;

			return isSpriteKit ? PlistType.SpriteKit : PlistType.Cocos2D;

		}
 static void SetIfNotPresent(PlistDictionary dict, string key, PlistObjectBase value)
 {
     if (!dict.ContainsKey(key))
     {
         dict[key] = value;
     }
 }
        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;
                }
            }
        }
示例#4
0
        public static bool EqualObject(object x, object y, double deltaReal, double deltaDateSeconds)
        {
            if (x.GetType() != y.GetType())
            {
                return(false);
            }

            if (x is bool || x is int || x is string)
            {
                bool eq = x.Equals(y);
                return(eq);
            }

            if (x is byte[])
            {
                byte[] xbyte = x as byte[];
                byte[] ybyte = y as byte[];
                if (xbyte.Length != ybyte.Length)
                {
                    return(false);
                }
                for (int i = 0; i < xbyte.Length; ++i)
                {
                    if (xbyte[i] != ybyte[i])
                    {
                        return(false);
                    }
                }
                return(true);
                // bool eq = (x as byte[]).SequenceEqual(y as byte[]);
                // return eq;
            }
            if (x is double)
            {
                bool eq = Math.Abs((double)x - (double)y) < deltaReal;
                return(eq);
            }
            if (x is DateTime)
            {
                var  deltaTime = (DateTime)x - (DateTime)y;
                bool eq        = Math.Abs(deltaTime.TotalSeconds) < deltaDateSeconds;
                return(eq);
            }

            if (x is PlistList)
            {
                PlistList xList = x as PlistList;
                PlistList yList = y as PlistList;
                if (xList.Count != yList.Count)
                {
                    return(false);
                }
                for (int i = 0; i < xList.Count; ++i)
                {
                    if (EqualObject(xList[i], yList[i], deltaReal, deltaDateSeconds) == false)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            if (x is PlistDictionary)
            {
                PlistDictionary xDict = x as PlistDictionary;
                PlistDictionary yDict = y as PlistDictionary;
                if (xDict.Count != yDict.Count)
                {
                    return(false);
                }

                foreach (var kv in xDict)
                {
                    if (yDict.ContainsKey(kv.Key) == false)
                    {
                        return(false);
                    }
                    var yValue = yDict[kv.Key];
                    if (EqualObject(kv.Value, yValue, deltaReal, deltaDateSeconds) == false)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            throw new PlistException("undefined data type");
        }
		static void SetIfNotPresent (PlistDictionary dict, string key, PlistObjectBase value)
		{
			if (!dict.ContainsKey (key))
				dict[key] = value;
		}
示例#6
0
		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;
			}

		}
示例#7
0
		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;
				}
			}
		}
		static bool SetNibProperty (PlistDictionary dict, IPhoneProject proj, FilePath mainNibProp, string propName)
		{
			if (!dict.ContainsKey (propName)) {
				if (mainNibProp.IsNullOrEmpty) {
					return false;
				} else {
					string mainNib = mainNibProp.ToRelative (proj.BaseDirectory);
					if (mainNib.EndsWith (".nib") || mainNib.EndsWith (".xib"))
					    mainNib = mainNib.Substring (0, mainNib.Length - 4).Replace ('\\', '/');
					dict[propName] = mainNib;
				}
			}
			return true;
		}
        public void loadGenericInfoFromDictionary(PlistDictionary dict, CCNode nd)
        {
            b2WorldDirty = false;
            _node        = nd;

            if (null != dict)
            {
                name = dict ["name"].AsString;
                uuid = dict ["uuid"].AsString;

                //tags loading
//				{
//					NSArray* loadedTags = [dict objectForKey:@"tags"];
//					if(loadedTags){
//						_tags = [[NSMutableArray alloc] initWithArray:loadedTags];
//					}
//				}

                //user properties loading
//				{
//					NSDictionary* userPropInfo  = [dict objectForKey:@"userPropertyInfo"];
//					NSString* userPropClassName = [dict objectForKey:@"userPropertyName"];
//					if(userPropInfo && userPropClassName)
//					{
//						Class userPropClass = NSClassFromString(userPropClassName);
//						if(userPropClass){
//							#pragma clang diagnostic push
//							#pragma clang diagnostic ignored "-Wundeclared-selector"
//							_userProperty = [userPropClass performSelector:@selector(customClassInstanceWithNode:)
//								withObject:_node];
//							#pragma clang diagnostic pop
//							if(_userProperty){
//								[_userProperty setPropertiesFromDictionary:userPropInfo];
//							}
//						}
//					}
//				}

                if (dict.ContainsKey("alpha"))
                {
                    _node.Opacity = (byte)dict ["alpha"].AsFloat;
                }

                if (dict.ContainsKey("rotation"))
                {
                    _node.Rotation = -dict ["rotation"].AsFloat;
                }

                if (dict.ContainsKey("zOrder"))
                {
                    _node.ZOrder = dict ["zOrder"].AsInt;
                }

                if (dict.ContainsKey("scale"))
                {
                    CCPoint scl = CCPoint.Parse(dict ["scale"].AsString);
                    _node.ScaleX = scl.X;
                    _node.ScaleY = scl.Y;
                }

                //for sprites the content size is set from the CCSpriteFrame
                if (dict.ContainsKey("size") && _node.GetType() != typeof(LHSprite))
                {
                    _node.ContentSize = CCSize.Parse(dict ["size"].AsString);
                }

                if (dict.ContainsKey("generalPosition")
                    &&
                    _node.GetType() != typeof(LHUINode)
                    &&
                    _node.GetType() != typeof(LHBackUINode)
                    &&
                    _node.GetType() != typeof(LHGameWorldNode))
                {
                    CCPoint unitPos = CCPoint.Parse(dict ["generalPosition"].AsString);
                    CCPoint pos     = LHUtils.positionForNode(_node, unitPos);

                    _node.Position = pos;
                }
//				if([dict objectForKey:@"generalPosition"]&&
//					![_node isKindOfClass:[LHUINode class]] &&
//					![_node isKindOfClass:[LHBackUINode class]] &&
//					![_node isKindOfClass:[LHGameWorldNode class]])
//				{
//
//					CGPoint unitPos = [dict pointForKey:@"generalPosition"];
//					CGPoint pos = [LHUtils positionForNode:_node
//						fromUnit:unitPos];
//
//					NSDictionary* devPositions = [dict objectForKey:@"devicePositions"];
//					if(devPositions)
//					{
//
//						NSString* unitPosStr = [LHUtils devicePosition:devPositions
//							forSize:LH_SCREEN_RESOLUTION];
//
//						if(unitPosStr){
//							CGPoint unitPos = LHPointFromString(unitPosStr);
//							pos = [LHUtils positionForNode:_node
//								fromUnit:unitPos];
//						}
//					}
//
//					[_node setPosition:pos];
//				}

//				if([dict objectForKey:@"anchor"] &&
//					![_node isKindOfClass:[LHUINode class]] &&
//					![_node isKindOfClass:[LHBackUINode class]] &&
//					![_node isKindOfClass:[LHGameWorldNode class]])
//				{
//					CGPoint anchor = [dict pointForKey:@"anchor"];
//					anchor.y = 1.0f - anchor.y;
//					[_node setAnchorPoint:anchor];
//				}
            }
        }