1 /* 2 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.shape; 27 28 import com.sun.javafx.geom.BaseBounds; 29 import com.sun.javafx.geom.BoxBounds; 30 import com.sun.javafx.geom.transform.BaseTransform; 31 import com.sun.javafx.jmx.MXNodeAlgorithm; 32 import com.sun.javafx.jmx.MXNodeAlgorithmContext; 33 import com.sun.javafx.scene.DirtyBits; 34 import com.sun.javafx.scene.NodeHelper; 35 import com.sun.javafx.scene.paint.MaterialHelper; 36 import com.sun.javafx.scene.shape.Shape3DHelper; 37 import com.sun.javafx.sg.prism.NGShape3D; 38 import javafx.application.ConditionalFeature; 39 import javafx.application.Platform; 40 import javafx.beans.property.ObjectProperty; 41 import javafx.beans.property.SimpleObjectProperty; 42 import javafx.beans.value.ChangeListener; 43 import javafx.beans.value.ObservableValue; 44 import javafx.beans.value.WeakChangeListener; 45 import javafx.scene.Node; 46 import javafx.scene.paint.Material; 47 import javafx.scene.paint.PhongMaterial; 48 import sun.util.logging.PlatformLogger; 49 50 51 /** 52 * The {@code Shape3D} base class provides definitions of common properties for 53 * objects that represent some form of 3D geometric shape. These properties 54 * include: 55 * <ul> 56 * <li>The {@link Material} to be applied to the fillable interior of the 57 * shape or the outline of the shape (see {@link #setMaterial}). 58 * <li>The draw model properties that defines how to render its geometry (see {@link #setDrawMode}). 59 * <li>The face culling properties that defines which face to cull (see {@link #setCullFace}). 60 * </ul> 61 * 62 * Note that this is a conditional feature. See 63 * {@link javafx.application.ConditionalFeature#SCENE3D ConditionalFeature.SCENE3D} 64 * for more information. 65 * 66 * <p> 67 * An application should not extend the Shape3D class directly. Doing so may lead to 68 * an UnsupportedOperationException being thrown. 69 * </p> 70 * 71 * @since JavaFX 8.0 72 */ 73 public abstract class Shape3D extends Node { 74 static { 75 // This is used by classes in different packages to get access to 76 // private and package private methods. 77 Shape3DHelper.setShape3DAccessor(new Shape3DHelper.Shape3DAccessor() { 78 @Override 79 public void doUpdatePeer(Node node) { 80 ((Shape3D) node).doUpdatePeer(); 81 } 82 83 @Override 84 public BaseBounds doComputeGeomBounds(Node node, 85 BaseBounds bounds, BaseTransform tx) { 86 return ((Shape3D) node).doComputeGeomBounds(bounds, tx); 87 } 88 89 @Override 90 public boolean doComputeContains(Node node, double localX, double localY) { 91 return ((Shape3D) node).doComputeContains(localX, localY); 92 } 93 94 @Override 95 public Object doProcessMXNode(Node node, MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) { 96 return ((Shape3D) node).doProcessMXNode(alg, ctx); 97 } 98 }); 99 } 100 101 // NOTE: Need a way to specify shape tessellation resolution, may use metric relate to window resolution 102 // Will not support dynamic refinement in FX8 103 104 // TODO: 3D - May provide user convenient utility to compose images in a single image for shapes such as Box or Cylinder 105 106 private static final PhongMaterial DEFAULT_MATERIAL = new PhongMaterial(); 107 108 protected Shape3D() { 109 if (!Platform.isSupported(ConditionalFeature.SCENE3D)) { 110 String logname = Shape3D.class.getName(); 111 PlatformLogger.getLogger(logname).warning("System can't support " 112 + "ConditionalFeature.SCENE3D"); 113 } 114 } 115 116 PredefinedMeshManager manager = PredefinedMeshManager.getInstance(); 117 int key = 0; 118 119 /** 120 * Defines the material this {@code Shape3D}. 121 * The default material is null. If {@code Material} is null, a PhongMaterial 122 * with a diffuse color of Color.LIGHTGRAY is used for rendering. 123 * 124 * @defaultValue null 125 */ 126 private ObjectProperty<Material> material; 127 128 public final void setMaterial(Material value) { 129 materialProperty().set(value); 130 } 131 132 public final Material getMaterial() { 133 return material == null ? null : material.get(); 134 } 135 136 public final ObjectProperty<Material> materialProperty() { 137 if (material == null) { 138 material = new SimpleObjectProperty<Material>(Shape3D.this, 139 "material") { 140 141 private Material old = null; 142 private final ChangeListener<Boolean> materialChangeListener = 143 (observable, oldValue, newValue) -> { 144 if (newValue) { 145 NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL); 146 } 147 }; 148 private final WeakChangeListener<Boolean> weakMaterialChangeListener = 149 new WeakChangeListener(materialChangeListener); 150 151 @Override protected void invalidated() { 152 if (old != null) { 153 MaterialHelper.dirtyProperty(old).removeListener(weakMaterialChangeListener); 154 } 155 Material newMaterial = get(); 156 if (newMaterial != null) { 157 MaterialHelper.dirtyProperty(newMaterial).addListener(weakMaterialChangeListener); 158 } 159 NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL); 160 NodeHelper.geomChanged(Shape3D.this); 161 old = newMaterial; 162 } 163 }; 164 } 165 return material; 166 } 167 168 /** 169 * Defines the draw mode used to render this {@code Shape3D}. 170 * {@link DrawMode#LINE} is not available on embedded platforms. 171 * If {@code drawMode} is set to {@link DrawMode#LINE} on an embedded 172 * platform the default value of {@link DrawMode#FILL} will be used instead. 173 * 174 * @defaultValue {@link DrawMode#FILL} 175 */ 176 private ObjectProperty<DrawMode> drawMode; 177 178 public final void setDrawMode(DrawMode value) { 179 drawModeProperty().set(value); 180 } 181 182 public final DrawMode getDrawMode() { 183 return drawMode == null ? DrawMode.FILL : drawMode.get(); 184 } 185 186 public final ObjectProperty<DrawMode> drawModeProperty() { 187 if (drawMode == null) { 188 drawMode = new SimpleObjectProperty<DrawMode>(Shape3D.this, 189 "drawMode", DrawMode.FILL) { 190 191 @Override 192 protected void invalidated() { 193 NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_DRAWMODE); 194 } 195 }; 196 } 197 return drawMode; 198 } 199 200 /** 201 * Defines the cullFace this {@code Shape3D}. 202 * 203 * @defaultValue CullFace.BACK 204 */ 205 private ObjectProperty<CullFace> cullFace; 206 207 public final void setCullFace(CullFace value) { 208 cullFaceProperty().set(value); 209 } 210 211 public final CullFace getCullFace() { 212 return cullFace == null ? CullFace.BACK : cullFace.get(); 213 } 214 215 public final ObjectProperty<CullFace> cullFaceProperty() { 216 if (cullFace == null) { 217 cullFace = new SimpleObjectProperty<CullFace>(Shape3D.this, 218 "cullFace", CullFace.BACK) { 219 220 @Override 221 protected void invalidated() { 222 NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_CULLFACE); 223 } 224 }; 225 } 226 return cullFace; 227 } 228 229 /* 230 * Note: This method MUST only be called via its accessor method. 231 */ 232 private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx) { 233 // TODO: 3D - Evaluate this logic 234 return new BoxBounds(0, 0, 0, 0, 0, 0); 235 } 236 237 /* 238 * Note: This method MUST only be called via its accessor method. 239 */ 240 private boolean doComputeContains(double localX, double localY) { 241 return false; 242 } 243 244 /* 245 * Note: This method MUST only be called via its accessor method. 246 */ 247 private void doUpdatePeer() { 248 final NGShape3D peer = NodeHelper.getPeer(this); 249 if (NodeHelper.isDirty(this, DirtyBits.MATERIAL)) { 250 Material mat = getMaterial() == null ? DEFAULT_MATERIAL : getMaterial(); 251 MaterialHelper.updatePG(mat); // new material should be updated 252 peer.setMaterial(MaterialHelper.getNGMaterial(mat)); 253 } 254 if (NodeHelper.isDirty(this, DirtyBits.NODE_DRAWMODE)) { 255 peer.setDrawMode(getDrawMode() == null ? DrawMode.FILL : getDrawMode()); 256 } 257 if (NodeHelper.isDirty(this, DirtyBits.NODE_CULLFACE)) { 258 peer.setCullFace(getCullFace() == null ? CullFace.BACK : getCullFace()); 259 } 260 } 261 262 /* 263 * Note: This method MUST only be called via its accessor method. 264 */ 265 private Object doProcessMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) { 266 throw new UnsupportedOperationException("Not supported yet."); 267 } 268 269 }