Ejemplo n.º 1
0
        void OpenAnimation(string animationFile)
        {
            ClearUndo();
            ClearRedo();

            savePath = animationFile;

            NewAnimation();
            keyframes.Clear();

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(animationFile);

            frameRate = Int32.Parse(xmlDocument.SelectSingleNode("/Animation/FrameRate").InnerText);
            frameRateComboBox.SelectedItem = frameRate.ToString();

            loopFrame = Int32.Parse(xmlDocument.SelectSingleNode("/Animation/LoopFrame").InnerText);

            XmlNodeList nodeList = xmlDocument.SelectNodes("/Animation/Texture");
            foreach (XmlNode node in nodeList)
            {
                LoadTexture(node.InnerText, animationFile);
            }

            nodeList = xmlDocument.SelectNodes("/Animation/Keyframe");
            foreach (XmlNode node in nodeList)
            {
                Keyframe keyframe = new Keyframe(Int32.Parse(node.Attributes["frame"].Value));
                if (node.Attributes["trigger"] != null)
                    keyframe.Trigger = node.Attributes["trigger"].Value;
                else
                    keyframe.Trigger = "";

                if (node.Attributes["vflip"] != null)
                    keyframe.FlipVertically = bool.Parse(node.Attributes["vflip"].Value);
                else
                    keyframe.FlipVertically = false;

                if (node.Attributes["hflip"] != null)
                    keyframe.FlipHorizontally = bool.Parse(node.Attributes["hflip"].Value);
                else
                    keyframe.FlipHorizontally = false;

                XmlNodeList boneList = node.SelectNodes("Bone");
                foreach (XmlNode boneNode in boneList)
                {
                    Bone bone = new Bone(boneNode.Attributes["name"].Value,
                        Int32.Parse(boneNode.SelectSingleNode("TextureIndex").InnerText),
                        Int32.Parse(boneNode.SelectSingleNode("ParentIndex").InnerText));

                    bone.Hidden = bool.Parse(boneNode.SelectSingleNode("Hidden").InnerText);

                    XmlNode tempNode = boneNode.SelectSingleNode("TextureFlipHorizontal");
                    if (tempNode != null)
                        bone.TextureFlipHorizontal = bool.Parse(tempNode.InnerText);
                    tempNode = boneNode.SelectSingleNode("TextureFlipVertical");
                    if (tempNode != null)
                        bone.TextureFlipVertical = bool.Parse(tempNode.InnerText);

                    bone.Position = new Vector2(float.Parse(boneNode.SelectSingleNode("Position/X").InnerText, CultureInfo.InvariantCulture),
                        float.Parse(boneNode.SelectSingleNode("Position/Y").InnerText, CultureInfo.InvariantCulture));
                    bone.Rotation = float.Parse(boneNode.SelectSingleNode("Rotation").InnerText, CultureInfo.InvariantCulture);
                    bone.Scale = new Vector2(float.Parse(boneNode.SelectSingleNode("Scale/X").InnerText, CultureInfo.InvariantCulture),
                        float.Parse(boneNode.SelectSingleNode("Scale/Y").InnerText, CultureInfo.InvariantCulture));

                    keyframe.AddBone(bone);
                }
                keyframe.SortBones();
                keyframe.UpdateBones();

                keyframes.Add(keyframe);
            }

            if (keyframes[0].Bones.Count != 0)
            {
                currentBoneIndex = 0;
                currentKeyframe = 0;
                atExistingKeyframe = true;
                existingKeyframe = keyframes[0];
            }
        }
Ejemplo n.º 2
0
        public static Keyframe Interpolate(int frameNumber, Keyframe key1, int key1FrameNumber, Keyframe key2, int key2FrameNumber)
        {
            Keyframe keyframe = new Keyframe(frameNumber);
            keyframe.FlipVertically = key1.FlipVertically;
            keyframe.FlipHorizontally = key1.FlipHorizontally;

            float t = (frameNumber - key1FrameNumber) / (float)(key2FrameNumber - key1FrameNumber);

            for (int boneIndex = 0; boneIndex < key1.Bones.Count; boneIndex++)
            {
                Bone bone = new Bone(key1.Bones[boneIndex].Name, key1.Bones[boneIndex].TextureIndex, key1.Bones[boneIndex].ParentIndex);
                bone.Position = Vector2.Lerp(key1.Bones[boneIndex].Position, key2.Bones[boneIndex].Position, t);
                bone.Scale = Vector2.Lerp(key1.Bones[boneIndex].Scale, key2.Bones[boneIndex].Scale, t);
                bone.Rotation = MathHelper.Lerp(key1.Bones[boneIndex].Rotation, key2.Bones[boneIndex].Rotation, t);
                bone.TextureFlipHorizontal = key1.Bones[boneIndex].TextureFlipHorizontal;
                bone.TextureFlipVertical = key1.Bones[boneIndex].TextureFlipVertical;
                bone.Hidden = key1.Bones[boneIndex].Hidden;

                keyframe.AddBone(bone);
            }

            keyframe.SortBones();
            keyframe.UpdateBones();
            return keyframe;
        }