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  * Zoom event indicates that user performed zooming gesture such as
  34  * dragging two fingers apart on track pad, touch screen or other
  35  * 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 zoomFactor} is the zooming amount
  43  * of this event, {@code totalZoomFactor} is the zooming amount of the whole
  44  * gesture. The values work well when multiplied with the node's {@code scale}
  45  * properties (values greater than {@code 1} for zooming in).
  46  * <p>
  47  * As all gestures, zooming 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 ZOOM} events are surounded by {@code ZOOM_STARTED}
  54  * and {@code ZOOM_FINISHED} events. If zooming inertia is active on the
  55  * given platform, some {@code ZOOM} events with {@code isInertia()} returning
  56  * {@code true} can come after {@code ZOOM_FINISHED}.
  57  *
  58  * @since JavaFX 2.2
  59  */
  60 public final class ZoomEvent extends GestureEvent {
  61 
  62     private static final long serialVersionUID = 20121107L;
  63 
  64     /**
  65      * Common supertype for all zoom event types.
  66      */
  67     public static final EventType<ZoomEvent> ANY =
  68             new EventType<ZoomEvent>(GestureEvent.ANY, "ANY_ZOOM");
  69 
  70     /**
  71      * This event occurs when user performs a zooming gesture such as
  72      * dragging two fingers apart.
  73      */
  74     public static final EventType<ZoomEvent> ZOOM =
  75             new EventType<ZoomEvent>(ZoomEvent.ANY, "ZOOM");
  76 
  77     /**
  78      * This event occurs when a zooming gesture is detected.
  79      */
  80     public static final EventType<ZoomEvent> ZOOM_STARTED =
  81             new EventType<ZoomEvent>(ZoomEvent.ANY, "ZOOM_STARTED");
  82 
  83     /**
  84      * This event occurs when a zooming gesture ends.
  85      */
  86     public static final EventType<ZoomEvent> ZOOM_FINISHED =
  87             new EventType<ZoomEvent>(ZoomEvent.ANY, "ZOOM_FINISHED");
  88 
  89     /**
  90      * Constructs new ZoomEvent 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 zoomFactor zoom amount
 105      * @param totalZoomFactor cumulative zoom amount
 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 ZoomEvent(@NamedArg("source") Object source, @NamedArg("target") EventTarget target, final @NamedArg("eventType") EventType<ZoomEvent> eventType,
 112             @NamedArg("x") double x, @NamedArg("y") double y,
 113             @NamedArg("screenX") double screenX, @NamedArg("screenY") double screenY,
 114             @NamedArg("shiftDown") boolean shiftDown,
 115             @NamedArg("controlDown") boolean controlDown,
 116             @NamedArg("altDown") boolean altDown,
 117             @NamedArg("metaDown") boolean metaDown,
 118             @NamedArg("direct") boolean direct,
 119             @NamedArg("inertia") boolean inertia,
 120             @NamedArg("zoomFactor") double zoomFactor,
 121             @NamedArg("totalZoomFactor") double totalZoomFactor,
 122             @NamedArg("pickResult") PickResult pickResult) {
 123 
 124         super(source, target, eventType, x, y, screenX, screenY,
 125                 shiftDown, controlDown, altDown, metaDown, direct, inertia, pickResult);
 126         this.zoomFactor = zoomFactor;
 127         this.totalZoomFactor = totalZoomFactor;
 128     }
 129 
 130     /**
 131      * Constructs new ZoomEvent 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 zoomFactor zoom amount
 144      * @param totalZoomFactor cumulative zoom amount
 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 ZoomEvent(final @NamedArg("eventType") EventType<ZoomEvent> 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,
 159             @NamedArg("zoomFactor") double zoomFactor,
 160             @NamedArg("totalZoomFactor") double totalZoomFactor,
 161             @NamedArg("pickResult") PickResult pickResult) {
 162         this(null, null, eventType, x, y, screenX, screenY, shiftDown, controlDown,
 163                 altDown, metaDown, direct, inertia, zoomFactor, totalZoomFactor,
 164                 pickResult);
 165     }
 166 
 167     private final double zoomFactor;
 168 
 169     /**
 170      * Gets the zooming amount of this event. The factor value works well when
 171      * multiplied with the node's {@code scale} properties (values greater
 172      * than {@code 1} for zooming in, values between {@code 0} and {@code 1}
 173      * for zooming out).
 174      * @return The zooming amount of this event
 175      */
 176     public double getZoomFactor() {
 177         return zoomFactor;
 178     }
 179 
 180     private final double totalZoomFactor;
 181 
 182     /**
 183      * Gets the zooming amount of this gesture. The factor value works well when
 184      * multiplied with the node's {@code scale} properties (values greater
 185      * than {@code 1} for zooming in, values between {@code 0} and {@code 1}
 186      * for zooming out).
 187      * @return The cumulative zooming amount of this gesture
 188      */
 189     public double getTotalZoomFactor() {
 190         return totalZoomFactor;
 191     }
 192 
 193     /**
 194      * Returns a string representation of this {@code ZoomEvent} object.
 195      * @return a string representation of this {@code ZoomEvent} object.
 196      */
 197     @Override public String toString() {
 198         final StringBuilder sb = new StringBuilder("ZoomEvent [");
 199 
 200         sb.append("source = ").append(getSource());
 201         sb.append(", target = ").append(getTarget());
 202         sb.append(", eventType = ").append(getEventType());
 203         sb.append(", consumed = ").append(isConsumed());
 204 
 205         sb.append(", zoomFactor = ").append(getZoomFactor());
 206         sb.append(", totalZoomFactor = ").append(getTotalZoomFactor());
 207         sb.append(", x = ").append(getX()).append(", y = ").append(getY())
 208                 .append(", z = ").append(getZ());
 209         sb.append(isDirect() ? ", direct" : ", indirect");
 210 
 211         if (isInertia()) {
 212             sb.append(", inertia");
 213         }
 214 
 215         if (isShiftDown()) {
 216             sb.append(", shiftDown");
 217         }
 218         if (isControlDown()) {
 219             sb.append(", controlDown");
 220         }
 221         if (isAltDown()) {
 222             sb.append(", altDown");
 223         }
 224         if (isMetaDown()) {
 225             sb.append(", metaDown");
 226         }
 227         if (isShortcutDown()) {
 228             sb.append(", shortcutDown");
 229         }
 230         sb.append(", pickResult = ").append(getPickResult());
 231 
 232         return sb.append("]").toString();
 233     }
 234 
 235     @Override
 236     public ZoomEvent copyFor(Object newSource, EventTarget newTarget) {
 237         return (ZoomEvent) super.copyFor(newSource, newTarget);
 238     }
 239 
 240     /**
 241      * Creates a copy of the given event with the given fields substituted.
 242      * @param newSource the new source of the copied event
 243      * @param newTarget the new target of the copied event
 244      * @param type the new eventType
 245      * @return the event copy with the fields substituted
 246      * @since JavaFX 8.0
 247      */
 248     public ZoomEvent copyFor(Object newSource, EventTarget newTarget, EventType<ZoomEvent> type) {
 249         ZoomEvent e = copyFor(newSource, newTarget);
 250         e.eventType = type;
 251         return e;
 252     }
 253 
 254     @Override
 255     public EventType<ZoomEvent> getEventType() {
 256         return (EventType<ZoomEvent>) super.getEventType();
 257     }
 258 
 259 
 260 }