OSDN Git Service

モデルデータ不備の異常系を別パッケージに
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / vmd / model / binio / BasicLoader.java
1 /*
2  * basic information builder
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.vmd.model.binio;
9
10 import jp.sourceforge.mikutoga.math.MkPos3D;
11 import jp.sourceforge.mikutoga.math.MkQuat;
12 import jp.sourceforge.mikutoga.parser.MmdFormatException;
13 import jp.sourceforge.mikutoga.parser.ParseStage;
14 import jp.sourceforge.mikutoga.vmd.model.BezierParam;
15 import jp.sourceforge.mikutoga.vmd.model.BoneMotion;
16 import jp.sourceforge.mikutoga.vmd.model.MorphMotion;
17 import jp.sourceforge.mikutoga.vmd.model.PosCurve;
18 import jp.sourceforge.mikutoga.vmd.model.VmdMotion;
19 import jp.sourceforge.mikutoga.vmd.parser.VmdBasicHandler;
20
21 /**
22  * ボーンモーション、モーフ情報のビルダ。
23  */
24 class BasicLoader implements VmdBasicHandler {
25
26     private final VmdMotion vmdMotion;
27
28     private BoneMotion currentBoneMotion;
29     private MorphMotion currentMorphMotion;
30
31     private boolean hasMoreData;
32
33
34     /**
35      * コンストラクタ。
36      * @param vmdMotion モーション情報
37      */
38     BasicLoader(VmdMotion vmdMotion){
39         super();
40         this.vmdMotion = vmdMotion;
41
42         this.currentBoneMotion  = null;
43         this.currentMorphMotion = null;
44
45         this.hasMoreData = false;
46
47         return;
48     }
49
50
51     /**
52      * ボーンモーションループか否か判定する。
53      * @param stage 判定対象
54      * @return モーションループならtrue
55      */
56     private static boolean isBoneMotionList(ParseStage stage){
57         if(stage == VmdBasicHandler.BONEMOTION_LIST) return true;
58         return false;
59     }
60
61     /**
62      * モーフループか否か判定する。
63      * @param stage 判定対象
64      * @return モーフループならtrue
65      */
66     private static boolean isMorphList(ParseStage stage){
67         if(stage == VmdBasicHandler.MORPH_LIST) return true;
68         return false;
69     }
70
71
72     /**
73      * {@inheritDoc}
74      * @throws MmdFormatException {@inheritDoc}
75      */
76     @Override
77     public void vmdParseStart() throws MmdFormatException{
78         // NOTHING
79         return;
80     }
81
82     /**
83      * {@inheritDoc}
84      * @param hasMoreDataArg {@inheritDoc}
85      * @throws MmdFormatException {@inheritDoc}
86      */
87     @Override
88     public void vmdParseEnd(boolean hasMoreDataArg)
89             throws MmdFormatException{
90         this.hasMoreData = hasMoreDataArg;
91         return;
92     }
93
94     /**
95      * {@inheritDoc}
96      * @param stage {@inheritDoc}
97      * @param loops {@inheritDoc}
98      * @throws MmdFormatException {@inheritDoc}
99      */
100     @Override
101     public void loopStart(ParseStage stage, int loops)
102             throws MmdFormatException{
103         if(isBoneMotionList(stage)){
104             this.currentBoneMotion = new BoneMotion();
105         }else if(isMorphList(stage)){
106             this.currentMorphMotion = new MorphMotion();
107         }
108         return;
109     }
110
111     /**
112      * {@inheritDoc}
113      * @param stage {@inheritDoc}
114      * @throws MmdFormatException {@inheritDoc}
115      */
116     @Override
117     public void loopNext(ParseStage stage)
118             throws MmdFormatException{
119         if(isBoneMotionList(stage)){
120             this.vmdMotion.addBoneMotion(this.currentBoneMotion);
121             this.currentBoneMotion = new BoneMotion();
122         }else if(isMorphList(stage)){
123             this.vmdMotion.addMorphMotion(this.currentMorphMotion);
124             this.currentMorphMotion = new MorphMotion();
125         }
126         return;
127     }
128
129     /**
130      * {@inheritDoc}
131      * @param stage {@inheritDoc}
132      * @throws MmdFormatException {@inheritDoc}
133      */
134     @Override
135     public void loopEnd(ParseStage stage)
136             throws MmdFormatException{
137         if(isBoneMotionList(stage)){
138             this.currentBoneMotion = null;
139         }else if(isMorphList(stage)){
140             this.currentMorphMotion = null;
141         }
142         return;
143     }
144
145     /**
146      * {@inheritDoc}
147      * @param header {@inheritDoc}
148      * @throws MmdFormatException {@inheritDoc}
149      */
150     @Override
151     public void vmdHeaderInfo(byte[] header) throws MmdFormatException{
152         // NOTHING
153         return;
154     }
155
156     /**
157      * {@inheritDoc}
158      * @param modelName {@inheritDoc}
159      * @throws MmdFormatException {@inheritDoc}
160      */
161     @Override
162     public void vmdModelName(String modelName) throws MmdFormatException{
163         this.vmdMotion.setModelName(modelName);
164         return;
165     }
166
167     /**
168      * {@inheritDoc}
169      * @param boneName {@inheritDoc}
170      * @param keyFrameNo {@inheritDoc}
171      * @throws MmdFormatException {@inheritDoc}
172      */
173     @Override
174     public void vmdBoneMotion(String boneName, int keyFrameNo)
175             throws MmdFormatException{
176         this.currentBoneMotion.setBoneName(boneName);
177         this.currentBoneMotion.setFrameNumber(keyFrameNo);
178         return;
179     }
180
181     /**
182      * {@inheritDoc}
183      * @param qx {@inheritDoc}
184      * @param qy {@inheritDoc}
185      * @param qz {@inheritDoc}
186      * @param qw {@inheritDoc}
187      * @throws MmdFormatException {@inheritDoc}
188      */
189     @Override
190     public void vmdBoneRotationQt(float qx, float qy, float qz, float qw)
191             throws MmdFormatException{
192         MkQuat rotation = this.currentBoneMotion.getRotation();
193         rotation.setQ1(qx);
194         rotation.setQ2(qy);
195         rotation.setQ3(qz);
196         rotation.setQW(qw);
197         return;
198     }
199
200     /**
201      * {@inheritDoc}
202      * @param xPos {@inheritDoc}
203      * @param yPos {@inheritDoc}
204      * @param zPos {@inheritDoc}
205      * @throws MmdFormatException {@inheritDoc}
206      */
207     @Override
208     public void vmdBonePosition(float xPos, float yPos, float zPos)
209             throws MmdFormatException{
210         MkPos3D position = this.currentBoneMotion.getPosition();
211         position.setPosition(xPos, yPos, zPos);
212         return;
213     }
214
215     /**
216      * {@inheritDoc}
217      * @param rP1x {@inheritDoc}
218      * @param rP1y {@inheritDoc}
219      * @param rP2x {@inheritDoc}
220      * @param rP2y {@inheritDoc}
221      * @throws MmdFormatException {@inheritDoc}
222      */
223     @Override
224     public void vmdBoneIntpltRot(byte rP1x, byte rP1y,
225                                       byte rP2x, byte rP2y )
226             throws MmdFormatException{
227         BezierParam bezier = this.currentBoneMotion.getIntpltRotation();
228         bezier.setP1(rP1x, rP1y);
229         bezier.setP2(rP2x, rP2y);
230         return;
231     }
232
233     /**
234      * {@inheritDoc}
235      * @param xP1x {@inheritDoc}
236      * @param xP1y {@inheritDoc}
237      * @param xP2x {@inheritDoc}
238      * @param xP2y {@inheritDoc}
239      * @throws MmdFormatException {@inheritDoc}
240      */
241     @Override
242     public void vmdBoneIntpltXpos(byte xP1x, byte xP1y,
243                                        byte xP2x, byte xP2y )
244             throws MmdFormatException{
245         PosCurve posCurve = this.currentBoneMotion.getPosCurve();
246         BezierParam bezier = posCurve.getIntpltXpos();
247         bezier.setP1(xP1x, xP1y);
248         bezier.setP2(xP2x, xP2y);
249         return;
250     }
251
252     /**
253      * {@inheritDoc}
254      * @param yP1x {@inheritDoc}
255      * @param yP1y {@inheritDoc}
256      * @param yP2x {@inheritDoc}
257      * @param yP2y {@inheritDoc}
258      * @throws MmdFormatException {@inheritDoc}
259      */
260     @Override
261     public void vmdBoneIntpltYpos(byte yP1x, byte yP1y,
262                                        byte yP2x, byte yP2y )
263             throws MmdFormatException{
264         PosCurve posCurve = this.currentBoneMotion.getPosCurve();
265         BezierParam bezier = posCurve.getIntpltYpos();
266         bezier.setP1(yP1x, yP1y);
267         bezier.setP2(yP2x, yP2y);
268         return;
269     }
270
271     /**
272      * {@inheritDoc}
273      * @param zP1x {@inheritDoc}
274      * @param zP1y {@inheritDoc}
275      * @param zP2x {@inheritDoc}
276      * @param zP2y {@inheritDoc}
277      * @throws MmdFormatException {@inheritDoc}
278      */
279     @Override
280     public void vmdBoneIntpltZpos(byte zP1x, byte zP1y,
281                                        byte zP2x, byte zP2y )
282             throws MmdFormatException{
283         PosCurve posCurve = this.currentBoneMotion.getPosCurve();
284         BezierParam bezier = posCurve.getIntpltZpos();
285         bezier.setP1(zP1x, zP1y);
286         bezier.setP2(zP2x, zP2y);
287         return;
288     }
289
290     /**
291      * {@inheritDoc}
292      * @param morphName {@inheritDoc}
293      * @param keyFrameNo {@inheritDoc}
294      * @param flex {@inheritDoc}
295      * @throws MmdFormatException {@inheritDoc}
296      */
297     @Override
298     public void vmdMorphMotion(String morphName, int keyFrameNo, float flex)
299             throws MmdFormatException{
300         this.currentMorphMotion.setMorphName(morphName);
301         this.currentMorphMotion.setFrameNumber(keyFrameNo);
302         this.currentMorphMotion.setFlex(flex);
303         return;
304     }
305
306     /**
307      * 読み残したデータがあるか判定する。
308      * @return 読み残したデータがあればtrue
309      */
310     boolean hasMoreData(){
311         return this.hasMoreData;
312     }
313
314 }