예제 #1
0
        private void SetAnimatedProperty(string pPropName, CCNode node, object pValue, float fTweenDuraion)
        {
            if (fTweenDuraion > 0)
            {
                // Create a fake keyframe to generate the action from
                var kf1 = new CCBKeyframe();
                kf1.Value = pValue;
                kf1.Time = fTweenDuraion;
                kf1.EasingType = kCCBKeyframeEasing.kCCBKeyframeEasingLinear;

                // Animate
                CCActionInterval tweenAction = GetAction(null, kf1, pPropName, node);
                node.RunAction(tweenAction);
            }
            else
            {
                // Just set the value

                if (pPropName == "position")
                {
                    // Get position type
                    var array = (List<CCBValue>) GetBaseValue(node, pPropName);
                    var type = (kCCBPositionType) array[2].GetIntValue();

                    // Get relative position
                    var value = (List<CCBValue>) pValue;
                    float x = value[0].GetFloatValue();
                    float y = value[1].GetFloatValue();

                    node.Position = CCBHelper.GetAbsolutePosition(new CCPoint(x, y), type, GetContainerSize(node.Parent), pPropName);
                }
                else if (pPropName == "scale")
                {
                    // Get scale type
                    var array = (List<CCBValue>) GetBaseValue(node, pPropName);
                    var type = (kCCBScaleType) array[2].GetIntValue();

                    // Get relative scale
                    var value = (List<CCBValue>) pValue;
                    float x = value[0].GetFloatValue();
                    float y = value[1].GetFloatValue();

                    CCBHelper.SetRelativeScale(node, x, y, type, pPropName);
                }
                else
                {
                    // [node setValue:value forKey:name];

                    // TODO only handle rotation, opacity, displayFrame, color
                    if (pPropName == "rotation")
                    {
                        float rotate = ((CCBValue) pValue).GetFloatValue();
                        node.Rotation = rotate;
                    }
                    else if (pPropName == "opacity")
                    {
                        byte opacity = ((CCBValue) pValue).GetByteValue();
                        ((ICCRGBAProtocol) node).Opacity = opacity;
                    }
                    else if (pPropName == "displayFrame")
                    {
                        ((CCSprite) node).DisplayFrame = (CCSpriteFrame) pValue;
                    }
                    else if (pPropName == "color")
                    {
                        var color = (ccColor3BWapper) pValue;
                        ((CCSprite) node).Color = color.getColor();
                    }
                    else
                    {
                        CCLog.Log("unsupported property name is {0}", pPropName);
                        Debug.Assert(false, "unsupported property now");
                    }
                }
            }
        }
예제 #2
0
        private CCBKeyframe ReadKeyframe(kCCBPropType type)
        {
            var keyframe = new CCBKeyframe();

            keyframe.Time = ReadFloat();

            var easingType = (kCCBKeyframeEasing) ReadInt(false);
            float easingOpt = 0;
            object value = null;

            if (easingType == kCCBKeyframeEasing.kCCBKeyframeEasingCubicIn
                || easingType == kCCBKeyframeEasing.kCCBKeyframeEasingCubicOut
                || easingType == kCCBKeyframeEasing.kCCBKeyframeEasingCubicInOut
                || easingType == kCCBKeyframeEasing.kCCBKeyframeEasingElasticIn
                || easingType == kCCBKeyframeEasing.kCCBKeyframeEasingElasticOut
                || easingType == kCCBKeyframeEasing.kCCBKeyframeEasingElasticInOut)
            {
                easingOpt = ReadFloat();
            }
            keyframe.EasingType = easingType;
            keyframe.EasingOpt = easingOpt;

            if (type == kCCBPropType.kCCBPropTypeCheck)
            {
                value = new CCBValue(ReadBool());
            }
            else if (type == kCCBPropType.kCCBPropTypeByte)
            {
                value = new CCBValue(ReadByte());
            }
            else if (type == kCCBPropType.kCCBPropTypeColor3)
            {
                byte r = ReadByte();
                byte g = ReadByte();
                byte b = ReadByte();

                var c = new CCColor3B(r, g, b);
                value = ccColor3BWapper.Create(c);
            }
            else if (type == kCCBPropType.kCCBPropTypeDegrees)
            {
                value = new CCBValue(ReadFloat());
            }
            else if (type == kCCBPropType.kCCBPropTypeScaleLock || type == kCCBPropType.kCCBPropTypePosition)
            {
                float a = ReadFloat();
                float b = ReadFloat();

                value = new List<CCBValue>
                    {
                        new CCBValue(a),
                        new CCBValue(b)
                    };
            }
            else if (type == kCCBPropType.kCCBPropTypeSpriteFrame)
            {
                string spriteSheet = ReadCachedString();
                string spriteFile = ReadCachedString();

                CCSpriteFrame spriteFrame;

                if (String.IsNullOrEmpty(spriteSheet))
                {
                    CCTexture2D texture = CCTextureCache.SharedTextureCache.AddImage(CCFileUtils.RemoveExtension(spriteFile));
                    var bounds = new CCRect(0, 0, texture.ContentSize.Width, texture.ContentSize.Height);
                    spriteFrame = CCSpriteFrame.Create(texture, bounds);
                }
                else
                {
                    CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache;

                    // Load the sprite sheet only if it is not loaded
                    if (!mLoadedSpriteSheets.Contains(spriteSheet))
                    {
                        frameCache.AddSpriteFramesWithFile(spriteSheet);
                        mLoadedSpriteSheets.Add(spriteSheet);
                    }

                    spriteFrame = frameCache.SpriteFrameByName(spriteFile);
                }
                value = spriteFrame;
            }

            keyframe.Value = value;

            return keyframe;
        }
예제 #3
0
        private CCActionInterval GetAction(CCBKeyframe pKeyframe0, CCBKeyframe pKeyframe1, string pPropName, CCNode node)
        {
            float duration = pKeyframe1.Time - (pKeyframe0 != null ? pKeyframe0.Time : 0);

            switch (pPropName)
            {
                case "rotation":
                    {
                        var value = (CCBValue) pKeyframe1.Value;
                        return CCBRotateTo.Create(duration, value.GetFloatValue());
                    }
                case "opacity":
                    {
                        var value = (CCBValue) pKeyframe1.Value;
                        return new CCFadeTo (duration, value.GetByteValue());
                    }
                case "color":
                    {
                        var color = (ccColor3BWapper) pKeyframe1.Value;
                        CCColor3B c = color.getColor();

                        return new CCTintTo (duration, c.R, c.G, c.B);
                    }
                case "visible":
                    {
                        var value = (CCBValue) pKeyframe1.Value;
                        if (value.GetBoolValue())
                        {
                            return new CCSequence (new CCDelayTime (duration), CCShow.Create());
                        }
                        return new CCSequence (new CCDelayTime (duration), CCHide.Create());
                    }
                case "displayFrame":
                    return new CCSequence (new CCDelayTime (duration), CCBSetSpriteFrame.Create((CCSpriteFrame) pKeyframe1.Value));
                case "position":
                    {
                        // Get position type
                        var array = (List<CCBValue>) GetBaseValue(node, pPropName);
                        var type = (kCCBPositionType) array[2].GetIntValue();

                        // Get relative position
                        var value = (List<CCBValue>) pKeyframe1.Value;
                        float x = value[0].GetFloatValue();
                        float y = value[1].GetFloatValue();

                        CCSize containerSize = GetContainerSize(node.Parent);

                        CCPoint absPos = CCBHelper.GetAbsolutePosition(new CCPoint(x, y), type, containerSize, pPropName);

                        return new CCMoveTo (duration, absPos);
                    }
                case "scale":
                    {
                        // Get position type
                        var array = (List<CCBValue>) GetBaseValue(node, pPropName);
                        var type = (kCCBScaleType) array[2].GetIntValue();

                        // Get relative scale
                        var value = (List<CCBValue>) pKeyframe1.Value;
                        float x = value[0].GetFloatValue();
                        float y = value[1].GetFloatValue();

                        if (type == kCCBScaleType.kCCBScaleTypeMultiplyResolution)
                        {
                            float resolutionScale = CCBReader.ResolutionScale;
                            x *= resolutionScale;
                            y *= resolutionScale;
                        }

                        return new CCScaleTo(duration, x, y);
                    }
                default:
                    CCLog.Log("CCBReader: Failed to create animation for property: {0}", pPropName);
                    break;
            }

            return null;
        }