Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.input;
027
028import javafx.event.EventTarget;
029import javafx.geometry.Point2D;
030import javafx.geometry.Point3D;
031import javafx.scene.Node;
032
033/**
034 * A container object that contains the result of a pick event
035 */
036public class PickResult {
037
038    /**
039     * An undefined face. This value is used for the intersected face
040     * if the picked node has no user-specified faces.
041     */
042    public static final int FACE_UNDEFINED = -1;
043
044    private Node node;
045    private Point3D point;
046    private double distance = Double.POSITIVE_INFINITY;
047    private int face = -1;
048    private Point2D texCoord;
049
050    /**
051     * Creates a new instance of PickResult.
052     * @param node The intersected node
053     * @param point The intersected point in local coordinate of the picked Node
054     * @param distance The intersected distance between camera position and the picked Node
055     * @param face The intersected face of the picked Node
056     * @param texCoord The intersected texture coordinates of the picked Node
057     */
058    public PickResult(Node node, Point3D point, double distance, int face, Point2D texCoord) {
059        this.node = node;
060        this.point = point;
061        this.distance = distance;
062        this.face = face;
063        this.texCoord = texCoord;
064    }
065
066    /**
067     * Creates a new instance of PickResult for a non-3d-shape target.
068     * Sets face to FACE_UNDEFINED and texCoord to null.
069     * @param node The intersected node
070     * @param point The intersected point in local coordinate of the picked Node
071     * @param distance The intersected distance between camera position and the picked Node
072     */
073    public PickResult(Node node, Point3D point, double distance) {
074        this.node = node;
075        this.point = point;
076        this.distance = distance;
077        this.face = FACE_UNDEFINED;
078        this.texCoord = null;
079    }
080
081    /**
082     * Creates a pick result for a 2D case where no additional information is needed.
083     * Converts the given scene coordinates to the target's local coordinate space
084     * and stores the value as the intersected point. Sets intersected node
085     * to the given target, distance to 1.0,
086     * face to FACE_UNDEFINED and texCoord to null.
087     * @param target The picked target (null in case of a Scene)
088     * @param sceneX The scene X coordinate
089     * @param sceneY The scene Y coordinate
090     */
091    public PickResult(EventTarget target, double sceneX, double sceneY) {
092        this(target instanceof Node ? (Node) target : null,
093                target instanceof Node ? ((Node) target).sceneToLocal(sceneX, sceneY, 0) : new Point3D(sceneX, sceneY, 0),
094                1.0);
095    }
096
097    /**
098     * Returns the intersected node.
099     * Returns null if there was no intersection with any node and the scene
100     * was picked.
101     *
102     * @return the picked node or null if no node was picked
103     */
104    public final Node getIntersectedNode() {
105        return node;
106    }
107
108    /**
109     * Returns the intersected point in local coordinate of the picked Node.
110     * If no node was picked, it returns the intersected point with the
111     * projection plane.
112     *
113     * @return new Point3D presenting the intersected point
114     */
115    public final Point3D getIntersectedPoint() {
116        return point;
117    }
118
119    /**
120     * Returns the intersected distance between camera position 
121     * and the intersected point.
122     *
123     * @return the distance from camera to the intersection
124     */
125    public final double getIntersectedDistance() {
126        return distance;
127    }
128
129    /**
130     * Returns the intersected face of the picked Node, FACE_UNDEFINED
131     *         if the node doesn't have user-specified faces
132     *         or was picked on bounds.
133     * 
134     * @return the picked face
135     */
136    public final int getIntersectedFace() {
137        return face;
138     }
139
140    /**
141     * Return the intersected texture coordinates of the picked 3d shape.
142     * If the picked target is not Shape3D or has pickOnBounds==true,
143     * it returns null.
144     *
145     * return new Point2D presenting the intersected TexCoord
146     */
147    public final Point2D getIntersectedTexCoord() {
148        return texCoord;
149    }
150
151    @Override
152    public String toString() {
153        final StringBuilder sb = new StringBuilder("PickResult [");
154        sb.append("node = ").append(getIntersectedNode())
155                .append(", point = ").append(getIntersectedPoint())
156                .append(", distance = ").append(getIntersectedDistance());
157        if (getIntersectedFace() != FACE_UNDEFINED) {
158                sb.append(", face = ").append(getIntersectedFace());
159        }
160        if (getIntersectedTexCoord() != null) {
161                sb.append(", texCoord = ").append(getIntersectedTexCoord());
162        }
163        return sb.toString();
164    }
165}