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 javafx.beans.NamedArg;
  29 import javafx.event.EventTarget;
  30 import javafx.event.EventType;
  31 
  32 /**
  33  * Rotate event indicates that user performed rotating gesture such as
  34  * dragging two fingers around each other on track pad,
  35  * touch screen or other similar device.
  36  * <p>
  37  * The event is delivered to the top-most
  38  * node picked on the gesture coordinates in time of the gesture start - the
  39  * whole gesture is delivered to the same node even if the coordinates change
  40  * during the gesture.
  41  * <p>
  42  * The event provides two values: {@code angle} is the rotation angle of this
  43  * event, {@code totalAngle} is the rotation angle of the whole gesture. Both
  44  * values are in degrees and work well when added to the node's {@code rotate}
  45  * property value (positive values for clockwise rotation).
  46  * <p>
  47  * As all gestures, rotation can be direct (performed directly at
  48  * the concrete coordinates as on touch screen - the center point among all
  49  * the touches is usually used as the gesture coordinates) or indirect (performed
  50  * indirectly as on track pad - the mouse cursor location is usually used
  51  * as the gesture coordinates).
  52  * <p>
  53  * The gesture's {@code ROTATE} events are surounded by {@code ROTATION_STARTED}
  54  * and {@code ROTATION_FINISHED} events. If rotation inertia is active on the
  55  * given platform, some {@code ROTATE} events with {@code isInertia()} returning
  56  * {@code true} can come after {@code ROTATION_FINISHED}.
  57  *
  58  * @since JavaFX 2.2
  59  */
  60 public final class RotateEvent extends GestureEvent {
  61 
  62     private static final long serialVersionUID = 20121107L;
  63 
  64     /**
  65      * Common supertype for all rotate event types.
  66      */
  67     public static final EventType<RotateEvent> ANY =
  68             new EventType<RotateEvent>(GestureEvent.ANY, "ANY_ROTATE");
  69 
  70     /**
  71      * This event occurs when user performs a rotating gesture such as
  72      * dragging two fingers around each other.
  73      */
  74     public static final EventType<RotateEvent> ROTATE =
  75             new EventType<RotateEvent>(RotateEvent.ANY, "ROTATE");
  76 
  77     /**
  78      * This event occurs when a rotating gesture is detected.
  79      */
  80     public static final EventType<RotateEvent> ROTATION_STARTED =
  81             new EventType<RotateEvent>(RotateEvent.ANY, "ROTATION_STARTED");
  82 
  83     /**
  84      * This event occurs when a rotating gesture ends.
  85      */
  86     public static final EventType<RotateEvent> ROTATION_FINISHED =
  87             new EventType<RotateEvent>(RotateEvent.ANY, "ROTATION_FINISHED");
  88 
  89     /**
  90      * Constructs new RotateEvent event.
  91      * @param source the source of the event. Can be null.
  92      * @param target the target of the event. Can be null.
  93      * @param eventType The type of the event.
  94      * @param x The x with respect to the scene.
  95      * @param y The y with respect to the scene.
  96      * @param screenX The x coordinate relative to screen.
  97      * @param screenY The y coordinate relative to screen.
  98      * @param shiftDown true if shift modifier was pressed.
  99      * @param controlDown true if control modifier was pressed.
 100      * @param altDown true if alt modifier was pressed.
 101      * @param metaDown true if meta modifier was pressed.
 102      * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
 103      * @param inertia if represents inertia of an already finished gesture.
 104      * @param angle the rotational angle
 105      * @param totalAngle the cumulative rotational angle
 106      * @param pickResult pick result. Can be null, in this case a 2D pick result
 107      *                   without any further values is constructed
 108      *                   based on the scene coordinates and the target
 109      * @since JavaFX 8.0
 110      */
 111     public RotateEvent(@NamedArg("source") Object source, @NamedArg("target") EventTarget target,
 112             final @NamedArg("eventType") EventType<RotateEvent> eventType,
 113             @NamedArg("x") double x, @NamedArg("y") double y,
 114             @NamedArg("screenX") double screenX, @NamedArg("screenY") double screenY,
 115             @NamedArg("shiftDown") boolean shiftDown,
 116             @NamedArg("controlDown") boolean controlDown,
 117             @NamedArg("altDown") boolean altDown,
 118             @NamedArg("metaDown") boolean metaDown,
 119             @NamedArg("direct") boolean direct,
 120             @NamedArg("inertia") boolean inertia, @NamedArg("angle") double angle, @NamedArg("totalAngle") double totalAngle,
 121             @NamedArg("pickResult") PickResult pickResult) {
 122 
 123         super(source, target, eventType, x, y, screenX, screenY,
 124                 shiftDown, controlDown, altDown, metaDown, direct, inertia,
 125                 pickResult);
 126         this.angle = angle;
 127         this.totalAngle = totalAngle;
 128     }
 129 
 130     /**
 131      * Constructs new RotateEvent event with null source and target
 132      * @param eventType The type of the event.
 133      * @param x The x with respect to the scene.
 134      * @param y The y with respect to the scene.
 135      * @param screenX The x coordinate relative to screen.
 136      * @param screenY The y coordinate relative to screen.
 137      * @param shiftDown true if shift modifier was pressed.
 138      * @param controlDown true if control modifier was pressed.
 139      * @param altDown true if alt modifier was pressed.
 140      * @param metaDown true if meta modifier was pressed.
 141      * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
 142      * @param inertia if represents inertia of an already finished gesture.
 143      * @param angle the rotational angle
 144      * @param totalAngle the cumulative rotational angle
 145      * @param pickResult pick result. Can be null, in this case a 2D pick result
 146      *                   without any further values is constructed
 147      *                   based on the scene coordinates
 148      * @since JavaFX 8.0
 149      */
 150     public RotateEvent(final @NamedArg("eventType") EventType<RotateEvent> eventType,
 151             @NamedArg("x") double x, @NamedArg("y") double y,
 152             @NamedArg("screenX") double screenX, @NamedArg("screenY") double screenY,
 153             @NamedArg("shiftDown") boolean shiftDown,
 154             @NamedArg("controlDown") boolean controlDown,
 155             @NamedArg("altDown") boolean altDown,
 156             @NamedArg("metaDown") boolean metaDown,
 157             @NamedArg("direct") boolean direct,
 158             @NamedArg("inertia") boolean inertia, @NamedArg("angle") double angle, @NamedArg("totalAngle") double totalAngle,
 159             @NamedArg("pickResult") PickResult pickResult) {
 160         this(null, null, eventType, x, y, screenX, screenY, shiftDown, controlDown,
 161                 altDown, metaDown, direct, inertia, angle, totalAngle, pickResult);
 162     }
 163 
 164     private final double angle;
 165 
 166     /**
 167      * Gets the rotation angle of this event.
 168      * The angle is in degrees and work well when added to the node's
 169      * {@code rotate} property value (positive values for clockwise rotation).
 170      * @return The rotation angle of this event
 171      */
 172     public double getAngle() {
 173         return angle;
 174     }
 175 
 176     private final double totalAngle;
 177 
 178     /**
 179      * Gets the cumulative rotation angle of this gesture.
 180      * The angle is in degrees and work well when added to the node's
 181      * {@code rotate} property value (positive values for clockwise rotation).
 182      * @return The cumulative rotation angle of this gesture
 183      */
 184     public double getTotalAngle() {
 185         return totalAngle;
 186     }
 187 
 188     /**
 189      * Returns a string representation of this {@code RotateEvent} object.
 190      * @return a string representation of this {@code RotateEvent} object.
 191      */
 192     @Override public String toString() {
 193         final StringBuilder sb = new StringBuilder("RotateEvent [");
 194 
 195         sb.append("source = ").append(getSource());
 196         sb.append(", target = ").append(getTarget());
 197         sb.append(", eventType = ").append(getEventType());
 198         sb.append(", consumed = ").append(isConsumed());
 199 
 200         sb.append(", angle = ").append(getAngle());
 201         sb.append(", totalAngle = ").append(getTotalAngle());
 202         sb.append(", x = ").append(getX()).append(", y = ").append(getY())
 203                 .append(", z = ").append(getZ());
 204         sb.append(isDirect() ? ", direct" : ", indirect");
 205 
 206         if (isInertia()) {
 207             sb.append(", inertia");
 208         }
 209 
 210         if (isShiftDown()) {
 211             sb.append(", shiftDown");
 212         }
 213         if (isControlDown()) {
 214             sb.append(", controlDown");
 215         }
 216         if (isAltDown()) {
 217             sb.append(", altDown");
 218         }
 219         if (isMetaDown()) {
 220             sb.append(", metaDown");
 221         }
 222         if (isShortcutDown()) {
 223             sb.append(", shortcutDown");
 224         }
 225         sb.append(", pickResult = ").append(getPickResult());
 226 
 227         return sb.append("]").toString();
 228     }
 229 
 230     @Override
 231     public RotateEvent copyFor(Object newSource, EventTarget newTarget) {
 232         return (RotateEvent) super.copyFor(newSource, newTarget);
 233     }
 234 
 235     /**
 236      * Creates a copy of the given event with the given fields substituted.
 237      * @param newSource the new source of the copied event
 238      * @param newTarget the new target of the copied event
 239      * @param type the new eventType
 240      * @return the event copy with the fields substituted
 241      * @since JavaFX 8.0
 242      */
 243     public RotateEvent copyFor(Object newSource, EventTarget newTarget, EventType<RotateEvent> type) {
 244         RotateEvent e = copyFor(newSource, newTarget);
 245         e.eventType = type;
 246         return e;
 247     }
 248 
 249     @Override
 250     public EventType<RotateEvent> getEventType() {
 251         return (EventType<RotateEvent>) super.getEventType();
 252     }
 253 
 254 
 255 }