OSDN Git Service

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