Ejemplo n.º 1
0
        public static MGSpriteFrame FrameWithTexture(Texture2D pobTexture, Rectangle rect, bool rotated, Vector2 offset, Vector2 originalSize)
        {
            MGSpriteFrame pSpriteFrame = new MGSpriteFrame();
            pSpriteFrame.InitWithTexture(pobTexture, rect, rotated, offset, originalSize);

            return pSpriteFrame;
        }
Ejemplo n.º 2
0
        public static MGSpriteFrame FrameWithTexture(Texture2D pobTexture, Rectangle rect)
        {
            MGSpriteFrame pSpriteFrame = new MGSpriteFrame(); ;
            pSpriteFrame.InitWithTexture(pobTexture, rect);

            return pSpriteFrame;
        }
Ejemplo n.º 3
0
 public void AddSpriteFrame(MGSpriteFrame pobFrame, string pszFrameName)
 {
     if (!m_pSpriteFrames.ContainsKey(pszFrameName))
     {
         m_pSpriteFrames.Add(pszFrameName, pobFrame);
     }
 }
Ejemplo n.º 4
0
        public void AddSpriteFramesWithDictionary(Dictionary<string, Object> pobDictionary, Texture2D pobTexture)
        {
            Dictionary<string, Object> metadataDict = null;
            if (pobDictionary.Keys.Contains("metadata"))
            {
                metadataDict = (Dictionary<string, Object>)pobDictionary["metadata"];
            }

            Dictionary<string, Object> framesDict = null;
            if (pobDictionary.Keys.Contains("frames"))
            {
                framesDict = (Dictionary<string, Object>)pobDictionary["frames"];
            }

            int format = 0;

            // get the format
            if (metadataDict != null)
            {
                format = int.Parse(metadataDict["format"].ToString());
            }

            Debug.Assert(format >= 0 && format <= 3);

            foreach (var key in framesDict.Keys)
            {
                Dictionary<string, Object> frameDict = framesDict[key] as Dictionary<string, Object>;
                MGSpriteFrame spriteFrame = new MGSpriteFrame();

                if (format == 0)
                {
                    float x = float.Parse(frameDict["x"].ToString());
                    float y = float.Parse(frameDict["y"].ToString());
                    float w = float.Parse(frameDict["width"].ToString());
                    float h = float.Parse(frameDict["height"].ToString());
                    float ox = float.Parse(frameDict["offsetX"].ToString());
                    float oy = float.Parse(frameDict["offsetY"].ToString());
                    int ow = int.Parse(frameDict["originalWidth"].ToString());
                    int oh = int.Parse(frameDict["originalHeight"].ToString());
                    // check ow/oh
                    if (ow == 0 || oh == 0)
                    {
                        Debug.WriteLine("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
                    }
                    // abs ow/oh
                    ow = Math.Abs(ow);
                    oh = Math.Abs(oh);
                    // create frame
                    spriteFrame = new MGSpriteFrame();
                    spriteFrame.InitWithTexture(pobTexture,
                                                new Rectangle((int)x, (int)y, (int)w, (int)h),
                                                false,
                                                new Vector2(ox, oy),
                                                new Vector2(ow, oh)
                                                );
                }
                else if (format == 1 || format == 2)
                {
                    Rectangle frame = MNS.RectangleFromString(frameDict["frame"].ToString());
                    bool rotated = false;

                    // rotation
                    if (format == 2)
                    {
                        if (frameDict.Keys.Contains("rotated"))
                        {
                            rotated = int.Parse(valueForKey("rotated", frameDict)) == 0 ? false : true;
                        }
                    }

                    Vector2 offset = MNS.Vector2FromString(valueForKey("offset", frameDict));
                    Vector2 sourceSize = MNS.Vector2FromString(valueForKey("sourceSize", frameDict));

                    // create frame
                    spriteFrame = new MGSpriteFrame();
                    spriteFrame.InitWithTexture(pobTexture,
                        frame,
                        rotated,
                        offset,
                        sourceSize
                        );
                }
                else
                    if (format == 3)
                    {
                        // get values
                        Rectangle spriteSize = MNS.RectangleFromString(valueForKey("spriteSize", frameDict));
                        Vector2 spriteOffset = MNS.Vector2FromString(valueForKey("spriteOffset", frameDict));
                        Vector2 spriteSourceSize = MNS.Vector2FromString(valueForKey("spriteSourceSize", frameDict));
                        Rectangle textureRect = MNS.RectangleFromString(valueForKey("textureRect", frameDict));
                        bool textureRotated = false;
                        if (frameDict.Keys.Contains("textureRotated"))
                        {
                            textureRotated = int.Parse(valueForKey("textureRotated", frameDict)) == 0 ? false : true;
                        }

                        // get aliases
                        var list = frameDict["aliases"];
                        List<object> aliases = (frameDict["aliases"] as List<object>);
                        string frameKey = key;
                        foreach (var item2 in aliases)
                        {
                            string oneAlias = item2.ToString();
                            if (m_pSpriteFramesAliases.Keys.Contains(oneAlias))
                            {
                                if (m_pSpriteFramesAliases[oneAlias] != null)
                                {
                                    Debug.WriteLine("cocos2d: WARNING: an alias with name {0} already exists", oneAlias);
                                }
                            }
                            if (!m_pSpriteFramesAliases.Keys.Contains(frameKey))
                            {
                                m_pSpriteFramesAliases.Add(frameKey, oneAlias);
                            }
                        }

                        // create frame
                        spriteFrame = new MGSpriteFrame();
                        spriteFrame.InitWithTexture(pobTexture,
                                        new Rectangle(textureRect.X, textureRect.Y, spriteSize.Width, spriteSize.Height),
                                        textureRotated,
                                        spriteOffset,
                                        spriteSourceSize);
                    }

                // add sprite frame
                if (!m_pSpriteFrames.Keys.Contains(key))
                {
                    m_pSpriteFrames.Add(key, spriteFrame);
                }
            }
        }