FindSlot() public method

public FindSlot ( String slotName ) : SlotData
slotName String
return SlotData
Esempio n. 1
0
 static int FindSlot(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.SlotData     o    = obj.FindSlot(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Esempio n. 2
0
        public SkeletonData ReadSkeletonData(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "reader cannot be null.");
            }

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

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

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

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

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

                skeletonData.bones.Add(data);
            }

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

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

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

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

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

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

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

                    skeletonData.ikConstraints.Add(data);
                }
            }

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

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

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

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

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

                    skeletonData.transformConstraints.Add(data);
                }
            }

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

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

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

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

                    skeletonData.pathConstraints.Add(data);
                }
            }

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

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

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

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

            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
		public SkeletonData ReadSkeletonData (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;
		}
Esempio n. 4
0
 /// <summary>Finds an attachment by looking in the <see cref="Skeleton.Skin"/> and <see cref="SkeletonData.DefaultSkin"/> using the slot name and attachment name.</summary>
 /// <returns>May be null.</returns>
 public Attachment GetAttachment(string slotName, string attachmentName)
 {
     return(GetAttachment(data.FindSlot(slotName).index, attachmentName));
 }
Esempio n. 5
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);
        }
Esempio n. 6
0
        private Attachment ReadAttachment(Dictionary <string, object> map, Skin skin, int slotIndex, string name, SkeletonData skeletonData)
        {
            float scale = Scale;

            name = GetString(map, "name", name);
            string text = GetString(map, "type", "region");

            if (text == "skinnedmesh")
            {
                text = "weightedmesh";
            }
            if (text == "weightedmesh")
            {
                text = "mesh";
            }
            if (text == "weightedlinkedmesh")
            {
                text = "linkedmesh";
            }
            AttachmentType attachmentType = (AttachmentType)Enum.Parse(typeof(AttachmentType), text, ignoreCase: true);
            string         @string        = GetString(map, "path", name);

            switch (attachmentType)
            {
            case AttachmentType.Region:
            {
                RegionAttachment regionAttachment = attachmentLoader.NewRegionAttachment(skin, name, @string);
                if (regionAttachment == null)
                {
                    return(null);
                }
                regionAttachment.Path     = @string;
                regionAttachment.x        = GetFloat(map, "x", 0f) * scale;
                regionAttachment.y        = GetFloat(map, "y", 0f) * scale;
                regionAttachment.scaleX   = GetFloat(map, "scaleX", 1f);
                regionAttachment.scaleY   = GetFloat(map, "scaleY", 1f);
                regionAttachment.rotation = GetFloat(map, "rotation", 0f);
                regionAttachment.width    = GetFloat(map, "width", 32f) * scale;
                regionAttachment.height   = GetFloat(map, "height", 32f) * scale;
                regionAttachment.UpdateOffset();
                if (map.ContainsKey("color"))
                {
                    string hexString2 = (string)map["color"];
                    regionAttachment.r = ToColor(hexString2, 0);
                    regionAttachment.g = ToColor(hexString2, 1);
                    regionAttachment.b = ToColor(hexString2, 2);
                    regionAttachment.a = ToColor(hexString2, 3);
                }
                regionAttachment.UpdateOffset();
                return(regionAttachment);
            }

            case AttachmentType.Boundingbox:
            {
                BoundingBoxAttachment boundingBoxAttachment = attachmentLoader.NewBoundingBoxAttachment(skin, name);
                if (boundingBoxAttachment == null)
                {
                    return(null);
                }
                ReadVertices(map, boundingBoxAttachment, GetInt(map, "vertexCount", 0) << 1);
                return(boundingBoxAttachment);
            }

            case AttachmentType.Mesh:
            case AttachmentType.Linkedmesh:
            {
                MeshAttachment meshAttachment = attachmentLoader.NewMeshAttachment(skin, name, @string);
                if (meshAttachment == null)
                {
                    return(null);
                }
                meshAttachment.Path = @string;
                if (map.ContainsKey("color"))
                {
                    string hexString = (string)map["color"];
                    meshAttachment.r = ToColor(hexString, 0);
                    meshAttachment.g = ToColor(hexString, 1);
                    meshAttachment.b = ToColor(hexString, 2);
                    meshAttachment.a = ToColor(hexString, 3);
                }
                meshAttachment.Width  = GetFloat(map, "width", 0f) * scale;
                meshAttachment.Height = GetFloat(map, "height", 0f) * scale;
                string string3 = GetString(map, "parent", null);
                if (string3 != null)
                {
                    meshAttachment.InheritDeform = GetBoolean(map, "deform", defaultValue: true);
                    linkedMeshes.Add(new LinkedMesh(meshAttachment, GetString(map, "skin", null), slotIndex, string3));
                    return(meshAttachment);
                }
                float[] floatArray = GetFloatArray(map, "uvs", 1f);
                ReadVertices(map, meshAttachment, floatArray.Length);
                meshAttachment.triangles = GetIntArray(map, "triangles");
                meshAttachment.regionUVs = floatArray;
                meshAttachment.UpdateUVs();
                if (map.ContainsKey("hull"))
                {
                    meshAttachment.HullLength = GetInt(map, "hull", 0) * 2;
                }
                if (map.ContainsKey("edges"))
                {
                    meshAttachment.Edges = GetIntArray(map, "edges");
                }
                return(meshAttachment);
            }

            case AttachmentType.Path:
            {
                PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, name);
                if (pathAttachment == null)
                {
                    return(null);
                }
                pathAttachment.closed        = GetBoolean(map, "closed", defaultValue: false);
                pathAttachment.constantSpeed = GetBoolean(map, "constantSpeed", defaultValue: true);
                int @int = GetInt(map, "vertexCount", 0);
                ReadVertices(map, pathAttachment, @int << 1);
                pathAttachment.lengths = GetFloatArray(map, "lengths", scale);
                return(pathAttachment);
            }

            case AttachmentType.Point:
            {
                PointAttachment pointAttachment = attachmentLoader.NewPointAttachment(skin, name);
                if (pointAttachment == null)
                {
                    return(null);
                }
                pointAttachment.x        = GetFloat(map, "x", 0f) * scale;
                pointAttachment.y        = GetFloat(map, "y", 0f) * scale;
                pointAttachment.rotation = GetFloat(map, "rotation", 0f);
                return(pointAttachment);
            }

            case AttachmentType.Clipping:
            {
                ClippingAttachment clippingAttachment = attachmentLoader.NewClippingAttachment(skin, name);
                if (clippingAttachment == null)
                {
                    return(null);
                }
                string string2 = GetString(map, "end", null);
                if (string2 != null)
                {
                    SlotData slotData = skeletonData.FindSlot(string2);
                    if (slotData == null)
                    {
                        throw new Exception("Clipping end slot not found: " + string2);
                    }
                    clippingAttachment.EndSlot = slotData;
                }
                ReadVertices(map, clippingAttachment, GetInt(map, "vertexCount", 0) << 1);
                return(clippingAttachment);
            }

            default:
                return(null);
            }
        }