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