/// <summary> /// Initializes a new instance of the <see cref="Slot" /> class. /// </summary> /// <param name="data">The data.</param> /// <param name="skeleton">The skeleton.</param> /// <param name="bone">The bone.</param> /// <exception cref="System.ArgumentNullException">data cannot be null.</exception> public Slot(SlotData data, Skeleton skeleton, Bone bone) { if (data == null) { throw new ArgumentNullException("data cannot be null."); } if (skeleton == null) { throw new ArgumentNullException("skeleton cannot be null."); } if (bone == null) { throw new ArgumentNullException("bone cannot be null."); } this.Data = data; Skeleton = skeleton; Bone = bone; this.SetToBindPose(); }
/// <summary> /// Adds the slot. /// </summary> /// <param name="slot">The slot.</param> /// <exception cref="System.ArgumentNullException">slot cannot be null.</exception> public void AddSlot(SlotData slot) { if (slot == null) { throw new ArgumentNullException("slot cannot be null."); } this.Slots.Add(slot); }
/// <summary> /// Reads the skeleton data. /// </summary> /// <param name="reader">The reader.</param> /// <returns>Return SkeletonData.</returns> /// <exception cref="System.ArgumentNullException">reader cannot be null.</exception> /// <exception cref="System.Exception">Invalid JSON.</exception> public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<string, object>; if (root == null) { throw new Exception("Invalid JSON."); } // 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"]); } } BoneData boneData = new BoneData((string)boneMap["name"], parent); boneData.Length = this.GetFloat(boneMap, "length", 0) * this.Scale; boneData.X = this.GetFloat(boneMap, "x", 0) * this.Scale; boneData.Y = this.GetFloat(boneMap, "y", 0) * this.Scale; boneData.Rotation = this.GetFloat(boneMap, "rotation", 0); boneData.ScaleX = this.GetFloat(boneMap, "scaleX", 1); boneData.ScaleY = this.GetFloat(boneMap, "scaleY", 1); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { var slots = (List<object>)root["slots"]; foreach (Dictionary<string, object> slotMap in (List<object>)slots) { string slotName = (string)slotMap["name"]; string boneName = (string)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { string color = (string)slotMap["color"]; slotData.R = ToColor(color, 0); slotData.G = ToColor(color, 1); slotData.B = ToColor(color, 2); slotData.A = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.AttachmentName = (string)slotMap["attachment"]; } skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { var skinMap = (Dictionary<string, object>)root["skins"]; foreach (KeyValuePair<string, object> entry in skinMap) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair<string, object> slotEntry in (Dictionary<string, object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<string, object> attachmentEntry in (Dictionary<string, object>)slotEntry.Value) { Attachment attachment = this.ReadAttachment(skin, attachmentEntry.Key, (Dictionary<string, object>)attachmentEntry.Value); skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.Name == "default") { skeletonData.DefaultSkin = skin; } } } // Animations. if (root.ContainsKey("animations")) { var animationMap = (Dictionary<string, object>)root["animations"]; foreach (KeyValuePair<string, object> entry in animationMap) { this.ReadAnimation(entry.Key, (Dictionary<string, object>)entry.Value, skeletonData); } } skeletonData.Bones.TrimExcess(); skeletonData.Slots.TrimExcess(); skeletonData.Skins.TrimExcess(); skeletonData.Animations.TrimExcess(); return skeletonData; }