OSDN Git Service

パッケージ変更。テスト整備。
[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  * ※剛体情報対応
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(InputStream 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 = parseLeInt();
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                     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             byte shapeType = parseByte();
97             float width = parseLeFloat();
98             float height = parseLeFloat();
99             float depth = parseLeFloat();
100             this.rigidHandler.pmdRigidShape(shapeType, width, height, depth);
101
102             float posX = parseLeFloat();
103             float posY = parseLeFloat();
104             float posZ = parseLeFloat();
105             this.rigidHandler.pmdRigidPosition(posX, posY, posZ);
106
107             float rotX = parseLeFloat();
108             float rotY = parseLeFloat();
109             float rotZ = parseLeFloat();
110             this.rigidHandler.pmdRigidRotation(rotX, rotY, rotZ);
111
112             float mass = parseLeFloat();
113             float dampingPos = parseLeFloat();
114             float dampingRot = parseLeFloat();
115             float restitution = parseLeFloat();
116             float friction = parseLeFloat();
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 = parseLeInt();
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                     parsePmdText(PmdLimits.MAXBYTES_JOINTNAME);
150             this.jointHandler.pmdJointName(jointName);
151
152             int rigidIdA = parseLeInt();
153             int rigidIdB = parseLeInt();
154             this.jointHandler.pmdJointLink(rigidIdA, rigidIdB);
155
156             float posX = parseLeFloat();
157             float posY = parseLeFloat();
158             float posZ = parseLeFloat();
159             this.jointHandler.pmdJointPosition(posX, posY, posZ);
160
161             float rotX = parseLeFloat();
162             float rotY = parseLeFloat();
163             float rotZ = parseLeFloat();
164             this.jointHandler.pmdJointRotation(rotX, rotY, rotZ);
165
166             float posXlim1 = parseLeFloat();
167             float posYlim1 = parseLeFloat();
168             float posZlim1 = parseLeFloat();
169             float posXlim2 = parseLeFloat();
170             float posYlim2 = parseLeFloat();
171             float posZlim2 = parseLeFloat();
172             this.jointHandler.pmdPositionLimit(posXlim1, posXlim2,
173                                                posYlim1, posYlim2,
174                                                posZlim1, posZlim2 );
175
176             float rotXlim1 = parseLeFloat();
177             float rotYlim1 = parseLeFloat();
178             float rotZlim1 = parseLeFloat();
179             float rotXlim2 = parseLeFloat();
180             float rotYlim2 = parseLeFloat();
181             float rotZlim2 = parseLeFloat();
182             this.jointHandler.pmdRotationLimit(rotXlim1, rotXlim2,
183                                                rotYlim1, rotYlim2,
184                                                rotZlim1, rotZlim2 );
185
186             float elasticPosX = parseLeFloat();
187             float elasticPosY = parseLeFloat();
188             float elasticPosZ = parseLeFloat();
189             this.jointHandler.pmdElasticPosition(elasticPosX,
190                                                  elasticPosY,
191                                                  elasticPosZ );
192
193             float elasticRotX = parseLeFloat();
194             float elasticRotY = parseLeFloat();
195             float elasticRotZ = parseLeFloat();
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 }