OSDN Git Service

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