OSDN Git Service

checkstyle対応
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / binio / MaterialBuilder.java
1 /*
2  * building material information
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model.binio;
9
10 import java.awt.Color;
11 import java.util.Iterator;
12 import java.util.List;
13 import jp.sfjp.mikutoga.bin.parser.ParseStage;
14 import jp.sfjp.mikutoga.pmd.model.ListUtil;
15 import jp.sfjp.mikutoga.pmd.model.Material;
16 import jp.sfjp.mikutoga.pmd.model.PmdModel;
17 import jp.sfjp.mikutoga.pmd.model.ShadeInfo;
18 import jp.sfjp.mikutoga.pmd.model.Surface;
19 import jp.sfjp.mikutoga.pmd.model.ToonMap;
20 import jp.sfjp.mikutoga.pmd.parser.PmdMaterialHandler;
21
22 /**
23  * マテリアル素材関連の通知をパーサから受け取る。
24  */
25 class MaterialBuilder implements PmdMaterialHandler {
26
27     private static final int TRIANGLE = 3;
28
29
30     private final List<Material> materialList;
31     private Iterator<Material> materialIt;
32     private Material currentMaterial = null;
33
34     private final List<Surface> surfacelList;
35     private Iterator<Surface> surfaceIt;
36
37     private final ToonMap toonMap;
38
39
40     /**
41      * コンストラクタ。
42      * @param model モデル
43      */
44     MaterialBuilder(PmdModel model){
45         super();
46
47         this.materialList = model.getMaterialList();
48         this.surfacelList = model.getSurfaceList();
49         this.toonMap = model.getToonMap();
50
51         return;
52     }
53
54
55     /**
56      * {@inheritDoc}
57      * @param stage {@inheritDoc}
58      * @param loops {@inheritDoc}
59      */
60     @Override
61     public void loopStart(ParseStage stage, int loops){
62         assert stage == PmdMaterialHandler.MATERIAL_LIST;
63
64         ListUtil.prepareDefConsList(this.materialList, Material.class, loops);
65
66         this.materialIt = this.materialList.iterator();
67         if(this.materialIt.hasNext()){
68             this.currentMaterial = this.materialIt.next();
69         }
70
71         this.surfaceIt = this.surfacelList.iterator();
72
73         return;
74     }
75
76     /**
77      * {@inheritDoc}
78      * @param stage {@inheritDoc}
79      */
80     @Override
81     public void loopNext(ParseStage stage){
82         assert stage == PmdMaterialHandler.MATERIAL_LIST;
83
84         if(this.materialIt.hasNext()){
85             this.currentMaterial = this.materialIt.next();
86         }
87
88         return;
89     }
90
91     /**
92      * {@inheritDoc}
93      * @param stage {@inheritDoc}
94      */
95     @Override
96     public void loopEnd(ParseStage stage){
97         assert stage == PmdMaterialHandler.MATERIAL_LIST;
98         return;
99     }
100
101     /**
102      * {@inheritDoc}
103      * @param red {@inheritDoc}
104      * @param green {@inheritDoc}
105      * @param blue {@inheritDoc}
106      * @param alpha {@inheritDoc}
107      */
108     @Override
109     public void pmdMaterialDiffuse(float red,
110                                    float green,
111                                    float blue,
112                                    float alpha ){
113         Color diffuse = new Color(red, green, blue, alpha);
114         this.currentMaterial.setDiffuseColor(diffuse);
115         return;
116     }
117
118     /**
119      * {@inheritDoc}
120      * @param red {@inheritDoc}
121      * @param green {@inheritDoc}
122      * @param blue {@inheritDoc}
123      */
124     @Override
125     public void pmdMaterialAmbient(float red,
126                                    float green,
127                                    float blue ){
128         Color ambient = new Color(red, green, blue);
129         this.currentMaterial.setAmbientColor(ambient);
130         return;
131     }
132
133     /**
134      * {@inheritDoc}
135      * @param red {@inheritDoc}
136      * @param green {@inheritDoc}
137      * @param blue {@inheritDoc}
138      * @param shininess {@inheritDoc}
139      */
140     @Override
141     public void pmdMaterialSpecular(float red,
142                                     float green,
143                                     float blue,
144                                     float shininess ){
145         Color specular = new Color(red, green, blue);
146         this.currentMaterial.setSpecularColor(specular);
147         this.currentMaterial.setShininess(shininess);
148         return;
149     }
150
151     /**
152      * {@inheritDoc}
153      * @param hasEdge {@inheritDoc}
154      * @param vertexNum {@inheritDoc}
155      */
156     @Override
157     public void pmdMaterialInfo(boolean hasEdge, int vertexNum){
158         this.currentMaterial.setEdgeAppearance(hasEdge);
159
160         List<Surface> list = this.currentMaterial.getSurfaceList();
161
162         int surfaceNum = vertexNum / TRIANGLE;
163         for(int ct = 1; ct <= surfaceNum; ct++){
164             Surface surface = this.surfaceIt.next();
165             list.add(surface);
166         }
167
168         return;
169     }
170
171     /**
172      * {@inheritDoc}
173      * @param toonIdx {@inheritDoc}
174      * @param textureFile {@inheritDoc}
175      * @param sphereFile {@inheritDoc}
176      */
177     @Override
178     public void pmdMaterialShading(int toonIdx,
179                                    String textureFile,
180                                    String sphereFile ){
181         ShadeInfo info = this.currentMaterial.getShadeInfo();
182
183         ToonMap map = this.toonMap;
184
185         info.setToonMap(map);
186         info.setToonIndex(toonIdx);
187         info.setTextureFileName(textureFile);
188         info.setSpheremapFileName(sphereFile);
189
190         return;
191     }
192
193 }