示例#1
0
        public static void TransformAnimationData(AnimationData animationData, ArmatureData armatureData)
        {
            SkinData        skinData     = armatureData.GetSkinData(null);
            List <BoneData> boneDataList = armatureData.BoneDataList;
            List <SlotData> slotDataList = skinData.SlotDataList;
            int             i            = boneDataList.Count;

            BoneData          boneData;
            TransformTimeline timeline;
            SlotData          slotData;
            DisplayData       displayData;
            TransformTimeline parentTimeline;
            List <Frame>      frameList;
            DBTransform       originTransform;
            Point             originPivot;
            TransformFrame    prevFrame;
            uint           frameListLength;
            TransformFrame frame;

            while (i-- > 0)
            {
                boneData = boneDataList[i];
                timeline = animationData.GetTimeline(boneData.Name);
                if (timeline == null)
                {
                    continue;
                }

                slotData = null;
                foreach (SlotData slotDataObj in slotDataList)
                {
                    if (slotDataObj.Parent == boneData.Name)
                    {
                        slotData = slotDataObj;
                        break;
                    }
                }

                parentTimeline = boneData.Parent != null?animationData.GetTimeline(boneData.Parent) : null;

                frameList = timeline.FrameList;

                originTransform = null;
                originPivot     = null;
                prevFrame       = null;
                frameListLength = (uint)frameList.Count;
                for (int j = 0; j < frameListLength; j++)
                {
                    frame = frameList[j] as TransformFrame;
                    if (parentTimeline != null)
                    {
                        //tweenValues to transform.
                        _helpTransform1.Copy(frame.Global);

                        //get transform from parent timeline.
                        GetTimelineTransform(parentTimeline, frame.Position, _helpTransform2);
                        TransformUtil.TransformPointWithParent(_helpTransform1, _helpTransform2);

                        //transform to tweenValues.
                        frame.Transform.Copy(_helpTransform1);
                    }
                    else
                    {
                        frame.Transform.Copy(frame.Global);
                    }

                    frame.Transform.X      -= boneData.Transform.X;
                    frame.Transform.Y      -= boneData.Transform.Y;
                    frame.Transform.SkewX  -= boneData.Transform.SkewX;
                    frame.Transform.SkewY  -= boneData.Transform.SkewY;
                    frame.Transform.ScaleX -= boneData.Transform.ScaleX;
                    frame.Transform.ScaleY -= boneData.Transform.ScaleY;

                    if (!timeline.Transformed)
                    {
                        if (slotData != null)
                        {
                            frame.ZOrder -= slotData.ZOrder;
                        }
                    }

                    if (originTransform == null)
                    {
                        originTransform = timeline.OriginTransform;
                        originTransform.Copy(frame.Transform);
                        originTransform.SkewX = TransformUtil.FormatRadian(originTransform.SkewX);
                        originTransform.SkewY = TransformUtil.FormatRadian(originTransform.SkewY);
                        originPivot           = timeline.OriginPivot;
                        originPivot.X         = frame.Pivot.X;
                        originPivot.Y         = frame.Pivot.Y;
                    }

                    frame.Transform.X      -= originTransform.X;
                    frame.Transform.Y      -= originTransform.Y;
                    frame.Transform.SkewX   = TransformUtil.FormatRadian(frame.Transform.SkewX - originTransform.SkewX);
                    frame.Transform.SkewY   = TransformUtil.FormatRadian(frame.Transform.SkewY - originTransform.SkewY);
                    frame.Transform.ScaleX -= originTransform.ScaleX;
                    frame.Transform.ScaleY -= originTransform.ScaleY;

                    if (!timeline.Transformed)
                    {
                        frame.Pivot.Y -= originPivot.X;
                        frame.Pivot.Y -= originPivot.Y;
                    }

                    if (prevFrame != null)
                    {
                        float dLX = frame.Transform.SkewX - prevFrame.Transform.SkewX;

                        if (prevFrame.TweenRotate != 0)
                        {
                            if (prevFrame.TweenRotate > 0)
                            {
                                if (dLX < 0)
                                {
                                    frame.Transform.SkewX += (float)Math.PI * 2f;
                                    frame.Transform.SkewY += (float)Math.PI * 2f;
                                }

                                if (prevFrame.TweenRotate > 1)
                                {
                                    frame.Transform.SkewX += (float)Math.PI * 2f * ((float)prevFrame.TweenRotate - 1f);
                                    frame.Transform.SkewY += (float)Math.PI * 2f * ((float)prevFrame.TweenRotate - 1f);
                                }
                            }
                            else
                            {
                                if (dLX > 0)
                                {
                                    frame.Transform.SkewX -= (float)Math.PI * 2f;
                                    frame.Transform.SkewY -= (float)Math.PI * 2f;
                                }

                                if (prevFrame.TweenRotate < 1)
                                {
                                    frame.Transform.SkewX += (float)Math.PI * 2f * ((float)prevFrame.TweenRotate + 1f);
                                    frame.Transform.SkewY += (float)Math.PI * 2f * ((float)prevFrame.TweenRotate + 1f);
                                }
                            }
                        }
                        else
                        {
                            frame.Transform.SkewX = prevFrame.Transform.SkewX + TransformUtil.FormatRadian(frame.Transform.SkewX - prevFrame.Transform.SkewX);
                            frame.Transform.SkewY = prevFrame.Transform.SkewY + TransformUtil.FormatRadian(frame.Transform.SkewY - prevFrame.Transform.SkewY);
                        }
                    }

                    prevFrame = frame;
                }
                timeline.Transformed = true;
            }
        }
        /**
         * Build and returns a new Armature instance.
         * @example
         * <listing>
         * var armature:Armature = factory.buildArmature('dragon');
         * </listing>
         * @param	armatureName The name of this Armature instance.
         * @param	The name of this animation.
         * @param	The name of this SkeletonData.
         * @param	The name of this textureAtlas.
         * @param	The name of this skin.
         * @return A Armature instance.
         */
        public Armature BuildArmature(string armatureName, string animationName = null, string skeletonName = null, string textureAtlasName = null, string skinName = null)
        {
            SkeletonData data                  = null;
            ArmatureData armatureData          = null;
            ArmatureData animationArmatureData = null;

            if (skeletonName != null)
            {
                data = _dataDic[skeletonName];
                if (data != null)
                {
                    armatureData = data.GetArmatureData(armatureName);
                }
            }
            else
            {
                foreach (KeyValuePair <string, SkeletonData> skeleton in _dataDic)
                {
                    data         = _dataDic[skeleton.Key];
                    armatureData = data.GetArmatureData(armatureName);
                    if (armatureData != null)
                    {
                        break;
                    }
                }
            }

            if (armatureData == null)
            {
                return(null);
            }

            _currentDataName = skeletonName;

            if (textureAtlasName != null)
            {
                _currentTextureAtlasName = textureAtlasName;
            }
            else
            {
                _currentTextureAtlasName = skeletonName;
            }

            Armature armature = generateArmature();

            armature.Name = armatureName;
            Bone bone;

            foreach (BoneData boneData in armatureData.BoneDataList)
            {
                bone               = new Bone();
                bone.Name          = boneData.Name;
                bone.FixedRotation = boneData.FixedRotation;
                bone.ScaleMode     = boneData.ScaleMode;
                bone.Origin.Copy(boneData.Transform);

                if (armatureData.GetBoneData(boneData.Parent) != null)
                {
                    armature.AddBone(bone, boneData.Parent);
                }
                else
                {
                    armature.AddBone(bone);
                }
            }

            if (animationName != null && animationName != armatureName)
            {
                animationArmatureData = data.GetArmatureData(animationName);
                if (animationArmatureData == null)
                {
                    foreach (KeyValuePair <string, SkeletonData> skeleton in _dataDic)
                    {
                        data = _dataDic[skeleton.Key];
                        animationArmatureData = data.GetArmatureData(animationName);
                        if (animationArmatureData != null)
                        {
                            break;
                        }
                    }
                }
            }

            if (animationArmatureData != null)
            {
                armature.Animation.AnimationDataList = animationArmatureData.AnimationDataList;
            }
            else
            {
                armature.Animation.AnimationDataList = armatureData.AnimationDataList;
            }

            SkinData skinData = armatureData.GetSkinData(skinName);

            if (skinData == null)
            {
                throw new ArgumentException();
            }

            Slot          slot;
            DisplayData   displayData;
            Armature      childArmature;
            int           i;
            List <object> helpArray = new List <object>();

            foreach (SlotData slotData in skinData.SlotDataList)
            {
                bone = armature.GetBone(slotData.Parent);
                if (bone == null)
                {
                    continue;
                }
                slot                 = generateSlot();
                slot.Name            = slotData.Name;
                slot.BlendMode       = slotData.BlendMode;
                slot._originZOrder   = slotData.ZOrder;
                slot._dislayDataList = slotData.DisplayDataList;

                helpArray.Clear();
                i = slotData.DisplayDataList.Count;
                while (i-- > 0)
                {
                    displayData = slotData.DisplayDataList[i];

                    switch (displayData.Type)
                    {
                    case DisplayData.ARMATURE:
                        childArmature = BuildArmature(displayData.Name, null, _currentDataName, _currentTextureAtlasName);
                        if (childArmature != null)
                        {
                            helpArray.Insert(0, childArmature);
                        }
                        break;

                    case DisplayData.IMAGE:
                    default:
                        helpArray.Insert(0, generateDisplay(_textureAtlasDic[_currentTextureAtlasName], displayData.Name, displayData.Pivot.X, displayData.Pivot.Y));
                        break;
                    }
                }
                slot.DisplayList = helpArray;
                slot.changeDisplay(0);
                bone.AddChild(slot);
            }

            //
            i = armature._boneList.Count;
            while (i-- > 0)
            {
                armature._boneList[i].update();
            }

            i = armature._slotList.Count;
            while (i-- > 0)
            {
                slot = armature._slotList[i];
                slot.update();
            }
            armature.UpdateSlotsZOrder();

            return(armature);
        }