OSDN Git Service

checkstyle対応
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / Material.java
1 /*
2  * material information
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model;
9
10 import java.awt.Color;
11 import java.awt.Transparency;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import jp.sfjp.mikutoga.corelib.I18nText;
16
17 /**
18  * マテリアル素材情報。
19  */
20 public class Material implements Iterable<Surface> {
21
22     // sRGBカラー情報配列インデックス
23     private static final int IDX_RED   = 0;
24     private static final int IDX_GREEN = 1;
25     private static final int IDX_BLUE  = 2;
26     private static final int IDX_ALPHA = 3;
27
28
29     private final I18nText materialName = new I18nText();
30
31     private Color diffuseColor;
32
33     private Color specularColor;
34     private float shininess;
35
36     private Color ambientColor;
37
38     private final ShadeInfo shadeInfo = new ShadeInfo();
39
40     private boolean edgeAppearance = true;
41
42     private final List<Surface> surfaceList = new ArrayList<Surface>();
43
44
45     /**
46      * コンストラクタ。
47      */
48     public Material(){
49         super();
50         return;
51     }
52
53
54     /**
55      * 色を不透明化する。
56      * @param color 色
57      * @return 不透明化した色。引数と同じ場合もありうる。
58      */
59     private static Color forceOpaque(Color color){
60         if(color.getTransparency() == Transparency.OPAQUE){
61             return color;
62         }
63
64         float[] rgba = null;
65         rgba = color.getRGBColorComponents(rgba);
66
67         Color result = new Color(rgba[IDX_RED],
68                                  rgba[IDX_GREEN],
69                                  rgba[IDX_BLUE],
70                                  1.0f );
71
72         return result;
73     }
74
75
76     /**
77      * マテリアル名を返す。
78      * PMDEditorのみでのサポート?
79      * @return マテリアル名
80      */
81     public I18nText getMaterialName(){
82         return this.materialName;
83     }
84
85     /**
86      * 拡散光を設定する。
87      * アルファ成分も反映される。
88      * @param color 拡散光
89      * @throws NullPointerException 引数がnull
90      */
91     public void setDiffuseColor(Color color) throws NullPointerException{
92         if(color == null) throw new NullPointerException();
93         this.diffuseColor = color;
94         return;
95     }
96
97     /**
98      * 拡散光を返す。
99      * @return 拡散光
100      */
101     public Color getDiffuseColor(){
102         return this.diffuseColor;
103     }
104
105     /**
106      * 反射光を設定する。
107      * 透過成分があれば不透明化される。
108      * @param color 反射光
109      * @throws NullPointerException 引数がnull
110      */
111     public void setSpecularColor(Color color)
112             throws NullPointerException{
113         if(color == null) throw new NullPointerException();
114         this.specularColor = forceOpaque(color);
115         return;
116     }
117
118     /**
119      * 反射光を返す。
120      * @return 反射光
121      */
122     public Color getSpecularColor(){
123         return this.specularColor;
124     }
125
126     /**
127      * 環境光を設定する。
128      * 透過成分があれば不透明化される。
129      * @param color 環境光
130      * @throws NullPointerException 引数がnull
131      */
132     public void setAmbientColor(Color color)
133             throws NullPointerException{
134         if(color == null) throw new NullPointerException();
135         this.ambientColor = forceOpaque(color);
136         return;
137     }
138
139     /**
140      * 環境光を返す。
141      * @return 環境光
142      */
143     public Color getAmbientColor(){
144         return this.ambientColor;
145     }
146
147     /**
148      * 光沢強度を設定する。
149      * MMDで用いられるのは1.0〜15.0あたり。
150      * @param shininess 光沢強度
151      */
152     public void setShininess(float shininess){
153         this.shininess = shininess;
154         return;
155     }
156
157     /**
158      * 光沢強度を返す。
159      * @return 光沢強度
160      */
161     public float getShininess(){
162         return this.shininess;
163     }
164
165     /**
166      * シェーディング設定を返す。
167      * @return シェーディング設定
168      */
169     public ShadeInfo getShadeInfo(){
170         return this.shadeInfo;
171     }
172
173     /**
174      * エッジを表示するか設定する。
175      * 頂点単位の設定より優先度は低い。
176      * @param show 表示するならtrue
177      */
178     public void setEdgeAppearance(boolean show){
179         this.edgeAppearance = show;
180         return;
181     }
182
183     /**
184      * エッジを表示するか判定する。
185      * 頂点単位の設定より優先度は低い。
186      * @return 表示するならtrue
187      */
188     public boolean getEdgeAppearance(){
189         return this.edgeAppearance;
190     }
191
192     /**
193      * 面リストを返す。
194      * @return 面リスト
195      */
196     public List<Surface> getSurfaceList(){
197         return this.surfaceList;
198     }
199
200     /**
201      * {@inheritDoc}
202      * @return {@inheritDoc}
203      */
204     @Override
205     public Iterator<Surface> iterator(){
206         return this.surfaceList.iterator();
207     }
208
209     /**
210      * {@inheritDoc}
211      * @return {@inheritDoc}
212      */
213     @Override
214     public String toString(){
215         StringBuilder result = new StringBuilder();
216         final String delim = ", ";
217
218         result.append("Material ");
219
220         float[] rgba = null;
221
222         rgba = this.diffuseColor.getRGBComponents(rgba);
223         result.append("diffuse=[")
224               .append(rgba[IDX_RED]).append(delim)
225               .append(rgba[IDX_GREEN]).append(delim)
226               .append(rgba[IDX_BLUE]).append(delim)
227               .append(rgba[IDX_ALPHA]).append(']')
228               .append(' ');
229
230         this.specularColor.getRGBComponents(rgba);
231         result.append("specular=[")
232               .append(rgba[IDX_RED]).append(delim)
233               .append(rgba[IDX_GREEN]).append(delim)
234               .append(rgba[IDX_BLUE]).append(delim)
235               .append(rgba[IDX_ALPHA]).append(']')
236               .append(' ');
237
238         this.ambientColor.getRGBComponents(rgba);
239         result.append("ambient=[")
240               .append(rgba[IDX_RED]).append(delim)
241               .append(rgba[IDX_GREEN]).append(delim)
242               .append(rgba[IDX_BLUE]).append(delim)
243               .append(rgba[IDX_ALPHA]).append(']')
244               .append(' ');
245
246         result.append("shininess=")
247               .append(this.shininess)
248               .append(' ');
249         result.append("edge=")
250               .append(this.edgeAppearance)
251               .append(' ');
252         result.append("surfaces=")
253               .append(this.surfaceList.size())
254               .append(' ');
255         result.append(this.shadeInfo);
256
257         return result.toString();
258     }
259
260 }