Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 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 com.sun.javafx.scene.input.InputEventUtils;
029import com.sun.javafx.tk.Toolkit;
030import java.io.IOException;
031import javafx.event.Event;
032import javafx.event.EventTarget;
033import javafx.event.EventType;
034import javafx.geometry.Point3D;
035
036/**
037 * An event indicating gesture input. Gestures are typically caused by
038 * direct (touch screen) or indirect (track pad) touch events.
039 *
040 * <p>
041 * Delivery of gestures is dependent on the capabilities of the underlying
042 * platform and connected input devices. For instance on a PC with mouse
043 * and keyboard there is no way of producing a rotating gesture.
044 * </p>
045 * @since 2.2
046 */
047public class GestureEvent extends InputEvent {
048
049    private static final long serialVersionUID = 20121107L;
050    
051    /**
052     * Common supertype for all gestures.
053     */
054    public static final EventType<GestureEvent> ANY =
055            new EventType<GestureEvent>(InputEvent.ANY, "GESTURE");
056
057    /**
058     * Creates a new instance of {@code GestureEvent}.
059     * @param eventType Type of the event
060     * @deprecated Do not use this constructor. Constructs empty event.
061     */
062    @Deprecated
063    protected GestureEvent(final EventType<? extends GestureEvent> eventType) {
064        this(eventType, 0, 0, 0, 0, false, false, false, false, false, false, null);
065    }
066
067    /**
068     * Creates a new instance of {@code GestureEvent}.
069     * @param source Event source
070     * @param target Event target
071     * @param eventType Type of the event
072     * @deprecated Do not use this constructor. Constructs empty event.
073     */
074    @Deprecated
075    protected GestureEvent(Object source, EventTarget target,
076            final EventType<? extends GestureEvent> eventType) {
077        super(source, target, eventType);
078        x = y = screenX = screenY = sceneX = sceneY = 0;
079        shiftDown = controlDown = altDown = metaDown = direct = inertia = false;
080    }
081
082    /**
083     * Constructs new GestureEvent event.
084     * @param source the source of the event. Can be null.
085     * @param target the target of the event. Can be null.
086     * @param eventType The type of the event.
087     * @param x The x with respect to the scene.
088     * @param y The y with respect to the scene.
089     * @param screenX The x coordinate relative to screen.
090     * @param screenY The y coordinate relative to screen.
091     * @param shiftDown true if shift modifier was pressed.
092     * @param controlDown true if control modifier was pressed.
093     * @param altDown true if alt modifier was pressed.
094     * @param metaDown true if meta modifier was pressed.
095     * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
096     * @param inertia if represents inertia of an already finished gesture.
097     * @param pickResult pick result. Can be null, in this case a 2D pick result
098     *                   without any further values is constructed
099     *                   based on the scene coordinates and the target
100     */
101    protected GestureEvent(Object source, EventTarget target, final EventType<? extends GestureEvent> eventType,
102            double x, double y, double screenX, double screenY,
103            boolean shiftDown, boolean controlDown, boolean altDown,
104            boolean metaDown, boolean direct, boolean inertia, PickResult pickResult) {
105        super(source, target, eventType);
106        this.x = x;
107        this.y = y;
108        this.screenX = screenX;
109        this.screenY = screenY;
110        this.sceneX = x;
111        this.sceneY = y;
112        this.shiftDown = shiftDown;
113        this.controlDown = controlDown;
114        this.altDown = altDown;
115        this.metaDown = metaDown;
116        this.direct = direct;
117        this.inertia = inertia;
118        this.pickResult = pickResult != null ? pickResult : new PickResult(target, x, y);
119        final Point3D p = InputEventUtils.recomputeCoordinates(this.pickResult, null);
120        this.x = p.getX();
121        this.y = p.getY();
122        this.z = p.getZ();
123    }
124
125    /**
126     * Constructs new GestureEvent event with empty source and target
127     * @param eventType The type of the event.
128     * @param x The x with respect to the scene.
129     * @param y The y with respect to the scene.
130     * @param screenX The x coordinate relative to screen.
131     * @param screenY The y coordinate relative to screen.
132     * @param shiftDown true if shift modifier was pressed.
133     * @param controlDown true if control modifier was pressed.
134     * @param altDown true if alt modifier was pressed.
135     * @param metaDown true if meta modifier was pressed.
136     * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
137     * @param inertia if represents inertia of an already finished gesture.
138     * @param pickResult pick result. Can be null, in this case a 2D pick result
139     *                   without any further values is constructed
140     *                   based on the scene coordinates
141     */
142    protected GestureEvent(final EventType<? extends GestureEvent> eventType,
143            double x, double y, double screenX, double screenY,
144            boolean shiftDown, boolean controlDown, boolean altDown,
145            boolean metaDown, boolean direct, boolean inertia,
146            PickResult pickResult) {
147        this(null, null, eventType, x, y, screenX, screenY, shiftDown, controlDown,
148                altDown, metaDown, direct, inertia, pickResult);
149    }
150
151    /**
152     * Fills the given event by this event's coordinates recomputed to the given
153     * source object.
154     * @param newEvent Event whose coordinates are to be filled
155     * @param newSource Source object to compute coordinates for
156     */
157    private void recomputeCoordinatesToSource(GestureEvent newEvent, Object newSource) {
158
159        final Point3D newCoordinates = InputEventUtils.recomputeCoordinates(
160                pickResult, newSource);
161
162        newEvent.x = newCoordinates.getX();
163        newEvent.y = newCoordinates.getY();
164        newEvent.z = newCoordinates.getZ();
165    }
166
167    /**
168     * @inheritDoc
169     */
170    @Override
171    public GestureEvent copyFor(Object newSource, EventTarget newTarget) {
172        GestureEvent e = (GestureEvent) super.copyFor(newSource, newTarget);
173        recomputeCoordinatesToSource(e, newSource);
174        return e;
175    }
176
177    private transient double x;
178
179    /**
180     * Gets the horizontal position of the event relative to the
181     * origin of the event's source.
182     *
183     * @return the horizontal position of the event relative to the
184     * origin of the event's source.
185     *
186     * @see #isDirect() 
187     */
188    public final double getX() {
189        return x;
190    }
191
192    private transient double y;
193
194    /**
195     * Gets the vertical position of the event relative to the
196     * origin of the event's source.
197     *
198     * @return the vertical position of the event relative to the
199     * origin of the event's source.
200     *
201     * @see #isDirect()
202     */
203    public final double getY() {
204        return y;
205    }
206
207    /**
208     * Depth z position of the event relative to the
209     * origin of the MouseEvent's node.
210     */
211    private transient double z;
212
213    /**
214     * Depth position of the event relative to the
215     * origin of the MouseEvent's source.
216     *
217     * @return depth position of the event relative to the
218     * origin of the MouseEvent's source.
219     */
220    public final double getZ() {
221        return z;
222    }
223
224    private final double screenX;
225
226    /**
227     * Gets the absolute horizontal position of the event.
228     * @return the absolute horizontal position of the event
229     *
230     * @see #isDirect()
231     */
232    public final double getScreenX() {
233        return screenX;
234    }
235
236    private final double screenY;
237
238    /**
239     * Gets the absolute vertical position of the event.
240     * @return the absolute vertical position of the event
241     *
242     * @see #isDirect()
243     */
244    public final double getScreenY() {
245        return screenY;
246    }
247
248    private final double sceneX;
249
250    /**
251     * Gets the horizontal position of the event relative to the
252     * origin of the {@code Scene} that contains the event's source.
253     * If the node is not in a {@code Scene}, then the value is relative to
254     * the boundsInParent of the root-most parent of the event's node.
255     * Note that in 3D scene, this represents the flat coordinates after
256     * applying the projection transformations.
257     *
258     * @return the horizontal position of the event relative to the
259     * origin of the {@code Scene} that contains the event's source
260     *
261     * @see #isDirect()
262     */
263    public final double getSceneX() {
264        return sceneX;
265    }
266
267    private final double sceneY;
268
269    /**
270     * Gets the vertical position of the event relative to the
271     * origin of the {@code Scene} that contains the event's source.
272     * If the node is not in a {@code Scene}, then the value is relative to
273     * the boundsInParent of the root-most parent of the event's node.
274     * Note that in 3D scene, this represents the flat coordinates after
275     * applying the projection transformations.
276     *
277     * @return the vertical position of the event relative to the
278     * origin of the {@code Scene} that contains the event's source
279     *
280     * @see #isDirect()
281     */
282    public final double getSceneY() {
283        return sceneY;
284    }
285
286    private final boolean shiftDown;
287
288    /**
289     * Indicates whether or not the Shift modifier is down on this event.
290     * @return true if the Shift modifier is down on this event
291     */
292    public final boolean isShiftDown() {
293        return shiftDown;
294    }
295
296    private final boolean controlDown;
297
298    /**
299     * Indicates whether or not the Control modifier is down on this event.
300     * @return true if the Control modifier is down on this event
301     */
302    public final boolean isControlDown() {
303        return controlDown;
304    }
305
306    private final boolean altDown;
307
308    /**
309     * Indicates whether or not the Alt modifier is down on this event.
310     * @return true if the Alt modifier is down on this event
311     */
312    public final boolean isAltDown() {
313        return altDown;
314    }
315
316    private final boolean metaDown;
317
318    /**
319     * Indicates whether or not the Meta modifier is down on this event.
320     * @return true if the Meta modifier is down on this event
321     */
322    public final boolean isMetaDown() {
323        return metaDown;
324    }
325
326    private final boolean direct;
327
328    /**
329     * Indicates whether this gesture is caused by a direct or indirect input
330     * device. With direct input device the gestures are performed directly at
331     * the concrete coordinates, a typical example would be a touch screen.
332     * With indirect device the gestures are performed indirectly and usually
333     * mouse cursor position is used as the gesture coordinates, a typical
334     * example would be a track pad.
335     * @return true if this event is caused by a direct input device
336     */
337    public final boolean isDirect() {
338        return direct;
339    }
340
341    private final boolean inertia;
342
343    /**
344     * Indicates if this event represents an inertia of an already finished
345     * gesture.
346     * @return true if this is an inertia event
347     */
348    public boolean isInertia() {
349        return inertia;
350    }
351
352    /**
353     * Information about the pick if the picked {@code Node} is a
354     * {@code Shape3D} node and its pickOnBounds is false.
355     */
356    private PickResult pickResult;
357
358    /**
359     * Returns information about the pick.
360     *
361     * @return new PickResult object that contains information about the pick
362     */
363    public final PickResult getPickResult() {
364        return pickResult;
365    }
366
367    /**
368     * Indicates whether or not the host platform common shortcut modifier is
369     * down on this event. This common shortcut modifier is a modifier key which
370     * is used commonly in shortcuts on the host platform. It is for example
371     * {@code control} on Windows and {@code meta} (command key) on Mac.
372     *
373     * @return {@code true} if the shortcut modifier is down, {@code false}
374     *      otherwise
375     */
376    public final boolean isShortcutDown() {
377        switch (Toolkit.getToolkit().getPlatformShortcutKey()) {
378            case SHIFT:
379                return shiftDown;
380
381            case CONTROL:
382                return controlDown;
383
384            case ALT:
385                return altDown;
386
387            case META:
388                return metaDown;
389
390            default:
391                return false;
392        }
393    }
394
395    /**
396     * Returns a string representation of this {@code GestureEvent} object.
397     * @return a string representation of this {@code GestureEvent} object.
398     */
399    @Override public String toString() {
400        final StringBuilder sb = new StringBuilder("GestureEvent [");
401
402        sb.append("source = ").append(getSource());
403        sb.append(", target = ").append(getTarget());
404        sb.append(", eventType = ").append(getEventType());
405        sb.append(", consumed = ").append(isConsumed());
406
407        sb.append(", x = ").append(getX()).append(", y = ").append(getY())
408                .append(", z = ").append(getZ());
409        sb.append(isDirect() ? ", direct" : ", indirect");
410
411        if (isInertia()) {
412            sb.append(", inertia");
413        }
414
415        if (isShiftDown()) {
416            sb.append(", shiftDown");
417        }
418        if (isControlDown()) {
419            sb.append(", controlDown");
420        }
421        if (isAltDown()) {
422            sb.append(", altDown");
423        }
424        if (isMetaDown()) {
425            sb.append(", metaDown");
426        }
427        if (isShortcutDown()) {
428            sb.append(", shortcutDown");
429        }
430        sb.append(", pickResult = ").append(getPickResult());
431
432        return sb.append("]").toString();
433    }
434
435    private void readObject(java.io.ObjectInputStream in)
436            throws IOException, ClassNotFoundException {
437        in.defaultReadObject();
438        x = sceneX;
439        y = sceneY;
440    }
441
442    @Override
443    public EventType<? extends GestureEvent> getEventType() {
444        return (EventType<? extends GestureEvent>) super.getEventType();
445    }
446}