public void AddToNode(CHR0Node parentNode)
        {
            CHR0EntryNode entryNode = new CHR0EntryNode();

            parentNode.AddChild(entryNode);
            entryNode.SetSize(parentNode.FrameCount, parentNode.Loop);
            entryNode.Name = Name;

            foreach (Keyframe keyframe in Keyframes)
            {
                keyframe.AddToNode(entryNode);
            }

            //entryNode.Keyframes.Clean();
        }
Пример #2
0
        public static CHR0Node Convert(string input)
        {
            try
            {
                CHR0Node chr0 = new CHR0Node
                {
                    Name    = Path.GetFileNameWithoutExtension(input),
                    Loop    = false,
                    Version = 4
                };

                for (TextReader reader = new StreamReader(input); reader.Peek() != -1;)
                {
                    string line = reader.ReadLine();
                    if (line.CompareTo("Animation Keyframe Data Frames") == 0)
                    {
                        line = reader.ReadLine();

                        //Remove any non-digit characters
                        string justNumbers   = new string(line.Where(char.IsDigit).ToArray());
                        int    keyFrameCount = int.Parse(justNumbers);
                        chr0.FrameCount = keyFrameCount;

                        for (line = reader.ReadLine();
                             line != null && line.CompareTo("End Of Animation Data") != 0;
                             line = reader.ReadLine())
                        {
                            if (line.CompareTo("Keyframe Data") == 0)
                            {
                                CHR0EntryNode node = new CHR0EntryNode();
                                chr0.AddChild(node);
                                node.Name = reader.ReadLine();
                                node.SetSize(keyFrameCount, chr0.Loop);

                                Coords translation = new Coords();
                                Coords rotation    = new Coords();
                                Coords scale       = new Coords();

                                for (line = reader.ReadLine();
                                     line != null && line.CompareTo("End of Keyframe Data") != 0;
                                     line = reader.ReadLine())
                                {
                                    //Use goto to retry the loop without advancing the reader, when it returns from getCoords it will have the next line we need from it.
retry:
                                    if (line.CompareTo("Translation") == 0)
                                    {
                                        reader.ReadLine(); //Skip first blank line
                                        translation = getCoords(reader, out line);
                                        goto retry;
                                    }
                                    else if (line.CompareTo("Rotation") == 0)
                                    {
                                        reader.ReadLine(); //Skip first blank line
                                        rotation = getCoords(reader, out line);
                                        goto retry;
                                    }
                                    else if (line.CompareTo("Scale") == 0)
                                    {
                                        reader.ReadLine(); //Skip first blank line
                                        scale = getCoords(reader, out line);
                                        goto retry;
                                    }
                                    else if (line.CompareTo("End of Keyframe Data") == 0
                                             ) //If line equals this now it's time to break
                                    {
                                        break;
                                    }
                                }

                                //Add frames to node
                                for (int frameCount = 0;
                                     translation.highestCount > frameCount || rotation.highestCount > frameCount ||
                                     scale.highestCount > frameCount;
                                     frameCount++)
                                {
                                    if (translation.highestCount > frameCount)
                                    {
                                        if (translation.x.Count != 0 && frameCount < translation.x.Count)
                                        {
                                            node.SetKeyframe(6, frameCount, translation.x[frameCount]);
                                        }

                                        if (translation.y.Count != 0 && frameCount < translation.y.Count)
                                        {
                                            node.SetKeyframe(7, frameCount, translation.y[frameCount]);
                                        }

                                        if (translation.z.Count != 0 && frameCount < translation.z.Count)
                                        {
                                            node.SetKeyframe(8, frameCount, translation.z[frameCount]);
                                        }
                                    }

                                    if (rotation.highestCount > frameCount)
                                    {
                                        if (rotation.x.Count != 0 && frameCount < rotation.x.Count)
                                        {
                                            node.SetKeyframe(3, frameCount, rotation.x[frameCount]);
                                        }

                                        if (rotation.y.Count != 0 && frameCount < rotation.y.Count)
                                        {
                                            node.SetKeyframe(4, frameCount, rotation.y[frameCount]);
                                        }

                                        if (rotation.z.Count != 0 && frameCount < rotation.z.Count)
                                        {
                                            node.SetKeyframe(5, frameCount, rotation.z[frameCount]);
                                        }
                                    }

                                    if (scale.highestCount > frameCount)
                                    {
                                        if (scale.x.Count != 0 && frameCount < scale.x.Count)
                                        {
                                            node.SetKeyframe(0, frameCount, scale.x[frameCount]);
                                        }

                                        if (scale.y.Count != 0 && frameCount < scale.y.Count)
                                        {
                                            node.SetKeyframe(1, frameCount, scale.y[frameCount]);
                                        }

                                        if (scale.z.Count != 0 && frameCount < scale.z.Count)
                                        {
                                            node.SetKeyframe(2, frameCount, scale.z[frameCount]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Not a valid CHR0 text file.");
                        return(null);
                    }
                }

                return(chr0);
            }
            catch (Exception e)
            {
                MessageBox.Show("There was a problem importing keyframes.\n\nError:\n" + e.ToString());
                return(null);
            }
        }
Пример #3
0
        public static CHR0Node Convert(string input)
        {
            try
            {
                CHR0Node chr0 = new CHR0Node
                {
                    Name = Path.GetFileNameWithoutExtension(input),
                    Loop = false,
                    Version = 4
                };
                List<string> boneNames = new List<string>();
                List<int> boneIDs = new List<int>();
                int lineNumber = 0;
                bool isReadingBones = false;
                bool isReadingFrames = false;
                int currentFrameID = new int();
                float radianFloat = 57.29579143313326f; //Angles are given in radians. To convert them into degrees, multiply them by this float.
                List<SMDFrame> allFrames = new List<SMDFrame>();
                for (TextReader reader = new StreamReader(input); reader.Peek() != -1;) //Writing data into Lists
                {
                    string line = reader.ReadLine();

                    if (line.CompareTo("end") == 0) //When reaching this line, we stop reading what we were reading
                    {
                        isReadingBones = false;
                        isReadingFrames = false;
                    }

                    if (isReadingBones)
                    {
                        string[] splitLine = line.Split('\"');
                        int boneID = int.Parse(splitLine[0].Split(' ')[0]);
                        string boneName = splitLine[1];
                        boneIDs.Add(boneID);
                        boneNames.Add(boneName);
                    }

                    if(isReadingFrames)
                    {
                        SMDFrame currentFrame = new SMDFrame();
                        if(line.Contains("time "))
                        {
                            currentFrameID = int.Parse(line.Split(' ')[1]);
                            line = reader.ReadLine();
                            lineNumber++;
                        }
                        string[] frameNumbers = line.Split(' ');

                        int boneID = int.Parse(frameNumbers[0], CultureInfo.InvariantCulture.NumberFormat);
                        float posX = float.Parse(frameNumbers[2], CultureInfo.InvariantCulture.NumberFormat);
                        float posY = float.Parse(frameNumbers[3], CultureInfo.InvariantCulture.NumberFormat);
                        float posZ = float.Parse(frameNumbers[4], CultureInfo.InvariantCulture.NumberFormat);
                        float rotX = float.Parse(frameNumbers[6], CultureInfo.InvariantCulture.NumberFormat) * radianFloat;
                        float rotY = float.Parse(frameNumbers[7], CultureInfo.InvariantCulture.NumberFormat) * radianFloat;
                        float rotZ = float.Parse(frameNumbers[8], CultureInfo.InvariantCulture.NumberFormat) * radianFloat;

                        currentFrame = new SMDFrame(currentFrameID, boneID, posX, posY, posZ, rotX, rotY, rotZ);

                        allFrames.Add(currentFrame);
                    }

                    if (lineNumber == 0) //First Line
                    {
                        if (line.CompareTo("version 1") != 0) //The first line contains the version number. It needs to be 1.
                        {
                            MessageBox.Show("Unsupported SMD Version. Please use SMD Version 1."); //I don't even know if there's versions >1 but let's not take risks
                            return null;
                        }
                    }

                    if (line.CompareTo("nodes") == 0) //When reaching this line, we start reading bones names
                    {
                        isReadingBones = true;
                    }

                    if (line.CompareTo("skeleton") == 0) //When reaching this line, we start reading frames
                    {
                        isReadingFrames = true;
                    }

                    lineNumber++;
                }

                foreach (int thisBone in boneIDs)
                {
                    CHR0EntryNode node = new CHR0EntryNode();
                    chr0.AddChild(node);
                    node.Name = boneNames[thisBone];
                    node.SetSize(currentFrameID + 1, true);

                    int keynum = 0;

                    foreach (SMDFrame currentFrame in allFrames)
                    {
                        //Keyframe Array documentation:
                        //  ---------------------
                        //  |       | X | Y | Z |
                        //  | Scale | 0 | 1 | 2 |
                        //  | Rot   | 3 | 4 | 5 |
                        //  | Pos   | 6 | 7 | 8 |
                        //  ---------------------
                        if (currentFrame.boneID == thisBone)
                        {
                            node.SetKeyframe(3, currentFrame.id, currentFrame.rotx);
                            node.SetKeyframe(4, currentFrame.id, currentFrame.roty);
                            node.SetKeyframe(5, currentFrame.id, currentFrame.rotz);
                            node.SetKeyframe(6, currentFrame.id, currentFrame.posx);
                            node.SetKeyframe(7, currentFrame.id, currentFrame.posy);
                            node.SetKeyframe(8, currentFrame.id, currentFrame.posz);

                            keynum++;
                        }
                    }

                    chr0.FrameCount = keynum;
                    chr0.Loop = true;
                }

                return chr0;
            }
            catch (Exception e)
            {
                MessageBox.Show("There was a problem importing keyframes.\n\nError:\n" + e);
                return null;
            }
        }