Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.paint;
027
028import com.sun.javafx.sg.PGPhongMaterial;
029import com.sun.javafx.tk.Toolkit;
030import javafx.beans.property.DoubleProperty;
031import javafx.beans.property.ObjectProperty;
032import javafx.beans.property.SimpleDoubleProperty;
033import javafx.beans.property.SimpleObjectProperty;
034import javafx.scene.image.Image;
035
036/**
037 * The {@code PhongMaterial} class provides definitions of properties that 
038 * represent a form of Phong shaded material.
039 *
040 * @since JavaFX 8
041 */
042public class PhongMaterial extends Material {
043
044    private boolean diffuseColorDirty = true;
045    private boolean specularColorDirty = true;
046    private boolean specularPowerDirty = true;
047    private boolean diffuseMapDirty = true;
048    private boolean specularMapDirty = true;
049    private boolean bumpMapDirty = true;
050    private boolean selfIlluminationMapDirty = true;
051
052    /**
053     * Creates a new instance of {@code PhongMaterial} class.
054     */
055    public PhongMaterial() {
056        // TODO: 3D - Need to document this ...
057        setDiffuseColor(Color.WHITE);        
058    }
059
060    // TODO: 3D - Need to document this ...
061    public PhongMaterial(Color diffuseColor) {
062        setDiffuseColor(diffuseColor);
063    }
064
065    // TODO: 3D - Need to document this ...
066    public PhongMaterial(Color diffuseColor, Image diffuseMap,
067            Image specularMap, Image bumpMap, Image selfIlluminationMap) {
068        setDiffuseColor(diffuseColor);
069        setDiffuseMap(diffuseMap);
070        setSpecularMap(specularMap);
071        setBumpMap(bumpMap);
072        setSelfIlluminationMap(selfIlluminationMap);
073    }
074
075    /**
076     * Specifies the diffuse color of this Material.
077     *
078     * @defaultValue Color.WHITE
079     */
080    private ObjectProperty<Color> diffuseColor;
081
082    public final void setDiffuseColor(Color value) {
083        diffuseColorProperty().set(value);
084    }
085
086    public final Color getDiffuseColor() {
087        return diffuseColor == null ? null : diffuseColor.get();
088    }
089
090    public final ObjectProperty<Color> diffuseColorProperty() {
091        if (diffuseColor == null) {
092            diffuseColor = new SimpleObjectProperty<Color>(PhongMaterial.this,
093                    "diffuseColor") {
094                @Override
095                protected void invalidated() {
096                    diffuseColorDirty = true;
097                    setDirty(true);
098                }
099            };
100        }
101        return diffuseColor;
102    }
103    
104    /**
105     * Specifies the specular color of this Material.
106     *
107     * @defaultValue null
108     */
109    private ObjectProperty<Color> specularColor;
110
111    public final void setSpecularColor(Color value) {
112        specularColorProperty().set(value);
113    }
114
115    public final Color getSpecularColor() {
116        return specularColor == null ? null : specularColor.get();
117    }
118
119    public final ObjectProperty<Color> specularColorProperty() {
120        if (specularColor == null) {
121            specularColor = new SimpleObjectProperty<Color>(PhongMaterial.this,
122                    "specularColor") {
123                @Override
124                protected void invalidated() {
125                    specularColorDirty = true;
126                    setDirty(true);
127                }
128            };
129        }
130        return specularColor;
131    }
132
133    /**
134     * Defines the specular power of this Material.
135     *
136     * @defaultValue 1.0
137     */
138    private DoubleProperty specularPower;
139
140    public final void setSpecularPower(double value) {
141        specularPowerProperty().set(value);
142    }
143
144    public final double getSpecularPower() {
145        return specularPower == null ? 1 : specularPower.get();
146    }
147
148    public final DoubleProperty specularPowerProperty() {
149        if (specularPower == null) {
150            specularPower = new SimpleDoubleProperty(PhongMaterial.this, 
151                    "specularPower", 1.0) {
152                @Override
153                public void invalidated() {
154                    specularPowerDirty = true;
155                    setDirty(true);
156                }
157            };
158        }
159        return specularPower;
160    }
161
162    /**
163     * The diffuse map of this {@code PhongMaterial).
164     *
165     * @defaultValue null
166     */
167    // TODO: 3D - Texture or Image? For Media it might be better to have it as a Texture
168    private ObjectProperty<Image> diffuseMap;
169
170    public final void setDiffuseMap(Image value) {
171        diffuseMapProperty().set(value);
172    }
173
174    public final Image getDiffuseMap() {
175        return diffuseMap == null ? null : diffuseMap.get();
176    }
177
178    public final ObjectProperty<Image> diffuseMapProperty() {
179        if (diffuseMap == null) {
180            diffuseMap = new SimpleObjectProperty<Image>(PhongMaterial.this,
181                    "diffuseMap") {
182                @Override
183                public void invalidated() {
184                    diffuseMapDirty = true;
185                    setDirty(true);
186                }
187            };
188        }
189        return diffuseMap;
190    }
191
192    /**
193     * The specular map of this {@code PhongMaterial).
194     *
195     * @defaultValue null
196     */
197    // TODO: 3D - Texture or Image? For Media it might be better to have it as a Texture
198    private ObjectProperty<Image> specularMap;
199
200    public final void setSpecularMap(Image value) {
201        specularMapProperty().set(value);
202    }
203
204    public final Image getSpecularMap() {
205        return specularMap == null ? null : specularMap.get();
206    }
207
208    public final ObjectProperty<Image> specularMapProperty() {
209        if (specularMap == null) {
210            specularMap = new SimpleObjectProperty<Image>(PhongMaterial.this,
211                    "specularMap") {
212                @Override
213                public void invalidated() {
214                    specularMapDirty = true;
215                    setDirty(true);
216                }
217            };
218        }
219        return specularMap;
220    }
221
222    /**
223     * The bump map of this {@code PhongMaterial).
224     *
225     * @defaultValue null
226     */
227    // TODO: 3D - Texture or Image? For Media it might be better to have it as a Texture
228    private ObjectProperty<Image> bumpMap;
229
230    public final void setBumpMap(Image value) {
231        bumpMapProperty().set(value);
232    }
233
234    public final Image getBumpMap() {
235        return bumpMap == null ? null : bumpMap.get();
236    }
237
238    public final ObjectProperty<Image> bumpMapProperty() {
239        if (bumpMap == null) {
240            bumpMap = new SimpleObjectProperty<Image>(PhongMaterial.this,
241                    "bumpMap") {
242                @Override
243                public void invalidated() {
244                    bumpMapDirty = true;
245                    setDirty(true);
246                }
247            };
248        }
249        return bumpMap;
250    }
251
252    /**
253     * The self illumination map of this {@code PhongMaterial).
254     *
255     * @defaultValue null
256     */
257    // TODO: 3D - Texture or Image? For Media it might be better to have it as a Texture
258    private ObjectProperty<Image> selfIlluminationMap;
259
260    public final void setSelfIlluminationMap(Image value) {
261        selfIlluminationMapProperty().set(value);
262    }
263 
264    public final Image getSelfIlluminationMap() {
265        return selfIlluminationMap == null ? null : selfIlluminationMap.get();
266    }
267    
268    public final ObjectProperty<Image> selfIlluminationMapProperty() {
269        if (selfIlluminationMap == null) {
270            selfIlluminationMap = new SimpleObjectProperty<Image>(PhongMaterial.this,
271                    "selfIlluminationMap") {
272                @Override
273                public void invalidated() {
274                    selfIlluminationMapDirty = true;
275                    setDirty(true);
276                }
277            };
278        }
279        return selfIlluminationMap;
280    }
281
282    @Override
283    void setDirty(boolean value) {
284        super.setDirty(value);
285        if (!value) {
286            diffuseColorDirty = false;
287            specularColorDirty = false;
288            specularPowerDirty = false;
289            diffuseMapDirty = false;
290            specularMapDirty = false;
291            bumpMapDirty = false;
292            selfIlluminationMapDirty = false;
293        }
294    }
295    
296    /** The peer node created by the graphics Toolkit/Pipeline implementation */
297    private PGPhongMaterial peer;
298    
299    /**
300     * @treatAsPrivate implementation detail
301     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
302     */
303    @Deprecated
304    @Override
305    public PGPhongMaterial impl_getPGMaterial() {
306        if (peer == null) {
307            peer = Toolkit.getToolkit().createPGPhongMaterial();
308        }
309        return peer;
310    }
311
312    /**
313     * @treatAsPrivate implementation detail
314     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
315     */
316    @Deprecated
317    @Override
318    public void impl_updatePG(){
319        if (!isDirty()) {
320            return;
321        }
322
323        PGPhongMaterial pMaterial = (PGPhongMaterial) impl_getPGMaterial();
324        if (diffuseColorDirty) {
325            pMaterial.setDiffuseColor(getDiffuseColor() == null ? null
326                    : Toolkit.getPaintAccessor().getPlatformPaint(getDiffuseColor()));
327        }
328        if (specularColorDirty) {
329            pMaterial.setSpecularColor(getSpecularColor() == null ? null
330                    : Toolkit.getPaintAccessor().getPlatformPaint(getSpecularColor()));
331        }
332        if (specularPowerDirty) {
333            pMaterial.setSpecularPower((float)getSpecularPower());
334        }
335        if (diffuseMapDirty) {
336            pMaterial.setDiffuseMap(getDiffuseMap()
337                    == null ? null : getDiffuseMap().impl_getPlatformImage());
338        }
339        if (specularMapDirty) {
340            pMaterial.setSpecularMap(getSpecularMap()
341                    == null ? null : getSpecularMap().impl_getPlatformImage());
342        }
343        if (bumpMapDirty) {
344            pMaterial.setBumpMap(getBumpMap()
345                    == null ? null : getBumpMap().impl_getPlatformImage());
346        }
347        if (selfIlluminationMapDirty) {
348            pMaterial.setSelfIllumMap(getSelfIlluminationMap()
349                    == null ? null : getSelfIlluminationMap().impl_getPlatformImage());
350        }
351
352        setDirty(false);
353    }
354
355    @Override public String toString() {
356        return "PGPhongMaterial[" + "diffuseColor=" + getDiffuseColor() +
357                ", specularColor=" + getSpecularColor() +
358                ", specularPower=" + getSpecularPower() +
359                ", diffuseMap=" + getDiffuseMap() +
360                ", specularMap=" + getSpecularMap() +
361                ", bumpMap=" + getBumpMap() +
362                ", selfIlluminationMap=" + getSelfIlluminationMap() + "]";
363    }
364
365}