1 /*
   2  * Copyright (c) 2013, 2018, 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.scene.DirtyBits;
  32 import com.sun.javafx.scene.NodeHelper;
  33 import com.sun.javafx.scene.paint.MaterialHelper;
  34 import com.sun.javafx.scene.shape.Shape3DHelper;
  35 import com.sun.javafx.sg.prism.NGShape3D;
  36 import javafx.application.ConditionalFeature;
  37 import javafx.application.Platform;
  38 import javafx.beans.property.ObjectProperty;
  39 import javafx.beans.property.SimpleObjectProperty;
  40 import javafx.beans.value.ChangeListener;
  41 import javafx.beans.value.WeakChangeListener;
  42 import javafx.scene.Node;
  43 import javafx.scene.paint.Material;
  44 import javafx.scene.paint.PhongMaterial;
  45 import sun.util.logging.PlatformLogger;
  46 
  47 
  48 /**
  49  * The {@code Shape3D} base class provides definitions of common properties for
  50  * objects that represent some form of 3D geometric shape.  These properties
  51  * include:
  52  * <ul>
  53  * <li>The {@link Material} to be applied to the fillable interior of the
  54  * shape or the outline of the shape (see {@link #setMaterial}).
  55  * <li>The draw model properties that defines how to render its geometry (see {@link #setDrawMode}).
  56  * <li>The face culling properties that defines which face to cull (see {@link #setCullFace}).
  57  * </ul>
  58  *
  59  * Note that this is a conditional feature. See
  60  * {@link javafx.application.ConditionalFeature#SCENE3D ConditionalFeature.SCENE3D}
  61  * for more information.
  62  *
  63  * <p>
  64  * An application should not extend the Shape3D class directly. Doing so may lead to
  65  * an UnsupportedOperationException being thrown.
  66  * </p>
  67  *
  68  * @since JavaFX 8.0
  69  */
  70 public abstract class Shape3D extends Node {
  71     static {
  72         // This is used by classes in different packages to get access to
  73         // private and package private methods.
  74         Shape3DHelper.setShape3DAccessor(new Shape3DHelper.Shape3DAccessor() {
  75             @Override
  76             public void doUpdatePeer(Node node) {
  77                 ((Shape3D) node).doUpdatePeer();
  78             }
  79 
  80             @Override
  81             public BaseBounds doComputeGeomBounds(Node node,
  82                     BaseBounds bounds, BaseTransform tx) {
  83                 return ((Shape3D) node).doComputeGeomBounds(bounds, tx);
  84             }
  85 
  86             @Override
  87             public boolean doComputeContains(Node node, double localX, double localY) {
  88                 return ((Shape3D) node).doComputeContains(localX, localY);
  89             }
  90         });
  91     }
  92 
  93     // NOTE: Need a way to specify shape tessellation resolution, may use metric relate to window resolution
  94     // Will not support dynamic refinement in FX8
  95 
  96     // TODO: 3D - May provide user convenient utility to compose images in a single image for shapes such as Box or Cylinder
  97 
  98     private static final PhongMaterial DEFAULT_MATERIAL = new PhongMaterial();
  99 
 100     protected Shape3D() {
 101         if (!Platform.isSupported(ConditionalFeature.SCENE3D)) {
 102             String logname = Shape3D.class.getName();
 103             PlatformLogger.getLogger(logname).warning("System can't support "
 104                                                       + "ConditionalFeature.SCENE3D");
 105         }
 106     }
 107 
 108     PredefinedMeshManager manager = PredefinedMeshManager.getInstance();
 109     Key key;
 110 
 111     /**
 112      * Used by the caching mechanism to compare between instances of the same shape.
 113      * Each shape implements equals and hashCode using its base parameters.
 114      */
 115     abstract static class Key {
 116 
 117         @Override
 118         public abstract boolean equals(Object obj);
 119 
 120         @Override
 121         public abstract int hashCode();
 122     }
 123 
 124     /**
 125      * Defines the material this {@code Shape3D}.
 126      * The default material is null. If {@code Material} is null, a PhongMaterial
 127      * with a diffuse color of Color.LIGHTGRAY is used for rendering.
 128      *
 129      * @defaultValue null
 130      */
 131     private ObjectProperty<Material> material;
 132 
 133     public final void setMaterial(Material value) {
 134         materialProperty().set(value);
 135     }
 136 
 137     public final Material getMaterial() {
 138         return material == null ? null : material.get();
 139     }
 140 
 141     public final ObjectProperty<Material> materialProperty() {
 142         if (material == null) {
 143             material = new SimpleObjectProperty<Material>(Shape3D.this,
 144                     "material") {
 145 
 146                 private Material old = null;
 147                 private final ChangeListener<Boolean> materialChangeListener =
 148                         (observable, oldValue, newValue) -> {
 149                             if (newValue) {
 150                                 NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL);
 151                             }
 152                         };
 153                 private final WeakChangeListener<Boolean> weakMaterialChangeListener =
 154                         new WeakChangeListener(materialChangeListener);
 155 
 156                 @Override protected void invalidated() {
 157                     if (old != null) {
 158                         MaterialHelper.dirtyProperty(old).removeListener(weakMaterialChangeListener);
 159                     }
 160                     Material newMaterial = get();
 161                     if (newMaterial != null) {
 162                         MaterialHelper.dirtyProperty(newMaterial).addListener(weakMaterialChangeListener);
 163                     }
 164                     NodeHelper.markDirty(Shape3D.this, DirtyBits.MATERIAL);
 165                     NodeHelper.geomChanged(Shape3D.this);
 166                     old = newMaterial;
 167                 }
 168             };
 169         }
 170         return material;
 171     }
 172 
 173     /**
 174      * Defines the draw mode used to render this {@code Shape3D}.
 175      * {@link DrawMode#LINE} is not available on embedded platforms.
 176      * If {@code drawMode} is set to {@link DrawMode#LINE} on an embedded
 177      * platform the default value of {@link DrawMode#FILL} will be used instead.
 178      *
 179      * @defaultValue {@link DrawMode#FILL}
 180      */
 181     private ObjectProperty<DrawMode> drawMode;
 182 
 183     public final void setDrawMode(DrawMode value) {
 184         drawModeProperty().set(value);
 185     }
 186 
 187     public final DrawMode getDrawMode() {
 188         return drawMode == null ? DrawMode.FILL : drawMode.get();
 189     }
 190 
 191     public final ObjectProperty<DrawMode> drawModeProperty() {
 192         if (drawMode == null) {
 193             drawMode = new SimpleObjectProperty<DrawMode>(Shape3D.this,
 194                     "drawMode", DrawMode.FILL) {
 195 
 196                 @Override
 197                 protected void invalidated() {
 198                     NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_DRAWMODE);
 199                 }
 200             };
 201         }
 202         return drawMode;
 203     }
 204 
 205     /**
 206      * Defines the cullFace this {@code Shape3D}.
 207      *
 208      * @defaultValue CullFace.BACK
 209      */
 210     private ObjectProperty<CullFace> cullFace;
 211 
 212     public final void setCullFace(CullFace value) {
 213         cullFaceProperty().set(value);
 214     }
 215 
 216     public final CullFace getCullFace() {
 217         return cullFace == null ? CullFace.BACK : cullFace.get();
 218     }
 219 
 220     public final ObjectProperty<CullFace> cullFaceProperty() {
 221         if (cullFace == null) {
 222             cullFace = new SimpleObjectProperty<CullFace>(Shape3D.this,
 223                     "cullFace", CullFace.BACK) {
 224 
 225                 @Override
 226                 protected void invalidated() {
 227                     NodeHelper.markDirty(Shape3D.this, DirtyBits.NODE_CULLFACE);
 228                 }
 229             };
 230         }
 231         return cullFace;
 232     }
 233 
 234     /*
 235      * Note: This method MUST only be called via its accessor method.
 236      */
 237     private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx) {
 238         // TODO: 3D - Evaluate this logic
 239         return new BoxBounds(0, 0, 0, 0, 0, 0);
 240     }
 241 
 242     /*
 243      * Note: This method MUST only be called via its accessor method.
 244      */
 245     private boolean doComputeContains(double localX, double localY) {
 246         return false;
 247     }
 248 
 249     /*
 250      * Note: This method MUST only be called via its accessor method.
 251      */
 252     private void doUpdatePeer() {
 253         final NGShape3D peer = NodeHelper.getPeer(this);
 254         if (NodeHelper.isDirty(this, DirtyBits.MATERIAL)) {
 255             Material mat = getMaterial() == null ? DEFAULT_MATERIAL : getMaterial();
 256             MaterialHelper.updatePG(mat); // new material should be updated
 257             peer.setMaterial(MaterialHelper.getNGMaterial(mat));
 258         }
 259         if (NodeHelper.isDirty(this, DirtyBits.NODE_DRAWMODE)) {
 260             peer.setDrawMode(getDrawMode() == null ? DrawMode.FILL : getDrawMode());
 261         }
 262         if (NodeHelper.isDirty(this, DirtyBits.NODE_CULLFACE)) {
 263             peer.setCullFace(getCullFace() == null ? CullFace.BACK : getCullFace());
 264         }
 265     }
 266 
 267 }