1 /*
   2  * Copyright (c) 2013, 2016, 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  * @since JavaFX 8.0
  67  */
  68 public abstract class Shape3D extends Node {
  69     static {
  70         // This is used by classes in different packages to get access to
  71         // private and package private methods.
  72         Shape3DHelper.setShape3DAccessor(new Shape3DHelper.Shape3DAccessor() {
  73             @Override
  74             public void doUpdatePeer(Node node) {
  75                 ((Shape3D) node).doUpdatePeer();
  76             }
  77 
  78             @Override
  79             public BaseBounds doComputeGeomBounds(Node node,
  80             BaseBounds bounds, BaseTransform tx) {
  81                 return ((Shape3D) node).doComputeGeomBounds(bounds, tx);
  82             }
  83 
  84             @Override
  85             public boolean doComputeContains(Node node, double localX, double localY) {
  86                 return ((Shape3D) node).doComputeContains(localX, localY);
  87             }
  88 
  89             @Override
  90             public Object doProcessMXNode(Node node, MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
  91                 return ((Shape3D) node).doProcessMXNode(alg, ctx);
  92             }
  93         });
  94     }
  95 
  96     // NOTE: Need a way to specify shape tessellation resolution, may use metric relate to window resolution
  97     // Will not support dynamic refinement in FX8
  98 
  99     // TODO: 3D - May provide user convenient utility to compose images in a single image for shapes such as Box or Cylinder
 100 
 101     private static final PhongMaterial DEFAULT_MATERIAL = new PhongMaterial();
 102 
 103     protected Shape3D() {
 104         if (!Platform.isSupported(ConditionalFeature.SCENE3D)) {
 105             String logname = Shape3D.class.getName();
 106             PlatformLogger.getLogger(logname).warning("System can't support "
 107                                                       + "ConditionalFeature.SCENE3D");
 108         }
 109     }
 110 
 111     PredefinedMeshManager manager = PredefinedMeshManager.getInstance();
 112     int key = 0;
 113 
 114     /**
 115      * Defines the material this {@code Shape3D}.
 116      * The default material is null. If {@code Material} is null, a PhongMaterial
 117      * with a diffuse color of Color.LIGHTGRAY is used for rendering.
 118      *
 119      * @defaultValue null
 120      */
 121     private ObjectProperty<Material> material;
 122 
 123     public final void setMaterial(Material value) {
 124         materialProperty().set(value);
 125     }
 126 
 127     public final Material getMaterial() {
 128         return material == null ? null : material.get();
 129     }
 130 
 131     public final ObjectProperty<Material> materialProperty() {
 132         if (material == null) {
 133             material = new SimpleObjectProperty<Material>(Shape3D.this,
 134                     "material") {
 135 
 136                 private Material old = null;
 137                 private final ChangeListener<Boolean> materialChangeListener =
 138                         (observable, oldValue, newValue) -> {
 139                             if (newValue) {
 140                                 NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL);
 141                             }
 142                         };
 143                 private final WeakChangeListener<Boolean> weakMaterialChangeListener =
 144                         new WeakChangeListener(materialChangeListener);
 145 
 146                 @Override protected void invalidated() {
 147                     if (old != null) {
 148                         MaterialHelper.dirtyProperty(old).removeListener(weakMaterialChangeListener);
 149                     }
 150                     Material newMaterial = get();
 151                     if (newMaterial != null) {
 152                         MaterialHelper.dirtyProperty(newMaterial).addListener(weakMaterialChangeListener);
 153                     }
 154                     NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL);
 155                     NodeHelper.geomChanged(Shape3D.this);
 156                     old = newMaterial;
 157                 }
 158             };
 159         }
 160         return material;
 161     }
 162 
 163     /**
 164      * Defines the draw mode used to render this {@code Shape3D}.
 165      * {@link DrawMode.LINE} is not available on embedded platforms.
 166      * If {@code drawMode} is set to {@link DrawMode.LINE} on an embedded
 167      * platform the default value of {@link DrawMode.FILL} will be used instead.
 168      *
 169      * @defaultValue DrawMode.FILL
 170      */
 171     private ObjectProperty<DrawMode> drawMode;
 172 
 173     public final void setDrawMode(DrawMode value) {
 174         drawModeProperty().set(value);
 175     }
 176 
 177     public final DrawMode getDrawMode() {
 178         return drawMode == null ? DrawMode.FILL : drawMode.get();
 179     }
 180 
 181     public final ObjectProperty<DrawMode> drawModeProperty() {
 182         if (drawMode == null) {
 183             drawMode = new SimpleObjectProperty<DrawMode>(Shape3D.this,
 184                     "drawMode", DrawMode.FILL) {
 185 
 186                 @Override
 187                 protected void invalidated() {
 188                     NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_DRAWMODE);
 189                 }
 190             };
 191         }
 192         return drawMode;
 193     }
 194 
 195     /**
 196      * Defines the cullFace this {@code Shape3D}.
 197      *
 198      * @defaultValue CullFace.BACK
 199      */
 200     private ObjectProperty<CullFace> cullFace;
 201 
 202     public final void setCullFace(CullFace value) {
 203         cullFaceProperty().set(value);
 204     }
 205 
 206     public final CullFace getCullFace() {
 207         return cullFace == null ? CullFace.BACK : cullFace.get();
 208     }
 209 
 210     public final ObjectProperty<CullFace> cullFaceProperty() {
 211         if (cullFace == null) {
 212             cullFace = new SimpleObjectProperty<CullFace>(Shape3D.this,
 213                     "cullFace", CullFace.BACK) {
 214 
 215                 @Override
 216                 protected void invalidated() {
 217                     NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_CULLFACE);
 218                 }
 219             };
 220         }
 221         return cullFace;
 222     }
 223 
 224     /*
 225      * Note: This method MUST only be called via its accessor method.
 226      */
 227     private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx) {
 228         // TODO: 3D - Evaluate this logic
 229         return new BoxBounds(0, 0, 0, 0, 0, 0);
 230     }
 231 
 232     /*
 233      * Note: This method MUST only be called via its accessor method.
 234      */
 235     private boolean doComputeContains(double localX, double localY) {
 236         return false;
 237     }
 238 
 239     /*
 240      * Note: This method MUST only be called via its accessor method.
 241      */
 242     private void doUpdatePeer() {
 243         final NGShape3D peer = NodeHelper.getPeer(this);
 244         if (NodeHelper.isDirty(this, DirtyBits.MATERIAL)) {
 245             Material mat = getMaterial() == null ? DEFAULT_MATERIAL : getMaterial();
 246             MaterialHelper.updatePG(mat); // new material should be updated
 247             peer.setMaterial(MaterialHelper.getNGMaterial(mat));
 248         }
 249         if (NodeHelper.isDirty(this, DirtyBits.NODE_DRAWMODE)) {
 250             peer.setDrawMode(getDrawMode() == null ? DrawMode.FILL : getDrawMode());
 251         }
 252         if (NodeHelper.isDirty(this, DirtyBits.NODE_CULLFACE)) {
 253             peer.setCullFace(getCullFace() == null ? CullFace.BACK : getCullFace());
 254         }
 255     }
 256 
 257     /*
 258      * Note: This method MUST only be called via its accessor method.
 259      */
 260     private Object doProcessMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx) {
 261         throw new UnsupportedOperationException("Not supported yet.");
 262     }
 263 
 264 }