OSDN Git Service

PmdLimitsパッケージ移動
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / pmd / parser / 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.sfjp.mikutoga.pmd.parser;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
13 import jp.sfjp.mikutoga.pmd.PmdLimits;
14
15 /**
16  * PMDモデルファイルのパーサ拡張その3。
17  * <p>※ 剛体情報対応
18  */
19 public class PmdParserExt3 extends PmdParserExt2 {
20
21     private PmdRigidHandler rigidHandler = NullHandler.HANDLER;
22     private PmdJointHandler jointHandler = NullHandler.HANDLER;
23
24     /**
25      * コンストラクタ。
26      * @param source 入力ソース
27      */
28     public PmdParserExt3(InputStream source){
29         super(source);
30         return;
31     }
32
33     /**
34      * 剛体ハンドラを登録する。
35      * @param handler 剛体ハンドラ
36      */
37     public void setRigidHandler(PmdRigidHandler handler){
38         if(handler == null){
39             this.rigidHandler = NullHandler.HANDLER;
40         }else{
41             this.rigidHandler = handler;
42         }
43         return;
44     }
45
46     /**
47      * ジョイントハンドラを登録する。
48      * @param handler ジョイントハンドラ
49      */
50     public void setJointHandler(PmdJointHandler handler){
51         if(handler == null){
52             this.jointHandler = NullHandler.HANDLER;
53         }else{
54             this.jointHandler = handler;
55         }
56         return;
57     }
58
59     /**
60      * {@inheritDoc}
61      * @throws IOException {@inheritDoc}
62      * @throws MmdFormatException {@inheritDoc}
63      */
64     @Override
65     protected void parseBody()
66             throws IOException, MmdFormatException {
67         super.parseBody();
68
69         if(hasMore()){
70             parseRigidList();
71             parseJointList();
72         }
73
74         return;
75     }
76
77     /**
78      * 剛体情報のパースと通知。
79      * @throws IOException IOエラー
80      * @throws MmdFormatException フォーマットエラー
81      */
82     private void parseRigidList() throws IOException, MmdFormatException{
83         int rigidNum = parseLeInt();
84
85         this.rigidHandler.loopStart(PmdRigidHandler.RIGID_LIST, rigidNum);
86
87         for(int ct = 0; ct < rigidNum; ct++){
88             String rigidName =
89                     parsePmdText(PmdLimits.MAXBYTES_RIGIDNAME);
90             this.rigidHandler.pmdRigidName(rigidName);
91
92             int linkedBoneId   = parseLeUShortAsInt();
93             int rigidGroupId   = parseUByteAsInt();
94             short collisionMap = parseLeShort();
95             this.rigidHandler.pmdRigidInfo(rigidGroupId, linkedBoneId);
96
97             parseRigidGeom();
98             parseRigidDynamics();
99
100             byte behaveType = parseByte();
101             this.rigidHandler.pmdRigidBehavior(behaveType, collisionMap);
102
103             this.rigidHandler.loopNext(PmdRigidHandler.RIGID_LIST);
104         }
105
106         this.rigidHandler.loopEnd(PmdRigidHandler.RIGID_LIST);
107
108         return;
109     }
110
111     /**
112      * 剛体ジオメトリのパースと通知。
113      * @throws IOException IOエラー
114      * @throws MmdFormatException フォーマットエラー
115      */
116     private void parseRigidGeom() throws IOException, MmdFormatException{
117         byte shapeType = parseByte();
118         float width  = parseLeFloat();
119         float height = parseLeFloat();
120         float depth  = parseLeFloat();
121
122         this.rigidHandler.pmdRigidShape(shapeType, width, height, depth);
123
124         float posX = parseLeFloat();
125         float posY = parseLeFloat();
126         float posZ = parseLeFloat();
127
128         this.rigidHandler.pmdRigidPosition(posX, posY, posZ);
129
130         float rotX = parseLeFloat();
131         float rotY = parseLeFloat();
132         float rotZ = parseLeFloat();
133
134         this.rigidHandler.pmdRigidRotation(rotX, rotY, rotZ);
135
136         return;
137     }
138
139     /**
140      * 剛体力学のパースと通知。
141      * @throws IOException IOエラー
142      * @throws MmdFormatException フォーマットエラー
143      */
144     private void parseRigidDynamics()
145         throws IOException, MmdFormatException{
146             float mass        = parseLeFloat();
147             float dampingPos  = parseLeFloat();
148             float dampingRot  = parseLeFloat();
149             float restitution = parseLeFloat();
150             float friction    = parseLeFloat();
151
152             this.rigidHandler.pmdRigidPhysics(
153                 mass, dampingPos, dampingRot, restitution, friction
154             );
155
156         return;
157     }
158
159     /**
160      * ジョイント情報のパースと通知。
161      * @throws IOException IOエラー
162      * @throws MmdFormatException フォーマットエラー
163      */
164     private void parseJointList() throws IOException, MmdFormatException{
165         int jointNum = parseLeInt();
166
167         this.jointHandler.loopStart(PmdJointHandler.JOINT_LIST, jointNum);
168
169         for(int ct = 0; ct < jointNum; ct++){
170             String jointName =
171                     parsePmdText(PmdLimits.MAXBYTES_JOINTNAME);
172             this.jointHandler.pmdJointName(jointName);
173
174             int rigidIdA = parseLeInt();
175             int rigidIdB = parseLeInt();
176             this.jointHandler.pmdJointLink(rigidIdA, rigidIdB);
177
178             parseJointGeom();
179             parseJointLimit();
180             parseJointElastic();
181
182             this.jointHandler.loopNext(PmdJointHandler.JOINT_LIST);
183         }
184
185         this.jointHandler.loopEnd(PmdJointHandler.JOINT_LIST);
186
187         return;
188     }
189
190     /**
191      * ジョイントジオメトリのパースと通知。
192      * @throws IOException IOエラー
193      * @throws MmdFormatException フォーマットエラー
194      */
195     private void parseJointGeom() throws IOException, MmdFormatException{
196         float posX = parseLeFloat();
197         float posY = parseLeFloat();
198         float posZ = parseLeFloat();
199
200         this.jointHandler.pmdJointPosition(posX, posY, posZ);
201
202         float rotX = parseLeFloat();
203         float rotY = parseLeFloat();
204         float rotZ = parseLeFloat();
205
206         this.jointHandler.pmdJointRotation(rotX, rotY, rotZ);
207
208         return;
209     }
210
211     /**
212      * ジョイント制約のパースと通知。
213      * @throws IOException IOエラー
214      * @throws MmdFormatException フォーマットエラー
215      */
216     private void parseJointLimit() throws IOException, MmdFormatException{
217         float posXlim1 = parseLeFloat();
218         float posYlim1 = parseLeFloat();
219         float posZlim1 = parseLeFloat();
220         float posXlim2 = parseLeFloat();
221         float posYlim2 = parseLeFloat();
222         float posZlim2 = parseLeFloat();
223
224         this.jointHandler.pmdPositionLimit(
225                 posXlim1, posXlim2,
226                 posYlim1, posYlim2,
227                 posZlim1, posZlim2
228                 );
229
230         float rotXlim1 = parseLeFloat();
231         float rotYlim1 = parseLeFloat();
232         float rotZlim1 = parseLeFloat();
233         float rotXlim2 = parseLeFloat();
234         float rotYlim2 = parseLeFloat();
235         float rotZlim2 = parseLeFloat();
236
237         this.jointHandler.pmdRotationLimit(
238                 rotXlim1, rotXlim2,
239                 rotYlim1, rotYlim2,
240                 rotZlim1, rotZlim2
241                 );
242
243         return;
244     }
245
246     /**
247      * ジョイント弾性のパースと通知。
248      * @throws IOException IOエラー
249      * @throws MmdFormatException フォーマットエラー
250      */
251     private void parseJointElastic()
252             throws IOException, MmdFormatException{
253         float elasticPosX = parseLeFloat();
254         float elasticPosY = parseLeFloat();
255         float elasticPosZ = parseLeFloat();
256
257         this.jointHandler.pmdElasticPosition(
258                 elasticPosX,
259                 elasticPosY,
260                 elasticPosZ
261                 );
262
263         float elasticRotX = parseLeFloat();
264         float elasticRotY = parseLeFloat();
265         float elasticRotZ = parseLeFloat();
266
267         this.jointHandler.pmdElasticRotation(
268                 elasticRotX,
269                 elasticRotY,
270                 elasticRotZ
271                 );
272
273         return;
274     }
275
276 }