Пример #1
0
        public static CHR0Node Convert(string input)
        {
            try
            {
                CHR0Node chr0 = new CHR0Node();
                chr0.Name    = Path.GetFileNameWithoutExtension(input);
                chr0.Loop    = false;
                chr0.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();
                                node.Name       = reader.ReadLine();
                                node._numFrames = keyFrameCount;

                                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(KeyFrameMode.TransX, frameCount, translation.x[frameCount]);
                                        }
                                        if (translation.y.Count != 0 && frameCount < translation.y.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.TransY, frameCount, translation.y[frameCount]);
                                        }
                                        if (translation.z.Count != 0 && frameCount < translation.z.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.TransZ, frameCount, translation.z[frameCount]);
                                        }
                                    }
                                    if (rotation.highestCount > frameCount)
                                    {
                                        if (rotation.x.Count != 0 && frameCount < rotation.x.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.RotX, frameCount, rotation.x[frameCount]);
                                        }
                                        if (rotation.y.Count != 0 && frameCount < rotation.y.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.RotY, frameCount, rotation.y[frameCount]);
                                        }
                                        if (rotation.z.Count != 0 && frameCount < rotation.z.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.RotZ, frameCount, rotation.z[frameCount]);
                                        }
                                    }
                                    if (scale.highestCount > frameCount)
                                    {
                                        if (scale.x.Count != 0 && frameCount < scale.x.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.ScaleX, frameCount, scale.x[frameCount]);
                                        }
                                        if (scale.y.Count != 0 && frameCount < scale.y.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.ScaleY, frameCount, scale.y[frameCount]);
                                        }
                                        if (scale.z.Count != 0 && frameCount < scale.z.Count)
                                        {
                                            node.SetKeyframe(KeyFrameMode.ScaleZ, frameCount, scale.z[frameCount]);
                                        }
                                    }
                                }
                                chr0.AddChild(node);
                            }
                        }
                    }
                    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);
            }
        }
Пример #2
0
        private unsafe void BoxChanged(object sender, EventArgs e)
        {
            NumericInputBox box = sender as NumericInputBox;
            AnimationFrame  kf;
            float *         pkf   = (float *)&kf;
            float           val   = box.Value;
            int             index = (int)box.Tag;
            int             x;

            if (val != _currentFrame[index])
            {
                int kfIndex = FindKeyframe(_currentPage);

                if (float.IsNaN(val))
                {
                    //Value removed find keyframe and zero it out
                    if (kfIndex >= 0)
                    {
                        kf = (AnimationFrame)listKeyframes.Items[kfIndex];
                        kf.forKeyframeCHR = true;
                        kf.SetBool(index, false);
                        pkf[index] = val;
                        for (x = 0; (x < 9) && float.IsNaN(pkf[x]); x++)
                        {
                            ;
                        }
                        if (x == 9)
                        {
                            listKeyframes.Items.RemoveAt(kfIndex);
                            listKeyframes.SelectedIndex = -1;
                        }
                        else
                        {
                            listKeyframes.Items[kfIndex] = kf;
                        }
                    }

                    _target.RemoveKeyframe(KeyFrameMode.ScaleX + index, _currentPage);
                    val       = _target.GetAnimFrame(_currentPage)[index];
                    box.Value = val;
                }
                else
                {
                    if (kfIndex >= 0)
                    {
                        kf = (AnimationFrame)listKeyframes.Items[kfIndex];
                        kf.forKeyframeCHR = true;
                        kf.SetBool(index, true);
                        pkf[index] = val;
                        listKeyframes.Items[kfIndex] = kf;
                    }
                    else
                    {
                        kf = AnimationFrame.Empty;
                        kf.forKeyframeCHR = true;
                        kf.SetBool(index, true);
                        kf.Index   = _currentPage;
                        pkf[index] = val;

                        int count = listKeyframes.Items.Count;
                        for (x = 0; (x < count) && (((AnimationFrame)listKeyframes.Items[x]).Index < _currentPage); x++)
                        {
                            ;
                        }

                        listKeyframes.Items.Insert(x, kf);
                        listKeyframes.SelectedIndex = x;
                    }

                    _target.SetKeyframe(KeyFrameMode.ScaleX + index, _currentPage, val);
                }

                _currentFrame[index] = val;
                UpdateBox(index);
            }
        }
Пример #3
0
        public static void BoneAnim2Chr0Entry(BoneAnim boneAnim, CHR0Node chr0)
        {
            CHR0EntryNode chr0Entry = chr0.CreateEntry(boneAnim.Name);

            chr0Entry.UseModelRotate       = false;
            chr0Entry.UseModelScale        = false;
            chr0Entry.UseModelTranslate    = false;
            chr0Entry.ScaleCompensateApply = boneAnim.ApplySegmentScaleCompensate;

            //Float for time/frame
            Dictionary <float, FSKAKeyNode> TranslateX = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> TranslateY = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> TranslateZ = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> RotateX    = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> RotateY    = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> RotateZ    = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> ScaleX     = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> ScaleY     = new Dictionary <float, FSKAKeyNode>();
            Dictionary <float, FSKAKeyNode> ScaleZ     = new Dictionary <float, FSKAKeyNode>();

            if (boneAnim.FlagsBase.HasFlag(BoneAnimFlagsBase.Translate))
            {
                TranslateX.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Translate.X,
                });
                TranslateY.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Translate.Y,
                });
                TranslateZ.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Translate.Z,
                });
            }
            if (boneAnim.FlagsBase.HasFlag(BoneAnimFlagsBase.Rotate))
            {
                RotateX.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Rotate.X,
                });
                RotateY.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Rotate.Y,
                });
                RotateZ.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Rotate.Z,
                });
            }

            if (boneAnim.FlagsBase.HasFlag(BoneAnimFlagsBase.Scale))
            {
                ScaleX.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Scale.X,
                });
                ScaleY.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Scale.Y,
                });
                ScaleZ.Add(0, new FSKAKeyNode()
                {
                    Value = boneAnim.BaseData.Scale.Z,
                });
            }
            else
            {
                ScaleX.Add(0, new FSKAKeyNode()
                {
                    Value = 1
                });
                ScaleY.Add(0, new FSKAKeyNode()
                {
                    Value = 1
                });
                ScaleZ.Add(0, new FSKAKeyNode()
                {
                    Value = 1
                });
            }

            foreach (var curve in boneAnim.Curves)
            {
                for (int frame = 0; frame < curve.Frames.Length; frame++)
                {
                    float time   = curve.Frames[frame];
                    float value  = 0;
                    float slope  = 0;
                    float slope2 = 0;
                    float delta  = 0;

                    float scale = curve.Scale;
                    if (scale <= 0)
                    {
                        scale = 1;
                    }

                    if (curve.CurveType == AnimCurveType.Cubic)
                    {
                        value  = curve.Offset + curve.Keys[frame, 0] * scale;
                        slope  = curve.Offset + curve.Keys[frame, 1] * scale;
                        slope2 = curve.Offset + curve.Keys[frame, 2] * scale;
                        delta  = curve.Offset + curve.Keys[frame, 3] * scale;
                    }
                    if (curve.CurveType == AnimCurveType.Linear)
                    {
                        value = curve.Offset + curve.Keys[frame, 0] * scale;
                        delta = curve.Offset + curve.Keys[frame, 1] * scale;
                    }
                    if (curve.CurveType == AnimCurveType.StepInt)
                    {
                        value = curve.Offset + curve.Keys[frame, 0] * scale;
                    }


                    switch (curve.AnimDataOffset)
                    {
                    case 0x10:
                        if (TranslateX.Count > 0 && frame == 0)
                        {
                            TranslateX.Remove(0);
                        }

                        TranslateX.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x14:
                        if (TranslateY.Count > 0 && frame == 0)
                        {
                            TranslateY.Remove(0);
                        }

                        TranslateY.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x18:
                        if (TranslateZ.Count > 0 && frame == 0)
                        {
                            TranslateZ.Remove(0);
                        }

                        TranslateZ.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x20:
                        if (RotateX.Count > 0 && frame == 0)
                        {
                            RotateX.Remove(0);
                        }

                        RotateX.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x24:
                        if (RotateY.Count > 0 && frame == 0)
                        {
                            RotateY.Remove(0);
                        }

                        RotateY.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x28:
                        if (RotateZ.Count > 0 && frame == 0)
                        {
                            RotateZ.Remove(0);
                        }

                        RotateZ.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x04:
                        if (ScaleX.Count > 0 && frame == 0)
                        {
                            ScaleX.Remove(0);
                        }

                        ScaleX.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x08:
                        if (ScaleY.Count > 0 && frame == 0)
                        {
                            ScaleY.Remove(0);
                        }

                        ScaleY.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;

                    case 0x0C:
                        if (ScaleZ.Count > 0 && frame == 0)
                        {
                            ScaleZ.Remove(0);
                        }

                        ScaleZ.Add(time, new FSKAKeyNode()
                        {
                            Value  = value,
                            Slope  = slope,
                            Slope2 = slope2,
                            Delta  = delta,
                            Frame  = time,
                        });
                        break;
                    }
                }
            }

            for (int frame = 0; frame < chr0.FrameCount; frame++)
            {
                if (TranslateX.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(6, frame, TranslateX[frame].Value);
                }
                if (TranslateY.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(7, frame, TranslateY[frame].Value);
                }
                if (TranslateZ.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(8, frame, TranslateZ[frame].Value);
                }
                if (RotateX.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(3, frame, RotateX[frame].Value * Rad2Deg);
                }
                if (RotateY.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(4, frame, RotateY[frame].Value * Rad2Deg);
                }
                if (RotateZ.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(5, frame, RotateZ[frame].Value * Rad2Deg);
                }
                if (ScaleX.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(0, frame, ScaleX[frame].Value);
                }
                if (ScaleY.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(1, frame, ScaleY[frame].Value);
                }
                if (ScaleZ.ContainsKey(frame))
                {
                    chr0Entry.SetKeyframe(2, frame, ScaleZ[frame].Value);
                }
            }
        }