1 /*
   2  * Copyright (c) 2012, 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;
  27 
  28 import javafx.application.ConditionalFeature;
  29 import javafx.application.Platform;
  30 import javafx.geometry.Rectangle2D;
  31 import javafx.scene.paint.Paint;
  32 import javafx.scene.transform.Transform;
  33 import com.sun.javafx.logging.PlatformLogger;
  34 
  35 /**
  36  * Parameters used to specify the rendering attributes for Node snapshot.
  37  * @since JavaFX 2.2
  38  */
  39 public class SnapshotParameters {
  40 
  41     private boolean depthBuffer;
  42     private Camera camera;
  43     private Transform transform;
  44     private Paint fill;
  45     private Rectangle2D viewport;
  46 
  47     /**
  48      * Constructs a new SnapshotParameters object with default values for
  49      * all rendering attributes.
  50      */
  51     public SnapshotParameters() {
  52     }
  53 
  54     /**
  55      * Gets the current depthBuffer flag.
  56      *
  57      * @return the depthBuffer flag
  58      */
  59     public boolean isDepthBuffer() {
  60         return depthBuffer;
  61     }
  62 
  63     boolean isDepthBufferInternal() {
  64         if(!Platform.isSupported(ConditionalFeature.SCENE3D)) {
  65             return false;
  66         }
  67         return depthBuffer;
  68     }
  69 
  70     /**
  71      * Sets the depthBuffer flag to the specified value.
  72      * The default value is false.
  73      *
  74      * Note that this is a conditional feature. See
  75      * {@link javafx.application.ConditionalFeature#SCENE3D ConditionalFeature.SCENE3D}
  76      *
  77      * @param depthBuffer the depthBuffer to set
  78      */
  79     public void setDepthBuffer(boolean depthBuffer) {
  80         if (depthBuffer && !Platform.isSupported(ConditionalFeature.SCENE3D)) {
  81             String logname = SnapshotParameters.class.getName();
  82             PlatformLogger.getLogger(logname).warning("System can't support "
  83                     + "ConditionalFeature.SCENE3D");
  84         }
  85         this.depthBuffer = depthBuffer;
  86     }
  87 
  88     /**
  89      * Gets the current camera.
  90      *
  91      * @return the camera
  92      */
  93     public Camera getCamera() {
  94         return camera;
  95     }
  96 
  97     Camera defaultCamera;
  98 
  99     Camera getEffectiveCamera() {
 100         if (camera instanceof PerspectiveCamera
 101                 && !Platform.isSupported(ConditionalFeature.SCENE3D)) {
 102             if (defaultCamera == null) {
 103                 // According to Scene.doSnapshot, temporarily, it adjusts camera
 104                 // viewport to the snapshot size. So, its viewport doesn't matter.
 105                 defaultCamera = new ParallelCamera();
 106             }
 107             return defaultCamera;
 108         }
 109         return camera;
 110     }
 111 
 112     /**
 113      * Sets the camera to the specified value.
 114      * The default value is null, which means a ParallelCamera will be used.
 115      *
 116      * @param camera the camera to set
 117      */
 118     public void setCamera(Camera camera) {
 119         if (camera instanceof PerspectiveCamera
 120                 && !Platform.isSupported(ConditionalFeature.SCENE3D)) {
 121             String logname = SnapshotParameters.class.getName();
 122             PlatformLogger.getLogger(logname).warning("System can't support "
 123                     + "ConditionalFeature.SCENE3D");
 124         }
 125         this.camera = camera;
 126     }
 127 
 128     /**
 129      * Gets the current transform.
 130      *
 131      * @return the transform
 132      */
 133     public Transform getTransform() {
 134         return transform;
 135     }
 136 
 137     /**
 138      * Sets the transform to the specified value. This transform is applied to
 139      * the node being rendered before any local transforms are applied.
 140      * A value of null indicates that the identity transform should be used.
 141      * The default value is null.
 142      *
 143      * @param transform the transform to set
 144      */
 145     public void setTransform(Transform transform) {
 146         this.transform = transform;
 147     }
 148 
 149     /**
 150      * Gets the current fill.
 151      *
 152      * @return the fill
 153      */
 154     public Paint getFill() {
 155         return fill;
 156     }
 157 
 158     /**
 159      * Sets the fill to the specified value. This is used to fill the entire
 160      * image being rendered prior to rendering the node. A value of null
 161      * indicates that the color white should be used for the fill.
 162      * The default value is null.
 163      *
 164      * @param fill the fill to set
 165      */
 166     public void setFill(Paint fill) {
 167         this.fill = fill;
 168     }
 169 
 170     /**
 171      * Gets the current viewport
 172      *
 173      * @return the viewport
 174      */
 175     public Rectangle2D getViewport() {
 176         return viewport;
 177     }
 178 
 179     /**
 180      * Sets the viewport used for rendering.
 181      * The viewport is specified in the parent coordinate system of the
 182      * node being rendered. It is not transformed by the transform
 183      * of this SnapshotParameters.
 184      * If this viewport is non-null it is used instead of the bounds of the
 185      * node being rendered and specifies the source rectangle that will be
 186      * rendered into the image.
 187      * In this case, the upper-left pixel of the viewport will map to
 188      * the upper-left pixel (0,0)
 189      * in the rendered image.
 190      * If the viewport is null, then the entire area of the node defined
 191      * by its boundsInParent, after first applying the
 192      * transform of this SnapshotParameters, will be rendered.
 193      * The default value is null.
 194      *
 195      * @param viewport the viewport to set
 196      */
 197     public void setViewport(Rectangle2D viewport) {
 198         this.viewport = viewport;
 199     }
 200 
 201     /**
 202      * Returns a deep clone of this SnapshotParameters
 203      *
 204      * @return a clone
 205      */
 206     SnapshotParameters copy() {
 207         SnapshotParameters params = new SnapshotParameters();
 208         params.camera = camera == null ? null : camera.copy();
 209         params.depthBuffer = depthBuffer;
 210         params.fill = fill;
 211         params.viewport = viewport;
 212         params.transform = transform == null ? null : transform.clone();
 213         return params;
 214     }
 215 }