/// <summary>
        /// New Attachment.
        /// </summary>
        /// <param name="skin">The skin.</param>
        /// <param name="type">The attachmentType.</param>
        /// <param name="name">The name.</param>
        /// <returns>May be null to not load any attachment.</returns>
        /// <exception cref="System.Exception">Region not found in atlas:  + name +  ( + type + )</exception>
        public Attachment NewAttachment(Skin skin, AttachmentType type, string name)
        {
            if (type == AttachmentType.region)
            {
                AtlasRegion region = this.atlas.FindRegion(name);
                if (region == null)
                {
                    throw new Exception("Region not found in atlas: " + name + " (" + type + ")");
                }

                RegionAttachment attachment = new RegionAttachment(name);
                attachment.Texture = region.Page.Texture;
                attachment.SetUVs(region.U, region.V, region.U2, region.V2, region.Rotate);
                attachment.RegionOffsetX = region.OffsetX;
                attachment.RegionOffsetY = region.OffsetY;
                attachment.RegionWidth = region.Width;
                attachment.RegionHeight = region.Height;
                attachment.RegionOriginalWidth = region.OriginalWidth;
                attachment.RegionOriginalHeight = region.OriginalHeight;

                return attachment;
            }
            else
            {
                throw new Exception("Unknown attachment type: " + type);
            }
        }
Example #2
0
        /// <summary>
        /// Adds the skin.
        /// </summary>
        /// <param name="skin">The skin.</param>
        /// <exception cref="System.ArgumentNullException">skin cannot be null.</exception>
        public void AddSkin(Skin skin)
        {
            if (skin == null)
            {
                throw new ArgumentNullException("skin cannot be null.");
            }

            this.Skins.Add(skin);
        }
Example #3
0
        /// <summary>
        /// Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.
        /// </summary>
        /// <param name="skeleton">The skeleton.</param>
        /// <param name="oldSkin">The old skin.</param>
        internal void AttachAll(Skeleton skeleton, Skin oldSkin)
        {
            foreach (KeyValuePair<KeyValuePair<int, string>, Attachment> entry in oldSkin.attachments)
            {
                int slotIndex = entry.Key.Key;
                Slot slot = skeleton.Slots[slotIndex];

                if (slot.Attachment == entry.Value)
                {
                    Attachment attachment = this.GetAttachment(slotIndex, entry.Key.Value);

                    if (attachment != null)
                    {
                        slot.Attachment = attachment;
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Reads the attachment.
        /// </summary>
        /// <param name="skin">The skin.</param>
        /// <param name="name">The name.</param>
        /// <param name="map">The map.</param>
        /// <returns>Return attachment.</returns>
        private Attachment ReadAttachment(Skin skin, string name, Dictionary<string, object> map)
        {
            if (map.ContainsKey("name"))
            {
                name = (string)map["name"];
            }

            AttachmentType type = AttachmentType.region;
            if (map.ContainsKey("type"))
            {
                type = (AttachmentType)Enum.Parse(typeof(AttachmentType), (string)map["type"], false);
            }

            Attachment attachment = this.attachmentLoader.NewAttachment(skin, type, name);

            if (attachment is RegionAttachment)
            {
                RegionAttachment regionAttachment = (RegionAttachment)attachment;
                regionAttachment.X = this.GetFloat(map, "x", 0) * this.Scale;
                regionAttachment.Y = this.GetFloat(map, "y", 0) * this.Scale;
                regionAttachment.ScaleX = this.GetFloat(map, "scaleX", 1);
                regionAttachment.ScaleY = this.GetFloat(map, "scaleY", 1);
                regionAttachment.Rotation = this.GetFloat(map, "rotation", 0);
                regionAttachment.Width = this.GetFloat(map, "width", 32) * this.Scale;
                regionAttachment.Height = this.GetFloat(map, "height", 32) * this.Scale;
                regionAttachment.UpdateOffset();
            }

            return attachment;
        }
Example #5
0
        /// <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;
        }
Example #6
0
        /// <summary>
        ///  Sets the skin used to look up attachments not found in the  <see cref="SkeletonData" /> class Attachments 
        ///  from the new skin are attached if the corresponding attachment from the old skin was attached.
        /// </summary>
        /// <param name="newSkin">The new skin. (may be null)</param>
        public void SetSkin(Skin newSkin)
        {
            if (Skin != null && newSkin != null)
            {
                newSkin.AttachAll(this, Skin);
            }

            Skin = newSkin;
        }