OSDN Git Service

パッケージ移動
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / vmd / parser / VmdLightingParser.java
1 /*
2  * VMD lighting data parser
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.vmd.parser;
9
10 import java.io.IOException;
11 import jp.sfjp.mikutoga.bin.parser.BinParser;
12 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
13 import jp.sfjp.mikutoga.bin.parser.ProxyParser;
14
15 /**
16  * VMDモーションファイルのライティング情報パーサ。
17  * 照明光源演出データと影演出データを含む。
18  * <p>古い版のVMDファイルには影演出データが記述されていない場合がある。
19  */
20 class VmdLightingParser extends ProxyParser {
21
22     private VmdLightingHandler handler = VmdUnifiedHandler.EMPTY;
23
24
25     /**
26      * コンストラクタ。
27      * @param parser 委譲先パーサ
28      */
29     VmdLightingParser(BinParser parser){
30         super(parser);
31         return;
32     }
33
34
35     /**
36      * ライティング情報通知用ハンドラを登録する。
37      * @param lightingHandler ハンドラ
38      */
39     void setLightingHandler(VmdLightingHandler lightingHandler){
40         if(lightingHandler == null){
41             this.handler = VmdUnifiedHandler.EMPTY;
42         }else{
43             this.handler = lightingHandler;
44         }
45
46         return;
47     }
48
49     /**
50      * ライティングデータのパースと通知。
51      * <p>影演出データが無ければ読みに行かない。
52      * @throws IOException IOエラー
53      * @throws MmdFormatException フォーマットエラー
54      */
55     void parse() throws IOException, MmdFormatException {
56         parseVmdLighting();
57         if(hasMore()){
58             parseVmdShadow();
59         }
60         return;
61     }
62
63     /**
64      * 光源モーションデータのパースと通知。
65      * @throws IOException IOエラー
66      * @throws MmdFormatException フォーマットエラー
67      */
68     private void parseVmdLighting() throws IOException, MmdFormatException{
69         int lightMotionNo = parseLeInt();
70
71         this.handler.loopStart(VmdLightingHandler.LUMINOUS_LIST,
72                 lightMotionNo);
73
74         for(int ct = 0; ct < lightMotionNo; ct++){
75             int keyFrameNo = parseLeInt();
76             this.handler.vmdLuminousMotion(keyFrameNo);
77
78             float rVal = parseLeFloat();
79             float gVal = parseLeFloat();
80             float bVal = parseLeFloat();
81             this.handler.vmdLuminousColor(rVal, gVal, bVal);
82
83             float xVec = parseLeFloat();
84             float yVec = parseLeFloat();
85             float zVec = parseLeFloat();
86             this.handler.vmdLuminousDirection(xVec, yVec, zVec);
87
88             this.handler.loopNext(VmdLightingHandler.LUMINOUS_LIST);
89         }
90
91         this.handler.loopEnd(VmdLightingHandler.LUMINOUS_LIST);
92
93         return;
94     }
95
96     /**
97      * 影演出データのパースと通知。
98      * @throws IOException IOエラー
99      * @throws MmdFormatException フォーマットエラー
100      */
101     private void parseVmdShadow() throws IOException, MmdFormatException{
102         int shadowMotionNo = parseLeInt();
103
104         this.handler.loopStart(VmdLightingHandler.SHADOW_LIST,
105                 shadowMotionNo);
106
107         for(int ct = 0; ct < shadowMotionNo; ct++){
108             int keyFrameNo = parseLeInt();
109             this.handler.vmdShadowMotion(keyFrameNo);
110
111             byte shadowMode = parseByte();
112             this.handler.vmdShadowMode(shadowMode);
113
114             float shadowScope = parseLeFloat();
115             this.handler.vmdShadowScopeRaw(shadowScope);
116
117             this.handler.loopNext(VmdLightingHandler.SHADOW_LIST);
118         }
119
120         this.handler.loopEnd(VmdLightingHandler.SHADOW_LIST);
121
122         return;
123     }
124
125 }