예제 #1
0
        public TransformConstraint(TransformConstraintData data, Skeleton skeleton)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data cannot be null.");
            }
            if (skeleton == null)
            {
                throw new ArgumentNullException("skeleton cannot be null.");
            }
            this.data      = data;
            translateMix   = data.translateMix;
            rotateMix      = data.rotateMix;
            scaleMix       = data.scaleMix;
            shearMix       = data.shearMix;
            offsetRotation = data.offsetRotation;
            offsetX        = data.offsetX;
            offsetY        = data.offsetY;
            offsetScaleX   = data.offsetScaleX;
            offsetScaleY   = data.offsetScaleY;
            offsetShearY   = data.offsetShearY;

            bone   = skeleton.FindBone(data.bone.name);
            target = skeleton.FindBone(data.target.name);
        }
예제 #2
0
        public TransformConstraint(TransformConstraintData data, Skeleton skeleton)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data", "data cannot be null.");
            }
            if (skeleton == null)
            {
                throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
            }
            this.data = data;
            mixRotate = data.mixRotate;
            mixX      = data.mixX;
            mixY      = data.mixY;
            mixScaleX = data.mixScaleX;
            mixScaleY = data.mixScaleY;
            mixShearY = data.mixShearY;
            bones     = new ExposedList <Bone>();
            foreach (BoneData boneData in data.bones)
            {
                bones.Add(skeleton.FindBone(boneData.name));
            }

            target = skeleton.FindBone(data.target.name);
        }
예제 #3
0
        /// <summary>Sets the bones and constraints to their setup pose values.</summary>
        public void SetBonesToSetupPose()
        {
            ExposedList <Bone> bones = this.bones;

            for (int i = 0, n = bones.Count; i < n; i++)
            {
                bones.Items[i].SetToSetupPose();
            }

            ExposedList <IkConstraint> ikConstraints = this.ikConstraints;

            for (int i = 0, n = ikConstraints.Count; i < n; i++)
            {
                IkConstraint constraint = ikConstraints.Items[i];
                constraint.bendDirection = constraint.data.bendDirection;
                constraint.mix           = constraint.data.mix;
            }

            ExposedList <TransformConstraint> transformConstraints = this.transformConstraints;

            for (int i = 0, n = transformConstraints.Count; i < n; i++)
            {
                TransformConstraint     constraint = transformConstraints.Items[i];
                TransformConstraintData data       = constraint.data;
                constraint.rotateMix    = data.rotateMix;
                constraint.translateMix = data.translateMix;
                constraint.scaleMix     = data.scaleMix;
                constraint.shearMix     = data.shearMix;
            }
        }
예제 #4
0
		public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
			if (data == null) throw new ArgumentNullException("data cannot be null.");
			if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
			this.data = data;
			translateMix = data.translateMix;
			x = data.x;
			y = data.y;

			bone = skeleton.FindBone(data.bone.name);
			target = skeleton.FindBone(data.target.name);
		}
예제 #5
0
        public void SetBonesToSetupPose()
        {
            Bone[] items = this.bones.Items;
            int    index = 0;
            int    count = this.bones.Count;

            while (index < count)
            {
                items[index].SetToSetupPose();
                index++;
            }
            IkConstraint[] constraintArray = this.ikConstraints.Items;
            int            num3            = 0;
            int            num4            = this.ikConstraints.Count;

            while (num3 < num4)
            {
                IkConstraint constraint = constraintArray[num3];
                constraint.bendDirection = constraint.data.bendDirection;
                constraint.mix           = constraint.data.mix;
                num3++;
            }
            TransformConstraint[] constraintArray2 = this.transformConstraints.Items;
            int num5 = 0;
            int num6 = this.transformConstraints.Count;

            while (num5 < num6)
            {
                TransformConstraint     constraint2 = constraintArray2[num5];
                TransformConstraintData data        = constraint2.data;
                constraint2.rotateMix    = data.rotateMix;
                constraint2.translateMix = data.translateMix;
                constraint2.scaleMix     = data.scaleMix;
                constraint2.shearMix     = data.shearMix;
                num5++;
            }
            PathConstraint[] constraintArray3 = this.pathConstraints.Items;
            int num7 = 0;
            int num8 = this.pathConstraints.Count;

            while (num7 < num8)
            {
                PathConstraint     constraint3 = constraintArray3[num7];
                PathConstraintData data        = constraint3.data;
                constraint3.position     = data.position;
                constraint3.spacing      = data.spacing;
                constraint3.rotateMix    = data.rotateMix;
                constraint3.translateMix = data.translateMix;
                num7++;
            }
        }
예제 #6
0
        public void SetBonesToSetupPose()
        {
            Bone[] items = this.bones.Items;
            int    i     = 0;
            int    count = this.bones.Count;

            while (i < count)
            {
                items[i].SetToSetupPose();
                i++;
            }
            IkConstraint[] items2 = this.ikConstraints.Items;
            int            j      = 0;
            int            count2 = this.ikConstraints.Count;

            while (j < count2)
            {
                IkConstraint ikConstraint = items2[j];
                ikConstraint.bendDirection = ikConstraint.data.bendDirection;
                ikConstraint.mix           = ikConstraint.data.mix;
                j++;
            }
            TransformConstraint[] items3 = this.transformConstraints.Items;
            int k      = 0;
            int count3 = this.transformConstraints.Count;

            while (k < count3)
            {
                TransformConstraint     transformConstraint     = items3[k];
                TransformConstraintData transformConstraintData = transformConstraint.data;
                transformConstraint.rotateMix    = transformConstraintData.rotateMix;
                transformConstraint.translateMix = transformConstraintData.translateMix;
                transformConstraint.scaleMix     = transformConstraintData.scaleMix;
                transformConstraint.shearMix     = transformConstraintData.shearMix;
                k++;
            }
            PathConstraint[] items4 = this.pathConstraints.Items;
            int l      = 0;
            int count4 = this.pathConstraints.Count;

            while (l < count4)
            {
                PathConstraint     pathConstraint     = items4[l];
                PathConstraintData pathConstraintData = pathConstraint.data;
                pathConstraint.position     = pathConstraintData.position;
                pathConstraint.spacing      = pathConstraintData.spacing;
                pathConstraint.rotateMix    = pathConstraintData.rotateMix;
                pathConstraint.translateMix = pathConstraintData.translateMix;
                l++;
            }
        }
예제 #7
0
        /// <summary>Sets the bones and constraints to their setup pose values.</summary>
        public void SetBonesToSetupPose()
        {
            var bones = this.bones.Items;

            for (int i = 0, n = this.bones.Count; i < n; i++)
            {
                bones[i].SetToSetupPose();
            }

            var ikConstraints = this.ikConstraints.Items;

            for (int i = 0, n = this.ikConstraints.Count; i < n; i++)
            {
                IkConstraint     constraint = ikConstraints[i];
                IkConstraintData data       = constraint.data;
                constraint.mix           = data.mix;
                constraint.softness      = data.softness;
                constraint.bendDirection = data.bendDirection;
                constraint.compress      = data.compress;
                constraint.stretch       = data.stretch;
            }

            var transformConstraints = this.transformConstraints.Items;

            for (int i = 0, n = this.transformConstraints.Count; i < n; i++)
            {
                TransformConstraint     constraint = transformConstraints[i];
                TransformConstraintData data       = constraint.data;
                constraint.mixRotate = data.mixRotate;
                constraint.mixX      = data.mixX;
                constraint.mixY      = data.mixY;
                constraint.mixScaleX = data.mixScaleX;
                constraint.mixScaleY = data.mixScaleY;
                constraint.mixShearY = data.mixShearY;
            }

            var pathConstraints = this.pathConstraints.Items;

            for (int i = 0, n = this.pathConstraints.Count; i < n; i++)
            {
                PathConstraint     constraint = pathConstraints[i];
                PathConstraintData data       = constraint.data;
                constraint.position  = data.position;
                constraint.spacing   = data.spacing;
                constraint.mixRotate = data.mixRotate;
                constraint.mixX      = data.mixX;
                constraint.mixY      = data.mixY;
            }
        }
		public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
			if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
			if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
			this.data = data;
			rotateMix = data.rotateMix;
			translateMix = data.translateMix;
			scaleMix = data.scaleMix;
			shearMix = data.shearMix;

			bones = new ExposedList<Bone>();
			foreach (BoneData boneData in data.bones)
				bones.Add (skeleton.FindBone (boneData.name));
			
			target = skeleton.FindBone(data.target.name);
		}
예제 #9
0
 static int FindTransformConstraint(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         Spine.SkeletonData            obj  = (Spine.SkeletonData)ToLua.CheckObject <Spine.SkeletonData>(L, 1);
         string                        arg0 = ToLua.CheckString(L, 2);
         Spine.TransformConstraintData o    = obj.FindTransformConstraint(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #10
0
        /// <summary>Sets the bones and constraints to their setup pose values.</summary>
        public void SetBonesToSetupPose()
        {
            var bonesItems = this.bones.Items;

            for (int i = 0, n = bones.Count; i < n; i++)
            {
                bonesItems[i].SetToSetupPose();
            }

            var ikConstraintsItems = this.ikConstraints.Items;

            for (int i = 0, n = ikConstraints.Count; i < n; i++)
            {
                IkConstraint constraint = ikConstraintsItems[i];
                constraint.mix           = constraint.data.mix;
                constraint.softness      = constraint.data.softness;
                constraint.bendDirection = constraint.data.bendDirection;
                constraint.compress      = constraint.data.compress;
                constraint.stretch       = constraint.data.stretch;
            }

            var transformConstraintsItems = this.transformConstraints.Items;

            for (int i = 0, n = transformConstraints.Count; i < n; i++)
            {
                TransformConstraint     constraint     = transformConstraintsItems[i];
                TransformConstraintData constraintData = constraint.data;
                constraint.rotateMix    = constraintData.rotateMix;
                constraint.translateMix = constraintData.translateMix;
                constraint.scaleMix     = constraintData.scaleMix;
                constraint.shearMix     = constraintData.shearMix;
            }

            var pathConstraintItems = this.pathConstraints.Items;

            for (int i = 0, n = pathConstraints.Count; i < n; i++)
            {
                PathConstraint     constraint     = pathConstraintItems[i];
                PathConstraintData constraintData = constraint.data;
                constraint.position     = constraintData.position;
                constraint.spacing      = constraintData.spacing;
                constraint.rotateMix    = constraintData.rotateMix;
                constraint.translateMix = constraintData.translateMix;
            }
        }
예제 #11
0
        public TransformConstraint(TransformConstraintData data, Skeleton skeleton)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data cannot be null.");
            }
            if (skeleton == null)
            {
                throw new ArgumentNullException("skeleton cannot be null.");
            }
            this.data    = data;
            translateMix = data.translateMix;
            x            = data.x;
            y            = data.y;

            bone   = skeleton.FindBone(data.bone.name);
            target = skeleton.FindBone(data.target.name);
        }
예제 #12
0
        // --- Transform constraints.

        /// <returns>May be null.</returns>
        public TransformConstraintData FindTransformConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <TransformConstraintData> transformConstraints = this.transformConstraints;

            for (int i = 0, n = transformConstraints.Count; i < n; i++)
            {
                TransformConstraintData transformConstraint = transformConstraints.Items[i];
                if (transformConstraint.name == constraintName)
                {
                    return(transformConstraint);
                }
            }
            return(null);
        }
예제 #13
0
		public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
			if (data == null) throw new ArgumentNullException("data cannot be null.");
			if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
			this.data = data;
			translateMix = data.translateMix;
			rotateMix = data.rotateMix;
			scaleMix = data.scaleMix;
			shearMix = data.shearMix;
			offsetRotation = data.offsetRotation;
			offsetX = data.offsetX;
			offsetY = data.offsetY;
			offsetScaleX = data.offsetScaleX;
			offsetScaleY = data.offsetScaleY;
			offsetShearY = data.offsetShearY;

			bone = skeleton.FindBone(data.bone.name);
			target = skeleton.FindBone(data.target.name);
		}
예제 #14
0
        public void SetBonesToSetupPose()
        {
            Bone[] items = bones.Items;
            int    i     = 0;

            for (int count = bones.Count; i < count; i++)
            {
                items[i].SetToSetupPose();
            }
            IkConstraint[] items2 = ikConstraints.Items;
            int            j      = 0;

            for (int count2 = ikConstraints.Count; j < count2; j++)
            {
                IkConstraint ikConstraint = items2[j];
                ikConstraint.bendDirection = ikConstraint.data.bendDirection;
                ikConstraint.mix           = ikConstraint.data.mix;
            }
            TransformConstraint[] items3 = transformConstraints.Items;
            int k = 0;

            for (int count3 = transformConstraints.Count; k < count3; k++)
            {
                TransformConstraint     transformConstraint     = items3[k];
                TransformConstraintData transformConstraintData = transformConstraint.data;
                transformConstraint.rotateMix    = transformConstraintData.rotateMix;
                transformConstraint.translateMix = transformConstraintData.translateMix;
                transformConstraint.scaleMix     = transformConstraintData.scaleMix;
                transformConstraint.shearMix     = transformConstraintData.shearMix;
            }
            PathConstraint[] items4 = pathConstraints.Items;
            int l = 0;

            for (int count4 = pathConstraints.Count; l < count4; l++)
            {
                PathConstraint     pathConstraint     = items4[l];
                PathConstraintData pathConstraintData = pathConstraint.data;
                pathConstraint.position     = pathConstraintData.position;
                pathConstraint.spacing      = pathConstraintData.spacing;
                pathConstraint.rotateMix    = pathConstraintData.rotateMix;
                pathConstraint.translateMix = pathConstraintData.translateMix;
            }
        }
예제 #15
0
        public TransformConstraintData FindTransformConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <TransformConstraintData> exposedList = transformConstraints;
            int i = 0;

            for (int count = exposedList.Count; i < count; i++)
            {
                TransformConstraintData transformConstraintData = exposedList.Items[i];
                if (transformConstraintData.name == constraintName)
                {
                    return(transformConstraintData);
                }
            }
            return(null);
        }
예제 #16
0
        public TransformConstraintData FindTransformConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <TransformConstraintData> transformConstraints = this.transformConstraints;
            int index = 0;
            int count = transformConstraints.Count;

            while (index < count)
            {
                TransformConstraintData data = transformConstraints.Items[index];
                if (data.name == constraintName)
                {
                    return(data);
                }
                index++;
            }
            return(null);
        }
 /// <summary>Copy constructor.</summary>
 public TransformConstraint(TransformConstraint constraint, Skeleton skeleton)
 {
     if (constraint == null)
     {
         throw new ArgumentNullException("constraint cannot be null.");
     }
     if (skeleton == null)
     {
         throw new ArgumentNullException("skeleton cannot be null.");
     }
     data  = constraint.data;
     bones = new ExposedList <Bone>(constraint.Bones.Count);
     foreach (Bone bone in constraint.Bones)
     {
         bones.Add(skeleton.Bones.Items[bone.data.index]);
     }
     target       = skeleton.Bones.Items[constraint.target.data.index];
     rotateMix    = constraint.rotateMix;
     translateMix = constraint.translateMix;
     scaleMix     = constraint.scaleMix;
     shearMix     = constraint.shearMix;
 }
 public TransformConstraint(TransformConstraintData data, Skeleton skeleton)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data", "data cannot be null.");
     }
     if (skeleton == null)
     {
         throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
     }
     this.data         = data;
     this.rotateMix    = data.rotateMix;
     this.translateMix = data.translateMix;
     this.scaleMix     = data.scaleMix;
     this.shearMix     = data.shearMix;
     this.bones        = new ExposedList <Bone>();
     foreach (BoneData data2 in data.bones)
     {
         this.bones.Add(skeleton.FindBone(data2.name));
     }
     this.target = skeleton.FindBone(data.target.name);
 }
예제 #19
0
 /// <summary>Copy constructor.</summary>
 public TransformConstraint(TransformConstraint constraint, Skeleton skeleton)
 {
     if (constraint == null)
     {
         throw new ArgumentNullException("constraint cannot be null.");
     }
     if (skeleton == null)
     {
         throw new ArgumentNullException("skeleton cannot be null.");
     }
     data  = constraint.data;
     bones = new ExposedList <Bone>(constraint.Bones.Count);
     foreach (Bone bone in constraint.Bones)
     {
         bones.Add(skeleton.Bones.Items[bone.data.index]);
     }
     target    = skeleton.Bones.Items[constraint.target.data.index];
     mixRotate = constraint.mixRotate;
     mixX      = constraint.mixX;
     mixY      = constraint.mixY;
     mixScaleX = constraint.mixScaleX;
     mixScaleY = constraint.mixScaleY;
     mixShearY = constraint.mixShearY;
 }
예제 #20
0
        private void ReadAnimation(String name, Stream input, SkeletonData skeletonData)
        {
            var   timelines = new ExposedList <Timeline>();
            float scale     = Scale;
            float duration  = 0;

            // Slot timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                int slotIndex = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    int timelineType = input.ReadByte();
                    int frameCount   = ReadVarint(input, true);
                    switch (timelineType)
                    {
                    case TIMELINE_COLOR: {
                        ColorTimeline timeline = new ColorTimeline(frameCount);
                        timeline.slotIndex = slotIndex;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            float time  = ReadFloat(input);
                            int   color = ReadInt(input);
                            float r     = ((color & 0xff000000) >> 24) / 255f;
                            float g     = ((color & 0x00ff0000) >> 16) / 255f;
                            float b     = ((color & 0x0000ff00) >> 8) / 255f;
                            float a     = ((color & 0x000000ff)) / 255f;
                            timeline.SetFrame(frameIndex, time, r, g, b, a);
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[frameCount * 5 - 5]);
                        break;
                    }

                    case TIMELINE_ATTACHMENT: {
                        AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
                        timeline.slotIndex = slotIndex;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, ReadFloat(input), ReadString(input));
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[frameCount - 1]);
                        break;
                    }
                    }
                }
            }

            // Bone timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                int boneIndex = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    int timelineType = input.ReadByte();
                    int frameCount   = ReadVarint(input, true);
                    switch (timelineType)
                    {
                    case TIMELINE_ROTATE: {
                        RotateTimeline timeline = new RotateTimeline(frameCount);
                        timeline.boneIndex = boneIndex;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input));
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[frameCount * 2 - 2]);
                        break;
                    }

                    case TIMELINE_TRANSLATE:
                    case TIMELINE_SCALE:
                    case TIMELINE_SHEAR: {
                        TranslateTimeline timeline;
                        float             timelineScale = 1;
                        if (timelineType == TIMELINE_SCALE)
                        {
                            timeline = new ScaleTimeline(frameCount);
                        }
                        else if (timelineType == TIMELINE_SHEAR)
                        {
                            timeline = new ShearTimeline(frameCount);
                        }
                        else
                        {
                            timeline      = new TranslateTimeline(frameCount);
                            timelineScale = scale;
                        }
                        timeline.boneIndex = boneIndex;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input) * timelineScale, ReadFloat(input)
                                              * timelineScale);
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[frameCount * 3 - 3]);
                        break;
                    }
                    }
                }
            }

            // IK timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                IkConstraintData constraint = skeletonData.ikConstraints.Items[ReadVarint(input, true)];
                int frameCount = ReadVarint(input, true);
                IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount);
                timeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(constraint);
                for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                {
                    timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadSByte(input));
                    if (frameIndex < frameCount - 1)
                    {
                        ReadCurve(input, frameIndex, timeline);
                    }
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[frameCount * 3 - 3]);
            }

            // Transform constraint timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                TransformConstraintData constraint = skeletonData.transformConstraints.Items[ReadVarint(input, true)];
                int frameCount = ReadVarint(input, true);
                TransformConstraintTimeline timeline = new TransformConstraintTimeline(frameCount);
                timeline.transformConstraintIndex = skeletonData.transformConstraints.IndexOf(constraint);
                for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                {
                    timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input));
                    if (frameIndex < frameCount - 1)
                    {
                        ReadCurve(input, frameIndex, timeline);
                    }
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[frameCount * 5 - 5]);
            }

            // FFD timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                Skin skin = skeletonData.skins.Items[ReadVarint(input, true)];
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    int slotIndex = ReadVarint(input, true);
                    for (int iii = 0, nnn = ReadVarint(input, true); iii < nnn; iii++)
                    {
                        Attachment  attachment = skin.GetAttachment(slotIndex, ReadString(input));
                        int         frameCount = ReadVarint(input, true);
                        FfdTimeline timeline   = new FfdTimeline(frameCount);
                        timeline.slotIndex  = slotIndex;
                        timeline.attachment = attachment;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            float time = ReadFloat(input);

                            float[] vertices;
                            int     vertexCount;
                            if (attachment is MeshAttachment)
                            {
                                vertexCount = ((MeshAttachment)attachment).vertices.Length;
                            }
                            else
                            {
                                vertexCount = ((WeightedMeshAttachment)attachment).weights.Length / 3 * 2;
                            }

                            int end = ReadVarint(input, true);
                            if (end == 0)
                            {
                                if (attachment is MeshAttachment)
                                {
                                    vertices = ((MeshAttachment)attachment).vertices;
                                }
                                else
                                {
                                    vertices = new float[vertexCount];
                                }
                            }
                            else
                            {
                                vertices = new float[vertexCount];
                                int start = ReadVarint(input, true);
                                end += start;
                                if (scale == 1)
                                {
                                    for (int v = start; v < end; v++)
                                    {
                                        vertices[v] = ReadFloat(input);
                                    }
                                }
                                else
                                {
                                    for (int v = start; v < end; v++)
                                    {
                                        vertices[v] = ReadFloat(input) * scale;
                                    }
                                }
                                if (attachment is MeshAttachment)
                                {
                                    float[] meshVertices = ((MeshAttachment)attachment).vertices;
                                    for (int v = 0, vn = vertices.Length; v < vn; v++)
                                    {
                                        vertices[v] += meshVertices[v];
                                    }
                                }
                            }

                            timeline.SetFrame(frameIndex, time, vertices);
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[frameCount - 1]);
                    }
                }
            }

            // Draw order timeline.
            int drawOrderCount = ReadVarint(input, true);

            if (drawOrderCount > 0)
            {
                DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
                int slotCount = skeletonData.slots.Count;
                for (int i = 0; i < drawOrderCount; i++)
                {
                    float time        = ReadFloat(input);
                    int   offsetCount = ReadVarint(input, true);
                    int[] drawOrder   = new int[slotCount];
                    for (int ii = slotCount - 1; ii >= 0; ii--)
                    {
                        drawOrder[ii] = -1;
                    }
                    int[] unchanged = new int[slotCount - offsetCount];
                    int   originalIndex = 0, unchangedIndex = 0;
                    for (int ii = 0; ii < offsetCount; ii++)
                    {
                        int slotIndex = ReadVarint(input, true);
                        // Collect unchanged items.
                        while (originalIndex != slotIndex)
                        {
                            unchanged[unchangedIndex++] = originalIndex++;
                        }
                        // Set changed items.
                        drawOrder[originalIndex + ReadVarint(input, true)] = originalIndex++;
                    }
                    // Collect remaining unchanged items.
                    while (originalIndex < slotCount)
                    {
                        unchanged[unchangedIndex++] = originalIndex++;
                    }
                    // Fill in unchanged items.
                    for (int ii = slotCount - 1; ii >= 0; ii--)
                    {
                        if (drawOrder[ii] == -1)
                        {
                            drawOrder[ii] = unchanged[--unchangedIndex];
                        }
                    }
                    timeline.SetFrame(i, time, drawOrder);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[drawOrderCount - 1]);
            }

            // Event timeline.
            int eventCount = ReadVarint(input, true);

            if (eventCount > 0)
            {
                EventTimeline timeline = new EventTimeline(eventCount);
                for (int i = 0; i < eventCount; i++)
                {
                    float     time      = ReadFloat(input);
                    EventData eventData = skeletonData.events.Items[ReadVarint(input, true)];
                    Event     e         = new Event(time, eventData);
                    e.Int    = ReadVarint(input, false);
                    e.Float  = ReadFloat(input);
                    e.String = ReadBoolean(input) ? ReadString(input) : eventData.String;
                    timeline.SetFrame(i, e);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[eventCount - 1]);
            }

            timelines.TrimExcess();
            skeletonData.animations.Add(new Animation(name, timelines, duration));
        }
예제 #21
0
        public SkeletonData ReadSkeletonData(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            float scale = Scale;

            var skeletonData = new SkeletonData();

            skeletonData.hash = ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = ReadFloat(input);
            skeletonData.height = ReadFloat(input);

            bool nonessential = ReadBoolean(input);

            if (nonessential)
            {
                skeletonData.imagesPath = ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }

            // Bones.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   name     = ReadString(input);
                BoneData parent   = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)];
                BoneData boneData = new BoneData(name, parent);
                boneData.rotation        = ReadFloat(input);
                boneData.x               = ReadFloat(input) * scale;
                boneData.y               = ReadFloat(input) * scale;
                boneData.scaleX          = ReadFloat(input);
                boneData.scaleY          = ReadFloat(input);
                boneData.shearX          = ReadFloat(input);
                boneData.shearY          = ReadFloat(input);
                boneData.length          = ReadFloat(input) * scale;
                boneData.inheritRotation = ReadBoolean(input);
                boneData.inheritScale    = ReadBoolean(input);
                if (nonessential)
                {
                    ReadInt(input);                               // Skip bone color.
                }
                skeletonData.bones.Add(boneData);
            }

            // IK constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input));
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                ikConstraintData.target        = skeletonData.bones.Items[ReadVarint(input, true)];
                ikConstraintData.mix           = ReadFloat(input);
                ikConstraintData.bendDirection = ReadSByte(input);
                skeletonData.ikConstraints.Add(ikConstraintData);
            }

            // Transform constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input));
                transformConstraintData.bone           = skeletonData.bones.Items[ReadVarint(input, true)];
                transformConstraintData.target         = skeletonData.bones.Items[ReadVarint(input, true)];
                transformConstraintData.offsetRotation = ReadFloat(input);
                transformConstraintData.offsetX        = ReadFloat(input) * scale;
                transformConstraintData.offsetY        = ReadFloat(input) * scale;
                transformConstraintData.offsetScaleX   = ReadFloat(input);
                transformConstraintData.offsetScaleY   = ReadFloat(input);
                transformConstraintData.offsetShearY   = ReadFloat(input);
                transformConstraintData.rotateMix      = ReadFloat(input);
                transformConstraintData.translateMix   = ReadFloat(input);
                transformConstraintData.scaleMix       = ReadFloat(input);
                transformConstraintData.shearMix       = ReadFloat(input);
                skeletonData.transformConstraints.Add(transformConstraintData);
            }

            // Slots.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   slotName = ReadString(input);
                BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)];
                SlotData slotData = new SlotData(slotName, boneData);
                int      color    = ReadInt(input);
                slotData.r = ((color & 0xff000000) >> 24) / 255f;
                slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
                slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
                slotData.a = ((color & 0x000000ff)) / 255f;
                slotData.attachmentName = ReadString(input);
                slotData.blendMode      = (BlendMode)ReadVarint(input, true);
                skeletonData.slots.Add(slotData);
            }

            // Default skin.
            Skin defaultSkin = ReadSkin(input, "default", nonessential);

            if (defaultSkin != null)
            {
                skeletonData.defaultSkin = defaultSkin;
                skeletonData.skins.Add(defaultSkin);
            }

            // Skins.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));
            }

            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
                Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null)
                {
                    throw new Exception("Skin not found: " + linkedMesh.skin);
                }
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                if (linkedMesh.mesh is MeshAttachment)
                {
                    MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh;
                    mesh.ParentMesh = (MeshAttachment)parent;
                    mesh.UpdateUVs();
                }
                else
                {
                    WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh;
                    mesh.ParentMesh = (WeightedMeshAttachment)parent;
                    mesh.UpdateUVs();
                }
            }
            linkedMeshes.Clear();

            // Events.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                EventData eventData = new EventData(ReadString(input));
                eventData.Int    = ReadVarint(input, false);
                eventData.Float  = ReadFloat(input);
                eventData.String = ReadString(input);
                skeletonData.events.Add(eventData);
            }

            // Animations.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                ReadAnimation(ReadString(input), input, skeletonData);
            }

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #22
0
        private void ReadAnimation(String name, Dictionary <String, Object> map, SkeletonData skeletonData)
        {
            var   timelines = new ExposedList <Timeline>();
            float duration  = 0;
            var   scale     = this.Scale;

            if (map.ContainsKey("slots"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["slots"])
                {
                    String slotName    = entry.Key;
                    int    slotIndex   = skeletonData.FindSlotIndex(slotName);
                    var    timelineMap = (Dictionary <String, Object>)entry.Value;

                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "color")
                        {
                            var timeline = new ColorTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float  time = (float)valueMap["time"];
                                String c    = (String)valueMap["color"];
                                timeline.SetFrame(frameIndex, time, ToColor(c, 0), ToColor(c, 1), ToColor(c, 2), ToColor(c, 3));
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 5 - 5]);
                        }
                        else if (timelineName == "attachment")
                        {
                            var timeline = new AttachmentTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex++, time, (String)valueMap["name"]);
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
                        }
                    }
                }
            }

            if (map.ContainsKey("bones"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["bones"])
                {
                    String boneName  = entry.Key;
                    int    boneIndex = skeletonData.FindBoneIndex(boneName);
                    if (boneIndex == -1)
                    {
                        throw new Exception("Bone not found: " + boneName);
                    }

                    var timelineMap = (Dictionary <String, Object>)entry.Value;
                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "rotate")
                        {
                            var timeline = new RotateTimeline(values.Count);
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex, time, (float)valueMap["angle"]);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 2 - 2]);
                        }
                        else if (timelineName == "translate" || timelineName == "scale" || timelineName == "shear")
                        {
                            TranslateTimeline timeline;
                            float             timelineScale = 1;
                            if (timelineName == "scale")
                            {
                                timeline = new ScaleTimeline(values.Count);
                            }
                            else if (timelineName == "shear")
                            {
                                timeline = new ShearTimeline(values.Count);
                            }
                            else
                            {
                                timeline      = new TranslateTimeline(values.Count);
                                timelineScale = scale;
                            }
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                float x    = GetFloat(valueMap, "x", 0);
                                float y    = GetFloat(valueMap, "y", 0);
                                timeline.SetFrame(frameIndex, time, (float)x * timelineScale, (float)y * timelineScale);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 3 - 3]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
                        }
                    }
                }
            }

            // IK timelines.
            if (map.ContainsKey("ik"))
            {
                foreach (KeyValuePair <String, Object> constraintMap in (Dictionary <String, Object>)map["ik"])
                {
                    IkConstraintData constraint = skeletonData.FindIkConstraint(constraintMap.Key);
                    var values   = (List <Object>)constraintMap.Value;
                    var timeline = new IkConstraintTimeline(values.Count);
                    timeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary <String, Object> valueMap in values)
                    {
                        float time         = (float)valueMap["time"];
                        float mix          = GetFloat(valueMap, "mix", 1);
                        bool  bendPositive = GetBoolean(valueMap, "bendPositive", true);
                        timeline.SetFrame(frameIndex, time, mix, bendPositive ? 1 : -1);
                        ReadCurve(timeline, frameIndex, valueMap);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 3 - 3]);
                }
            }

            // Transform constraint timelines.
            if (map.ContainsKey("transform"))
            {
                foreach (KeyValuePair <String, Object> constraintMap in (Dictionary <String, Object>)map["transform"])
                {
                    TransformConstraintData constraint = skeletonData.FindTransformConstraint(constraintMap.Key);
                    var values   = (List <Object>)constraintMap.Value;
                    var timeline = new TransformConstraintTimeline(values.Count);
                    timeline.transformConstraintIndex = skeletonData.transformConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary <String, Object> valueMap in values)
                    {
                        float time         = (float)valueMap["time"];
                        float rotateMix    = GetFloat(valueMap, "rotateMix", 1);
                        float translateMix = GetFloat(valueMap, "translateMix", 1);
                        float scaleMix     = GetFloat(valueMap, "scaleMix", 1);
                        float shearMix     = GetFloat(valueMap, "shearMix", 1);
                        timeline.SetFrame(frameIndex, time, rotateMix, translateMix, scaleMix, shearMix);
                        ReadCurve(timeline, frameIndex, valueMap);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 5 - 5]);
                }
            }

            // FFD timelines.
            if (map.ContainsKey("ffd"))
            {
                foreach (KeyValuePair <String, Object> ffdMap in (Dictionary <String, Object>)map["ffd"])
                {
                    Skin skin = skeletonData.FindSkin(ffdMap.Key);
                    foreach (KeyValuePair <String, Object> slotMap in (Dictionary <String, Object>)ffdMap.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(slotMap.Key);
                        foreach (KeyValuePair <String, Object> meshMap in (Dictionary <String, Object>)slotMap.Value)
                        {
                            var        values     = (List <Object>)meshMap.Value;
                            var        timeline   = new FfdTimeline(values.Count);
                            Attachment attachment = skin.GetAttachment(slotIndex, meshMap.Key);
                            if (attachment == null)
                            {
                                throw new Exception("FFD attachment not found: " + meshMap.Key);
                            }
                            timeline.slotIndex  = slotIndex;
                            timeline.attachment = attachment;

                            int vertexCount;
                            if (attachment is MeshAttachment)
                            {
                                vertexCount = ((MeshAttachment)attachment).vertices.Length;
                            }
                            else
                            {
                                vertexCount = ((WeightedMeshAttachment)attachment).Weights.Length / 3 * 2;
                            }

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float[] vertices;
                                if (!valueMap.ContainsKey("vertices"))
                                {
                                    if (attachment is MeshAttachment)
                                    {
                                        vertices = ((MeshAttachment)attachment).vertices;
                                    }
                                    else
                                    {
                                        vertices = new float[vertexCount];
                                    }
                                }
                                else
                                {
                                    var verticesValue = (List <Object>)valueMap["vertices"];
                                    vertices = new float[vertexCount];
                                    int start = GetInt(valueMap, "offset", 0);
                                    if (scale == 1)
                                    {
                                        for (int i = 0, n = verticesValue.Count; i < n; i++)
                                        {
                                            vertices[i + start] = (float)verticesValue[i];
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0, n = verticesValue.Count; i < n; i++)
                                        {
                                            vertices[i + start] = (float)verticesValue[i] * scale;
                                        }
                                    }
                                    if (attachment is MeshAttachment)
                                    {
                                        float[] meshVertices = ((MeshAttachment)attachment).vertices;
                                        for (int i = 0; i < vertexCount; i++)
                                        {
                                            vertices[i] += meshVertices[i];
                                        }
                                    }
                                }

                                timeline.SetFrame(frameIndex, (float)valueMap["time"], vertices);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                    }
                }
            }

            if (map.ContainsKey("drawOrder") || map.ContainsKey("draworder"))
            {
                var values     = (List <Object>)map[map.ContainsKey("drawOrder") ? "drawOrder" : "draworder"];
                var timeline   = new DrawOrderTimeline(values.Count);
                int slotCount  = skeletonData.slots.Count;
                int frameIndex = 0;
                foreach (Dictionary <String, Object> drawOrderMap in values)
                {
                    int[] drawOrder = null;
                    if (drawOrderMap.ContainsKey("offsets"))
                    {
                        drawOrder = new int[slotCount];
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            drawOrder[i] = -1;
                        }
                        var   offsets = (List <Object>)drawOrderMap["offsets"];
                        int[] unchanged = new int[slotCount - offsets.Count];
                        int   originalIndex = 0, unchangedIndex = 0;
                        foreach (Dictionary <String, Object> offsetMap in offsets)
                        {
                            int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
                            if (slotIndex == -1)
                            {
                                throw new Exception("Slot not found: " + offsetMap["slot"]);
                            }
                            // Collect unchanged items.
                            while (originalIndex != slotIndex)
                            {
                                unchanged[unchangedIndex++] = originalIndex++;
                            }
                            // Set changed items.
                            int index = originalIndex + (int)(float)offsetMap["offset"];
                            drawOrder[index] = originalIndex++;
                        }
                        // Collect remaining unchanged items.
                        while (originalIndex < slotCount)
                        {
                            unchanged[unchangedIndex++] = originalIndex++;
                        }
                        // Fill in unchanged items.
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            if (drawOrder[i] == -1)
                            {
                                drawOrder[i] = unchanged[--unchangedIndex];
                            }
                        }
                    }
                    timeline.SetFrame(frameIndex++, (float)drawOrderMap["time"], drawOrder);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            if (map.ContainsKey("events"))
            {
                var eventsMap  = (List <Object>)map["events"];
                var timeline   = new EventTimeline(eventsMap.Count);
                int frameIndex = 0;
                foreach (Dictionary <String, Object> eventMap in eventsMap)
                {
                    EventData eventData = skeletonData.FindEvent((String)eventMap["name"]);
                    if (eventData == null)
                    {
                        throw new Exception("Event not found: " + eventMap["name"]);
                    }
                    var e = new Event((float)eventMap["time"], eventData);
                    e.Int    = GetInt(eventMap, "int", eventData.Int);
                    e.Float  = GetFloat(eventMap, "float", eventData.Float);
                    e.String = GetString(eventMap, "string", eventData.String);
                    timeline.SetFrame(frameIndex++, e);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            timelines.TrimExcess();
            skeletonData.animations.Add(new Animation(name, timelines, duration));
        }
예제 #23
0
        internal static void SetPropertyToSetupPose(this Skeleton skeleton, int propertyID)
        {
            int          num          = propertyID >> 24;
            TimelineType timelineType = (TimelineType)num;
            int          num2         = propertyID - (num << 24);

            switch (timelineType)
            {
            case TimelineType.Event:
                break;

            case TimelineType.Rotate:
            {
                Bone bone = skeleton.bones.Items[num2];
                bone.rotation = bone.data.rotation;
                break;
            }

            case TimelineType.Translate:
            {
                Bone bone = skeleton.bones.Items[num2];
                bone.x = bone.data.x;
                bone.y = bone.data.y;
                break;
            }

            case TimelineType.Scale:
            {
                Bone bone = skeleton.bones.Items[num2];
                bone.scaleX = bone.data.scaleX;
                bone.scaleY = bone.data.scaleY;
                break;
            }

            case TimelineType.Shear:
            {
                Bone bone = skeleton.bones.Items[num2];
                bone.shearX = bone.data.shearX;
                bone.shearY = bone.data.shearY;
                break;
            }

            case TimelineType.Attachment:
                skeleton.SetSlotAttachmentToSetupPose(num2);
                break;

            case TimelineType.Color:
                skeleton.slots.Items[num2].SetColorToSetupPose();
                break;

            case TimelineType.TwoColor:
                skeleton.slots.Items[num2].SetColorToSetupPose();
                break;

            case TimelineType.Deform:
                skeleton.slots.Items[num2].attachmentVertices.Clear();
                break;

            case TimelineType.DrawOrder:
                skeleton.SetDrawOrderToSetupPose();
                break;

            case TimelineType.IkConstraint:
            {
                IkConstraint ikConstraint = skeleton.ikConstraints.Items[num2];
                ikConstraint.mix           = ikConstraint.data.mix;
                ikConstraint.bendDirection = ikConstraint.data.bendDirection;
                break;
            }

            case TimelineType.TransformConstraint:
            {
                TransformConstraint     transformConstraint = skeleton.transformConstraints.Items[num2];
                TransformConstraintData data = transformConstraint.data;
                transformConstraint.rotateMix    = data.rotateMix;
                transformConstraint.translateMix = data.translateMix;
                transformConstraint.scaleMix     = data.scaleMix;
                transformConstraint.shearMix     = data.shearMix;
                break;
            }

            case TimelineType.PathConstraintPosition:
            {
                PathConstraint pathConstraint = skeleton.pathConstraints.Items[num2];
                pathConstraint.position = pathConstraint.data.position;
                break;
            }

            case TimelineType.PathConstraintSpacing:
            {
                PathConstraint pathConstraint = skeleton.pathConstraints.Items[num2];
                pathConstraint.spacing = pathConstraint.data.spacing;
                break;
            }

            case TimelineType.PathConstraintMix:
            {
                PathConstraint pathConstraint = skeleton.pathConstraints.Items[num2];
                pathConstraint.rotateMix    = pathConstraint.data.rotateMix;
                pathConstraint.translateMix = pathConstraint.data.translateMix;
                break;
            }
            }
        }
예제 #24
0
        public SkeletonData ReadSkeletonData(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            float        scale        = Scale;
            SkeletonData skeletonData = new SkeletonData();

            skeletonData.hash = ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = ReadFloat(input);
            skeletonData.height = ReadFloat(input);
            bool flag = ReadBoolean(input);

            if (flag)
            {
                skeletonData.fps        = ReadFloat(input);
                skeletonData.imagesPath = ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }
            int i = 0;

            for (int num = ReadVarint(input, optimizePositive: true); i < num; i++)
            {
                string   name     = ReadString(input);
                BoneData parent   = (i != 0) ? skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)] : null;
                BoneData boneData = new BoneData(i, name, parent);
                boneData.rotation      = ReadFloat(input);
                boneData.x             = ReadFloat(input) * scale;
                boneData.y             = ReadFloat(input) * scale;
                boneData.scaleX        = ReadFloat(input);
                boneData.scaleY        = ReadFloat(input);
                boneData.shearX        = ReadFloat(input);
                boneData.shearY        = ReadFloat(input);
                boneData.length        = ReadFloat(input) * scale;
                boneData.transformMode = TransformModeValues[ReadVarint(input, optimizePositive: true)];
                if (flag)
                {
                    ReadInt(input);
                }
                skeletonData.bones.Add(boneData);
            }
            int j = 0;

            for (int num2 = ReadVarint(input, optimizePositive: true); j < num2; j++)
            {
                string   name2     = ReadString(input);
                BoneData boneData2 = skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)];
                SlotData slotData  = new SlotData(j, name2, boneData2);
                int      num3      = ReadInt(input);
                slotData.r = (float)((num3 & 4278190080u) >> 24) / 255f;
                slotData.g = (float)((num3 & 0xFF0000) >> 16) / 255f;
                slotData.b = (float)((num3 & 0xFF00) >> 8) / 255f;
                slotData.a = (float)(num3 & 0xFF) / 255f;
                int num4 = ReadInt(input);
                if (num4 != -1)
                {
                    slotData.hasSecondColor = true;
                    slotData.r2             = (float)((num4 & 0xFF0000) >> 16) / 255f;
                    slotData.g2             = (float)((num4 & 0xFF00) >> 8) / 255f;
                    slotData.b2             = (float)(num4 & 0xFF) / 255f;
                }
                slotData.attachmentName = ReadString(input);
                slotData.blendMode      = (BlendMode)ReadVarint(input, optimizePositive: true);
                skeletonData.slots.Add(slotData);
            }
            int k = 0;

            for (int num5 = ReadVarint(input, optimizePositive: true); k < num5; k++)
            {
                IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input));
                ikConstraintData.order = ReadVarint(input, optimizePositive: true);
                int l = 0;
                for (int num6 = ReadVarint(input, optimizePositive: true); l < num6; l++)
                {
                    ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)]);
                }
                ikConstraintData.target        = skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)];
                ikConstraintData.mix           = ReadFloat(input);
                ikConstraintData.bendDirection = ReadSByte(input);
                skeletonData.ikConstraints.Add(ikConstraintData);
            }
            int m = 0;

            for (int num7 = ReadVarint(input, optimizePositive: true); m < num7; m++)
            {
                TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input));
                transformConstraintData.order = ReadVarint(input, optimizePositive: true);
                int n = 0;
                for (int num8 = ReadVarint(input, optimizePositive: true); n < num8; n++)
                {
                    transformConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)]);
                }
                transformConstraintData.target         = skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)];
                transformConstraintData.local          = ReadBoolean(input);
                transformConstraintData.relative       = ReadBoolean(input);
                transformConstraintData.offsetRotation = ReadFloat(input);
                transformConstraintData.offsetX        = ReadFloat(input) * scale;
                transformConstraintData.offsetY        = ReadFloat(input) * scale;
                transformConstraintData.offsetScaleX   = ReadFloat(input);
                transformConstraintData.offsetScaleY   = ReadFloat(input);
                transformConstraintData.offsetShearY   = ReadFloat(input);
                transformConstraintData.rotateMix      = ReadFloat(input);
                transformConstraintData.translateMix   = ReadFloat(input);
                transformConstraintData.scaleMix       = ReadFloat(input);
                transformConstraintData.shearMix       = ReadFloat(input);
                skeletonData.transformConstraints.Add(transformConstraintData);
            }
            int num9 = 0;

            for (int num10 = ReadVarint(input, optimizePositive: true); num9 < num10; num9++)
            {
                PathConstraintData pathConstraintData = new PathConstraintData(ReadString(input));
                pathConstraintData.order = ReadVarint(input, optimizePositive: true);
                int num11 = 0;
                for (int num12 = ReadVarint(input, optimizePositive: true); num11 < num12; num11++)
                {
                    pathConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, optimizePositive: true)]);
                }
                pathConstraintData.target         = skeletonData.slots.Items[ReadVarint(input, optimizePositive: true)];
                pathConstraintData.positionMode   = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, optimizePositive: true));
                pathConstraintData.spacingMode    = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, optimizePositive: true));
                pathConstraintData.rotateMode     = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, optimizePositive: true));
                pathConstraintData.offsetRotation = ReadFloat(input);
                pathConstraintData.position       = ReadFloat(input);
                if (pathConstraintData.positionMode == PositionMode.Fixed)
                {
                    pathConstraintData.position *= scale;
                }
                pathConstraintData.spacing = ReadFloat(input);
                if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                {
                    pathConstraintData.spacing *= scale;
                }
                pathConstraintData.rotateMix    = ReadFloat(input);
                pathConstraintData.translateMix = ReadFloat(input);
                skeletonData.pathConstraints.Add(pathConstraintData);
            }
            Skin skin = ReadSkin(input, skeletonData, "default", flag);

            if (skin != null)
            {
                skeletonData.defaultSkin = skin;
                skeletonData.skins.Add(skin);
            }
            int num13 = 0;

            for (int num14 = ReadVarint(input, optimizePositive: true); num13 < num14; num13++)
            {
                skeletonData.skins.Add(ReadSkin(input, skeletonData, ReadString(input), flag));
            }
            int num15 = 0;

            for (int count = linkedMeshes.Count; num15 < count; num15++)
            {
                SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[num15];
                Skin skin2 = (linkedMesh.skin != null) ? skeletonData.FindSkin(linkedMesh.skin) : skeletonData.DefaultSkin;
                if (skin2 == null)
                {
                    throw new Exception("Skin not found: " + linkedMesh.skin);
                }
                Attachment attachment = skin2.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (attachment == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)attachment;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();
            int num16 = 0;

            for (int num17 = ReadVarint(input, optimizePositive: true); num16 < num17; num16++)
            {
                EventData eventData = new EventData(ReadString(input));
                eventData.Int    = ReadVarint(input, optimizePositive: false);
                eventData.Float  = ReadFloat(input);
                eventData.String = ReadString(input);
                skeletonData.events.Add(eventData);
            }
            int num18 = 0;

            for (int num19 = ReadVarint(input, optimizePositive: true); num18 < num19; num18++)
            {
                ReadAnimation(ReadString(input), input, skeletonData);
            }
            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            skeletonData.pathConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #25
0
		public SkeletonData ReadSkeletonData (TextReader reader) {
			if (reader == null) throw new ArgumentNullException("reader cannot be null.");

			var skeletonData = new SkeletonData();

			var root = Json.Deserialize(reader) as Dictionary<String, Object>;
			if (root == null) throw new Exception("Invalid JSON.");

			// Skeleton.
			if (root.ContainsKey("skeleton")) {
				var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
				skeletonData.hash = (String)skeletonMap["hash"];
				skeletonData.version = (String)skeletonMap["spine"];
				skeletonData.width = GetFloat(skeletonMap, "width", 0);
				skeletonData.height = GetFloat(skeletonMap, "height", 0);
			}

			// Bones.
			foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) {
				BoneData parent = null;
				if (boneMap.ContainsKey("parent")) {
					parent = skeletonData.FindBone((String)boneMap["parent"]);
					if (parent == null)
						throw new Exception("Parent bone not found: " + boneMap["parent"]);
				}
				var boneData = new BoneData((String)boneMap["name"], parent);
				boneData.length = GetFloat(boneMap, "length", 0) * Scale;
				boneData.x = GetFloat(boneMap, "x", 0) * Scale;
				boneData.y = GetFloat(boneMap, "y", 0) * Scale;
				boneData.rotation = GetFloat(boneMap, "rotation", 0);
				boneData.scaleX = GetFloat(boneMap, "scaleX", 1);
				boneData.scaleY = GetFloat(boneMap, "scaleY", 1);
				boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true);
				boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true);
				skeletonData.bones.Add(boneData);
			}

			// IK constraints.
			if (root.ContainsKey("ik")) {
				foreach (Dictionary<String, Object> ikMap in (List<Object>)root["ik"]) {
					IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]);

					foreach (String boneName in (List<Object>)ikMap["bones"]) {
						BoneData bone = skeletonData.FindBone(boneName);
						if (bone == null) throw new Exception("IK bone not found: " + boneName);
						ikConstraintData.bones.Add(bone);
					}

					String targetName = (String)ikMap["target"];
					ikConstraintData.target = skeletonData.FindBone(targetName);
					if (ikConstraintData.target == null) throw new Exception("Target bone not found: " + targetName);

					ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1;
					ikConstraintData.mix = GetFloat(ikMap, "mix", 1);

					skeletonData.ikConstraints.Add(ikConstraintData);
				}
			}

			// Transform constraints.
			if (root.ContainsKey("transform")) {
				foreach (Dictionary<String, Object> transformMap in (List<Object>)root["ik"]) {
					TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]);

					String boneName = (String)transformMap["bone"];
					transformConstraintData.target = skeletonData.FindBone(boneName);
					if (transformConstraintData.target == null) throw new Exception("Bone not found: " + boneName);

					String targetName = (String)transformMap["target"];
					transformConstraintData.target = skeletonData.FindBone(targetName);
					if (transformConstraintData.target == null) throw new Exception("Target bone not found: " + targetName);

					transformConstraintData.translateMix = GetFloat(transformMap, "mix", 1);
					transformConstraintData.x = GetFloat(transformMap, "x", 0);
					transformConstraintData.y = GetFloat(transformMap, "y", 0);

					skeletonData.transformConstraints.Add(transformConstraintData);
				}
			}

			// Slots.
			if (root.ContainsKey("slots")) {
				foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) {
					var slotName = (String)slotMap["name"];
					var boneName = (String)slotMap["bone"];
					BoneData boneData = skeletonData.FindBone(boneName);
					if (boneData == null)
						throw new Exception("Slot bone not found: " + boneName);
					var slotData = new SlotData(slotName, boneData);

					if (slotMap.ContainsKey("color")) {
						var color = (String)slotMap["color"];
						slotData.r = ToColor(color, 0);
						slotData.g = ToColor(color, 1);
						slotData.b = ToColor(color, 2);
						slotData.a = ToColor(color, 3);
					}

					if (slotMap.ContainsKey("attachment"))
						slotData.attachmentName = (String)slotMap["attachment"];

					if (slotMap.ContainsKey("blend"))
						slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false);
					else
						slotData.blendMode = BlendMode.normal;

					skeletonData.slots.Add(slotData);
				}
			}

			// Skins.
			if (root.ContainsKey("skins")) {
				foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) {
					var skin = new Skin(entry.Key);
					foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) {
						int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key);
						foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) {
							Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value);
							if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment);
						}
					}
					skeletonData.skins.Add(skin);
					if (skin.name == "default")
						skeletonData.defaultSkin = skin;
				}
			}

			// Events.
			if (root.ContainsKey("events")) {
				foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) {
					var entryMap = (Dictionary<String, Object>)entry.Value;
					var eventData = new EventData(entry.Key);
					eventData.Int = GetInt(entryMap, "int", 0);
					eventData.Float = GetFloat(entryMap, "float", 0);
					eventData.String = GetString(entryMap, "string", null);
					skeletonData.events.Add(eventData);
				}
			}

			// Animations.
			if (root.ContainsKey("animations")) {
				foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"])
					ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData);
			}

			skeletonData.bones.TrimExcess();
			skeletonData.slots.TrimExcess();
			skeletonData.skins.TrimExcess();
			skeletonData.events.TrimExcess();
			skeletonData.animations.TrimExcess();
			skeletonData.ikConstraints.TrimExcess();
			return skeletonData;
		}
예제 #26
0
        public SkeletonData ReadSkeletonData(Stream input, String skelPath, String animPath, int limitCount = 0, bool backUp = true)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            float scale = Scale;

            var skeletonData = new SkeletonData();

            skeletonData.hash = ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = ReadFloat(input);
            skeletonData.height = ReadFloat(input);

            bool nonessential = ReadBoolean(input);

            if (nonessential)
            {
                skeletonData.fps        = ReadFloat(input);
                skeletonData.imagesPath = ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }

            // Bones.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   name   = ReadString(input);
                BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)];
                BoneData data   = new BoneData(i, name, parent);
                data.rotation      = ReadFloat(input);
                data.x             = ReadFloat(input) * scale;
                data.y             = ReadFloat(input) * scale;
                data.scaleX        = ReadFloat(input);
                data.scaleY        = ReadFloat(input);
                data.shearX        = ReadFloat(input);
                data.shearY        = ReadFloat(input);
                data.length        = ReadFloat(input) * scale;
                data.transformMode = TransformModeValues[ReadVarint(input, true)];
                if (nonessential)
                {
                    ReadInt(input);               // Skip bone color.
                }
                skeletonData.bones.Add(data);
            }

            // Slots.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   slotName = ReadString(input);
                BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)];
                SlotData slotData = new SlotData(i, slotName, boneData);
                int      color    = ReadInt(input);
                slotData.r = ((color & 0xff000000) >> 24) / 255f;
                slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
                slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
                slotData.a = ((color & 0x000000ff)) / 255f;
                slotData.attachmentName = ReadString(input);
                slotData.blendMode      = (BlendMode)ReadVarint(input, true);
                skeletonData.slots.Add(slotData);
            }

            // IK constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                IkConstraintData data = new IkConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target        = skeletonData.bones.Items[ReadVarint(input, true)];
                data.mix           = ReadFloat(input);
                data.bendDirection = ReadSByte(input);
                skeletonData.ikConstraints.Add(data);
            }

            // Transform constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                TransformConstraintData data = new TransformConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target         = skeletonData.bones.Items[ReadVarint(input, true)];
                data.offsetRotation = ReadFloat(input);
                data.offsetX        = ReadFloat(input) * scale;
                data.offsetY        = ReadFloat(input) * scale;
                data.offsetScaleX   = ReadFloat(input);
                data.offsetScaleY   = ReadFloat(input);
                data.offsetShearY   = ReadFloat(input);
                data.rotateMix      = ReadFloat(input);
                data.translateMix   = ReadFloat(input);
                data.scaleMix       = ReadFloat(input);
                data.shearMix       = ReadFloat(input);
                skeletonData.transformConstraints.Add(data);
            }

            // Path constraints
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                PathConstraintData data = new PathConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target         = skeletonData.slots.Items[ReadVarint(input, true)];
                data.positionMode   = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true));
                data.spacingMode    = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true));
                data.rotateMode     = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true));
                data.offsetRotation = ReadFloat(input);
                data.position       = ReadFloat(input);
                if (data.positionMode == PositionMode.Fixed)
                {
                    data.position *= scale;
                }
                data.spacing = ReadFloat(input);
                if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                {
                    data.spacing *= scale;
                }
                data.rotateMix    = ReadFloat(input);
                data.translateMix = ReadFloat(input);
                skeletonData.pathConstraints.Add(data);
            }

            // Default skin.
            Skin defaultSkin = ReadSkin(input, "default", nonessential);

            if (defaultSkin != null)
            {
                skeletonData.defaultSkin = defaultSkin;
                skeletonData.skins.Add(defaultSkin);
            }

            // Skins.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));
            }

            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
                Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null)
                {
                    throw new Exception("Skin not found: " + linkedMesh.skin);
                }
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();

            // Events.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                EventData data = new EventData(ReadString(input));
                data.Int    = ReadVarint(input, false);
                data.Float  = ReadFloat(input);
                data.String = ReadString(input);
                skeletonData.events.Add(data);
            }

            long _pos = input.Position;
            // Animations.
            int _animationCount = ReadVarint(input, true);

            // 传入的animPath是个bundle path,首先我们要获得真的地址
            var resolver     = ""; //new Ngame.ABSystem.AssetBundlePathResolver(); update wumiao
            var realAnimPath = ""; //resolver.ABResPlayGroundAssetPath + "/" + animPath; update wumiao

            //如果存在先删除,清空数据
            if (Directory.Exists(realAnimPath))
            {
                Directory.Delete(realAnimPath, true);
            }

            if (!Directory.Exists(realAnimPath))
            {
                Directory.CreateDirectory(realAnimPath);
            }

            int    _length     = skelPath.LastIndexOf(".");
            string _backupPath = skelPath.Insert(_length, "_backup");

            //超过限制数量就拆动画, 否则重定向输入源到backup
            // HACK 怨念临时修改,只要动画数量不为0就拆,这里传入的limitcount一定是0
            if (_animationCount > limitCount)
            {
                //备份
                if (backUp)
                {
                    if (File.Exists(_backupPath))
                    {
                        // redo
                        UnityEditor.AssetDatabase.DeleteAsset(_backupPath);
                    }
                    UnityEditor.AssetDatabase.CopyAsset(skelPath, _backupPath);
                }
            }
            else
            {
                if (backUp && File.Exists(_backupPath))
                {
                    //如果存在备份文件,则把input源重定向到备份文件
                    input = new MemoryStream(UnityEditor.AssetDatabase.LoadAssetAtPath <TextAsset>(_backupPath).bytes);
                }
                else
                {
                    Debug.LogErrorFormat("{0}动画已拆分并且不存在备份");
                    goto EndOfSplit;
                }
            }

            string fullPath = Path.GetFullPath(skelPath);

            using (FileStream fs = new FileStream(fullPath, FileMode.Truncate))
            {
                byte[] bytes = new byte[_pos];
                input.Seek(0, SeekOrigin.Begin);
                input.Read(bytes, 0, (int)_pos);
                fs.Write(bytes, 0, bytes.Length);

                _animationCount = ReadVarint(input, true);

                //将动画长度写为0
                fs.WriteByte(Byte.MinValue);
                //约定动画的长度不超过255
                fs.WriteByte((byte)_animationCount);

                for (int i = 0, n = _animationCount; i < n; i++)
                {
                    long   _prePos  = input.Position;
                    string _aniName = ReadString(input);

                    //强制规范命名。播放动画时同样需要
                    string _aniFileName = _aniName;
                    foreach (char invalidChar in Path.GetInvalidFileNameChars())
                    {
                        _aniFileName = _aniFileName.Replace(invalidChar.ToString(), "_");
                    }

                    long _afterPos = input.Position;
                    long _count    = _afterPos - _prePos;
                    bytes = new byte[_count];
                    input.Seek(_prePos, SeekOrigin.Begin);
                    input.Read(bytes, 0, (int)_count);
                    fs.Write(bytes, 0, bytes.Length);

                    string _name = Path.GetFileName(fullPath);

                    string _aniPath = realAnimPath + "/" + _aniFileName + ".bytes";
                    using (FileStream anifs = new FileStream(_aniPath, FileMode.Create))
                    {
                        _prePos = input.Position;
                        ReadAnimation(_aniName, input, skeletonData);
                        _afterPos = input.Position;
                        _count    = _afterPos - _prePos;
                        bytes     = new byte[_count];
                        input.Seek(_prePos, SeekOrigin.Begin);
                        input.Read(bytes, 0, (int)_count);
                        anifs.Write(bytes, 0, bytes.Length);

                        anifs.Close();
                    }
                }

                //Assets/NScene/Resources/
                //int _index = animPath.IndexOf("Resources/");
                //animPath = animPath.Remove(0, _index + 10);

                // 怨念修改
                // 现在传入的已经是bundle path了,直接写入文件即可
                WriteString(fs, animPath);
                fs.Close();
            }

EndOfSplit:
            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            skeletonData.pathConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #27
0
        public SkeletonData ReadSkeletonData(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            float scale = Scale;

            var skeletonData = new SkeletonData();

            skeletonData.hash = ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = ReadFloat(input);
            skeletonData.height = ReadFloat(input);

            bool nonessential = ReadBoolean(input);

            if (nonessential)
            {
                skeletonData.fps        = ReadFloat(input);
                skeletonData.imagesPath = ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }

            // Bones.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   name   = ReadString(input);
                BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)];
                BoneData data   = new BoneData(i, name, parent);
                data.rotation      = ReadFloat(input);
                data.x             = ReadFloat(input) * scale;
                data.y             = ReadFloat(input) * scale;
                data.scaleX        = ReadFloat(input);
                data.scaleY        = ReadFloat(input);
                data.shearX        = ReadFloat(input);
                data.shearY        = ReadFloat(input);
                data.length        = ReadFloat(input) * scale;
                data.transformMode = TransformModeValues[ReadVarint(input, true)];
                if (nonessential)
                {
                    ReadInt(input);                               // Skip bone color.
                }
                skeletonData.bones.Add(data);
            }

            // Slots.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                String   slotName = ReadString(input);
                BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)];
                SlotData slotData = new SlotData(i, slotName, boneData);
                int      color    = ReadInt(input);
                slotData.r = ((color & 0xff000000) >> 24) / 255f;
                slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
                slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
                slotData.a = ((color & 0x000000ff)) / 255f;

                int darkColor = ReadInt(input);                 // 0x00rrggbb
                if (darkColor != -1)
                {
                    slotData.hasSecondColor = true;
                    slotData.r2             = ((darkColor & 0x00ff0000) >> 16) / 255f;
                    slotData.g2             = ((darkColor & 0x0000ff00) >> 8) / 255f;
                    slotData.b2             = ((darkColor & 0x000000ff)) / 255f;
                }

                slotData.attachmentName = ReadString(input);
                slotData.blendMode      = (BlendMode)ReadVarint(input, true);
                skeletonData.slots.Add(slotData);
            }

            // IK constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                IkConstraintData data = new IkConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target        = skeletonData.bones.Items[ReadVarint(input, true)];
                data.mix           = ReadFloat(input);
                data.bendDirection = ReadSByte(input);
                skeletonData.ikConstraints.Add(data);
            }

            // Transform constraints.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                TransformConstraintData data = new TransformConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target         = skeletonData.bones.Items[ReadVarint(input, true)];
                data.local          = ReadBoolean(input);
                data.relative       = ReadBoolean(input);
                data.offsetRotation = ReadFloat(input);
                data.offsetX        = ReadFloat(input) * scale;
                data.offsetY        = ReadFloat(input) * scale;
                data.offsetScaleX   = ReadFloat(input);
                data.offsetScaleY   = ReadFloat(input);
                data.offsetShearY   = ReadFloat(input);
                data.rotateMix      = ReadFloat(input);
                data.translateMix   = ReadFloat(input);
                data.scaleMix       = ReadFloat(input);
                data.shearMix       = ReadFloat(input);
                skeletonData.transformConstraints.Add(data);
            }

            // Path constraints
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                PathConstraintData data = new PathConstraintData(ReadString(input));
                data.order = ReadVarint(input, true);
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
                }
                data.target         = skeletonData.slots.Items[ReadVarint(input, true)];
                data.positionMode   = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true));
                data.spacingMode    = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true));
                data.rotateMode     = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true));
                data.offsetRotation = ReadFloat(input);
                data.position       = ReadFloat(input);
                if (data.positionMode == PositionMode.Fixed)
                {
                    data.position *= scale;
                }
                data.spacing = ReadFloat(input);
                if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                {
                    data.spacing *= scale;
                }
                data.rotateMix    = ReadFloat(input);
                data.translateMix = ReadFloat(input);
                skeletonData.pathConstraints.Add(data);
            }

            // Default skin.
            Skin defaultSkin = ReadSkin(input, skeletonData, "default", nonessential);

            if (defaultSkin != null)
            {
                skeletonData.defaultSkin = defaultSkin;
                skeletonData.skins.Add(defaultSkin);
            }

            // Skins.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                skeletonData.skins.Add(ReadSkin(input, skeletonData, ReadString(input), nonessential));
            }

            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
                Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null)
                {
                    throw new Exception("Skin not found: " + linkedMesh.skin);
                }
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();

            // Events.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                EventData data = new EventData(ReadString(input));
                data.Int    = ReadVarint(input, false);
                data.Float  = ReadFloat(input);
                data.String = ReadString(input);
                skeletonData.events.Add(data);
            }

            // Animations.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                ReadAnimation(ReadString(input), input, skeletonData);
            }

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            skeletonData.pathConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #28
0
        public SkeletonData ReadSkeletonData(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            float        scale        = this.Scale;
            SkeletonData skeletonData = new SkeletonData();

            skeletonData.hash = this.ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = this.ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = this.ReadFloat(input);
            skeletonData.height = this.ReadFloat(input);
            bool flag = SkeletonBinary.ReadBoolean(input);

            if (flag)
            {
                skeletonData.imagesPath = this.ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }
            int i   = 0;
            int num = SkeletonBinary.ReadVarint(input, true);

            while (i < num)
            {
                string   name     = this.ReadString(input);
                BoneData parent   = (i != 0) ? skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)] : null;
                BoneData boneData = new BoneData(i, name, parent);
                boneData.rotation        = this.ReadFloat(input);
                boneData.x               = this.ReadFloat(input) * scale;
                boneData.y               = this.ReadFloat(input) * scale;
                boneData.scaleX          = this.ReadFloat(input);
                boneData.scaleY          = this.ReadFloat(input);
                boneData.shearX          = this.ReadFloat(input);
                boneData.shearY          = this.ReadFloat(input);
                boneData.length          = this.ReadFloat(input) * scale;
                boneData.inheritRotation = SkeletonBinary.ReadBoolean(input);
                boneData.inheritScale    = SkeletonBinary.ReadBoolean(input);
                if (flag)
                {
                    SkeletonBinary.ReadInt(input);
                }
                skeletonData.bones.Add(boneData);
                i++;
            }
            int j    = 0;
            int num2 = SkeletonBinary.ReadVarint(input, true);

            while (j < num2)
            {
                string   name2     = this.ReadString(input);
                BoneData boneData2 = skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)];
                SlotData slotData  = new SlotData(j, name2, boneData2);
                int      num3      = SkeletonBinary.ReadInt(input);
                slotData.r = (float)((num3 & 4278190080u) >> 24) / 255f;
                slotData.g = (float)((num3 & 16711680) >> 16) / 255f;
                slotData.b = (float)((num3 & 65280) >> 8) / 255f;
                slotData.a = (float)(num3 & 255) / 255f;
                slotData.attachmentName = this.ReadString(input);
                slotData.blendMode      = (BlendMode)SkeletonBinary.ReadVarint(input, true);
                skeletonData.slots.Add(slotData);
                j++;
            }
            int k    = 0;
            int num4 = SkeletonBinary.ReadVarint(input, true);

            while (k < num4)
            {
                IkConstraintData ikConstraintData = new IkConstraintData(this.ReadString(input));
                int l    = 0;
                int num5 = SkeletonBinary.ReadVarint(input, true);
                while (l < num5)
                {
                    ikConstraintData.bones.Add(skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)]);
                    l++;
                }
                ikConstraintData.target        = skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)];
                ikConstraintData.mix           = this.ReadFloat(input);
                ikConstraintData.bendDirection = (int)SkeletonBinary.ReadSByte(input);
                skeletonData.ikConstraints.Add(ikConstraintData);
                k++;
            }
            int m    = 0;
            int num6 = SkeletonBinary.ReadVarint(input, true);

            while (m < num6)
            {
                TransformConstraintData transformConstraintData = new TransformConstraintData(this.ReadString(input));
                int n    = 0;
                int num7 = SkeletonBinary.ReadVarint(input, true);
                while (n < num7)
                {
                    transformConstraintData.bones.Add(skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)]);
                    n++;
                }
                transformConstraintData.target         = skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)];
                transformConstraintData.offsetRotation = this.ReadFloat(input);
                transformConstraintData.offsetX        = this.ReadFloat(input) * scale;
                transformConstraintData.offsetY        = this.ReadFloat(input) * scale;
                transformConstraintData.offsetScaleX   = this.ReadFloat(input);
                transformConstraintData.offsetScaleY   = this.ReadFloat(input);
                transformConstraintData.offsetShearY   = this.ReadFloat(input);
                transformConstraintData.rotateMix      = this.ReadFloat(input);
                transformConstraintData.translateMix   = this.ReadFloat(input);
                transformConstraintData.scaleMix       = this.ReadFloat(input);
                transformConstraintData.shearMix       = this.ReadFloat(input);
                skeletonData.transformConstraints.Add(transformConstraintData);
                m++;
            }
            int num8 = 0;
            int num9 = SkeletonBinary.ReadVarint(input, true);

            while (num8 < num9)
            {
                PathConstraintData pathConstraintData = new PathConstraintData(this.ReadString(input));
                int num10 = 0;
                int num11 = SkeletonBinary.ReadVarint(input, true);
                while (num10 < num11)
                {
                    pathConstraintData.bones.Add(skeletonData.bones.Items[SkeletonBinary.ReadVarint(input, true)]);
                    num10++;
                }
                pathConstraintData.target         = skeletonData.slots.Items[SkeletonBinary.ReadVarint(input, true)];
                pathConstraintData.positionMode   = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(SkeletonBinary.ReadVarint(input, true));
                pathConstraintData.spacingMode    = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(SkeletonBinary.ReadVarint(input, true));
                pathConstraintData.rotateMode     = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(SkeletonBinary.ReadVarint(input, true));
                pathConstraintData.offsetRotation = this.ReadFloat(input);
                pathConstraintData.position       = this.ReadFloat(input);
                if (pathConstraintData.positionMode == PositionMode.Fixed)
                {
                    pathConstraintData.position *= scale;
                }
                pathConstraintData.spacing = this.ReadFloat(input);
                if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                {
                    pathConstraintData.spacing *= scale;
                }
                pathConstraintData.rotateMix    = this.ReadFloat(input);
                pathConstraintData.translateMix = this.ReadFloat(input);
                skeletonData.pathConstraints.Add(pathConstraintData);
                num8++;
            }
            Skin skin = this.ReadSkin(input, "default", flag);

            if (skin != null)
            {
                skeletonData.defaultSkin = skin;
                skeletonData.skins.Add(skin);
            }
            int num12 = 0;
            int num13 = SkeletonBinary.ReadVarint(input, true);

            while (num12 < num13)
            {
                skeletonData.skins.Add(this.ReadSkin(input, this.ReadString(input), flag));
                num12++;
            }
            int num14 = 0;
            int count = this.linkedMeshes.Count;

            while (num14 < count)
            {
                SkeletonJson.LinkedMesh linkedMesh = this.linkedMeshes[num14];
                Skin skin2 = (linkedMesh.skin != null) ? skeletonData.FindSkin(linkedMesh.skin) : skeletonData.DefaultSkin;
                if (skin2 == null)
                {
                    throw new Exception("Skin not found: " + linkedMesh.skin);
                }
                Attachment attachment = skin2.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (attachment == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)attachment;
                linkedMesh.mesh.UpdateUVs();
                num14++;
            }
            this.linkedMeshes.Clear();
            int num15 = 0;
            int num16 = SkeletonBinary.ReadVarint(input, true);

            while (num15 < num16)
            {
                EventData eventData = new EventData(this.ReadString(input));
                eventData.Int    = SkeletonBinary.ReadVarint(input, false);
                eventData.Float  = this.ReadFloat(input);
                eventData.String = this.ReadString(input);
                skeletonData.events.Add(eventData);
                num15++;
            }
            int num17 = 0;
            int num18 = SkeletonBinary.ReadVarint(input, true);

            while (num17 < num18)
            {
                this.ReadAnimation(this.ReadString(input), input, skeletonData);
                num17++;
            }
            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            skeletonData.pathConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #29
0
        internal static void SetPropertyToSetupPose(this Skeleton skeleton, int propertyID)
        {
            Bone           bone;
            PathConstraint constraint2;
            int            num   = propertyID >> 0x18;
            TimelineType   type  = (TimelineType)num;
            int            index = propertyID - (num << 0x18);

            switch (type)
            {
            case TimelineType.Rotate:
                bone          = skeleton.bones.Items[index];
                bone.rotation = bone.data.rotation;
                break;

            case TimelineType.Translate:
                bone   = skeleton.bones.Items[index];
                bone.x = bone.data.x;
                bone.y = bone.data.y;
                break;

            case TimelineType.Scale:
                bone        = skeleton.bones.Items[index];
                bone.scaleX = bone.data.scaleX;
                bone.scaleY = bone.data.scaleY;
                break;

            case TimelineType.Shear:
                bone        = skeleton.bones.Items[index];
                bone.shearX = bone.data.shearX;
                bone.shearY = bone.data.shearY;
                break;

            case TimelineType.Attachment:
                skeleton.SetSlotAttachmentToSetupPose(index);
                break;

            case TimelineType.Color:
                skeleton.slots.Items[index].SetColorToSetupPose();
                break;

            case TimelineType.Deform:
                skeleton.slots.Items[index].attachmentVertices.Clear(true);
                break;

            case TimelineType.DrawOrder:
                skeleton.SetDrawOrderToSetupPose();
                break;

            case TimelineType.IkConstraint:
            {
                IkConstraint constraint = skeleton.ikConstraints.Items[index];
                constraint.mix           = constraint.data.mix;
                constraint.bendDirection = constraint.data.bendDirection;
                break;
            }

            case TimelineType.TransformConstraint:
            {
                TransformConstraint     constraint3 = skeleton.transformConstraints.Items[index];
                TransformConstraintData data        = constraint3.data;
                constraint3.rotateMix    = data.rotateMix;
                constraint3.translateMix = data.translateMix;
                constraint3.scaleMix     = data.scaleMix;
                constraint3.shearMix     = data.shearMix;
                break;
            }

            case TimelineType.PathConstraintPosition:
                constraint2          = skeleton.pathConstraints.Items[index];
                constraint2.position = constraint2.data.position;
                break;

            case TimelineType.PathConstraintSpacing:
                constraint2         = skeleton.pathConstraints.Items[index];
                constraint2.spacing = constraint2.data.spacing;
                break;

            case TimelineType.PathConstraintMix:
                constraint2              = skeleton.pathConstraints.Items[index];
                constraint2.rotateMix    = constraint2.data.rotateMix;
                constraint2.translateMix = constraint2.data.translateMix;
                break;

            case TimelineType.TwoColor:
                skeleton.slots.Items[index].SetColorToSetupPose();
                break;
            }
        }
예제 #30
0
		public SkeletonData ReadSkeletonData (Stream input) {
			if (input == null) throw new ArgumentNullException("input cannot be null.");
			float scale = Scale;

			var skeletonData = new SkeletonData();
			skeletonData.hash = ReadString(input);
			if (skeletonData.hash.Length == 0) skeletonData.hash = null;
			skeletonData.version = ReadString(input);
			if (skeletonData.version.Length == 0) skeletonData.version = null;
			skeletonData.width = ReadFloat(input);
			skeletonData.height = ReadFloat(input);

			bool nonessential = ReadBoolean(input);

			if (nonessential) {
				skeletonData.imagesPath = ReadString(input);
				if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null;
			}

			// Bones.
			for (int i = 0, n = ReadInt(input, true); i < n; i++) {
				String name = ReadString(input);
				BoneData parent = null;
				int parentIndex = ReadInt(input, true) - 1;
				if (parentIndex != -1) parent = skeletonData.bones.Items[parentIndex];
				BoneData boneData = new BoneData(name, parent);
				boneData.x = ReadFloat(input) * scale;
				boneData.y = ReadFloat(input) * scale;
				boneData.scaleX = ReadFloat(input);
				boneData.scaleY = ReadFloat(input);
				boneData.rotation = ReadFloat(input);
				boneData.length = ReadFloat(input) * scale;
				boneData.inheritScale = ReadBoolean(input);
				boneData.inheritRotation = ReadBoolean(input);
				if (nonessential) ReadInt(input); // Skip bone color.
				skeletonData.bones.Add(boneData);
			}

			// IK constraints.
			for (int i = 0, n = ReadInt(input, true); i < n; i++) {
				IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input));
				for (int ii = 0, nn = ReadInt(input, true); ii < nn; ii++)
					ikConstraintData.bones.Add(skeletonData.bones.Items[ReadInt(input, true)]);
				ikConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)];
				ikConstraintData.mix = ReadFloat(input);
				ikConstraintData.bendDirection = ReadSByte(input);
				skeletonData.ikConstraints.Add(ikConstraintData);
			}

			// Transform constraints.
			for (int i = 0, n = ReadInt(input, true); i < n; i++) {
				TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input));
				transformConstraintData.bone = skeletonData.bones.Items[ReadInt(input, true)];
				transformConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)];
				transformConstraintData.translateMix = ReadFloat(input);
				transformConstraintData.x = ReadFloat(input);
				transformConstraintData.y = ReadFloat(input);
				skeletonData.transformConstraints.Add(transformConstraintData);
			}

			// Slots.
			for (int i = 0, n = ReadInt(input, true); i < n; i++) {
				String slotName = ReadString(input);
				BoneData boneData = skeletonData.bones.Items[ReadInt(input, true)];
				SlotData slotData = new SlotData(slotName, boneData);
				int color = ReadInt(input);
				slotData.r = ((color & 0xff000000) >> 24) / 255f;
				slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
				slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
				slotData.a = ((color & 0x000000ff)) / 255f;
				slotData.attachmentName = ReadString(input);
				slotData.blendMode = (BlendMode)ReadInt(input, true);
				skeletonData.slots.Add(slotData);
			}

			// Default skin.
			Skin defaultSkin = ReadSkin(input, "default", nonessential);
			if (defaultSkin != null) {
				skeletonData.defaultSkin = defaultSkin;
				skeletonData.skins.Add(defaultSkin);
			}

			// Skins.
			for (int i = 0, n = ReadInt(input, true); i < n; i++)
				skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));

			// Events.
			for (int i = 0, n = ReadInt(input, true); i < n; i++) {
				EventData eventData = new EventData(ReadString(input));
				eventData.Int = ReadInt(input, false);
				eventData.Float = ReadFloat(input);
				eventData.String = ReadString(input);
				skeletonData.events.Add(eventData);
			}

			// Animations.
			for (int i = 0, n = ReadInt(input, true); i < n; i++)
				ReadAnimation(ReadString(input), input, skeletonData);

			skeletonData.bones.TrimExcess();
			skeletonData.slots.TrimExcess();
			skeletonData.skins.TrimExcess();
			skeletonData.events.TrimExcess();
			skeletonData.animations.TrimExcess();
			skeletonData.ikConstraints.TrimExcess();
			return skeletonData;
		}
		public SkeletonData ReadSkeletonData (TextReader reader) {
			if (reader == null) throw new ArgumentNullException("reader", "reader cannot be null.");

			var scale = this.Scale;
			var skeletonData = new SkeletonData();

			var root = Json.Deserialize(reader) as Dictionary<String, Object>;
			if (root == null) throw new Exception("Invalid JSON.");

			// Skeleton.
			if (root.ContainsKey("skeleton")) {
				var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
				skeletonData.hash = (String)skeletonMap["hash"];
				skeletonData.version = (String)skeletonMap["spine"];
				skeletonData.width = GetFloat(skeletonMap, "width", 0);
				skeletonData.height = GetFloat(skeletonMap, "height", 0);
				skeletonData.fps = GetFloat(skeletonMap, "fps", 0);
				skeletonData.imagesPath = GetString(skeletonMap, "images", null);
			}

			// Bones.
			foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) {
				BoneData parent = null;
				if (boneMap.ContainsKey("parent")) {
					parent = skeletonData.FindBone((String)boneMap["parent"]);
					if (parent == null)
						throw new Exception("Parent bone not found: " + boneMap["parent"]);
				}
				var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent);
				data.length = GetFloat(boneMap, "length", 0) * scale;
				data.x = GetFloat(boneMap, "x", 0) * scale;
				data.y = GetFloat(boneMap, "y", 0) * scale;
				data.rotation = GetFloat(boneMap, "rotation", 0);
				data.scaleX = GetFloat(boneMap, "scaleX", 1);
				data.scaleY = GetFloat(boneMap, "scaleY", 1);
				data.shearX = GetFloat(boneMap, "shearX", 0);
				data.shearY = GetFloat(boneMap, "shearY", 0);

				string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString());
				data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true);

				skeletonData.bones.Add(data);
			}

			// Slots.
			if (root.ContainsKey("slots")) {
				foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) {
					var slotName = (String)slotMap["name"];
					var boneName = (String)slotMap["bone"];
					BoneData boneData = skeletonData.FindBone(boneName);
					if (boneData == null) throw new Exception("Slot bone not found: " + boneName);
					var data = new SlotData(skeletonData.Slots.Count, slotName, boneData);

					if (slotMap.ContainsKey("color")) {
						var color = (String)slotMap["color"];
						data.r = ToColor(color, 0);
						data.g = ToColor(color, 1);
						data.b = ToColor(color, 2);
						data.a = ToColor(color, 3);
					}
						
					data.attachmentName = GetString(slotMap, "attachment", null);
					if (slotMap.ContainsKey("blend"))
						data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false);
					else
						data.blendMode = BlendMode.normal;
					skeletonData.slots.Add(data);
				}
			}

			// IK constraints.
			if (root.ContainsKey("ik")) {
				foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["ik"]) {
					IkConstraintData data = new IkConstraintData((String)constraintMap["name"]);
					data.order = GetInt(constraintMap, "order", 0);

					foreach (String boneName in (List<Object>)constraintMap["bones"]) {
						BoneData bone = skeletonData.FindBone(boneName);
						if (bone == null) throw new Exception("IK constraint bone not found: " + boneName);
						data.bones.Add(bone);
					}
					
					String targetName = (String)constraintMap["target"];
					data.target = skeletonData.FindBone(targetName);
					if (data.target == null) throw new Exception("Target bone not found: " + targetName);

					data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1;
					data.mix = GetFloat(constraintMap, "mix", 1);

					skeletonData.ikConstraints.Add(data);
				}
			}

			// Transform constraints.
			if (root.ContainsKey("transform")) {
				foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["transform"]) {
					TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]);
					data.order = GetInt(constraintMap, "order", 0);

					foreach (String boneName in (List<Object>)constraintMap["bones"]) {
						BoneData bone = skeletonData.FindBone(boneName);
						if (bone == null) throw new Exception("Transform constraint bone not found: " + boneName);
						data.bones.Add(bone);
					}

					String targetName = (String)constraintMap["target"];
					data.target = skeletonData.FindBone(targetName);
					if (data.target == null) throw new Exception("Target bone not found: " + targetName);

					data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
					data.offsetX = GetFloat(constraintMap, "x", 0) * scale;
					data.offsetY = GetFloat(constraintMap, "y", 0) * scale;
					data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0);
					data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0);
					data.offsetShearY = GetFloat(constraintMap, "shearY", 0);

					data.rotateMix = GetFloat(constraintMap, "rotateMix", 1);
					data.translateMix = GetFloat(constraintMap, "translateMix", 1);
					data.scaleMix = GetFloat(constraintMap, "scaleMix", 1);
					data.shearMix = GetFloat(constraintMap, "shearMix", 1);

					skeletonData.transformConstraints.Add(data);
				}
			}

			// Path constraints.
			if(root.ContainsKey("path")) {
				foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["path"]) {
					PathConstraintData data = new PathConstraintData((String)constraintMap["name"]);
					data.order = GetInt(constraintMap, "order", 0);

					foreach (String boneName in (List<Object>)constraintMap["bones"]) {
						BoneData bone = skeletonData.FindBone(boneName);
						if (bone == null) throw new Exception("Path bone not found: " + boneName);
						data.bones.Add(bone);
					}

					String targetName = (String)constraintMap["target"];
					data.target = skeletonData.FindSlot(targetName);
					if (data.target == null) throw new Exception("Target slot not found: " + targetName);

					data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true);
					data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true);
					data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true);
					data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
					data.position = GetFloat(constraintMap, "position", 0);
					if (data.positionMode == PositionMode.Fixed) data.position *= scale;
					data.spacing = GetFloat(constraintMap, "spacing", 0);
					if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
					data.rotateMix = GetFloat(constraintMap, "rotateMix", 1);
					data.translateMix = GetFloat(constraintMap, "translateMix", 1);

					skeletonData.pathConstraints.Add(data);
				}
			}

			// Skins.
			if (root.ContainsKey("skins")) {
					foreach (KeyValuePair<String, Object> skinMap in (Dictionary<String, Object>)root["skins"]) {
					var skin = new Skin(skinMap.Key);
					foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)skinMap.Value) {
						int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key);
						foreach (KeyValuePair<String, Object> entry in ((Dictionary<String, Object>)slotEntry.Value)) {
							try {
								Attachment attachment = ReadAttachment((Dictionary<String, Object>)entry.Value, skin, slotIndex, entry.Key);
								if (attachment != null) skin.AddAttachment(slotIndex, entry.Key, attachment);
							} catch (Exception e) {
								throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e);
							}
						} 
					}
					skeletonData.skins.Add(skin);
					if (skin.name == "default") skeletonData.defaultSkin = skin;
				}
			}

			// Linked meshes.
			for (int i = 0, n = linkedMeshes.Count; i < n; i++) {
				LinkedMesh linkedMesh = linkedMeshes[i];
				Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin);
				if (skin == null) throw new Exception("Slot not found: " + linkedMesh.skin);
				Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
				if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
				linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
				linkedMesh.mesh.UpdateUVs();
			}
			linkedMeshes.Clear();

			// Events.
			if (root.ContainsKey("events")) {
				foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) {
					var entryMap = (Dictionary<String, Object>)entry.Value;
					var data = new EventData(entry.Key);
					data.Int = GetInt(entryMap, "int", 0);
					data.Float = GetFloat(entryMap, "float", 0);
					data.String = GetString(entryMap, "string", string.Empty);
					skeletonData.events.Add(data);
				}
			}

			// Animations.
			if (root.ContainsKey("animations")) {
				foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) {
					try {
						ReadAnimation((Dictionary<String, Object>)entry.Value, entry.Key, skeletonData);
					} catch (Exception e) {
						throw new Exception("Error reading animation: " + entry.Key, e);
					}
				}   
			}

			skeletonData.bones.TrimExcess();
			skeletonData.slots.TrimExcess();
			skeletonData.skins.TrimExcess();
			skeletonData.events.TrimExcess();
			skeletonData.animations.TrimExcess();
			skeletonData.ikConstraints.TrimExcess();
			return skeletonData;
		}
예제 #32
0
        private void ReadAnimation(Dictionary <String, Object> map, String name, SkeletonData skeletonData)
        {
            var   scale     = this.Scale;
            var   timelines = new ExposedList <Timeline>();
            float duration  = 0;

            // Slot timelines.
            if (map.ContainsKey("slots"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["slots"])
                {
                    String slotName    = entry.Key;
                    int    slotIndex   = skeletonData.FindSlotIndex(slotName);
                    var    timelineMap = (Dictionary <String, Object>)entry.Value;
                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "color")
                        {
                            var timeline = new ColorTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float  time = (float)valueMap["time"];
                                String c    = (valueMap.ContainsKey("color"))?
                                              (String)valueMap["color"] : "FFFFFFFF";
                                timeline.SetFrame(frameIndex, time, ToColor(c, 0), ToColor(c, 1), ToColor(c, 2), ToColor(c, 3));
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1] * ColorTimeline.ENTRIES);
                        }
                        else if (timelineName == "attachment")
                        {
                            var timeline = new AttachmentTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex++, time, (String)valueMap["name"]);
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
                        }
                    }
                }
            }

            // Bone timelines.
            if (map.ContainsKey("bones"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["bones"])
                {
                    String boneName  = entry.Key;
                    int    boneIndex = skeletonData.FindBoneIndex(boneName);
                    if (boneIndex == -1)
                    {
                        throw new Exception("Bone not found: " + boneName);
                    }
                    var timelineMap = (Dictionary <String, Object>)entry.Value;
                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "rotate")
                        {
                            var timeline = new RotateTimeline(values.Count);
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], (float)valueMap["angle"]);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * RotateTimeline.ENTRIES]);
                        }
                        else if (timelineName == "translate" || timelineName == "scale" || timelineName == "shear")
                        {
                            TranslateTimeline timeline;
                            float             timelineScale = 1;
                            if (timelineName == "scale")
                            {
                                timeline = new ScaleTimeline(values.Count);
                            }
                            else if (timelineName == "shear")
                            {
                                timeline = new ShearTimeline(values.Count);
                            }
                            else
                            {
                                timeline      = new TranslateTimeline(values.Count);
                                timelineScale = scale;
                            }
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                float x    = GetFloat(valueMap, "x", 0);
                                float y    = GetFloat(valueMap, "y", 0);
                                timeline.SetFrame(frameIndex, time, x * timelineScale, y * timelineScale);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * TranslateTimeline.ENTRIES]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
                        }
                    }
                }
            }

            // IK constraint timelines.
            if (map.ContainsKey("ik"))
            {
                foreach (KeyValuePair <String, Object> constraintMap in (Dictionary <String, Object>)map["ik"])
                {
                    IkConstraintData constraint = skeletonData.FindIkConstraint(constraintMap.Key);
                    var values   = (List <Object>)constraintMap.Value;
                    var timeline = new IkConstraintTimeline(values.Count);
                    timeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary <String, Object> valueMap in values)
                    {
                        float time         = (float)valueMap["time"];
                        float mix          = GetFloat(valueMap, "mix", 1);
                        bool  bendPositive = GetBoolean(valueMap, "bendPositive", true);
                        timeline.SetFrame(frameIndex, time, mix, bendPositive ? 1 : -1);
                        ReadCurve(valueMap, timeline, frameIndex);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * IkConstraintTimeline.ENTRIES]);
                }
            }

            // Transform constraint timelines.
            if (map.ContainsKey("transform"))
            {
                foreach (KeyValuePair <String, Object> constraintMap in (Dictionary <String, Object>)map["transform"])
                {
                    TransformConstraintData constraint = skeletonData.FindTransformConstraint(constraintMap.Key);
                    var values   = (List <Object>)constraintMap.Value;
                    var timeline = new TransformConstraintTimeline(values.Count);
                    timeline.transformConstraintIndex = skeletonData.transformConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary <String, Object> valueMap in values)
                    {
                        float time         = (float)valueMap["time"];
                        float rotateMix    = GetFloat(valueMap, "rotateMix", 1);
                        float translateMix = GetFloat(valueMap, "translateMix", 1);
                        float scaleMix     = GetFloat(valueMap, "scaleMix", 1);
                        float shearMix     = GetFloat(valueMap, "shearMix", 1);
                        timeline.SetFrame(frameIndex, time, rotateMix, translateMix, scaleMix, shearMix);
                        ReadCurve(valueMap, timeline, frameIndex);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * TransformConstraintTimeline.ENTRIES]);
                }
            }

            // Path constraint timelines.
            if (map.ContainsKey("paths"))
            {
                foreach (KeyValuePair <String, Object> constraintMap in (Dictionary <String, Object>)map["paths"])
                {
                    int index = skeletonData.FindPathConstraintIndex(constraintMap.Key);
                    if (index == -1)
                    {
                        throw new Exception("Path constraint not found: " + constraintMap.Key);
                    }
                    PathConstraintData data = skeletonData.pathConstraints.Items[index];
                    var timelineMap         = (Dictionary <String, Object>)constraintMap.Value;
                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "position" || timelineName == "spacing")
                        {
                            PathConstraintPositionTimeline timeline;
                            float timelineScale = 1;
                            if (timelineName == "spacing")
                            {
                                timeline = new PathConstraintSpacingTimeline(values.Count);
                                if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                                {
                                    timelineScale = scale;
                                }
                            }
                            else
                            {
                                timeline = new PathConstraintPositionTimeline(values.Count);
                                if (data.positionMode == PositionMode.Fixed)
                                {
                                    timelineScale = scale;
                                }
                            }
                            timeline.pathConstraintIndex = index;
                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], GetFloat(valueMap, timelineName, 0) * timelineScale);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * PathConstraintPositionTimeline.ENTRIES]);
                        }
                        else if (timelineName == "mix")
                        {
                            PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(values.Count);
                            timeline.pathConstraintIndex = index;
                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], GetFloat(valueMap, "rotateMix", 1), GetFloat(valueMap, "translateMix", 1));
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * PathConstraintMixTimeline.ENTRIES]);
                        }
                    }
                }
            }

            // Deform timelines.
            if (map.ContainsKey("deform"))
            {
                foreach (KeyValuePair <String, Object> deformMap in (Dictionary <String, Object>)map["deform"])
                {
                    Skin skin = skeletonData.FindSkin(deformMap.Key);
                    foreach (KeyValuePair <String, Object> slotMap in (Dictionary <String, Object>)deformMap.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(slotMap.Key);
                        if (slotIndex == -1)
                        {
                            throw new Exception("Slot not found: " + slotMap.Key);
                        }
                        foreach (KeyValuePair <String, Object> timelineMap in (Dictionary <String, Object>)slotMap.Value)
                        {
                            var values = (List <Object>)timelineMap.Value;
                            VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, timelineMap.Key);
                            if (attachment == null)
                            {
                                throw new Exception("Deform attachment not found: " + timelineMap.Key);
                            }
                            bool    weighted     = attachment.bones != null;
                            float[] vertices     = attachment.vertices;
                            int     deformLength = weighted ? vertices.Length / 3 * 2 : vertices.Length;

                            var timeline = new DeformTimeline(values.Count);
                            timeline.slotIndex  = slotIndex;
                            timeline.attachment = attachment;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float[] deform;
                                if (!valueMap.ContainsKey("vertices"))
                                {
                                    deform = weighted ? new float[deformLength] : vertices;
                                }
                                else
                                {
                                    deform = new float[deformLength];
                                    int     start         = GetInt(valueMap, "offset", 0);
                                    float[] verticesValue = GetFloatArray(valueMap, "vertices", 1);
                                    Array.Copy(verticesValue, 0, deform, start, verticesValue.Length);
                                    if (scale != 1)
                                    {
                                        for (int i = start, n = i + verticesValue.Length; i < n; i++)
                                        {
                                            deform[i] *= scale;
                                        }
                                    }

                                    if (!weighted)
                                    {
                                        for (int i = 0; i < deformLength; i++)
                                        {
                                            deform[i] += vertices[i];
                                        }
                                    }
                                }

                                timeline.SetFrame(frameIndex, (float)valueMap["time"], deform);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                    }
                }
            }

            // Draw order timeline.
            if (map.ContainsKey("drawOrder") || map.ContainsKey("draworder"))
            {
                var values     = (List <Object>)map[map.ContainsKey("drawOrder") ? "drawOrder" : "draworder"];
                var timeline   = new DrawOrderTimeline(values.Count);
                int slotCount  = skeletonData.slots.Count;
                int frameIndex = 0;
                foreach (Dictionary <String, Object> drawOrderMap in values)
                {
                    int[] drawOrder = null;
                    if (drawOrderMap.ContainsKey("offsets"))
                    {
                        drawOrder = new int[slotCount];
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            drawOrder[i] = -1;
                        }
                        var   offsets = (List <Object>)drawOrderMap["offsets"];
                        int[] unchanged = new int[slotCount - offsets.Count];
                        int   originalIndex = 0, unchangedIndex = 0;
                        foreach (Dictionary <String, Object> offsetMap in offsets)
                        {
                            int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
                            if (slotIndex == -1)
                            {
                                throw new Exception("Slot not found: " + offsetMap["slot"]);
                            }
                            // Collect unchanged items.
                            while (originalIndex != slotIndex)
                            {
                                unchanged[unchangedIndex++] = originalIndex++;
                            }
                            // Set changed items.
                            int index = originalIndex + (int)(float)offsetMap["offset"];
                            drawOrder[index] = originalIndex++;
                        }
                        // Collect remaining unchanged items.
                        while (originalIndex < slotCount)
                        {
                            unchanged[unchangedIndex++] = originalIndex++;
                        }
                        // Fill in unchanged items.
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            if (drawOrder[i] == -1)
                            {
                                drawOrder[i] = unchanged[--unchangedIndex];
                            }
                        }
                    }
                    timeline.SetFrame(frameIndex++, (float)drawOrderMap["time"], drawOrder);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            // Event timeline.
            if (map.ContainsKey("events"))
            {
                var eventsMap  = (List <Object>)map["events"];
                var timeline   = new EventTimeline(eventsMap.Count);
                int frameIndex = 0;
                foreach (Dictionary <String, Object> eventMap in eventsMap)
                {
                    EventData eventData = skeletonData.FindEvent((String)eventMap["name"]);
                    if (eventData == null)
                    {
                        throw new Exception("Event not found: " + eventMap["name"]);
                    }
                    var e = new Event((float)eventMap["time"], eventData);
                    e.Int    = GetInt(eventMap, "int", eventData.Int);
                    e.Float  = GetFloat(eventMap, "float", eventData.Float);
                    e.String = GetString(eventMap, "string", eventData.String);
                    timeline.SetFrame(frameIndex++, e);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            timelines.TrimExcess();
            skeletonData.animations.Add(new Animation(name, timelines, duration));
        }
예제 #33
0
        public override void Apply(Skeleton skeleton, float lastTime, float time, ExposedList <Event> firedEvents, float alpha, MixPose pose, MixDirection direction)
        {
            TransformConstraint constraint = skeleton.transformConstraints.Items[this.transformConstraintIndex];

            float[] frames = this.frames;
            if (time < frames[0])
            {
                TransformConstraintData data = constraint.data;
                if (pose != MixPose.Setup)
                {
                    if (pose != MixPose.Current)
                    {
                        return;
                    }
                }
                else
                {
                    constraint.rotateMix    = data.rotateMix;
                    constraint.translateMix = data.translateMix;
                    constraint.scaleMix     = data.scaleMix;
                    constraint.shearMix     = data.shearMix;
                    return;
                }
                constraint.rotateMix    += (data.rotateMix - constraint.rotateMix) * alpha;
                constraint.translateMix += (data.translateMix - constraint.translateMix) * alpha;
                constraint.scaleMix     += (data.scaleMix - constraint.scaleMix) * alpha;
                constraint.shearMix     += (data.shearMix - constraint.shearMix) * alpha;
            }
            else
            {
                float num;
                float num2;
                float num3;
                float num4;
                if (time >= frames[frames.Length - 5])
                {
                    int length = frames.Length;
                    num  = frames[length + -4];
                    num2 = frames[length + -3];
                    num3 = frames[length + -2];
                    num4 = frames[length + -1];
                }
                else
                {
                    int index = Animation.BinarySearch(frames, time, 5);
                    num  = frames[index + -4];
                    num2 = frames[index + -3];
                    num3 = frames[index + -2];
                    num4 = frames[index + -1];
                    float num7         = frames[index];
                    float curvePercent = base.GetCurvePercent((index / 5) - 1, 1f - ((time - num7) / (frames[index + -5] - num7)));
                    num  += (frames[index + 1] - num) * curvePercent;
                    num2 += (frames[index + 2] - num2) * curvePercent;
                    num3 += (frames[index + 3] - num3) * curvePercent;
                    num4 += (frames[index + 4] - num4) * curvePercent;
                }
                if (pose == MixPose.Setup)
                {
                    TransformConstraintData data = constraint.data;
                    constraint.rotateMix    = data.rotateMix + ((num - data.rotateMix) * alpha);
                    constraint.translateMix = data.translateMix + ((num2 - data.translateMix) * alpha);
                    constraint.scaleMix     = data.scaleMix + ((num3 - data.scaleMix) * alpha);
                    constraint.shearMix     = data.shearMix + ((num4 - data.shearMix) * alpha);
                }
                else
                {
                    constraint.rotateMix    += (num - constraint.rotateMix) * alpha;
                    constraint.translateMix += (num2 - constraint.translateMix) * alpha;
                    constraint.scaleMix     += (num3 - constraint.scaleMix) * alpha;
                    constraint.shearMix     += (num4 - constraint.shearMix) * alpha;
                }
            }
        }
예제 #34
0
        public SkeletonData ReadSkeletonData(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input cannot be null.");
            }
            float scale = Scale;

            var skeletonData = new SkeletonData();

            skeletonData.hash = ReadString(input);
            if (skeletonData.hash.Length == 0)
            {
                skeletonData.hash = null;
            }
            skeletonData.version = ReadString(input);
            if (skeletonData.version.Length == 0)
            {
                skeletonData.version = null;
            }
            skeletonData.width  = ReadFloat(input);
            skeletonData.height = ReadFloat(input);

            bool nonessential = ReadBoolean(input);

            if (nonessential)
            {
                skeletonData.imagesPath = ReadString(input);
                if (skeletonData.imagesPath.Length == 0)
                {
                    skeletonData.imagesPath = null;
                }
            }

            // Bones.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                String   name        = ReadString(input);
                BoneData parent      = null;
                int      parentIndex = ReadInt(input, true) - 1;
                if (parentIndex != -1)
                {
                    parent = skeletonData.bones.Items[parentIndex];
                }
                BoneData boneData = new BoneData(name, parent);
                boneData.x               = ReadFloat(input) * scale;
                boneData.y               = ReadFloat(input) * scale;
                boneData.scaleX          = ReadFloat(input);
                boneData.scaleY          = ReadFloat(input);
                boneData.rotation        = ReadFloat(input);
                boneData.length          = ReadFloat(input) * scale;
                boneData.inheritScale    = ReadBoolean(input);
                boneData.inheritRotation = ReadBoolean(input);
                if (nonessential)
                {
                    ReadInt(input);                               // Skip bone color.
                }
                skeletonData.bones.Add(boneData);
            }

            // IK constraints.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input));
                for (int ii = 0, nn = ReadInt(input, true); ii < nn; ii++)
                {
                    ikConstraintData.bones.Add(skeletonData.bones.Items[ReadInt(input, true)]);
                }
                ikConstraintData.target        = skeletonData.bones.Items[ReadInt(input, true)];
                ikConstraintData.mix           = ReadFloat(input);
                ikConstraintData.bendDirection = ReadSByte(input);
                skeletonData.ikConstraints.Add(ikConstraintData);
            }

            // Transform constraints.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input));
                transformConstraintData.bone         = skeletonData.bones.Items[ReadInt(input, true)];
                transformConstraintData.target       = skeletonData.bones.Items[ReadInt(input, true)];
                transformConstraintData.translateMix = ReadFloat(input);
                transformConstraintData.x            = ReadFloat(input) * scale;
                transformConstraintData.y            = ReadFloat(input) * scale;
                skeletonData.transformConstraints.Add(transformConstraintData);
            }

            // Slots.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                String   slotName = ReadString(input);
                BoneData boneData = skeletonData.bones.Items[ReadInt(input, true)];
                SlotData slotData = new SlotData(slotName, boneData);
                int      color    = ReadInt(input);
                slotData.r = ((color & 0xff000000) >> 24) / 255f;
                slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
                slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
                slotData.a = ((color & 0x000000ff)) / 255f;
                slotData.attachmentName = ReadString(input);
                slotData.blendMode      = (BlendMode)ReadInt(input, true);
                skeletonData.slots.Add(slotData);
            }

            // Default skin.
            Skin defaultSkin = ReadSkin(input, "default", nonessential);

            if (defaultSkin != null)
            {
                skeletonData.defaultSkin = defaultSkin;
                skeletonData.skins.Add(defaultSkin);
            }

            // Skins.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));
            }

            // Events.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                EventData eventData = new EventData(ReadString(input));
                eventData.Int    = ReadInt(input, false);
                eventData.Float  = ReadFloat(input);
                eventData.String = ReadString(input);
                skeletonData.events.Add(eventData);
            }

            // Animations.
            for (int i = 0, n = ReadInt(input, true); i < n; i++)
            {
                ReadAnimation(ReadString(input), input, skeletonData);
            }

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #35
0
        public SkeletonData ReadSkeletonData(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "reader cannot be null.");
            }

            var scale        = this.Scale;
            var skeletonData = new SkeletonData();

            var root = Json.Deserialize(reader) as Dictionary <String, Object>;

            if (root == null)
            {
                throw new Exception("Invalid JSON.");
            }

            // Skeleton.
            if (root.ContainsKey("skeleton"))
            {
                var skeletonMap = (Dictionary <String, Object>)root["skeleton"];
                skeletonData.hash    = (String)skeletonMap["hash"];
                skeletonData.version = (String)skeletonMap["spine"];
                skeletonData.width   = GetFloat(skeletonMap, "width", 0);
                skeletonData.height  = GetFloat(skeletonMap, "height", 0);
            }

            // Bones.
            foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"])
            {
                BoneData parent = null;
                if (boneMap.ContainsKey("parent"))
                {
                    parent = skeletonData.FindBone((String)boneMap["parent"]);
                    if (parent == null)
                    {
                        throw new Exception("Parent bone not found: " + boneMap["parent"]);
                    }
                }
                var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent);
                data.length          = GetFloat(boneMap, "length", 0) * scale;
                data.x               = GetFloat(boneMap, "x", 0) * scale;
                data.y               = GetFloat(boneMap, "y", 0) * scale;
                data.rotation        = GetFloat(boneMap, "rotation", 0);
                data.scaleX          = GetFloat(boneMap, "scaleX", 1);
                data.scaleY          = GetFloat(boneMap, "scaleY", 1);
                data.shearX          = GetFloat(boneMap, "shearX", 0);
                data.shearY          = GetFloat(boneMap, "shearY", 0);
                data.inheritRotation = GetBoolean(boneMap, "inheritRotation", true);
                data.inheritScale    = GetBoolean(boneMap, "inheritScale", true);

                skeletonData.bones.Add(data);
            }

            // Slots.
            if (root.ContainsKey("slots"))
            {
                foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"])
                {
                    var      slotName = (String)slotMap["name"];
                    var      boneName = (String)slotMap["bone"];
                    BoneData boneData = skeletonData.FindBone(boneName);
                    if (boneData == null)
                    {
                        throw new Exception("Slot bone not found: " + boneName);
                    }
                    var data = new SlotData(skeletonData.Slots.Count, slotName, boneData);

                    if (slotMap.ContainsKey("color"))
                    {
                        var color = (String)slotMap["color"];
                        data.r = ToColor(color, 0);
                        data.g = ToColor(color, 1);
                        data.b = ToColor(color, 2);
                        data.a = ToColor(color, 3);
                    }

                    data.attachmentName = GetString(slotMap, "attachment", null);
                    if (slotMap.ContainsKey("blend"))
                    {
                        data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false);
                    }
                    else
                    {
                        data.blendMode = BlendMode.normal;
                    }
                    skeletonData.slots.Add(data);
                }
            }

            // IK constraints.
            if (root.ContainsKey("ik"))
            {
                foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["ik"])
                {
                    IkConstraintData data = new IkConstraintData((String)constraintMap["name"]);

                    foreach (String boneName in (List <Object>)constraintMap["bones"])
                    {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null)
                        {
                            throw new Exception("IK constraint bone not found: " + boneName);
                        }
                        data.bones.Add(bone);
                    }

                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindBone(targetName);
                    if (data.target == null)
                    {
                        throw new Exception("Target bone not found: " + targetName);
                    }

                    data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1;
                    data.mix           = GetFloat(constraintMap, "mix", 1);

                    skeletonData.ikConstraints.Add(data);
                }
            }

            // Transform constraints.
            if (root.ContainsKey("transform"))
            {
                foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["transform"])
                {
                    TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]);

                    foreach (String boneName in (List <Object>)constraintMap["bones"])
                    {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null)
                        {
                            throw new Exception("Transform constraint bone not found: " + boneName);
                        }
                        data.bones.Add(bone);
                    }

                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindBone(targetName);
                    if (data.target == null)
                    {
                        throw new Exception("Target bone not found: " + targetName);
                    }

                    data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
                    data.offsetX        = GetFloat(constraintMap, "x", 0) * scale;
                    data.offsetY        = GetFloat(constraintMap, "y", 0) * scale;
                    data.offsetScaleX   = GetFloat(constraintMap, "scaleX", 0);
                    data.offsetScaleY   = GetFloat(constraintMap, "scaleY", 0);
                    data.offsetShearY   = GetFloat(constraintMap, "shearY", 0);

                    data.rotateMix    = GetFloat(constraintMap, "rotateMix", 1);
                    data.translateMix = GetFloat(constraintMap, "translateMix", 1);
                    data.scaleMix     = GetFloat(constraintMap, "scaleMix", 1);
                    data.shearMix     = GetFloat(constraintMap, "shearMix", 1);

                    skeletonData.transformConstraints.Add(data);
                }
            }

            // Path constraints.
            if (root.ContainsKey("path"))
            {
                foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["path"])
                {
                    PathConstraintData data = new PathConstraintData((String)constraintMap["name"]);

                    foreach (String boneName in (List <Object>)constraintMap["bones"])
                    {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null)
                        {
                            throw new Exception("Path bone not found: " + boneName);
                        }
                        data.bones.Add(bone);
                    }

                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindSlot(targetName);
                    if (data.target == null)
                    {
                        throw new Exception("Target slot not found: " + targetName);
                    }

                    data.positionMode   = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true);
                    data.spacingMode    = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true);
                    data.rotateMode     = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true);
                    data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
                    data.position       = GetFloat(constraintMap, "position", 0);
                    if (data.positionMode == PositionMode.Fixed)
                    {
                        data.position *= scale;
                    }
                    data.spacing = GetFloat(constraintMap, "spacing", 0);
                    if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                    {
                        data.spacing *= scale;
                    }
                    data.rotateMix    = GetFloat(constraintMap, "rotateMix", 1);
                    data.translateMix = GetFloat(constraintMap, "translateMix", 1);

                    skeletonData.pathConstraints.Add(data);
                }
            }

            // Skins.
            if (root.ContainsKey("skins"))
            {
                foreach (KeyValuePair <String, Object> skinMap in (Dictionary <String, Object>)root["skins"])
                {
                    var skin = new Skin(skinMap.Key);
                    foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)skinMap.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key);
                        foreach (KeyValuePair <String, Object> entry in ((Dictionary <String, Object>)slotEntry.Value))
                        {
                            try {
                                Attachment attachment = ReadAttachment((Dictionary <String, Object>)entry.Value, skin, slotIndex, entry.Key);
                                if (attachment != null)
                                {
                                    skin.AddAttachment(slotIndex, entry.Key, attachment);
                                }
                            } catch (Exception e) {
                                throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e);
                            }
                        }
                    }
                    skeletonData.skins.Add(skin);
                    if (skin.name == "default")
                    {
                        skeletonData.defaultSkin = skin;
                    }
                }
            }

            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                LinkedMesh linkedMesh = linkedMeshes[i];
                Skin       skin       = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null)
                {
                    throw new Exception("Slot not found: " + linkedMesh.skin);
                }
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();

            // Events.
            if (root.ContainsKey("events"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"])
                {
                    var entryMap = (Dictionary <String, Object>)entry.Value;
                    var data     = new EventData(entry.Key);
                    data.Int    = GetInt(entryMap, "int", 0);
                    data.Float  = GetFloat(entryMap, "float", 0);
                    data.String = GetString(entryMap, "string", null);
                    skeletonData.events.Add(data);
                }
            }

            // Animations.
            if (root.ContainsKey("animations"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"])
                {
                    try {
                        ReadAnimation((Dictionary <String, Object>)entry.Value, entry.Key, skeletonData);
                    } catch (Exception e) {
                        throw new Exception("Error reading animation: " + entry.Key, e);
                    }
                }
            }

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
		public SkeletonData ReadSkeletonData (Stream input) {
			if (input == null) throw new ArgumentNullException("input");
			float scale = Scale;

			var skeletonData = new SkeletonData();
			skeletonData.hash = ReadString(input);
			if (skeletonData.hash.Length == 0) skeletonData.hash = null;
			skeletonData.version = ReadString(input);
			if (skeletonData.version.Length == 0) skeletonData.version = null;
			skeletonData.width = ReadFloat(input);
			skeletonData.height = ReadFloat(input);

			bool nonessential = ReadBoolean(input);

			if (nonessential) {
				skeletonData.fps = ReadFloat(input);
				skeletonData.imagesPath = ReadString(input);
				if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null;
			}

			// Bones.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				String name = ReadString(input);
				BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)];
				BoneData data = new BoneData(i, name, parent);
				data.rotation = ReadFloat(input);		
				data.x = ReadFloat(input) * scale;
				data.y = ReadFloat(input) * scale;
				data.scaleX = ReadFloat(input);
				data.scaleY = ReadFloat(input);
				data.shearX = ReadFloat(input);
				data.shearY = ReadFloat(input);
				data.length = ReadFloat(input) * scale;
				data.transformMode = TransformModeValues[ReadVarint(input, true)];
				if (nonessential) ReadInt(input); // Skip bone color.
				skeletonData.bones.Add(data);
			}

			// Slots.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				String slotName = ReadString(input);
				BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)];
				SlotData slotData = new SlotData(i, slotName, boneData);
				int color = ReadInt(input);
				slotData.r = ((color & 0xff000000) >> 24) / 255f;
				slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
				slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
				slotData.a = ((color & 0x000000ff)) / 255f;
				slotData.attachmentName = ReadString(input);
				slotData.blendMode = (BlendMode)ReadVarint(input, true);
				skeletonData.slots.Add(slotData);
			}

			// IK constraints.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				IkConstraintData data = new IkConstraintData(ReadString(input));
				data.order = ReadVarint(input, true);
				for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
					data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
				data.target = skeletonData.bones.Items[ReadVarint(input, true)];
				data.mix = ReadFloat(input);
				data.bendDirection = ReadSByte(input);
				skeletonData.ikConstraints.Add(data);
			}

			// Transform constraints.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				TransformConstraintData data = new TransformConstraintData(ReadString(input));
				data.order = ReadVarint(input, true);
				for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
				    data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
				data.target = skeletonData.bones.Items[ReadVarint(input, true)];
				data.offsetRotation = ReadFloat(input);
				data.offsetX = ReadFloat(input) * scale;
				data.offsetY = ReadFloat(input) * scale;
				data.offsetScaleX = ReadFloat(input);
				data.offsetScaleY = ReadFloat(input);
				data.offsetShearY = ReadFloat(input);
				data.rotateMix = ReadFloat(input);
				data.translateMix = ReadFloat(input);
				data.scaleMix = ReadFloat(input);
				data.shearMix = ReadFloat(input);
				skeletonData.transformConstraints.Add(data);
			}

			// Path constraints
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				PathConstraintData data = new PathConstraintData(ReadString(input));
				data.order = ReadVarint(input, true);
				for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
					data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
				data.target = skeletonData.slots.Items[ReadVarint(input, true)];
				data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true));
				data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true));
				data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true));
				data.offsetRotation = ReadFloat(input);
				data.position = ReadFloat(input);
				if (data.positionMode == PositionMode.Fixed) data.position *= scale;
				data.spacing = ReadFloat(input);
				if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
				data.rotateMix = ReadFloat(input);
				data.translateMix = ReadFloat(input);
				skeletonData.pathConstraints.Add(data);
			}

			// Default skin.
			Skin defaultSkin = ReadSkin(input, "default", nonessential);
			if (defaultSkin != null) {
				skeletonData.defaultSkin = defaultSkin;
				skeletonData.skins.Add(defaultSkin);
			}

			// Skins.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++)
				skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));

			// Linked meshes.
			for (int i = 0, n = linkedMeshes.Count; i < n; i++) {
				SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
				Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
				if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin);
				Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
				if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
				linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
				linkedMesh.mesh.UpdateUVs();
			}
			linkedMeshes.Clear();

			// Events.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				EventData data = new EventData(ReadString(input));
				data.Int = ReadVarint(input, false);
				data.Float = ReadFloat(input);
				data.String = ReadString(input);
				skeletonData.events.Add(data);
			}

			// Animations.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++)
				ReadAnimation(ReadString(input), input, skeletonData);

			skeletonData.bones.TrimExcess();
			skeletonData.slots.TrimExcess();
			skeletonData.skins.TrimExcess();
			skeletonData.events.TrimExcess();
			skeletonData.animations.TrimExcess();
			skeletonData.ikConstraints.TrimExcess();
			skeletonData.pathConstraints.TrimExcess();
			return skeletonData;
		}
예제 #37
0
		public SkeletonData ReadSkeletonData (Stream input) {
			if (input == null) throw new ArgumentNullException("input");
			float scale = Scale;

			var skeletonData = new SkeletonData();
			skeletonData.hash = ReadString(input);
			if (skeletonData.hash.Length == 0) skeletonData.hash = null;
			skeletonData.version = ReadString(input);
			if (skeletonData.version.Length == 0) skeletonData.version = null;
			skeletonData.width = ReadFloat(input);
			skeletonData.height = ReadFloat(input);

			bool nonessential = ReadBoolean(input);

			if (nonessential) {
				skeletonData.imagesPath = ReadString(input);
				if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null;
			}

			// Bones.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				String name = ReadString(input);
				BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)];
				BoneData boneData = new BoneData(name, parent);
				boneData.x = ReadFloat(input) * scale;
				boneData.y = ReadFloat(input) * scale;
				boneData.scaleX = ReadFloat(input);
				boneData.scaleY = ReadFloat(input);
				boneData.rotation = ReadFloat(input);
				boneData.length = ReadFloat(input) * scale;
				boneData.inheritScale = ReadBoolean(input);
				boneData.inheritRotation = ReadBoolean(input);
				if (nonessential) ReadInt(input); // Skip bone color.
				skeletonData.bones.Add(boneData);
			}

			// IK constraints.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input));
				for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
					ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]);
				ikConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)];
				ikConstraintData.mix = ReadFloat(input);
				ikConstraintData.bendDirection = ReadSByte(input);
				skeletonData.ikConstraints.Add(ikConstraintData);
			}

			// Transform constraints.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input));
				transformConstraintData.bone = skeletonData.bones.Items[ReadVarint(input, true)];
				transformConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)];
				transformConstraintData.translateMix = ReadFloat(input);
				transformConstraintData.x = ReadFloat(input) * scale;
				transformConstraintData.y = ReadFloat(input) * scale;
				skeletonData.transformConstraints.Add(transformConstraintData);
			}

			// Slots.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				String slotName = ReadString(input);
				BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)];
				SlotData slotData = new SlotData(slotName, boneData);
				int color = ReadInt(input);
				slotData.r = ((color & 0xff000000) >> 24) / 255f;
				slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
				slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
				slotData.a = ((color & 0x000000ff)) / 255f;
				slotData.attachmentName = ReadString(input);
				slotData.blendMode = (BlendMode)ReadVarint(input, true);
				skeletonData.slots.Add(slotData);
			}

			// Default skin.
			Skin defaultSkin = ReadSkin(input, "default", nonessential);
			if (defaultSkin != null) {
				skeletonData.defaultSkin = defaultSkin;
				skeletonData.skins.Add(defaultSkin);
			}

			// Skins.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++)
				skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential));

			// Linked meshes.
			for (int i = 0, n = linkedMeshes.Count; i < n; i++) {
				SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
				Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
				if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin);
				Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
				if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
				if (linkedMesh.mesh is MeshAttachment) {
					MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh;
					mesh.ParentMesh = (MeshAttachment)parent;
					mesh.UpdateUVs();
				} else {
					WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh;
					mesh.ParentMesh = (WeightedMeshAttachment)parent;
					mesh.UpdateUVs();
				}
			}
			linkedMeshes.Clear();

			// Events.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++) {
				EventData eventData = new EventData(ReadString(input));
				eventData.Int = ReadVarint(input, false);
				eventData.Float = ReadFloat(input);
				eventData.String = ReadString(input);
				skeletonData.events.Add(eventData);
			}

			// Animations.
			for (int i = 0, n = ReadVarint(input, true); i < n; i++)
				ReadAnimation(ReadString(input), input, skeletonData);

			skeletonData.bones.TrimExcess();
			skeletonData.slots.TrimExcess();
			skeletonData.skins.TrimExcess();
			skeletonData.events.TrimExcess();
			skeletonData.animations.TrimExcess();
			skeletonData.ikConstraints.TrimExcess();
			return skeletonData;
		}
예제 #38
0
        public SkeletonData ReadSkeletonData(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader cannot be null.");
            }

            var scale        = this.Scale;
            var skeletonData = new SkeletonData();

            var root = Json.Deserialize(reader) as Dictionary <String, Object>;

            if (root == null)
            {
                throw new Exception("Invalid JSON.");
            }

            // Skeleton.
            if (root.ContainsKey("skeleton"))
            {
                var skeletonMap = (Dictionary <String, Object>)root["skeleton"];
                skeletonData.hash    = (String)skeletonMap["hash"];
                skeletonData.version = (String)skeletonMap["spine"];
                skeletonData.width   = GetFloat(skeletonMap, "width", 0);
                skeletonData.height  = GetFloat(skeletonMap, "height", 0);
            }

            // Bones.
            foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"])
            {
                BoneData parent = null;
                if (boneMap.ContainsKey("parent"))
                {
                    parent = skeletonData.FindBone((String)boneMap["parent"]);
                    if (parent == null)
                    {
                        throw new Exception("Parent bone not found: " + boneMap["parent"]);
                    }
                }
                var boneData = new BoneData((String)boneMap["name"], parent);
                boneData.length          = GetFloat(boneMap, "length", 0) * scale;
                boneData.x               = GetFloat(boneMap, "x", 0) * scale;
                boneData.y               = GetFloat(boneMap, "y", 0) * scale;
                boneData.rotation        = GetFloat(boneMap, "rotation", 0);
                boneData.scaleX          = GetFloat(boneMap, "scaleX", 1);
                boneData.scaleY          = GetFloat(boneMap, "scaleY", 1);
                boneData.inheritScale    = GetBoolean(boneMap, "inheritScale", true);
                boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true);
                skeletonData.bones.Add(boneData);
            }

            // IK constraints.
            if (root.ContainsKey("ik"))
            {
                foreach (Dictionary <String, Object> ikMap in (List <Object>)root["ik"])
                {
                    IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]);

                    foreach (String boneName in (List <Object>)ikMap["bones"])
                    {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null)
                        {
                            throw new Exception("IK bone not found: " + boneName);
                        }
                        ikConstraintData.bones.Add(bone);
                    }

                    String targetName = (String)ikMap["target"];
                    ikConstraintData.target = skeletonData.FindBone(targetName);
                    if (ikConstraintData.target == null)
                    {
                        throw new Exception("Target bone not found: " + targetName);
                    }

                    ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1;
                    ikConstraintData.mix           = GetFloat(ikMap, "mix", 1);

                    skeletonData.ikConstraints.Add(ikConstraintData);
                }
            }

            // Transform constraints.
            if (root.ContainsKey("transform"))
            {
                foreach (Dictionary <String, Object> transformMap in (List <Object>)root["transform"])
                {
                    TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]);

                    String boneName = (String)transformMap["bone"];
                    transformConstraintData.bone = skeletonData.FindBone(boneName);
                    if (transformConstraintData.bone == null)
                    {
                        throw new Exception("Bone not found: " + boneName);
                    }

                    String targetName = (String)transformMap["target"];
                    transformConstraintData.target = skeletonData.FindBone(targetName);
                    if (transformConstraintData.target == null)
                    {
                        throw new Exception("Target bone not found: " + targetName);
                    }

                    transformConstraintData.translateMix = GetFloat(transformMap, "translateMix", 1);
                    transformConstraintData.x            = GetFloat(transformMap, "x", 0) * scale;
                    transformConstraintData.y            = GetFloat(transformMap, "y", 0) * scale;

                    skeletonData.transformConstraints.Add(transformConstraintData);
                }
            }

            // Slots.
            if (root.ContainsKey("slots"))
            {
                foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"])
                {
                    var      slotName = (String)slotMap["name"];
                    var      boneName = (String)slotMap["bone"];
                    BoneData boneData = skeletonData.FindBone(boneName);
                    if (boneData == null)
                    {
                        throw new Exception("Slot bone not found: " + boneName);
                    }
                    var slotData = new SlotData(slotName, boneData);

                    if (slotMap.ContainsKey("color"))
                    {
                        var color = (String)slotMap["color"];
                        slotData.r = ToColor(color, 0);
                        slotData.g = ToColor(color, 1);
                        slotData.b = ToColor(color, 2);
                        slotData.a = ToColor(color, 3);
                    }

                    if (slotMap.ContainsKey("attachment"))
                    {
                        slotData.attachmentName = (String)slotMap["attachment"];
                    }

                    if (slotMap.ContainsKey("blend"))
                    {
                        slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false);
                    }
                    else
                    {
                        slotData.blendMode = BlendMode.normal;
                    }

                    skeletonData.slots.Add(slotData);
                }
            }

            // Skins.
            if (root.ContainsKey("skins"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["skins"])
                {
                    var skin = new Skin(entry.Key);
                    foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key);
                        foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value))
                        {
                            Attachment attachment = ReadAttachment(skin, slotIndex, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value);
                            if (attachment != null)
                            {
                                skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment);
                            }
                        }
                    }
                    skeletonData.skins.Add(skin);
                    if (skin.name == "default")
                    {
                        skeletonData.defaultSkin = skin;
                    }
                }
            }

            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                LinkedMesh linkedMesh = linkedMeshes[i];
                Skin       skin       = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null)
                {
                    throw new Exception("Slot not found: " + linkedMesh.skin);
                }
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                if (linkedMesh.mesh is MeshAttachment)
                {
                    MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh;
                    mesh.ParentMesh = (MeshAttachment)parent;
                    mesh.UpdateUVs();
                }
                else
                {
                    WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh;
                    mesh.ParentMesh = (WeightedMeshAttachment)parent;
                    mesh.UpdateUVs();
                }
            }
            linkedMeshes.Clear();

            // Events.
            if (root.ContainsKey("events"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"])
                {
                    var entryMap  = (Dictionary <String, Object>)entry.Value;
                    var eventData = new EventData(entry.Key);
                    eventData.Int    = GetInt(entryMap, "int", 0);
                    eventData.Float  = GetFloat(entryMap, "float", 0);
                    eventData.String = GetString(entryMap, "string", null);
                    skeletonData.events.Add(eventData);
                }
            }

            // Animations.
            if (root.ContainsKey("animations"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"])
                {
                    ReadAnimation(entry.Key, (Dictionary <String, Object>)entry.Value, skeletonData);
                }
            }

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }