OSDN Git Service

2e5df3587b36bbf4f2fab7ac32f8dbe702eaba56
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / parser / pmd / PmdParserExt3.java
1 /*
2  * pmd parser extension 3
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.parser.pmd;
9
10 import java.io.IOException;
11 import jp.sourceforge.mikutoga.parser.MmdFormatException;
12 import jp.sourceforge.mikutoga.parser.MmdSource;
13
14 /**
15  * PMDモデルファイルのパーサ拡張その3。
16  * ※剛体情報対応
17  */
18 public class PmdParserExt3 extends PmdParserExt2 {
19
20     private static final int RIGID_DATA_SZ = 83;
21     private static final int JOINT_DATA_SZ = 124;
22
23     private PmdRigidHandler rigidHandler = null;
24     private PmdJointHandler jointHandler = null;
25
26     /**
27      * コンストラクタ。
28      * @param source 入力ソース
29      */
30     public PmdParserExt3(MmdSource source){
31         super(source);
32         return;
33     }
34
35     /**
36      * 剛体ハンドラを登録する。
37      * @param handler 剛体ハンドラ
38      */
39     public void setRigidHandler(PmdRigidHandler handler){
40         this.rigidHandler = handler;
41         return;
42     }
43
44     /**
45      * ジョイントハンドラを登録する。
46      * @param handler ジョイントハンドラ
47      */
48     public void setJointHandler(PmdJointHandler handler){
49         this.jointHandler = handler;
50         return;
51     }
52
53     /**
54      * {@inheritDoc}
55      * @throws IOException {@inheritDoc}
56      * @throws MmdFormatException {@inheritDoc}
57      */
58     @Override
59     protected void parseBody()
60             throws IOException, MmdFormatException {
61         super.parseBody();
62
63         if(hasMore()){
64             parseRigidList();
65             parseJointList();
66         }
67
68         return;
69     }
70
71     /**
72      * 剛体情報のパースと通知。
73      * @throws IOException IOエラー
74      * @throws MmdFormatException フォーマットエラー
75      */
76     private void parseRigidList() throws IOException, MmdFormatException{
77         int rigidNum = parseInteger();
78
79         if(this.rigidHandler == null){
80             skip(RIGID_DATA_SZ * rigidNum);
81             return;
82         }
83
84         this.rigidHandler.loopStart(PmdRigidHandler.RIGID_LIST, rigidNum);
85
86         for(int ct = 0; ct < rigidNum; ct++){
87             String rigidName =
88                     parseZeroTermWin31J(PmdLimits.MAXBYTES_RIGIDNAME);
89             this.rigidHandler.pmdRigidName(rigidName);
90
91             int linkedBoneId = parseUShortAsInteger();
92             int rigidGroupId = parseUByteAsInteger();
93             short collisionMap = parseShort();
94             this.rigidHandler.pmdRigidInfo(rigidGroupId, linkedBoneId);
95
96             byte shapeType = parseByte();
97             float width = parseFloat();
98             float height = parseFloat();
99             float depth = parseFloat();
100             this.rigidHandler.pmdRigidShape(shapeType, width, height, depth);
101
102             float posX = parseFloat();
103             float posY = parseFloat();
104             float posZ = parseFloat();
105             this.rigidHandler.pmdRigidPosition(posX, posY, posZ);
106
107             float rotX = parseFloat();
108             float rotY = parseFloat();
109             float rotZ = parseFloat();
110             this.rigidHandler.pmdRigidRotation(rotX, rotY, rotZ);
111
112             float mass = parseFloat();
113             float dampingPos = parseFloat();
114             float dampingRot = parseFloat();
115             float restitution = parseFloat();
116             float friction = parseFloat();
117             this.rigidHandler.pmdRigidPhysics(mass,
118                                               dampingPos, dampingRot,
119                                               restitution, friction );
120
121             byte behaveType = parseByte();
122             this.rigidHandler.pmdRigidBehavior(behaveType, collisionMap);
123
124             this.rigidHandler.loopNext(PmdRigidHandler.RIGID_LIST);
125         }
126
127         this.rigidHandler.loopEnd(PmdRigidHandler.RIGID_LIST);
128
129         return;
130     }
131
132     /**
133      * ジョイント情報のパースと通知。
134      * @throws IOException IOエラー
135      * @throws MmdFormatException フォーマットエラー
136      */
137     private void parseJointList() throws IOException, MmdFormatException{
138         int jointNum = parseInteger();
139
140         if(this.jointHandler == null){
141             skip(JOINT_DATA_SZ * jointNum);
142             return;
143         }
144
145         this.jointHandler.loopStart(PmdJointHandler.JOINT_LIST, jointNum);
146
147         for(int ct = 0; ct < jointNum; ct++){
148             String jointName =
149                     parseZeroTermWin31J(PmdLimits.MAXBYTES_JOINTNAME);
150             this.jointHandler.pmdJointName(jointName);
151
152             int rigidIdA = parseInteger();
153             int rigidIdB = parseInteger();
154             this.jointHandler.pmdJointLink(rigidIdA, rigidIdB);
155
156             float posX = parseFloat();
157             float posY = parseFloat();
158             float posZ = parseFloat();
159             this.jointHandler.pmdJointPosition(posX, posY, posZ);
160
161             float rotX = parseFloat();
162             float rotY = parseFloat();
163             float rotZ = parseFloat();
164             this.jointHandler.pmdJointRotation(rotX, rotY, rotZ);
165
166             float posXlim1 = parseFloat();
167             float posYlim1 = parseFloat();
168             float posZlim1 = parseFloat();
169             float posXlim2 = parseFloat();
170             float posYlim2 = parseFloat();
171             float posZlim2 = parseFloat();
172             this.jointHandler.pmdPositionLimit(posXlim1, posXlim2,
173                                                posYlim1, posYlim2,
174                                                posZlim1, posZlim2 );
175
176             float rotXlim1 = parseFloat();
177             float rotYlim1 = parseFloat();
178             float rotZlim1 = parseFloat();
179             float rotXlim2 = parseFloat();
180             float rotYlim2 = parseFloat();
181             float rotZlim2 = parseFloat();
182             this.jointHandler.pmdRotationLimit(rotXlim1, rotXlim2,
183                                                rotYlim1, rotYlim2,
184                                                rotZlim1, rotZlim2 );
185
186             float elasticPosX = parseFloat();
187             float elasticPosY = parseFloat();
188             float elasticPosZ = parseFloat();
189             this.jointHandler.pmdElasticPosition(elasticPosX,
190                                                  elasticPosY,
191                                                  elasticPosZ );
192
193             float elasticRotX = parseFloat();
194             float elasticRotY = parseFloat();
195             float elasticRotZ = parseFloat();
196             this.jointHandler.pmdElasticRotation(elasticRotX,
197                                                  elasticRotY,
198                                                  elasticRotZ );
199
200             this.jointHandler.loopNext(PmdJointHandler.JOINT_LIST);
201         }
202
203         this.jointHandler.loopEnd(PmdJointHandler.JOINT_LIST);
204
205         return;
206     }
207
208 }