OSDN Git Service

assign MqoBone#node_id.
[tdcgexplorer/tso2mqo.git] / MqxReader.cs
index 2bf3730..c9a610e 100644 (file)
@@ -49,6 +49,52 @@ namespace Tso2MqoGui
             return true;
         }
 
+        Dictionary<int, MqoBone> bone_idmap;
+
+        // create bone_idmap
+        // map id to bone
+        void CreateBoneMap()
+        {
+            bone_idmap = new Dictionary<int, MqoBone>();
+            foreach (MqoBone bone in bones)
+            {
+                bone_idmap[bone.id] = bone;
+            }
+        }
+        void UpdateBones()
+        {
+            // assign node.parent
+            foreach (MqoBone bone in bones)
+            {
+                if (bone.pid != 0)
+                {
+                    bone.parent = bone_idmap[bone.pid];
+                }
+                else
+                    bone.parent = null;
+            }
+            // assign node.path
+            foreach (MqoBone bone in bones)
+            {
+                if (bone.parent != null)
+                {
+                    bone.path = bone.parent.path + "|" + bone.name;
+                }
+                else
+                    bone.path = "|" + bone.name;
+            }
+            // assign node.local_position
+            foreach (MqoBone bone in bones)
+            {
+                if (bone.parent != null)
+                {
+                    bone.local_position = bone.world_position - bone.parent.world_position;
+                }
+                else
+                    bone.local_position = bone.world_position;
+            }
+        }
+
         public void Read(XmlReader reader)
         {
             reader.Read();
@@ -67,12 +113,18 @@ namespace Tso2MqoGui
             int i = 0;
             while (reader.IsStartElement("Bone"))
             {
-                MqoBone bone = new MqoBone();
+                MqoBone bone = new MqoBone(i);
                 bone.Read(reader);
                 this.bones[i++] = bone;
             }
             reader.ReadEndElement();//BoneSet
 
+            len = i;
+            Array.Resize(ref bones, len);
+
+            CreateBoneMap();
+            UpdateBones();
+
             while (reader.IsStartElement("Obj"))
             {
                 //Console.WriteLine("Obj");
@@ -112,9 +164,6 @@ namespace Tso2MqoGui
             }
             foreach (MqoBone bone in bones)
             {
-                if (bone == null)
-                    continue;
-
                 foreach (MqoWeit weit in bone.weits)
                 {
                     Dictionary<int, List<MqoWeit>> map = weitmap[weit.object_id];
@@ -139,12 +188,12 @@ namespace Tso2MqoGui
 
             for (int i = 0; i < len; ++i)
             {
-                this.weits[i].bone_id = weits[i].bone_id;
+                this.weits[i].node_id = weits[i].node_id;
                 this.weits[i].weit = weits[i].weit; 
             }
             for (int i = len; i < 4; ++i)
             {
-                this.weits[i].bone_id = 1;
+                this.weits[i].node_id = 0;
                 this.weits[i].weit = 0.0f;
             }
         }