1 /* 2 * Copyright (c) 2010, 2013, 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.input; 27 28 import com.sun.javafx.scene.input.InputEventUtils; 29 import java.io.IOException; 30 import java.io.Serializable; 31 import javafx.beans.NamedArg; 32 import javafx.event.EventTarget; 33 import javafx.geometry.Point3D; 34 import javafx.scene.Node; 35 import javafx.scene.Scene; 36 37 /** 38 * Touch point represents a single point of a multi-touch action, typically 39 * one finger touching a screen. It is contained in {@link TouchEvent}. 40 * <p> 41 * The touch point has its coordinates, state (see {@link State}) and ID. The 42 * ID is sequential number of this touch point unique in scope of a single 43 * multi-touch gesture. 44 * <p> 45 * Each touch point is by default delivered to a single node during its whole 46 * trajectory - to the node on which it was pressed. There is a grabbing API 47 * to modify this behavior. The above means that when touch point is pressed, 48 * it is automatically grabbed by the top-most node on the press coordinates. 49 * Any time during the gesture {@code grab()} and {@code ungrab()} methods 50 * can be used to alter the event delivery target. When grabbed by a different 51 * node, it will next time be targeted to it; when ungrabbed, it will be 52 * always targeted to the top-most node on the current location. 53 * 54 * @since JavaFX 2.2 55 */ 56 public final class TouchPoint implements Serializable{ 57 58 private transient EventTarget target; 59 private transient Object source; 60 61 /** 62 * Creates new instance of TouchPoint. 63 * @param id ID of the new touch point 64 * @param state state of the new touch point 65 * @param x The x with respect to the scene. 66 * @param y The y with respect to the scene. 67 * @param screenX The x coordinate relative to screen. 68 * @param screenY The y coordinate relative to screen. 69 * @param pickResult pick result. Can be null, in this case a 2D pick result 70 * without any further values is constructed 71 * based on the scene coordinates and target 72 * @since JavaFX 8.0 73 */ 74 public TouchPoint(@NamedArg("id") int id, @NamedArg("state") State state, @NamedArg("x") double x, @NamedArg("y") double y, @NamedArg("screenX") double screenX, 75 @NamedArg("screenY") double screenY, @NamedArg("target") EventTarget target, @NamedArg("pickResult") PickResult pickResult) { 76 this.target = target; 77 this.id = id; 78 this.state = state; 79 this.x = x; 80 this.y = y; 81 this.sceneX = x; 82 this.sceneY = y; 83 this.screenX = screenX; 84 this.screenY = screenY; 85 this.pickResult = pickResult != null ? pickResult : new PickResult(target, x, y); 86 final Point3D p = InputEventUtils.recomputeCoordinates(this.pickResult, null); 87 this.x = p.getX(); 88 this.y = p.getY(); 89 this.z = p.getZ(); 90 } 91 92 /** 93 * Recomputes this touch point (coordinates, relevancy) for the given event 94 * source object. 95 * @param oldSource Source object of the current values 96 * @param newSource Source object to compute values for 97 */ 98 void recomputeToSource(Object oldSource, Object newSource) { 99 100 final Point3D newCoordinates = InputEventUtils.recomputeCoordinates( 101 pickResult, newSource); 102 103 x = newCoordinates.getX(); 104 y = newCoordinates.getY(); 105 z = newCoordinates.getZ(); 106 107 source = newSource; 108 } 109 110 /** 111 * Distinguishes between touch points targeted to the given node or some 112 * of its children from touch points targeted somewhere else. This allows 113 * for testing all touch points carried by one touch event on their 114 * relevance for a given node. 115 * @param target Node or other event target to be tested 116 * @return true if this touch point is targeted to the given target or 117 * some of its children 118 */ 119 public boolean belongsTo(EventTarget target) { 120 121 if (this.target instanceof Node) { 122 Node n = (Node) this.target; 123 124 if (target instanceof Scene) { 125 return n.getScene() == target; 126 } 127 while (n != null) { 128 if (n == target) { 129 return true; 130 } 131 n = n.getParent(); 132 } 133 } 134 135 return target == this.target; 136 } 137 138 /** 139 * @treatAsPrivate implementation detail 140 * @deprecated This is an internal API that is not intended for use and will be removed in the next version 141 */ 142 @Deprecated 143 public void impl_reset() { 144 final Point3D p = InputEventUtils.recomputeCoordinates(pickResult, null); 145 x = p.getX(); 146 y = p.getY(); 147 z = p.getZ(); 148 } 149 150 private EventTarget grabbed = null; 151 152 /** 153 * Gets event target which has grabbed this touch point. 154 * @return The current grabbed target, null if the touch point is ungrabbed 155 */ 156 public EventTarget getGrabbed() { 157 return grabbed; 158 } 159 160 /** 161 * Grabs this touch point by current event source. Next event containing 162 * this touch point will be targeted to the same node whose event handler 163 * called this method. 164 */ 165 public void grab() { 166 if (source instanceof EventTarget) { 167 grabbed = (EventTarget) source; 168 } else { 169 throw new IllegalStateException("Cannot grab touch point, " 170 + "source is not an instance of EventTarget: " + source); 171 } 172 } 173 174 /** 175 * Grabs this touch point by the given target. Next event containing this 176 * touch point will be targeted to it. 177 * @param target Target by which to grab the touch point 178 */ 179 public void grab(EventTarget target) { 180 grabbed = target; 181 } 182 183 /** 184 * Ungrabs this touch point from its target. Since the next event this 185 * touch point will be delivered to the top-most node picked on its 186 * respective location until it is grabbed again or released. 187 */ 188 public void ungrab() { 189 grabbed = null; 190 } 191 192 private int id; 193 194 /** 195 * Gets identifier of this touch point. The number is sequential and unique 196 * in scope of one multi touch gesture. The first pressed touch point has id 197 * {@code 1}, each subsequently pressed touch points gets the next ordinal 198 * number until all touch points are released and the counter is reset. 199 * 200 * @return the identifier of this touch point. 201 */ 202 public final int getId() { 203 return id; 204 } 205 206 private State state; 207 208 /** 209 * Gets state of this touch point 210 * @return state of this touch point 211 */ 212 public final State getState() { 213 return state; 214 } 215 216 217 private transient double x; 218 219 /** 220 * Gets the horizontal position of the touch point relative to the 221 * origin of the TouchEvent's source. 222 * 223 * @return the horizontal position of the touch point relative to the 224 * origin of the TouchEvent's source. 225 */ 226 public final double getX() { 227 return x; 228 } 229 230 private transient double y; 231 232 /** 233 * Gets the vertical position of the touch point relative to the 234 * origin of the TouchEvent's source. 235 * 236 * @return the vertical position of the touch point relative to the 237 * origin of the TouchEvent's source. 238 */ 239 public final double getY() { 240 return y; 241 } 242 243 /** 244 * Depth z position of the event relative to the 245 * origin of the MouseEvent's node. 246 */ 247 private transient double z; 248 249 /** 250 * Depth position of the event relative to the 251 * origin of the MouseEvent's source. 252 * 253 * @return depth position of the event relative to the 254 * origin of the MouseEvent's source. 255 * @since JavaFX 8.0 256 */ 257 public final double getZ() { 258 return z; 259 } 260 261 private double screenX; 262 263 /** 264 * Gets the absolute horizontal position of the touch point. 265 * @return the absolute horizontal position of the touch point 266 */ 267 public final double getScreenX() { 268 return screenX; 269 } 270 271 private double screenY; 272 273 /** 274 * Gets the absolute vertical position of the touch point. 275 * @return the absolute vertical position of the touch point 276 */ 277 public final double getScreenY() { 278 return screenY; 279 } 280 281 private double sceneX; 282 283 /** 284 * Gets the horizontal position of the touch point relative to the 285 * origin of the {@code Scene} that contains the TouchEvent's source. 286 * If the node is not in a {@code Scene}, then the value is relative to 287 * the boundsInParent of the root-most parent of the TouchEvent's node. 288 * Note that in 3D scene, this represents the flat coordinates after 289 * applying the projection transformations. 290 * 291 * @return the horizontal position of the touch point relative to the 292 * origin of the {@code Scene} that contains the TouchEvent's source 293 */ 294 public final double getSceneX() { 295 return sceneX; 296 } 297 298 private double sceneY; 299 300 /** 301 * Gets the vertical position of the touch point relative to the 302 * origin of the {@code Scene} that contains the TouchEvent's source. 303 * If the node is not in a {@code Scene}, then the value is relative to 304 * the boundsInParent of the root-most parent of the TouchEvent's node. 305 * Note that in 3D scene, this represents the flat coordinates after 306 * applying the projection transformations. 307 * 308 * @return the vertical position of the touch point relative to the 309 * origin of the {@code Scene} that contains the TouchEvent's source 310 */ 311 public final double getSceneY() { 312 return sceneY; 313 } 314 315 /** 316 * Information about the pick if the picked {@code Node} is a 317 * {@code Shape3D} node and its pickOnBounds is false. 318 */ 319 private PickResult pickResult; 320 321 /** 322 * Returns information about the pick. 323 * 324 * @return new PickResult object that contains information about the pick 325 * @since JavaFX 8.0 326 */ 327 public final PickResult getPickResult() { 328 return pickResult; 329 } 330 331 /** 332 * Gets event target on which the touch event carrying this touch point 333 * is fired. 334 * @return Event target for this touch point 335 */ 336 public EventTarget getTarget() { 337 return target; 338 } 339 340 /** 341 * Returns a string representation of this {@code TouchPoint} object. 342 * @return a string representation of this {@code TouchPoint} object. 343 */ 344 @Override public String toString() { 345 final StringBuilder sb = new StringBuilder("TouchPoint ["); 346 347 sb.append("state = ").append(getState()); 348 sb.append(", id = ").append(getId()); 349 sb.append(", target = ").append(getTarget()); 350 sb.append(", x = ").append(getX()).append(", y = ").append(getY()) 351 .append(", z = ").append(getZ()); 352 sb.append(", pickResult = ").append(getPickResult()); 353 354 return sb.append("]").toString(); 355 } 356 357 private void readObject(java.io.ObjectInputStream in) 358 throws IOException, ClassNotFoundException { 359 in.defaultReadObject(); 360 x = sceneX; 361 y = sceneY; 362 } 363 364 /** 365 * Represents current state of the touch point 366 * 367 * @since JavaFX 2.2 368 */ 369 public enum State { 370 /** 371 * The touch point has just been pressed (touched for the first time) 372 */ 373 PRESSED, 374 /** 375 * The touch point has been moved 376 */ 377 MOVED, 378 /** 379 * The touch point remains pressed and still (without moving) 380 */ 381 STATIONARY, 382 /** 383 * The touch point has been released 384 */ 385 RELEASED 386 } 387 }