예제 #1
0
		public PathConstraint (PathConstraintData 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;
			bones = new ExposedList<Bone>(data.Bones.Count);
			foreach (BoneData boneData in data.bones)
				bones.Add(skeleton.FindBone(boneData.name));
			target = skeleton.FindSlot(data.target.name);
			position = data.position;
			spacing = data.spacing;
			rotateMix = data.rotateMix;
			translateMix = data.translateMix;
		}
예제 #2
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++;
            }
        }
예제 #3
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++;
            }
        }
예제 #4
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;
            }
        }
예제 #5
0
 static int FindPathConstraint(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.PathConstraintData o    = obj.FindPathConstraint(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #6
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;
            }
        }
예제 #7
0
        // --- Path constraints.

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

            for (int i = 0, n = pathConstraints.Count; i < n; i++)
            {
                PathConstraintData constraint = pathConstraints.Items[i];
                if (constraint.name.Equals(constraintName))
                {
                    return(constraint);
                }
            }
            return(null);
        }
예제 #8
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;
            }
        }
예제 #9
0
        public PathConstraintData FindPathConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <PathConstraintData> exposedList = pathConstraints;
            int i = 0;

            for (int count = exposedList.Count; i < count; i++)
            {
                PathConstraintData pathConstraintData = exposedList.Items[i];
                if (pathConstraintData.name.Equals(constraintName))
                {
                    return(pathConstraintData);
                }
            }
            return(null);
        }
예제 #10
0
        public PathConstraintData FindPathConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <PathConstraintData> pathConstraints = this.pathConstraints;
            int index = 0;
            int count = pathConstraints.Count;

            while (index < count)
            {
                PathConstraintData data = pathConstraints.Items[index];
                if (data.name.Equals(constraintName))
                {
                    return(data);
                }
                index++;
            }
            return(null);
        }
 /// <summary>Copy constructor.</summary>
 public PathConstraint(PathConstraint 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.slots.Items[constraint.target.data.index];
     position     = constraint.position;
     spacing      = constraint.spacing;
     rotateMix    = constraint.rotateMix;
     translateMix = constraint.translateMix;
 }
 public PathConstraint(PathConstraintData 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;
     bones     = new ExposedList <Bone>(data.Bones.Count);
     foreach (BoneData boneData in data.bones)
     {
         bones.Add(skeleton.FindBone(boneData.name));
     }
     target       = skeleton.FindSlot(data.target.name);
     position     = data.position;
     spacing      = data.spacing;
     rotateMix    = data.rotateMix;
     translateMix = data.translateMix;
 }
예제 #13
0
        public SkeletonData ReadSkeletonData(NewStream 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);
        }
예제 #14
0
        public SkeletonData ReadSkeletonData(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "reader cannot be null.");
            }
            float        scale        = Scale;
            SkeletonData skeletonData = new SkeletonData();
            Dictionary <string, object> dictionary = Json.Deserialize(reader) as Dictionary <string, object>;

            if (dictionary == null)
            {
                throw new Exception("Invalid JSON.");
            }
            if (dictionary.ContainsKey("skeleton"))
            {
                Dictionary <string, object> dictionary2 = (Dictionary <string, object>)dictionary["skeleton"];
                skeletonData.hash       = (string)dictionary2["hash"];
                skeletonData.version    = (string)dictionary2["spine"];
                skeletonData.width      = GetFloat(dictionary2, "width", 0f);
                skeletonData.height     = GetFloat(dictionary2, "height", 0f);
                skeletonData.fps        = GetFloat(dictionary2, "fps", 0f);
                skeletonData.imagesPath = GetString(dictionary2, "images", null);
            }
            foreach (Dictionary <string, object> item in (List <object>)dictionary["bones"])
            {
                BoneData boneData = null;
                if (item.ContainsKey("parent"))
                {
                    boneData = skeletonData.FindBone((string)item["parent"]);
                    if (boneData == null)
                    {
                        throw new Exception("Parent bone not found: " + item["parent"]);
                    }
                }
                BoneData boneData2 = new BoneData(skeletonData.Bones.Count, (string)item["name"], boneData);
                boneData2.length   = GetFloat(item, "length", 0f) * scale;
                boneData2.x        = GetFloat(item, "x", 0f) * scale;
                boneData2.y        = GetFloat(item, "y", 0f) * scale;
                boneData2.rotation = GetFloat(item, "rotation", 0f);
                boneData2.scaleX   = GetFloat(item, "scaleX", 1f);
                boneData2.scaleY   = GetFloat(item, "scaleY", 1f);
                boneData2.shearX   = GetFloat(item, "shearX", 0f);
                boneData2.shearY   = GetFloat(item, "shearY", 0f);
                string @string = GetString(item, "transform", TransformMode.Normal.ToString());
                boneData2.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), @string, ignoreCase: true);
                skeletonData.bones.Add(boneData2);
            }
            if (dictionary.ContainsKey("slots"))
            {
                foreach (Dictionary <string, object> item2 in (List <object>)dictionary["slots"])
                {
                    string   name      = (string)item2["name"];
                    string   text      = (string)item2["bone"];
                    BoneData boneData3 = skeletonData.FindBone(text);
                    if (boneData3 == null)
                    {
                        throw new Exception("Slot bone not found: " + text);
                    }
                    SlotData slotData = new SlotData(skeletonData.Slots.Count, name, boneData3);
                    if (item2.ContainsKey("color"))
                    {
                        string hexString = (string)item2["color"];
                        slotData.r = ToColor(hexString, 0);
                        slotData.g = ToColor(hexString, 1);
                        slotData.b = ToColor(hexString, 2);
                        slotData.a = ToColor(hexString, 3);
                    }
                    if (item2.ContainsKey("dark"))
                    {
                        string hexString2 = (string)item2["dark"];
                        slotData.r2             = ToColor(hexString2, 0, 6);
                        slotData.g2             = ToColor(hexString2, 1, 6);
                        slotData.b2             = ToColor(hexString2, 2, 6);
                        slotData.hasSecondColor = true;
                    }
                    slotData.attachmentName = GetString(item2, "attachment", null);
                    if (item2.ContainsKey("blend"))
                    {
                        slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (string)item2["blend"], ignoreCase: true);
                    }
                    else
                    {
                        slotData.blendMode = BlendMode.Normal;
                    }
                    skeletonData.slots.Add(slotData);
                }
            }
            if (dictionary.ContainsKey("ik"))
            {
                foreach (Dictionary <string, object> item3 in (List <object>)dictionary["ik"])
                {
                    IkConstraintData ikConstraintData = new IkConstraintData((string)item3["name"]);
                    ikConstraintData.order = GetInt(item3, "order", 0);
                    foreach (string item4 in (List <object>)item3["bones"])
                    {
                        BoneData boneData4 = skeletonData.FindBone(item4);
                        if (boneData4 == null)
                        {
                            throw new Exception("IK constraint bone not found: " + item4);
                        }
                        ikConstraintData.bones.Add(boneData4);
                    }
                    string text3 = (string)item3["target"];
                    ikConstraintData.target = skeletonData.FindBone(text3);
                    if (ikConstraintData.target == null)
                    {
                        throw new Exception("Target bone not found: " + text3);
                    }
                    ikConstraintData.bendDirection = (GetBoolean(item3, "bendPositive", defaultValue: true) ? 1 : (-1));
                    ikConstraintData.mix           = GetFloat(item3, "mix", 1f);
                    skeletonData.ikConstraints.Add(ikConstraintData);
                }
            }
            if (dictionary.ContainsKey("transform"))
            {
                foreach (Dictionary <string, object> item5 in (List <object>)dictionary["transform"])
                {
                    TransformConstraintData transformConstraintData = new TransformConstraintData((string)item5["name"]);
                    transformConstraintData.order = GetInt(item5, "order", 0);
                    foreach (string item6 in (List <object>)item5["bones"])
                    {
                        BoneData boneData5 = skeletonData.FindBone(item6);
                        if (boneData5 == null)
                        {
                            throw new Exception("Transform constraint bone not found: " + item6);
                        }
                        transformConstraintData.bones.Add(boneData5);
                    }
                    string text5 = (string)item5["target"];
                    transformConstraintData.target = skeletonData.FindBone(text5);
                    if (transformConstraintData.target == null)
                    {
                        throw new Exception("Target bone not found: " + text5);
                    }
                    transformConstraintData.local          = GetBoolean(item5, "local", defaultValue: false);
                    transformConstraintData.relative       = GetBoolean(item5, "relative", defaultValue: false);
                    transformConstraintData.offsetRotation = GetFloat(item5, "rotation", 0f);
                    transformConstraintData.offsetX        = GetFloat(item5, "x", 0f) * scale;
                    transformConstraintData.offsetY        = GetFloat(item5, "y", 0f) * scale;
                    transformConstraintData.offsetScaleX   = GetFloat(item5, "scaleX", 0f);
                    transformConstraintData.offsetScaleY   = GetFloat(item5, "scaleY", 0f);
                    transformConstraintData.offsetShearY   = GetFloat(item5, "shearY", 0f);
                    transformConstraintData.rotateMix      = GetFloat(item5, "rotateMix", 1f);
                    transformConstraintData.translateMix   = GetFloat(item5, "translateMix", 1f);
                    transformConstraintData.scaleMix       = GetFloat(item5, "scaleMix", 1f);
                    transformConstraintData.shearMix       = GetFloat(item5, "shearMix", 1f);
                    skeletonData.transformConstraints.Add(transformConstraintData);
                }
            }
            if (dictionary.ContainsKey("path"))
            {
                foreach (Dictionary <string, object> item7 in (List <object>)dictionary["path"])
                {
                    PathConstraintData pathConstraintData = new PathConstraintData((string)item7["name"]);
                    pathConstraintData.order = GetInt(item7, "order", 0);
                    foreach (string item8 in (List <object>)item7["bones"])
                    {
                        BoneData boneData6 = skeletonData.FindBone(item8);
                        if (boneData6 == null)
                        {
                            throw new Exception("Path bone not found: " + item8);
                        }
                        pathConstraintData.bones.Add(boneData6);
                    }
                    string text7 = (string)item7["target"];
                    pathConstraintData.target = skeletonData.FindSlot(text7);
                    if (pathConstraintData.target == null)
                    {
                        throw new Exception("Target slot not found: " + text7);
                    }
                    pathConstraintData.positionMode   = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(item7, "positionMode", "percent"), ignoreCase: true);
                    pathConstraintData.spacingMode    = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(item7, "spacingMode", "length"), ignoreCase: true);
                    pathConstraintData.rotateMode     = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(item7, "rotateMode", "tangent"), ignoreCase: true);
                    pathConstraintData.offsetRotation = GetFloat(item7, "rotation", 0f);
                    pathConstraintData.position       = GetFloat(item7, "position", 0f);
                    if (pathConstraintData.positionMode == PositionMode.Fixed)
                    {
                        pathConstraintData.position *= scale;
                    }
                    pathConstraintData.spacing = GetFloat(item7, "spacing", 0f);
                    if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                    {
                        pathConstraintData.spacing *= scale;
                    }
                    pathConstraintData.rotateMix    = GetFloat(item7, "rotateMix", 1f);
                    pathConstraintData.translateMix = GetFloat(item7, "translateMix", 1f);
                    skeletonData.pathConstraints.Add(pathConstraintData);
                }
            }
            if (dictionary.ContainsKey("skins"))
            {
                foreach (KeyValuePair <string, object> item9 in (Dictionary <string, object>)dictionary["skins"])
                {
                    Skin skin = new Skin(item9.Key);
                    foreach (KeyValuePair <string, object> item10 in (Dictionary <string, object>)item9.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(item10.Key);
                        foreach (KeyValuePair <string, object> item11 in (Dictionary <string, object>)item10.Value)
                        {
                            try
                            {
                                Attachment attachment = ReadAttachment((Dictionary <string, object>)item11.Value, skin, slotIndex, item11.Key, skeletonData);
                                if (attachment != null)
                                {
                                    skin.AddAttachment(slotIndex, item11.Key, attachment);
                                }
                            }
                            catch (Exception innerException)
                            {
                                throw new Exception("Error reading attachment: " + item11.Key + ", skin: " + skin, innerException);
                            }
                        }
                    }
                    skeletonData.skins.Add(skin);
                    if (skin.name == "default")
                    {
                        skeletonData.defaultSkin = skin;
                    }
                }
            }
            int i = 0;

            for (int count = linkedMeshes.Count; i < count; i++)
            {
                LinkedMesh linkedMesh = linkedMeshes[i];
                Skin       skin2      = (linkedMesh.skin != null) ? skeletonData.FindSkin(linkedMesh.skin) : skeletonData.defaultSkin;
                if (skin2 == null)
                {
                    throw new Exception("Slot not found: " + linkedMesh.skin);
                }
                Attachment attachment2 = skin2.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (attachment2 == null)
                {
                    throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                }
                linkedMesh.mesh.ParentMesh = (MeshAttachment)attachment2;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();
            if (dictionary.ContainsKey("events"))
            {
                foreach (KeyValuePair <string, object> item12 in (Dictionary <string, object>)dictionary["events"])
                {
                    Dictionary <string, object> map = (Dictionary <string, object>)item12.Value;
                    EventData eventData             = new EventData(item12.Key);
                    eventData.Int    = GetInt(map, "int", 0);
                    eventData.Float  = GetFloat(map, "float", 0f);
                    eventData.String = GetString(map, "string", string.Empty);
                    skeletonData.events.Add(eventData);
                }
            }
            if (dictionary.ContainsKey("animations"))
            {
                foreach (KeyValuePair <string, object> item13 in (Dictionary <string, object>)dictionary["animations"])
                {
                    try
                    {
                        ReadAnimation((Dictionary <string, object>)item13.Value, item13.Key, skeletonData);
                    }
                    catch (Exception innerException2)
                    {
                        throw new Exception("Error reading animation: " + item13.Key, innerException2);
                    }
                }
            }
            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #15
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);
        }
        public void Update()
        {
            PathAttachment attachment = target.Attachment as PathAttachment;

            if (attachment == null)
            {
                return;
            }

            float rotateMix = this.rotateMix, translateMix = this.translateMix;
            bool  translate = translateMix > 0, rotate = rotateMix > 0;

            if (!translate && !rotate)
            {
                return;
            }

            PathConstraintData data = this.data;
            bool       percentSpacing = data.spacingMode == SpacingMode.Percent;
            RotateMode rotateMode = data.rotateMode;
            bool       tangents = rotateMode == RotateMode.Tangent, scale = rotateMode == RotateMode.ChainScale;
            int        boneCount = this.bones.Count, spacesCount = tangents ? boneCount : boneCount + 1;

            Bone[] bonesItems = this.bones.Items;
            ExposedList <float> spaces = this.spaces.Resize(spacesCount), lengths = null;
            float spacing = this.spacing;

            if (scale || !percentSpacing)
            {
                if (scale)
                {
                    lengths = this.lengths.Resize(boneCount);
                }
                bool lengthSpacing = data.spacingMode == SpacingMode.Length;
                for (int i = 0, n = spacesCount - 1; i < n;)
                {
                    Bone  bone        = bonesItems[i];
                    float setupLength = bone.data.length;
                    if (setupLength < PathConstraint.Epsilon)
                    {
                        if (scale)
                        {
                            lengths.Items[i] = 0;
                        }
                        spaces.Items[++i] = 0;
                    }
                    else if (percentSpacing)
                    {
                        if (scale)
                        {
                            float x = setupLength * bone.a, y = setupLength * bone.c;
                            float length = (float)Math.Sqrt(x * x + y * y);
                            lengths.Items[i] = length;
                        }
                        spaces.Items[++i] = spacing;
                    }
                    else
                    {
                        float x = setupLength * bone.a, y = setupLength * bone.c;
                        float length = (float)Math.Sqrt(x * x + y * y);
                        if (scale)
                        {
                            lengths.Items[i] = length;
                        }
                        spaces.Items[++i] = (lengthSpacing ? setupLength + spacing : spacing) * length / setupLength;
                    }
                }
            }
            else
            {
                for (int i = 1; i < spacesCount; i++)
                {
                    spaces.Items[i] = spacing;
                }
            }

            float[] positions = ComputeWorldPositions(attachment, spacesCount, tangents,
                                                      data.positionMode == PositionMode.Percent, percentSpacing);
            float boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation;
            bool  tip;

            if (offsetRotation == 0)
            {
                tip = rotateMode == RotateMode.Chain;
            }
            else
            {
                tip = false;
                Bone p = target.bone;
                offsetRotation *= p.a * p.d - p.b * p.c > 0 ? MathUtils.DegRad : -MathUtils.DegRad;
            }
            for (int i = 0, p = 3; i < boneCount; i++, p += 3)
            {
                Bone bone = bonesItems[i];
                bone.worldX += (boneX - bone.worldX) * translateMix;
                bone.worldY += (boneY - bone.worldY) * translateMix;
                float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
                if (scale)
                {
                    float length = lengths.Items[i];
                    if (length >= PathConstraint.Epsilon)
                    {
                        float s = ((float)Math.Sqrt(dx * dx + dy * dy) / length - 1) * rotateMix + 1;
                        bone.a *= s;
                        bone.c *= s;
                    }
                }
                boneX = x;
                boneY = y;
                if (rotate)
                {
                    float a = bone.a, b = bone.b, c = bone.c, d = bone.d, r, cos, sin;
                    if (tangents)
                    {
                        r = positions[p - 1];
                    }
                    else if (spaces.Items[i + 1] < PathConstraint.Epsilon)
                    {
                        r = positions[p + 2];
                    }
                    else
                    {
                        r = MathUtils.Atan2(dy, dx);
                    }
                    r -= MathUtils.Atan2(c, a);
                    if (tip)
                    {
                        cos = MathUtils.Cos(r);
                        sin = MathUtils.Sin(r);
                        float length = bone.data.length;
                        boneX += (length * (cos * a - sin * c) - dx) * rotateMix;
                        boneY += (length * (sin * a + cos * c) - dy) * rotateMix;
                    }
                    else
                    {
                        r += offsetRotation;
                    }
                    if (r > MathUtils.PI)
                    {
                        r -= MathUtils.PI2;
                    }
                    else if (r < -MathUtils.PI)                     //
                    {
                        r += MathUtils.PI2;
                    }
                    r     *= rotateMix;
                    cos    = MathUtils.Cos(r);
                    sin    = MathUtils.Sin(r);
                    bone.a = cos * a - sin * c;
                    bone.b = cos * b - sin * d;
                    bone.c = sin * a + cos * c;
                    bone.d = sin * b + cos * d;
                }
                bone.appliedValid = false;
            }
        }
예제 #17
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);
        }
예제 #18
0
        private void ReadAnimation(Dictionary <string, object> map, string name, SkeletonData skeletonData)
        {
            float scale = Scale;
            ExposedList <Timeline> exposedList = new ExposedList <Timeline>();
            float num = 0f;

            if (map.ContainsKey("slots"))
            {
                foreach (KeyValuePair <string, object> item3 in (Dictionary <string, object>)map["slots"])
                {
                    string key       = item3.Key;
                    int    slotIndex = skeletonData.FindSlotIndex(key);
                    Dictionary <string, object> dictionary = (Dictionary <string, object>)item3.Value;
                    foreach (KeyValuePair <string, object> item4 in dictionary)
                    {
                        List <object> list = (List <object>)item4.Value;
                        string        key2 = item4.Key;
                        if (key2 == "attachment")
                        {
                            AttachmentTimeline attachmentTimeline = new AttachmentTimeline(list.Count);
                            attachmentTimeline.slotIndex = slotIndex;
                            int num2 = 0;
                            foreach (Dictionary <string, object> item5 in list)
                            {
                                float time = (float)item5["time"];
                                attachmentTimeline.SetFrame(num2++, time, (string)item5["name"]);
                            }
                            exposedList.Add(attachmentTimeline);
                            num = Math.Max(num, attachmentTimeline.frames[attachmentTimeline.FrameCount - 1]);
                        }
                        else if (key2 == "color")
                        {
                            ColorTimeline colorTimeline = new ColorTimeline(list.Count);
                            colorTimeline.slotIndex = slotIndex;
                            int num4 = 0;
                            foreach (Dictionary <string, object> item6 in list)
                            {
                                float  time2     = (float)item6["time"];
                                string hexString = (string)item6["color"];
                                colorTimeline.SetFrame(num4, time2, ToColor(hexString, 0), ToColor(hexString, 1), ToColor(hexString, 2), ToColor(hexString, 3));
                                ReadCurve(item6, colorTimeline, num4);
                                num4++;
                            }
                            exposedList.Add(colorTimeline);
                            num = Math.Max(num, colorTimeline.frames[(colorTimeline.FrameCount - 1) * 5]);
                        }
                        else
                        {
                            if (!(key2 == "twoColor"))
                            {
                                throw new Exception("Invalid timeline type for a slot: " + key2 + " (" + key + ")");
                            }
                            TwoColorTimeline twoColorTimeline = new TwoColorTimeline(list.Count);
                            twoColorTimeline.slotIndex = slotIndex;
                            int num5 = 0;
                            foreach (Dictionary <string, object> item7 in list)
                            {
                                float  time3      = (float)item7["time"];
                                string hexString2 = (string)item7["light"];
                                string hexString3 = (string)item7["dark"];
                                twoColorTimeline.SetFrame(num5, time3, ToColor(hexString2, 0), ToColor(hexString2, 1), ToColor(hexString2, 2), ToColor(hexString2, 3), ToColor(hexString3, 0, 6), ToColor(hexString3, 1, 6), ToColor(hexString3, 2, 6));
                                ReadCurve(item7, twoColorTimeline, num5);
                                num5++;
                            }
                            exposedList.Add(twoColorTimeline);
                            num = Math.Max(num, twoColorTimeline.frames[(twoColorTimeline.FrameCount - 1) * 8]);
                        }
                    }
                }
            }
            if (map.ContainsKey("bones"))
            {
                foreach (KeyValuePair <string, object> item8 in (Dictionary <string, object>)map["bones"])
                {
                    string key3 = item8.Key;
                    int    num6 = skeletonData.FindBoneIndex(key3);
                    if (num6 == -1)
                    {
                        throw new Exception("Bone not found: " + key3);
                    }
                    Dictionary <string, object> dictionary5 = (Dictionary <string, object>)item8.Value;
                    foreach (KeyValuePair <string, object> item9 in dictionary5)
                    {
                        List <object> list2 = (List <object>)item9.Value;
                        string        key4  = item9.Key;
                        if (key4 == "rotate")
                        {
                            RotateTimeline rotateTimeline = new RotateTimeline(list2.Count);
                            rotateTimeline.boneIndex = num6;
                            int num7 = 0;
                            foreach (Dictionary <string, object> item10 in list2)
                            {
                                rotateTimeline.SetFrame(num7, (float)item10["time"], (float)item10["angle"]);
                                ReadCurve(item10, rotateTimeline, num7);
                                num7++;
                            }
                            exposedList.Add(rotateTimeline);
                            num = Math.Max(num, rotateTimeline.frames[(rotateTimeline.FrameCount - 1) * 2]);
                        }
                        else
                        {
                            if (!(key4 == "translate") && !(key4 == "scale") && !(key4 == "shear"))
                            {
                                throw new Exception("Invalid timeline type for a bone: " + key4 + " (" + key3 + ")");
                            }
                            float             num8 = 1f;
                            TranslateTimeline translateTimeline;
                            if (key4 == "scale")
                            {
                                translateTimeline = new ScaleTimeline(list2.Count);
                            }
                            else if (key4 == "shear")
                            {
                                translateTimeline = new ShearTimeline(list2.Count);
                            }
                            else
                            {
                                translateTimeline = new TranslateTimeline(list2.Count);
                                num8 = scale;
                            }
                            translateTimeline.boneIndex = num6;
                            int num9 = 0;
                            foreach (Dictionary <string, object> item11 in list2)
                            {
                                float time4  = (float)item11["time"];
                                float @float = GetFloat(item11, "x", 0f);
                                float float2 = GetFloat(item11, "y", 0f);
                                translateTimeline.SetFrame(num9, time4, @float * num8, float2 * num8);
                                ReadCurve(item11, translateTimeline, num9);
                                num9++;
                            }
                            exposedList.Add(translateTimeline);
                            num = Math.Max(num, translateTimeline.frames[(translateTimeline.FrameCount - 1) * 3]);
                        }
                    }
                }
            }
            if (map.ContainsKey("ik"))
            {
                foreach (KeyValuePair <string, object> item12 in (Dictionary <string, object>)map["ik"])
                {
                    IkConstraintData     item  = skeletonData.FindIkConstraint(item12.Key);
                    List <object>        list3 = (List <object>)item12.Value;
                    IkConstraintTimeline ikConstraintTimeline = new IkConstraintTimeline(list3.Count);
                    ikConstraintTimeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(item);
                    int num10 = 0;
                    foreach (Dictionary <string, object> item13 in list3)
                    {
                        float time5   = (float)item13["time"];
                        float float3  = GetFloat(item13, "mix", 1f);
                        bool  boolean = GetBoolean(item13, "bendPositive", defaultValue: true);
                        ikConstraintTimeline.SetFrame(num10, time5, float3, boolean ? 1 : (-1));
                        ReadCurve(item13, ikConstraintTimeline, num10);
                        num10++;
                    }
                    exposedList.Add(ikConstraintTimeline);
                    num = Math.Max(num, ikConstraintTimeline.frames[(ikConstraintTimeline.FrameCount - 1) * 3]);
                }
            }
            if (map.ContainsKey("transform"))
            {
                foreach (KeyValuePair <string, object> item14 in (Dictionary <string, object>)map["transform"])
                {
                    TransformConstraintData     item2 = skeletonData.FindTransformConstraint(item14.Key);
                    List <object>               list4 = (List <object>)item14.Value;
                    TransformConstraintTimeline transformConstraintTimeline = new TransformConstraintTimeline(list4.Count);
                    transformConstraintTimeline.transformConstraintIndex = skeletonData.transformConstraints.IndexOf(item2);
                    int num11 = 0;
                    foreach (Dictionary <string, object> item15 in list4)
                    {
                        float time6  = (float)item15["time"];
                        float float4 = GetFloat(item15, "rotateMix", 1f);
                        float float5 = GetFloat(item15, "translateMix", 1f);
                        float float6 = GetFloat(item15, "scaleMix", 1f);
                        float float7 = GetFloat(item15, "shearMix", 1f);
                        transformConstraintTimeline.SetFrame(num11, time6, float4, float5, float6, float7);
                        ReadCurve(item15, transformConstraintTimeline, num11);
                        num11++;
                    }
                    exposedList.Add(transformConstraintTimeline);
                    num = Math.Max(num, transformConstraintTimeline.frames[(transformConstraintTimeline.FrameCount - 1) * 5]);
                }
            }
            if (map.ContainsKey("paths"))
            {
                foreach (KeyValuePair <string, object> item16 in (Dictionary <string, object>)map["paths"])
                {
                    int num12 = skeletonData.FindPathConstraintIndex(item16.Key);
                    if (num12 == -1)
                    {
                        throw new Exception("Path constraint not found: " + item16.Key);
                    }
                    PathConstraintData          pathConstraintData = skeletonData.pathConstraints.Items[num12];
                    Dictionary <string, object> dictionary10       = (Dictionary <string, object>)item16.Value;
                    foreach (KeyValuePair <string, object> item17 in dictionary10)
                    {
                        List <object> list5 = (List <object>)item17.Value;
                        string        key5  = item17.Key;
                        switch (key5)
                        {
                        case "position":
                        case "spacing":
                        {
                            float num14 = 1f;
                            PathConstraintPositionTimeline pathConstraintPositionTimeline;
                            if (key5 == "spacing")
                            {
                                pathConstraintPositionTimeline = new PathConstraintSpacingTimeline(list5.Count);
                                if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                                {
                                    num14 = scale;
                                }
                            }
                            else
                            {
                                pathConstraintPositionTimeline = new PathConstraintPositionTimeline(list5.Count);
                                if (pathConstraintData.positionMode == PositionMode.Fixed)
                                {
                                    num14 = scale;
                                }
                            }
                            pathConstraintPositionTimeline.pathConstraintIndex = num12;
                            int num15 = 0;
                            foreach (Dictionary <string, object> item18 in list5)
                            {
                                pathConstraintPositionTimeline.SetFrame(num15, (float)item18["time"], GetFloat(item18, key5, 0f) * num14);
                                ReadCurve(item18, pathConstraintPositionTimeline, num15);
                                num15++;
                            }
                            exposedList.Add(pathConstraintPositionTimeline);
                            num = Math.Max(num, pathConstraintPositionTimeline.frames[(pathConstraintPositionTimeline.FrameCount - 1) * 2]);
                            break;
                        }

                        case "mix":
                        {
                            PathConstraintMixTimeline pathConstraintMixTimeline = new PathConstraintMixTimeline(list5.Count);
                            pathConstraintMixTimeline.pathConstraintIndex = num12;
                            int num13 = 0;
                            foreach (Dictionary <string, object> item19 in list5)
                            {
                                pathConstraintMixTimeline.SetFrame(num13, (float)item19["time"], GetFloat(item19, "rotateMix", 1f), GetFloat(item19, "translateMix", 1f));
                                ReadCurve(item19, pathConstraintMixTimeline, num13);
                                num13++;
                            }
                            exposedList.Add(pathConstraintMixTimeline);
                            num = Math.Max(num, pathConstraintMixTimeline.frames[(pathConstraintMixTimeline.FrameCount - 1) * 3]);
                            break;
                        }
                        }
                    }
                }
            }
            if (map.ContainsKey("deform"))
            {
                foreach (KeyValuePair <string, object> item20 in (Dictionary <string, object>)map["deform"])
                {
                    Skin skin = skeletonData.FindSkin(item20.Key);
                    foreach (KeyValuePair <string, object> item21 in (Dictionary <string, object>)item20.Value)
                    {
                        int num16 = skeletonData.FindSlotIndex(item21.Key);
                        if (num16 == -1)
                        {
                            throw new Exception("Slot not found: " + item21.Key);
                        }
                        foreach (KeyValuePair <string, object> item22 in (Dictionary <string, object>)item21.Value)
                        {
                            List <object>    list6            = (List <object>)item22.Value;
                            VertexAttachment vertexAttachment = (VertexAttachment)skin.GetAttachment(num16, item22.Key);
                            if (vertexAttachment == null)
                            {
                                throw new Exception("Deform attachment not found: " + item22.Key);
                            }
                            bool           flag           = vertexAttachment.bones != null;
                            float[]        vertices       = vertexAttachment.vertices;
                            int            num17          = (!flag) ? vertices.Length : (vertices.Length / 3 * 2);
                            DeformTimeline deformTimeline = new DeformTimeline(list6.Count);
                            deformTimeline.slotIndex  = num16;
                            deformTimeline.attachment = vertexAttachment;
                            int num18 = 0;
                            foreach (Dictionary <string, object> item23 in list6)
                            {
                                float[] array;
                                if (!item23.ContainsKey("vertices"))
                                {
                                    array = ((!flag) ? vertices : new float[num17]);
                                }
                                else
                                {
                                    array = new float[num17];
                                    int     @int       = GetInt(item23, "offset", 0);
                                    float[] floatArray = GetFloatArray(item23, "vertices", 1f);
                                    Array.Copy(floatArray, 0, array, @int, floatArray.Length);
                                    if (scale != 1f)
                                    {
                                        int i = @int;
                                        for (int num19 = i + floatArray.Length; i < num19; i++)
                                        {
                                            array[i] *= scale;
                                        }
                                    }
                                    if (!flag)
                                    {
                                        for (int j = 0; j < num17; j++)
                                        {
                                            array[j] += vertices[j];
                                        }
                                    }
                                }
                                deformTimeline.SetFrame(num18, (float)item23["time"], array);
                                ReadCurve(item23, deformTimeline, num18);
                                num18++;
                            }
                            exposedList.Add(deformTimeline);
                            num = Math.Max(num, deformTimeline.frames[deformTimeline.FrameCount - 1]);
                        }
                    }
                }
            }
            if (map.ContainsKey("drawOrder") || map.ContainsKey("draworder"))
            {
                List <object>     list7             = (List <object>)map[(!map.ContainsKey("drawOrder")) ? "draworder" : "drawOrder"];
                DrawOrderTimeline drawOrderTimeline = new DrawOrderTimeline(list7.Count);
                int count = skeletonData.slots.Count;
                int num20 = 0;
                foreach (Dictionary <string, object> item24 in list7)
                {
                    int[] array2 = null;
                    if (item24.ContainsKey("offsets"))
                    {
                        array2 = new int[count];
                        for (int num21 = count - 1; num21 >= 0; num21--)
                        {
                            array2[num21] = -1;
                        }
                        List <object> list8  = (List <object>)item24["offsets"];
                        int[]         array3 = new int[count - list8.Count];
                        int           num22  = 0;
                        int           num23  = 0;
                        foreach (Dictionary <string, object> item25 in list8)
                        {
                            int num24 = skeletonData.FindSlotIndex((string)item25["slot"]);
                            if (num24 == -1)
                            {
                                throw new Exception("Slot not found: " + item25["slot"]);
                            }
                            while (num22 != num24)
                            {
                                array3[num23++] = num22++;
                            }
                            int num27 = num22 + (int)(float)item25["offset"];
                            array2[num27] = num22++;
                        }
                        while (num22 < count)
                        {
                            array3[num23++] = num22++;
                        }
                        for (int num31 = count - 1; num31 >= 0; num31--)
                        {
                            if (array2[num31] == -1)
                            {
                                array2[num31] = array3[--num23];
                            }
                        }
                    }
                    drawOrderTimeline.SetFrame(num20++, (float)item24["time"], array2);
                }
                exposedList.Add(drawOrderTimeline);
                num = Math.Max(num, drawOrderTimeline.frames[drawOrderTimeline.FrameCount - 1]);
            }
            if (map.ContainsKey("events"))
            {
                List <object> list9         = (List <object>)map["events"];
                EventTimeline eventTimeline = new EventTimeline(list9.Count);
                int           num33         = 0;
                foreach (Dictionary <string, object> item26 in list9)
                {
                    EventData eventData = skeletonData.FindEvent((string)item26["name"]);
                    if (eventData == null)
                    {
                        throw new Exception("Event not found: " + item26["name"]);
                    }
                    Event @event = new Event((float)item26["time"], eventData);
                    @event.Int    = GetInt(item26, "int", eventData.Int);
                    @event.Float  = GetFloat(item26, "float", eventData.Float);
                    @event.String = GetString(item26, "string", eventData.String);
                    eventTimeline.SetFrame(num33++, @event);
                }
                exposedList.Add(eventTimeline);
                num = Math.Max(num, eventTimeline.frames[eventTimeline.FrameCount - 1]);
            }
            exposedList.TrimExcess();
            skeletonData.animations.Add(new Animation(name, exposedList, num));
        }
예제 #19
0
        public void Update()
        {
            PathAttachment attachment = target.Attachment as PathAttachment;

            if (attachment == null)
            {
                return;
            }

            float mixRotate = this.mixRotate, mixX = this.mixX, mixY = this.mixY;

            if (mixRotate == 0 && mixX == 0 && mixY == 0)
            {
                return;
            }

            PathConstraintData data = this.data;
            bool tangents = data.rotateMode == RotateMode.Tangent, scale = data.rotateMode == RotateMode.ChainScale;
            int  boneCount = this.bones.Count, spacesCount = tangents ? boneCount : boneCount + 1;

            Bone[]  bonesItems = this.bones.Items;
            float[] spaces     = this.spaces.Resize(spacesCount).Items, lengths = scale ? this.lengths.Resize(boneCount).Items : null;
            float   spacing    = this.spacing;

            switch (data.spacingMode)
            {
            case SpacingMode.Percent:
                if (scale)
                {
                    for (int i = 0, n = spacesCount - 1; i < n; i++)
                    {
                        Bone  bone        = bonesItems[i];
                        float setupLength = bone.data.length;
                        if (setupLength < PathConstraint.Epsilon)
                        {
                            lengths[i] = 0;
                        }
                        else
                        {
                            float x = setupLength * bone.a, y = setupLength * bone.c;
                            lengths[i] = (float)Math.Sqrt(x * x + y * y);
                        }
                    }
                }
                ArraysFill(spaces, 1, spacesCount, spacing);
                break;

            case SpacingMode.Proportional: {
                float sum = 0;
                for (int i = 0, n = spacesCount - 1; i < n;)
                {
                    Bone  bone        = bonesItems[i];
                    float setupLength = bone.data.length;
                    if (setupLength < PathConstraint.Epsilon)
                    {
                        if (scale)
                        {
                            lengths[i] = 0;
                        }
                        spaces[++i] = spacing;
                    }
                    else
                    {
                        float x = setupLength * bone.a, y = setupLength * bone.c;
                        float length = (float)Math.Sqrt(x * x + y * y);
                        if (scale)
                        {
                            lengths[i] = length;
                        }
                        spaces[++i] = length;
                        sum        += length;
                    }
                }
                if (sum > 0)
                {
                    sum = spacesCount / sum * spacing;
                    for (int i = 1; i < spacesCount; i++)
                    {
                        spaces[i] *= sum;
                    }
                }
                break;
            }

            default: {
                bool lengthSpacing = data.spacingMode == SpacingMode.Length;
                for (int i = 0, n = spacesCount - 1; i < n;)
                {
                    Bone  bone        = bonesItems[i];
                    float setupLength = bone.data.length;
                    if (setupLength < PathConstraint.Epsilon)
                    {
                        if (scale)
                        {
                            lengths[i] = 0;
                        }
                        spaces[++i] = spacing;
                    }
                    else
                    {
                        float x = setupLength * bone.a, y = setupLength * bone.c;
                        float length = (float)Math.Sqrt(x * x + y * y);
                        if (scale)
                        {
                            lengths[i] = length;
                        }
                        spaces[++i] = (lengthSpacing ? setupLength + spacing : spacing) * length / setupLength;
                    }
                }
                break;
            }
            }

            float[] positions = ComputeWorldPositions(attachment, spacesCount, tangents);
            float   boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation;
            bool    tip;

            if (offsetRotation == 0)
            {
                tip = data.rotateMode == RotateMode.Chain;
            }
            else
            {
                tip = false;
                Bone p = target.bone;
                offsetRotation *= p.a * p.d - p.b * p.c > 0 ? MathUtils.DegRad : -MathUtils.DegRad;
            }
            for (int i = 0, p = 3; i < boneCount; i++, p += 3)
            {
                Bone bone = bonesItems[i];
                bone.worldX += (boneX - bone.worldX) * mixX;
                bone.worldY += (boneY - bone.worldY) * mixY;
                float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
                if (scale)
                {
                    float length = lengths[i];
                    if (length >= PathConstraint.Epsilon)
                    {
                        float s = ((float)Math.Sqrt(dx * dx + dy * dy) / length - 1) * mixRotate + 1;
                        bone.a *= s;
                        bone.c *= s;
                    }
                }
                boneX = x;
                boneY = y;
                if (mixRotate > 0)
                {
                    float a = bone.a, b = bone.b, c = bone.c, d = bone.d, r, cos, sin;
                    if (tangents)
                    {
                        r = positions[p - 1];
                    }
                    else if (spaces[i + 1] < PathConstraint.Epsilon)
                    {
                        r = positions[p + 2];
                    }
                    else
                    {
                        r = MathUtils.Atan2(dy, dx);
                    }
                    r -= MathUtils.Atan2(c, a);
                    if (tip)
                    {
                        cos = MathUtils.Cos(r);
                        sin = MathUtils.Sin(r);
                        float length = bone.data.length;
                        boneX += (length * (cos * a - sin * c) - dx) * mixRotate;
                        boneY += (length * (sin * a + cos * c) - dy) * mixRotate;
                    }
                    else
                    {
                        r += offsetRotation;
                    }
                    if (r > MathUtils.PI)
                    {
                        r -= MathUtils.PI2;
                    }
                    else if (r < -MathUtils.PI)                     //
                    {
                        r += MathUtils.PI2;
                    }
                    r     *= mixRotate;
                    cos    = MathUtils.Cos(r);
                    sin    = MathUtils.Sin(r);
                    bone.a = cos * a - sin * c;
                    bone.b = cos * b - sin * d;
                    bone.c = sin * a + cos * c;
                    bone.d = sin * b + cos * d;
                }
                bone.UpdateAppliedTransform();
            }
        }
예제 #20
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);
        }
		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;
		}
예제 #22
0
        public void Update()
        {
            PathAttachment attachment = target.Attachment as PathAttachment;

            if (attachment == null)
            {
                return;
            }

            float rotateMix = this.rotateMix, translateMix = this.translateMix;
            bool  translate = translateMix > 0, rotate = rotateMix > 0;

            if (!translate && !rotate)
            {
                return;
            }

            PathConstraintData data = this.data;
            SpacingMode        spacingMode = data.spacingMode;
            bool       lengthSpacing = spacingMode == SpacingMode.Length;
            RotateMode rotateMode = data.rotateMode;
            bool       tangents = rotateMode == RotateMode.Tangent, scale = rotateMode == RotateMode.ChainScale;
            int        boneCount = this.bones.Count, spacesCount = tangents ? boneCount : boneCount + 1;

            Bone[] bones = this.bones.Items;
            ExposedList <float> spaces = this.spaces.Resize(spacesCount), lengths = null;
            float spacing = this.spacing;

            if (scale || lengthSpacing)
            {
                if (scale)
                {
                    lengths = this.lengths.Resize(boneCount);
                }
                for (int i = 0, n = spacesCount - 1; i < n;)
                {
                    Bone  bone = bones[i];
                    float length = bone.data.length, x = length * bone.a, y = length * bone.c;
                    length = (float)Math.Sqrt(x * x + y * y);
                    if (scale)
                    {
                        lengths.Items[i] = length;
                    }
                    spaces.Items[++i] = lengthSpacing ? Math.Max(0, length + spacing) : spacing;
                }
            }
            else
            {
                for (int i = 1; i < spacesCount; i++)
                {
                    spaces.Items[i] = spacing;
                }
            }

            float[] positions = ComputeWorldPositions(attachment, spacesCount, tangents,
                                                      data.positionMode == PositionMode.Percent, spacingMode == SpacingMode.Percent);
            Skeleton skeleton = target.Skeleton;
            float    skeletonX = skeleton.x, skeletonY = skeleton.y;
            float    boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation;
            bool     tip = rotateMode == RotateMode.Chain && offsetRotation == 0;

            for (int i = 0, p = 3; i < boneCount; i++, p += 3)
            {
                Bone bone = (Bone)bones[i];
                bone.worldX += (boneX - skeletonX - bone.worldX) * translateMix;
                bone.worldY += (boneY - skeletonY - bone.worldY) * translateMix;
                float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
                if (scale)
                {
                    float length = lengths.Items[i];
                    if (length != 0)
                    {
                        float s = ((float)Math.Sqrt(dx * dx + dy * dy) / length - 1) * rotateMix + 1;
                        bone.a *= s;
                        bone.c *= s;
                    }
                }
                boneX = x;
                boneY = y;
                if (rotate)
                {
                    float a = bone.a, b = bone.b, c = bone.c, d = bone.d, r, cos, sin;
                    if (tangents)
                    {
                        r = positions[p - 1];
                    }
                    else if (spaces.Items[i + 1] == 0)
                    {
                        r = positions[p + 2];
                    }
                    else
                    {
                        r = MathUtils.Atan2(dy, dx);
                    }
                    r -= MathUtils.Atan2(c, a) - offsetRotation * MathUtils.degRad;
                    if (tip)
                    {
                        cos = MathUtils.Cos(r);
                        sin = MathUtils.Sin(r);
                        float length = bone.data.length;
                        boneX += (length * (cos * a - sin * c) - dx) * rotateMix;
                        boneY += (length * (sin * a + cos * c) - dy) * rotateMix;
                    }
                    if (r > MathUtils.PI)
                    {
                        r -= MathUtils.PI2;
                    }
                    else if (r < -MathUtils.PI)                     //
                    {
                        r += MathUtils.PI2;
                    }
                    r     *= rotateMix;
                    cos    = MathUtils.Cos(r);
                    sin    = MathUtils.Sin(r);
                    bone.a = cos * a - sin * c;
                    bone.b = cos * b - sin * d;
                    bone.c = sin * a + cos * c;
                    bone.d = sin * b + cos * d;
                }
            }
        }
예제 #23
0
        private void ReadAnimation(string name, Stream input, SkeletonData skeletonData)
        {
            ExposedList <Timeline> exposedList = new ExposedList <Timeline>();
            float scale = Scale;
            float num   = 0f;
            int   i     = 0;

            for (int num2 = ReadVarint(input, optimizePositive: true); i < num2; i++)
            {
                int slotIndex = ReadVarint(input, optimizePositive: true);
                int j         = 0;
                for (int num3 = ReadVarint(input, optimizePositive: true); j < num3; j++)
                {
                    int num4 = input.ReadByte();
                    int num5 = ReadVarint(input, optimizePositive: true);
                    switch (num4)
                    {
                    case 0:
                    {
                        AttachmentTimeline attachmentTimeline = new AttachmentTimeline(num5);
                        attachmentTimeline.slotIndex = slotIndex;
                        for (int m = 0; m < num5; m++)
                        {
                            attachmentTimeline.SetFrame(m, ReadFloat(input), ReadString(input));
                        }
                        exposedList.Add(attachmentTimeline);
                        num = Math.Max(num, attachmentTimeline.frames[num5 - 1]);
                        break;
                    }

                    case 1:
                    {
                        ColorTimeline colorTimeline = new ColorTimeline(num5);
                        colorTimeline.slotIndex = slotIndex;
                        for (int l = 0; l < num5; l++)
                        {
                            float time2 = ReadFloat(input);
                            int   num8  = ReadInt(input);
                            float r3    = (float)((num8 & 4278190080u) >> 24) / 255f;
                            float g3    = (float)((num8 & 0xFF0000) >> 16) / 255f;
                            float b3    = (float)((num8 & 0xFF00) >> 8) / 255f;
                            float a2    = (float)(num8 & 0xFF) / 255f;
                            colorTimeline.SetFrame(l, time2, r3, g3, b3, a2);
                            if (l < num5 - 1)
                            {
                                ReadCurve(input, l, colorTimeline);
                            }
                        }
                        exposedList.Add(colorTimeline);
                        num = Math.Max(num, colorTimeline.frames[(colorTimeline.FrameCount - 1) * 5]);
                        break;
                    }

                    case 2:
                    {
                        TwoColorTimeline twoColorTimeline = new TwoColorTimeline(num5);
                        twoColorTimeline.slotIndex = slotIndex;
                        for (int k = 0; k < num5; k++)
                        {
                            float time = ReadFloat(input);
                            int   num6 = ReadInt(input);
                            float r    = (float)((num6 & 4278190080u) >> 24) / 255f;
                            float g    = (float)((num6 & 0xFF0000) >> 16) / 255f;
                            float b    = (float)((num6 & 0xFF00) >> 8) / 255f;
                            float a    = (float)(num6 & 0xFF) / 255f;
                            int   num7 = ReadInt(input);
                            float r2   = (float)((num7 & 0xFF0000) >> 16) / 255f;
                            float g2   = (float)((num7 & 0xFF00) >> 8) / 255f;
                            float b2   = (float)(num7 & 0xFF) / 255f;
                            twoColorTimeline.SetFrame(k, time, r, g, b, a, r2, g2, b2);
                            if (k < num5 - 1)
                            {
                                ReadCurve(input, k, twoColorTimeline);
                            }
                        }
                        exposedList.Add(twoColorTimeline);
                        num = Math.Max(num, twoColorTimeline.frames[(twoColorTimeline.FrameCount - 1) * 8]);
                        break;
                    }
                    }
                }
            }
            int n = 0;

            for (int num9 = ReadVarint(input, optimizePositive: true); n < num9; n++)
            {
                int boneIndex = ReadVarint(input, optimizePositive: true);
                int num10     = 0;
                for (int num11 = ReadVarint(input, optimizePositive: true); num10 < num11; num10++)
                {
                    int num12 = input.ReadByte();
                    int num13 = ReadVarint(input, optimizePositive: true);
                    switch (num12)
                    {
                    case 0:
                    {
                        RotateTimeline rotateTimeline = new RotateTimeline(num13);
                        rotateTimeline.boneIndex = boneIndex;
                        for (int num16 = 0; num16 < num13; num16++)
                        {
                            rotateTimeline.SetFrame(num16, ReadFloat(input), ReadFloat(input));
                            if (num16 < num13 - 1)
                            {
                                ReadCurve(input, num16, rotateTimeline);
                            }
                        }
                        exposedList.Add(rotateTimeline);
                        num = Math.Max(num, rotateTimeline.frames[(num13 - 1) * 2]);
                        break;
                    }

                    case 1:
                    case 2:
                    case 3:
                    {
                        float             num14 = 1f;
                        TranslateTimeline translateTimeline;
                        switch (num12)
                        {
                        case 2:
                            translateTimeline = new ScaleTimeline(num13);
                            break;

                        case 3:
                            translateTimeline = new ShearTimeline(num13);
                            break;

                        default:
                            translateTimeline = new TranslateTimeline(num13);
                            num14             = scale;
                            break;
                        }
                        translateTimeline.boneIndex = boneIndex;
                        for (int num15 = 0; num15 < num13; num15++)
                        {
                            translateTimeline.SetFrame(num15, ReadFloat(input), ReadFloat(input) * num14, ReadFloat(input) * num14);
                            if (num15 < num13 - 1)
                            {
                                ReadCurve(input, num15, translateTimeline);
                            }
                        }
                        exposedList.Add(translateTimeline);
                        num = Math.Max(num, translateTimeline.frames[(num13 - 1) * 3]);
                        break;
                    }
                    }
                }
            }
            int num17 = 0;

            for (int num18 = ReadVarint(input, optimizePositive: true); num17 < num18; num17++)
            {
                int ikConstraintIndex = ReadVarint(input, optimizePositive: true);
                int num19             = ReadVarint(input, optimizePositive: true);
                IkConstraintTimeline ikConstraintTimeline = new IkConstraintTimeline(num19);
                ikConstraintTimeline.ikConstraintIndex = ikConstraintIndex;
                for (int num20 = 0; num20 < num19; num20++)
                {
                    ikConstraintTimeline.SetFrame(num20, ReadFloat(input), ReadFloat(input), ReadSByte(input));
                    if (num20 < num19 - 1)
                    {
                        ReadCurve(input, num20, ikConstraintTimeline);
                    }
                }
                exposedList.Add(ikConstraintTimeline);
                num = Math.Max(num, ikConstraintTimeline.frames[(num19 - 1) * 3]);
            }
            int num21 = 0;

            for (int num22 = ReadVarint(input, optimizePositive: true); num21 < num22; num21++)
            {
                int transformConstraintIndex = ReadVarint(input, optimizePositive: true);
                int num23 = ReadVarint(input, optimizePositive: true);
                TransformConstraintTimeline transformConstraintTimeline = new TransformConstraintTimeline(num23);
                transformConstraintTimeline.transformConstraintIndex = transformConstraintIndex;
                for (int num24 = 0; num24 < num23; num24++)
                {
                    transformConstraintTimeline.SetFrame(num24, ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input), ReadFloat(input));
                    if (num24 < num23 - 1)
                    {
                        ReadCurve(input, num24, transformConstraintTimeline);
                    }
                }
                exposedList.Add(transformConstraintTimeline);
                num = Math.Max(num, transformConstraintTimeline.frames[(num23 - 1) * 5]);
            }
            int num25 = 0;

            for (int num26 = ReadVarint(input, optimizePositive: true); num25 < num26; num25++)
            {
                int num27 = ReadVarint(input, optimizePositive: true);
                PathConstraintData pathConstraintData = skeletonData.pathConstraints.Items[num27];
                int num28 = 0;
                for (int num29 = ReadVarint(input, optimizePositive: true); num28 < num29; num28++)
                {
                    int num30 = ReadSByte(input);
                    int num31 = ReadVarint(input, optimizePositive: true);
                    switch (num30)
                    {
                    case 0:
                    case 1:
                    {
                        float num33 = 1f;
                        PathConstraintPositionTimeline pathConstraintPositionTimeline;
                        if (num30 == 1)
                        {
                            pathConstraintPositionTimeline = new PathConstraintSpacingTimeline(num31);
                            if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                            {
                                num33 = scale;
                            }
                        }
                        else
                        {
                            pathConstraintPositionTimeline = new PathConstraintPositionTimeline(num31);
                            if (pathConstraintData.positionMode == PositionMode.Fixed)
                            {
                                num33 = scale;
                            }
                        }
                        pathConstraintPositionTimeline.pathConstraintIndex = num27;
                        for (int num34 = 0; num34 < num31; num34++)
                        {
                            pathConstraintPositionTimeline.SetFrame(num34, ReadFloat(input), ReadFloat(input) * num33);
                            if (num34 < num31 - 1)
                            {
                                ReadCurve(input, num34, pathConstraintPositionTimeline);
                            }
                        }
                        exposedList.Add(pathConstraintPositionTimeline);
                        num = Math.Max(num, pathConstraintPositionTimeline.frames[(num31 - 1) * 2]);
                        break;
                    }

                    case 2:
                    {
                        PathConstraintMixTimeline pathConstraintMixTimeline = new PathConstraintMixTimeline(num31);
                        pathConstraintMixTimeline.pathConstraintIndex = num27;
                        for (int num32 = 0; num32 < num31; num32++)
                        {
                            pathConstraintMixTimeline.SetFrame(num32, ReadFloat(input), ReadFloat(input), ReadFloat(input));
                            if (num32 < num31 - 1)
                            {
                                ReadCurve(input, num32, pathConstraintMixTimeline);
                            }
                        }
                        exposedList.Add(pathConstraintMixTimeline);
                        num = Math.Max(num, pathConstraintMixTimeline.frames[(num31 - 1) * 3]);
                        break;
                    }
                    }
                }
            }
            int num35 = 0;

            for (int num36 = ReadVarint(input, optimizePositive: true); num35 < num36; num35++)
            {
                Skin skin  = skeletonData.skins.Items[ReadVarint(input, optimizePositive: true)];
                int  num37 = 0;
                for (int num38 = ReadVarint(input, optimizePositive: true); num37 < num38; num37++)
                {
                    int slotIndex2 = ReadVarint(input, optimizePositive: true);
                    int num39      = 0;
                    for (int num40 = ReadVarint(input, optimizePositive: true); num39 < num40; num39++)
                    {
                        VertexAttachment vertexAttachment = (VertexAttachment)skin.GetAttachment(slotIndex2, ReadString(input));
                        bool             flag             = vertexAttachment.bones != null;
                        float[]          vertices         = vertexAttachment.vertices;
                        int            num41          = (!flag) ? vertices.Length : (vertices.Length / 3 * 2);
                        int            num42          = ReadVarint(input, optimizePositive: true);
                        DeformTimeline deformTimeline = new DeformTimeline(num42);
                        deformTimeline.slotIndex  = slotIndex2;
                        deformTimeline.attachment = vertexAttachment;
                        for (int num43 = 0; num43 < num42; num43++)
                        {
                            float   time3 = ReadFloat(input);
                            int     num44 = ReadVarint(input, optimizePositive: true);
                            float[] array;
                            if (num44 == 0)
                            {
                                array = ((!flag) ? vertices : new float[num41]);
                            }
                            else
                            {
                                array = new float[num41];
                                int num45 = ReadVarint(input, optimizePositive: true);
                                num44 += num45;
                                if (scale == 1f)
                                {
                                    for (int num46 = num45; num46 < num44; num46++)
                                    {
                                        array[num46] = ReadFloat(input);
                                    }
                                }
                                else
                                {
                                    for (int num47 = num45; num47 < num44; num47++)
                                    {
                                        array[num47] = ReadFloat(input) * scale;
                                    }
                                }
                                if (!flag)
                                {
                                    int num48 = 0;
                                    for (int num49 = array.Length; num48 < num49; num48++)
                                    {
                                        array[num48] += vertices[num48];
                                    }
                                }
                            }
                            deformTimeline.SetFrame(num43, time3, array);
                            if (num43 < num42 - 1)
                            {
                                ReadCurve(input, num43, deformTimeline);
                            }
                        }
                        exposedList.Add(deformTimeline);
                        num = Math.Max(num, deformTimeline.frames[num42 - 1]);
                    }
                }
            }
            int num50 = ReadVarint(input, optimizePositive: true);

            if (num50 > 0)
            {
                DrawOrderTimeline drawOrderTimeline = new DrawOrderTimeline(num50);
                int count = skeletonData.slots.Count;
                for (int num51 = 0; num51 < num50; num51++)
                {
                    float time4  = ReadFloat(input);
                    int   num52  = ReadVarint(input, optimizePositive: true);
                    int[] array2 = new int[count];
                    for (int num53 = count - 1; num53 >= 0; num53--)
                    {
                        array2[num53] = -1;
                    }
                    int[] array3 = new int[count - num52];
                    int   num54  = 0;
                    int   num55  = 0;
                    for (int num56 = 0; num56 < num52; num56++)
                    {
                        int num57 = ReadVarint(input, optimizePositive: true);
                        while (num54 != num57)
                        {
                            array3[num55++] = num54++;
                        }
                        array2[num54 + ReadVarint(input, optimizePositive: true)] = num54++;
                    }
                    while (num54 < count)
                    {
                        array3[num55++] = num54++;
                    }
                    for (int num63 = count - 1; num63 >= 0; num63--)
                    {
                        if (array2[num63] == -1)
                        {
                            array2[num63] = array3[--num55];
                        }
                    }
                    drawOrderTimeline.SetFrame(num51, time4, array2);
                }
                exposedList.Add(drawOrderTimeline);
                num = Math.Max(num, drawOrderTimeline.frames[num50 - 1]);
            }
            int num64 = ReadVarint(input, optimizePositive: true);

            if (num64 > 0)
            {
                EventTimeline eventTimeline = new EventTimeline(num64);
                for (int num65 = 0; num65 < num64; num65++)
                {
                    float     time5     = ReadFloat(input);
                    EventData eventData = skeletonData.events.Items[ReadVarint(input, optimizePositive: true)];
                    Event     @event    = new Event(time5, eventData);
                    @event.Int    = ReadVarint(input, optimizePositive: false);
                    @event.Float  = ReadFloat(input);
                    @event.String = ((!ReadBoolean(input)) ? eventData.String : ReadString(input));
                    eventTimeline.SetFrame(num65, @event);
                }
                exposedList.Add(eventTimeline);
                num = Math.Max(num, eventTimeline.frames[num64 - 1]);
            }
            exposedList.TrimExcess();
            skeletonData.animations.Add(new Animation(name, exposedList, num));
        }
예제 #24
0
        private void ReadAnimation(String name, NewStream 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 SLOT_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[(timeline.FrameCount - 1) * ColorTimeline.ENTRIES]);
                        break;
                    }

                    case SLOT_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 BONE_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 - 1) * RotateTimeline.ENTRIES]);
                        break;
                    }

                    case BONE_TRANSLATE:
                    case BONE_SCALE:
                    case BONE_SHEAR: {
                        TranslateTimeline timeline;
                        float             timelineScale = 1;
                        if (timelineType == BONE_SCALE)
                        {
                            timeline = new ScaleTimeline(frameCount);
                        }
                        else if (timelineType == BONE_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 - 1) * TranslateTimeline.ENTRIES]);
                        break;
                    }
                    }
                }
            }

            // IK timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                int index      = ReadVarint(input, true);
                int frameCount = ReadVarint(input, true);
                IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount);
                timeline.ikConstraintIndex = index;
                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 - 1) * IkConstraintTimeline.ENTRIES]);
            }

            // Transform constraint timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                int index      = ReadVarint(input, true);
                int frameCount = ReadVarint(input, true);
                TransformConstraintTimeline timeline = new TransformConstraintTimeline(frameCount);
                timeline.transformConstraintIndex = index;
                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 - 1) * TransformConstraintTimeline.ENTRIES]);
            }

            // Path constraint timelines.
            for (int i = 0, n = ReadVarint(input, true); i < n; i++)
            {
                int index = ReadVarint(input, true);
                PathConstraintData data = skeletonData.pathConstraints.Items[index];
                for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++)
                {
                    int timelineType = ReadSByte(input);
                    int frameCount   = ReadVarint(input, true);
                    switch (timelineType)
                    {
                    case PATH_POSITION:
                    case PATH_SPACING: {
                        PathConstraintPositionTimeline timeline;
                        float timelineScale = 1;
                        if (timelineType == PATH_SPACING)
                        {
                            timeline = new PathConstraintSpacingTimeline(frameCount);
                            if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                            {
                                timelineScale = scale;
                            }
                        }
                        else
                        {
                            timeline = new PathConstraintPositionTimeline(frameCount);
                            if (data.positionMode == PositionMode.Fixed)
                            {
                                timelineScale = scale;
                            }
                        }
                        timeline.pathConstraintIndex = index;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input) * timelineScale);
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintPositionTimeline.ENTRIES]);
                        break;
                    }

                    case PATH_MIX: {
                        PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frameCount);
                        timeline.pathConstraintIndex = index;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, ReadFloat(input), ReadFloat(input), ReadFloat(input));
                            if (frameIndex < frameCount - 1)
                            {
                                ReadCurve(input, frameIndex, timeline);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintMixTimeline.ENTRIES]);
                        break;
                    }
                    }
                }
            }

            // Deform 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++)
                    {
                        VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, ReadString(input));
                        bool             weighted   = attachment.bones != null;
                        float[]          vertices   = attachment.vertices;
                        int deformLength            = weighted ? vertices.Length / 3 * 2 : vertices.Length;

                        int            frameCount = ReadVarint(input, true);
                        DeformTimeline timeline   = new DeformTimeline(frameCount);
                        timeline.slotIndex  = slotIndex;
                        timeline.attachment = attachment;

                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            float   time = ReadFloat(input);
                            float[] deform;
                            int     end = ReadVarint(input, true);
                            if (end == 0)
                            {
                                deform = weighted ? new float[deformLength] : vertices;
                            }
                            else
                            {
                                deform = new float[deformLength];
                                int start = ReadVarint(input, true);
                                end += start;
                                if (scale == 1)
                                {
                                    for (int v = start; v < end; v++)
                                    {
                                        deform[v] = ReadFloat(input);
                                    }
                                }
                                else
                                {
                                    for (int v = start; v < end; v++)
                                    {
                                        deform[v] = ReadFloat(input) * scale;
                                    }
                                }
                                if (!weighted)
                                {
                                    for (int v = 0, vn = deform.Length; v < vn; v++)
                                    {
                                        deform[v] += vertices[v];
                                    }
                                }
                            }

                            timeline.SetFrame(frameIndex, time, deform);
                            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));
        }
		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;
		}
예제 #26
0
        private void ReadAnimation(string name, Stream input, SkeletonData skeletonData)
        {
            ExposedList <Timeline> exposedList = new ExposedList <Timeline>();
            float scale = this.Scale;
            float num   = 0f;
            int   i     = 0;
            int   num2  = SkeletonBinary.ReadVarint(input, true);

            while (i < num2)
            {
                int slotIndex = SkeletonBinary.ReadVarint(input, true);
                int j         = 0;
                int num3      = SkeletonBinary.ReadVarint(input, true);
                while (j < num3)
                {
                    int num4 = input.ReadByte();
                    int num5 = SkeletonBinary.ReadVarint(input, true);
                    if (num4 != 1)
                    {
                        if (num4 == 0)
                        {
                            AttachmentTimeline attachmentTimeline = new AttachmentTimeline(num5);
                            attachmentTimeline.slotIndex = slotIndex;
                            for (int k = 0; k < num5; k++)
                            {
                                attachmentTimeline.SetFrame(k, this.ReadFloat(input), this.ReadString(input));
                            }
                            exposedList.Add(attachmentTimeline);
                            num = Math.Max(num, attachmentTimeline.frames[num5 - 1]);
                        }
                    }
                    else
                    {
                        ColorTimeline colorTimeline = new ColorTimeline(num5);
                        colorTimeline.slotIndex = slotIndex;
                        for (int l = 0; l < num5; l++)
                        {
                            float time = this.ReadFloat(input);
                            int   num6 = SkeletonBinary.ReadInt(input);
                            float r    = (float)((num6 & 4278190080u) >> 24) / 255f;
                            float g    = (float)((num6 & 16711680) >> 16) / 255f;
                            float b    = (float)((num6 & 65280) >> 8) / 255f;
                            float a    = (float)(num6 & 255) / 255f;
                            colorTimeline.SetFrame(l, time, r, g, b, a);
                            if (l < num5 - 1)
                            {
                                this.ReadCurve(input, l, colorTimeline);
                            }
                        }
                        exposedList.Add(colorTimeline);
                        num = Math.Max(num, colorTimeline.frames[(colorTimeline.FrameCount - 1) * 5]);
                    }
                    j++;
                }
                i++;
            }
            int m    = 0;
            int num7 = SkeletonBinary.ReadVarint(input, true);

            while (m < num7)
            {
                int boneIndex = SkeletonBinary.ReadVarint(input, true);
                int n         = 0;
                int num8      = SkeletonBinary.ReadVarint(input, true);
                while (n < num8)
                {
                    int num9  = input.ReadByte();
                    int num10 = SkeletonBinary.ReadVarint(input, true);
                    switch (num9)
                    {
                    case 0:
                    {
                        RotateTimeline rotateTimeline = new RotateTimeline(num10);
                        rotateTimeline.boneIndex = boneIndex;
                        for (int num11 = 0; num11 < num10; num11++)
                        {
                            rotateTimeline.SetFrame(num11, this.ReadFloat(input), this.ReadFloat(input));
                            if (num11 < num10 - 1)
                            {
                                this.ReadCurve(input, num11, rotateTimeline);
                            }
                        }
                        exposedList.Add(rotateTimeline);
                        num = Math.Max(num, rotateTimeline.frames[(num10 - 1) * 2]);
                        break;
                    }

                    case 1:
                    case 2:
                    case 3:
                    {
                        float             num12 = 1f;
                        TranslateTimeline translateTimeline;
                        if (num9 == 2)
                        {
                            translateTimeline = new ScaleTimeline(num10);
                        }
                        else if (num9 == 3)
                        {
                            translateTimeline = new ShearTimeline(num10);
                        }
                        else
                        {
                            translateTimeline = new TranslateTimeline(num10);
                            num12             = scale;
                        }
                        translateTimeline.boneIndex = boneIndex;
                        for (int num13 = 0; num13 < num10; num13++)
                        {
                            translateTimeline.SetFrame(num13, this.ReadFloat(input), this.ReadFloat(input) * num12, this.ReadFloat(input) * num12);
                            if (num13 < num10 - 1)
                            {
                                this.ReadCurve(input, num13, translateTimeline);
                            }
                        }
                        exposedList.Add(translateTimeline);
                        num = Math.Max(num, translateTimeline.frames[(num10 - 1) * 3]);
                        break;
                    }
                    }
                    n++;
                }
                m++;
            }
            int num14 = 0;
            int num15 = SkeletonBinary.ReadVarint(input, true);

            while (num14 < num15)
            {
                int ikConstraintIndex = SkeletonBinary.ReadVarint(input, true);
                int num16             = SkeletonBinary.ReadVarint(input, true);
                IkConstraintTimeline ikConstraintTimeline = new IkConstraintTimeline(num16);
                ikConstraintTimeline.ikConstraintIndex = ikConstraintIndex;
                for (int num17 = 0; num17 < num16; num17++)
                {
                    ikConstraintTimeline.SetFrame(num17, this.ReadFloat(input), this.ReadFloat(input), (int)SkeletonBinary.ReadSByte(input));
                    if (num17 < num16 - 1)
                    {
                        this.ReadCurve(input, num17, ikConstraintTimeline);
                    }
                }
                exposedList.Add(ikConstraintTimeline);
                num = Math.Max(num, ikConstraintTimeline.frames[(num16 - 1) * 3]);
                num14++;
            }
            int num18 = 0;
            int num19 = SkeletonBinary.ReadVarint(input, true);

            while (num18 < num19)
            {
                int transformConstraintIndex = SkeletonBinary.ReadVarint(input, true);
                int num20 = SkeletonBinary.ReadVarint(input, true);
                TransformConstraintTimeline transformConstraintTimeline = new TransformConstraintTimeline(num20);
                transformConstraintTimeline.transformConstraintIndex = transformConstraintIndex;
                for (int num21 = 0; num21 < num20; num21++)
                {
                    transformConstraintTimeline.SetFrame(num21, this.ReadFloat(input), this.ReadFloat(input), this.ReadFloat(input), this.ReadFloat(input), this.ReadFloat(input));
                    if (num21 < num20 - 1)
                    {
                        this.ReadCurve(input, num21, transformConstraintTimeline);
                    }
                }
                exposedList.Add(transformConstraintTimeline);
                num = Math.Max(num, transformConstraintTimeline.frames[(num20 - 1) * 5]);
                num18++;
            }
            int num22 = 0;
            int num23 = SkeletonBinary.ReadVarint(input, true);

            while (num22 < num23)
            {
                int num24 = SkeletonBinary.ReadVarint(input, true);
                PathConstraintData pathConstraintData = skeletonData.pathConstraints.Items[num24];
                int num25 = 0;
                int num26 = SkeletonBinary.ReadVarint(input, true);
                while (num25 < num26)
                {
                    int num27 = (int)SkeletonBinary.ReadSByte(input);
                    int num28 = SkeletonBinary.ReadVarint(input, true);
                    if (num27 != 0 && num27 != 1)
                    {
                        if (num27 == 2)
                        {
                            PathConstraintMixTimeline pathConstraintMixTimeline = new PathConstraintMixTimeline(num28);
                            pathConstraintMixTimeline.pathConstraintIndex = num24;
                            for (int num29 = 0; num29 < num28; num29++)
                            {
                                pathConstraintMixTimeline.SetFrame(num29, this.ReadFloat(input), this.ReadFloat(input), this.ReadFloat(input));
                                if (num29 < num28 - 1)
                                {
                                    this.ReadCurve(input, num29, pathConstraintMixTimeline);
                                }
                            }
                            exposedList.Add(pathConstraintMixTimeline);
                            num = Math.Max(num, pathConstraintMixTimeline.frames[(num28 - 1) * 3]);
                        }
                    }
                    else
                    {
                        float num30 = 1f;
                        PathConstraintPositionTimeline pathConstraintPositionTimeline;
                        if (num27 == 1)
                        {
                            pathConstraintPositionTimeline = new PathConstraintSpacingTimeline(num28);
                            if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed)
                            {
                                num30 = scale;
                            }
                        }
                        else
                        {
                            pathConstraintPositionTimeline = new PathConstraintPositionTimeline(num28);
                            if (pathConstraintData.positionMode == PositionMode.Fixed)
                            {
                                num30 = scale;
                            }
                        }
                        pathConstraintPositionTimeline.pathConstraintIndex = num24;
                        for (int num31 = 0; num31 < num28; num31++)
                        {
                            pathConstraintPositionTimeline.SetFrame(num31, this.ReadFloat(input), this.ReadFloat(input) * num30);
                            if (num31 < num28 - 1)
                            {
                                this.ReadCurve(input, num31, pathConstraintPositionTimeline);
                            }
                        }
                        exposedList.Add(pathConstraintPositionTimeline);
                        num = Math.Max(num, pathConstraintPositionTimeline.frames[(num28 - 1) * 2]);
                    }
                    num25++;
                }
                num22++;
            }
            int num32 = 0;
            int num33 = SkeletonBinary.ReadVarint(input, true);

            while (num32 < num33)
            {
                Skin skin  = skeletonData.skins.Items[SkeletonBinary.ReadVarint(input, true)];
                int  num34 = 0;
                int  num35 = SkeletonBinary.ReadVarint(input, true);
                while (num34 < num35)
                {
                    int slotIndex2 = SkeletonBinary.ReadVarint(input, true);
                    int num36      = 0;
                    int num37      = SkeletonBinary.ReadVarint(input, true);
                    while (num36 < num37)
                    {
                        VertexAttachment vertexAttachment = (VertexAttachment)skin.GetAttachment(slotIndex2, this.ReadString(input));
                        bool             flag             = vertexAttachment.bones != null;
                        float[]          vertices         = vertexAttachment.vertices;
                        int            num38          = (!flag) ? vertices.Length : (vertices.Length / 3 * 2);
                        int            num39          = SkeletonBinary.ReadVarint(input, true);
                        DeformTimeline deformTimeline = new DeformTimeline(num39);
                        deformTimeline.slotIndex  = slotIndex2;
                        deformTimeline.attachment = vertexAttachment;
                        for (int num40 = 0; num40 < num39; num40++)
                        {
                            float   time2 = this.ReadFloat(input);
                            int     num41 = SkeletonBinary.ReadVarint(input, true);
                            float[] array;
                            if (num41 == 0)
                            {
                                array = ((!flag) ? vertices : new float[num38]);
                            }
                            else
                            {
                                array = new float[num38];
                                int num42 = SkeletonBinary.ReadVarint(input, true);
                                num41 += num42;
                                if (scale == 1f)
                                {
                                    for (int num43 = num42; num43 < num41; num43++)
                                    {
                                        array[num43] = this.ReadFloat(input);
                                    }
                                }
                                else
                                {
                                    for (int num44 = num42; num44 < num41; num44++)
                                    {
                                        array[num44] = this.ReadFloat(input) * scale;
                                    }
                                }
                                if (!flag)
                                {
                                    int num45 = 0;
                                    int num46 = array.Length;
                                    while (num45 < num46)
                                    {
                                        array[num45] += vertices[num45];
                                        num45++;
                                    }
                                }
                            }
                            deformTimeline.SetFrame(num40, time2, array);
                            if (num40 < num39 - 1)
                            {
                                this.ReadCurve(input, num40, deformTimeline);
                            }
                        }
                        exposedList.Add(deformTimeline);
                        num = Math.Max(num, deformTimeline.frames[num39 - 1]);
                        num36++;
                    }
                    num34++;
                }
                num32++;
            }
            int num47 = SkeletonBinary.ReadVarint(input, true);

            if (num47 > 0)
            {
                DrawOrderTimeline drawOrderTimeline = new DrawOrderTimeline(num47);
                int count = skeletonData.slots.Count;
                for (int num48 = 0; num48 < num47; num48++)
                {
                    float time3  = this.ReadFloat(input);
                    int   num49  = SkeletonBinary.ReadVarint(input, true);
                    int[] array2 = new int[count];
                    for (int num50 = count - 1; num50 >= 0; num50--)
                    {
                        array2[num50] = -1;
                    }
                    int[] array3 = new int[count - num49];
                    int   num51  = 0;
                    int   num52  = 0;
                    for (int num53 = 0; num53 < num49; num53++)
                    {
                        int num54 = SkeletonBinary.ReadVarint(input, true);
                        while (num51 != num54)
                        {
                            array3[num52++] = num51++;
                        }
                        array2[num51 + SkeletonBinary.ReadVarint(input, true)] = num51++;
                    }
                    while (num51 < count)
                    {
                        array3[num52++] = num51++;
                    }
                    for (int num55 = count - 1; num55 >= 0; num55--)
                    {
                        if (array2[num55] == -1)
                        {
                            array2[num55] = array3[--num52];
                        }
                    }
                    drawOrderTimeline.SetFrame(num48, time3, array2);
                }
                exposedList.Add(drawOrderTimeline);
                num = Math.Max(num, drawOrderTimeline.frames[num47 - 1]);
            }
            int num56 = SkeletonBinary.ReadVarint(input, true);

            if (num56 > 0)
            {
                EventTimeline eventTimeline = new EventTimeline(num56);
                for (int num57 = 0; num57 < num56; num57++)
                {
                    float     time4     = this.ReadFloat(input);
                    EventData eventData = skeletonData.events.Items[SkeletonBinary.ReadVarint(input, true)];
                    eventTimeline.SetFrame(num57, new Event(time4, eventData)
                    {
                        Int    = SkeletonBinary.ReadVarint(input, false),
                        Float  = this.ReadFloat(input),
                        String = ((!SkeletonBinary.ReadBoolean(input)) ? eventData.String : this.ReadString(input))
                    });
                }
                exposedList.Add(eventTimeline);
                num = Math.Max(num, eventTimeline.frames[num56 - 1]);
            }
            exposedList.TrimExcess();
            skeletonData.animations.Add(new Animation(name, exposedList, num));
        }
예제 #27
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));
        }
예제 #28
0
        void ReadPathAnimation(ref ExposedList <Timeline> timelines, SkeletonDataStream input, ref float duration, float scale, ref SkeletonData skeletonData)
        {
            // Path constraint timelines.
            for (int i = 0, n = SkeletonDataStream.sp_readVarint(input.ptr, 1); i < n; i++)
            {
                int index = SkeletonDataStream.sp_readVarint(input.ptr, 1);
                PathConstraintData data = skeletonData.pathConstraints.Items[index];
                for (int ii = 0, nn = SkeletonDataStream.sp_readVarint(input.ptr, 1); ii < nn; ii++)
                {
                    int timelineType = input.ReadSByte();
                    int frameCount   = SkeletonDataStream.sp_readVarint(input.ptr, 1);
                    switch (timelineType)
                    {
                    case PATH_POSITION:
                    case PATH_SPACING: {
                        PathConstraintPositionTimeline timeline;
                        float timelineScale = 1;
                        if (timelineType == PATH_SPACING)
                        {
                            timeline = new PathConstraintSpacingTimeline(frameCount);
                            if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed)
                            {
                                timelineScale = scale;
                            }
                        }
                        else
                        {
                            timeline = new PathConstraintPositionTimeline(frameCount);
                            if (data.positionMode == PositionMode.Fixed)
                            {
                                timelineScale = scale;
                            }
                        }
                        timeline.pathConstraintIndex = index;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, SkeletonDataStream.sp_readFloat(input.ptr), SkeletonDataStream.sp_readFloat(input.ptr) * timelineScale);
                            if (frameIndex < frameCount - 1)
                            {
                                SkeletonDataStream.sp_readCurve(input.ptr, frameIndex, timeline.curves);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintPositionTimeline.ENTRIES]);
                        break;
                    }

                    case PATH_MIX: {
                        PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frameCount);
                        timeline.pathConstraintIndex = index;
                        for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        {
                            timeline.SetFrame(frameIndex, SkeletonDataStream.sp_readFloat(input.ptr), SkeletonDataStream.sp_readFloat(input.ptr), SkeletonDataStream.sp_readFloat(input.ptr));
                            if (frameIndex < frameCount - 1)
                            {
                                SkeletonDataStream.sp_readCurve(input.ptr, frameIndex, timeline.curves);
                            }
                        }
                        timelines.Add(timeline);
                        duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintMixTimeline.ENTRIES]);
                        break;
                    }
                    }
                }
            }
        }
예제 #29
0
        public void Update()
        {
            PathAttachment pathAttachment = target.Attachment as PathAttachment;

            if (pathAttachment == null)
            {
                return;
            }
            float num   = rotateMix;
            float num2  = translateMix;
            bool  flag  = num2 > 0f;
            bool  flag2 = num > 0f;

            if (!flag && !flag2)
            {
                return;
            }
            PathConstraintData pathConstraintData = data;
            SpacingMode        spacingMode        = pathConstraintData.spacingMode;
            bool       flag3      = spacingMode == SpacingMode.Length;
            RotateMode rotateMode = pathConstraintData.rotateMode;
            bool       flag4      = rotateMode == RotateMode.Tangent;
            bool       flag5      = rotateMode == RotateMode.ChainScale;
            int        count      = bones.Count;
            int        num3       = (!flag4) ? (count + 1) : count;

            Bone[] items = bones.Items;
            ExposedList <float> exposedList  = spaces.Resize(num3);
            ExposedList <float> exposedList2 = null;
            float num4 = spacing;

            if (flag5 || flag3)
            {
                if (flag5)
                {
                    exposedList2 = lengths.Resize(count);
                }
                int num5 = 0;
                int num6 = num3 - 1;
                while (num5 < num6)
                {
                    Bone  bone = items[num5];
                    float num7 = bone.data.length;
                    if (num7 == 0f)
                    {
                        num7 = 1E-09f;
                    }
                    float num8  = num7 * bone.a;
                    float num9  = num7 * bone.c;
                    float num10 = (float)Math.Sqrt(num8 * num8 + num9 * num9);
                    if (flag5)
                    {
                        exposedList2.Items[num5] = num7;
                    }
                    exposedList.Items[++num5] = ((!flag3) ? num4 : Math.Max(0f, num7 + num4)) * num10 / num7;
                }
            }
            else
            {
                for (int i = 1; i < num3; i++)
                {
                    exposedList.Items[i] = num4;
                }
            }
            float[] array = ComputeWorldPositions(pathAttachment, num3, flag4, pathConstraintData.positionMode == PositionMode.Percent, spacingMode == SpacingMode.Percent);
            float   num11 = array[0];
            float   num12 = array[1];
            float   num13 = pathConstraintData.offsetRotation;
            bool    flag6;

            if (num13 == 0f)
            {
                flag6 = (rotateMode == RotateMode.Chain);
            }
            else
            {
                flag6 = false;
                Bone bone2 = target.bone;
                num13 *= ((!(bone2.a * bone2.d - bone2.b * bone2.c > 0f)) ? (-(float)Math.PI / 180f) : ((float)Math.PI / 180f));
            }
            int num14 = 0;
            int num15 = 3;

            while (num14 < count)
            {
                Bone bone3 = items[num14];
                bone3.worldX += (num11 - bone3.worldX) * num2;
                bone3.worldY += (num12 - bone3.worldY) * num2;
                float num16 = array[num15];
                float num17 = array[num15 + 1];
                float num18 = num16 - num11;
                float num19 = num17 - num12;
                if (flag5)
                {
                    float num20 = exposedList2.Items[num14];
                    if (num20 != 0f)
                    {
                        float num21 = ((float)Math.Sqrt(num18 * num18 + num19 * num19) / num20 - 1f) * num + 1f;
                        bone3.a *= num21;
                        bone3.c *= num21;
                    }
                }
                num11 = num16;
                num12 = num17;
                if (flag2)
                {
                    float a     = bone3.a;
                    float b     = bone3.b;
                    float c     = bone3.c;
                    float d     = bone3.d;
                    float num22 = flag4 ? array[num15 - 1] : ((exposedList.Items[num14 + 1] != 0f) ? MathUtils.Atan2(num19, num18) : array[num15 + 2]);
                    num22 -= MathUtils.Atan2(c, a);
                    float num23;
                    float num24;
                    if (flag6)
                    {
                        num23 = MathUtils.Cos(num22);
                        num24 = MathUtils.Sin(num22);
                        float length = bone3.data.length;
                        num11 += (length * (num23 * a - num24 * c) - num18) * num;
                        num12 += (length * (num24 * a + num23 * c) - num19) * num;
                    }
                    else
                    {
                        num22 += num13;
                    }
                    if (num22 > (float)Math.PI)
                    {
                        num22 -= (float)Math.PI * 2f;
                    }
                    else if (num22 < -(float)Math.PI)
                    {
                        num22 += (float)Math.PI * 2f;
                    }
                    num22  *= num;
                    num23   = MathUtils.Cos(num22);
                    num24   = MathUtils.Sin(num22);
                    bone3.a = num23 * a - num24 * c;
                    bone3.b = num23 * b - num24 * d;
                    bone3.c = num24 * a + num23 * c;
                    bone3.d = num24 * b + num23 * d;
                }
                bone3.appliedValid = false;
                num14++;
                num15 += 3;
            }
        }
예제 #30
0
        public void Update()
        {
            PathAttachment path = this.target.Attachment as PathAttachment;

            if (path != null)
            {
                float rotateMix    = this.rotateMix;
                float translateMix = this.translateMix;
                bool  flag         = translateMix > 0f;
                bool  flag2        = rotateMix > 0f;
                if (flag || flag2)
                {
                    bool flag6;
                    PathConstraintData data        = this.data;
                    SpacingMode        spacingMode = data.spacingMode;
                    bool                flag3      = spacingMode == SpacingMode.Length;
                    RotateMode          rotateMode = data.rotateMode;
                    bool                tangents   = rotateMode == RotateMode.Tangent;
                    bool                flag5      = rotateMode == RotateMode.ChainScale;
                    int                 count      = this.bones.Count;
                    int                 newSize    = !tangents ? (count + 1) : count;
                    Bone[]              items      = this.bones.Items;
                    ExposedList <float> list       = this.spaces.Resize(newSize);
                    ExposedList <float> list2      = null;
                    float               spacing    = this.spacing;
                    if (flag5 || flag3)
                    {
                        if (flag5)
                        {
                            list2 = this.lengths.Resize(count);
                        }
                        int num6 = 0;
                        int num7 = newSize - 1;
                        while (num6 < num7)
                        {
                            Bone  bone   = items[num6];
                            float length = bone.data.length;
                            if (length < 1E-05f)
                            {
                                if (flag5)
                                {
                                    list2.Items[num6] = 0f;
                                }
                                list.Items[++num6] = 0f;
                            }
                            else
                            {
                                float num9  = length * bone.a;
                                float num10 = length * bone.c;
                                float num11 = (float)Math.Sqrt((double)((num9 * num9) + (num10 * num10)));
                                if (flag5)
                                {
                                    list2.Items[num6] = num11;
                                }
                                list.Items[++num6] = ((!flag3 ? spacing : (length + spacing)) * num11) / length;
                            }
                        }
                    }
                    else
                    {
                        for (int j = 1; j < newSize; j++)
                        {
                            list.Items[j] = spacing;
                        }
                    }
                    float[] numArray       = this.ComputeWorldPositions(path, newSize, tangents, data.positionMode == PositionMode.Percent, spacingMode == SpacingMode.Percent);
                    float   num13          = numArray[0];
                    float   num14          = numArray[1];
                    float   offsetRotation = data.offsetRotation;
                    if (offsetRotation == 0f)
                    {
                        flag6 = rotateMode == RotateMode.Chain;
                    }
                    else
                    {
                        flag6 = false;
                        Bone bone = this.target.bone;
                        offsetRotation *= (((bone.a * bone.d) - (bone.b * bone.c)) <= 0f) ? -0.01745329f : 0.01745329f;
                    }
                    int index = 0;
                    for (int i = 3; index < count; i += 3)
                    {
                        Bone bone3 = items[index];
                        bone3.worldX += (num13 - bone3.worldX) * translateMix;
                        bone3.worldY += (num14 - bone3.worldY) * translateMix;
                        float num18 = numArray[i];
                        float num19 = numArray[i + 1];
                        float x     = num18 - num13;
                        float y     = num19 - num14;
                        if (flag5)
                        {
                            float num22 = list2.Items[index];
                            if (num22 >= 1E-05f)
                            {
                                float num23 = (((((float)Math.Sqrt((double)((x * x) + (y * y)))) / num22) - 1f) * rotateMix) + 1f;
                                bone3.a *= num23;
                                bone3.c *= num23;
                            }
                        }
                        num13 = num18;
                        num14 = num19;
                        if (flag2)
                        {
                            float num28;
                            float num29;
                            float num30;
                            float a = bone3.a;
                            float b = bone3.b;
                            float c = bone3.c;
                            float d = bone3.d;
                            if (tangents)
                            {
                                num28 = numArray[i - 1];
                            }
                            else if (list.Items[index + 1] < 1E-05f)
                            {
                                num28 = numArray[i + 2];
                            }
                            else
                            {
                                num28 = MathUtils.Atan2(y, x);
                            }
                            num28 -= MathUtils.Atan2(c, a);
                            if (flag6)
                            {
                                num29 = MathUtils.Cos(num28);
                                num30 = MathUtils.Sin(num28);
                                float length = bone3.data.length;
                                num13 += ((length * ((num29 * a) - (num30 * c))) - x) * rotateMix;
                                num14 += ((length * ((num30 * a) + (num29 * c))) - y) * rotateMix;
                            }
                            else
                            {
                                num28 += offsetRotation;
                            }
                            if (num28 > 3.141593f)
                            {
                                num28 -= 6.283185f;
                            }
                            else if (num28 < -3.141593f)
                            {
                                num28 += 6.283185f;
                            }
                            num28  *= rotateMix;
                            num29   = MathUtils.Cos(num28);
                            num30   = MathUtils.Sin(num28);
                            bone3.a = (num29 * a) - (num30 * c);
                            bone3.b = (num29 * b) - (num30 * d);
                            bone3.c = (num30 * a) + (num29 * c);
                            bone3.d = (num30 * b) + (num29 * d);
                        }
                        bone3.appliedValid = false;
                        index++;
                    }
                }
            }
        }
예제 #31
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);
        }
예제 #32
0
        public void Update()
        {
            PathAttachment pathAttachment = this.target.Attachment as PathAttachment;

            if (pathAttachment == null)
            {
                return;
            }
            float num   = this.rotateMix;
            float num2  = this.translateMix;
            bool  flag  = num2 > 0f;
            bool  flag2 = num > 0f;

            if (!flag && !flag2)
            {
                return;
            }
            PathConstraintData pathConstraintData = this.data;
            SpacingMode        spacingMode        = pathConstraintData.spacingMode;
            bool       flag3      = spacingMode == SpacingMode.Length;
            RotateMode rotateMode = pathConstraintData.rotateMode;
            bool       flag4      = rotateMode == RotateMode.Tangent;
            bool       flag5      = rotateMode == RotateMode.ChainScale;
            int        count      = this.bones.Count;
            int        num3       = (!flag4) ? (count + 1) : count;

            Bone[] items = this.bones.Items;
            ExposedList <float> exposedList  = this.spaces.Resize(num3);
            ExposedList <float> exposedList2 = null;
            float num4 = this.spacing;

            if (flag5 || flag3)
            {
                if (flag5)
                {
                    exposedList2 = this.lengths.Resize(count);
                }
                int i    = 0;
                int num5 = num3 - 1;
                while (i < num5)
                {
                    Bone  bone = items[i];
                    float num6 = bone.data.length;
                    float num7 = num6 * bone.a;
                    float num8 = num6 * bone.c;
                    num6 = (float)Math.Sqrt((double)(num7 * num7 + num8 * num8));
                    if (flag5)
                    {
                        exposedList2.Items[i] = num6;
                    }
                    exposedList.Items[++i] = ((!flag3) ? num4 : Math.Max(0f, num6 + num4));
                }
            }
            else
            {
                for (int j = 1; j < num3; j++)
                {
                    exposedList.Items[j] = num4;
                }
            }
            float[]  array          = this.ComputeWorldPositions(pathAttachment, num3, flag4, pathConstraintData.positionMode == PositionMode.Percent, spacingMode == SpacingMode.Percent);
            Skeleton skeleton       = this.target.Skeleton;
            float    x              = skeleton.x;
            float    y              = skeleton.y;
            float    num9           = array[0];
            float    num10          = array[1];
            float    offsetRotation = pathConstraintData.offsetRotation;
            bool     flag6          = rotateMode == RotateMode.Chain && offsetRotation == 0f;
            int      k              = 0;
            int      num11          = 3;

            while (k < count)
            {
                Bone bone2 = items[k];
                bone2.worldX += (num9 - x - bone2.worldX) * num2;
                bone2.worldY += (num10 - y - bone2.worldY) * num2;
                float num12 = array[num11];
                float num13 = array[num11 + 1];
                float num14 = num12 - num9;
                float num15 = num13 - num10;
                if (flag5)
                {
                    float num16 = exposedList2.Items[k];
                    if (num16 != 0f)
                    {
                        float num17 = ((float)Math.Sqrt((double)(num14 * num14 + num15 * num15)) / num16 - 1f) * num + 1f;
                        bone2.a *= num17;
                        bone2.c *= num17;
                    }
                }
                num9  = num12;
                num10 = num13;
                if (flag2)
                {
                    float a = bone2.a;
                    float b = bone2.b;
                    float c = bone2.c;
                    float d = bone2.d;
                    float num18;
                    if (flag4)
                    {
                        num18 = array[num11 - 1];
                    }
                    else if (exposedList.Items[k + 1] == 0f)
                    {
                        num18 = array[num11 + 2];
                    }
                    else
                    {
                        num18 = MathUtils.Atan2(num15, num14);
                    }
                    num18 -= MathUtils.Atan2(c, a) - offsetRotation * 0.0174532924f;
                    float num19;
                    float num20;
                    if (flag6)
                    {
                        num19 = MathUtils.Cos(num18);
                        num20 = MathUtils.Sin(num18);
                        float length = bone2.data.length;
                        num9  += (length * (num19 * a - num20 * c) - num14) * num;
                        num10 += (length * (num20 * a + num19 * c) - num15) * num;
                    }
                    if (num18 > 3.14159274f)
                    {
                        num18 -= 6.28318548f;
                    }
                    else if (num18 < -3.14159274f)
                    {
                        num18 += 6.28318548f;
                    }
                    num18  *= num;
                    num19   = MathUtils.Cos(num18);
                    num20   = MathUtils.Sin(num18);
                    bone2.a = num19 * a - num20 * c;
                    bone2.b = num19 * b - num20 * d;
                    bone2.c = num20 * a + num19 * c;
                    bone2.d = num20 * b + num19 * d;
                }
                k++;
                num11 += 3;
            }
        }