JavaCAD
DragSupport.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates.
3  * All rights reserved. Use is subject to license terms.
4  *
5  * This file is available and licensed under the following license:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the distribution.
16  * - Neither the name of Oracle Corporation nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package eu.mihosoft.vrl.v3d.ext.openjfx.utils3d;
33 
34 
35 import javafx.beans.property.Property;
36 import javafx.event.EventHandler;
37 import javafx.geometry.Orientation;
38 import javafx.scene.Scene;
39 import javafx.scene.input.KeyCode;
40 import javafx.scene.input.KeyEvent;
41 import javafx.scene.input.MouseButton;
42 import javafx.scene.input.MouseEvent;
43 
44 
45 // TODO: Auto-generated Javadoc
50 public class DragSupport {
51 
53  public EventHandler<KeyEvent> keyboardEventHandler;
54 
56  public EventHandler<MouseEvent> mouseEventHandler;
57 
59  private Number anchor;
60 
62  private double dragAnchor;
63 
65  private MouseEvent lastMouseEvent;
66 
68  private Scene target;
69 
81  public DragSupport(Scene target, final KeyCode modifier, final Orientation orientation, final Property<Number> property) {
82  this(target, modifier, MouseButton.PRIMARY, orientation, property, 1);
83  }
84 
94  public DragSupport(Scene target, final KeyCode modifier, MouseButton mouseButton, final Orientation orientation, final Property<Number> property) {
95  this(target, modifier, mouseButton, orientation, property, 1);
96  }
97 
101  public void detach() {
102  target.removeEventHandler(MouseEvent.ANY, mouseEventHandler);
103  target.removeEventHandler(KeyEvent.ANY, keyboardEventHandler);
104  }
105 
117  public DragSupport(Scene target, final KeyCode modifier, final Orientation orientation, final Property<Number> property, final double factor) {
118  this(target, modifier, MouseButton.PRIMARY, orientation, property, factor);
119  }
120 
131  public DragSupport(Scene target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property<Number> property, final double factor) {
132  this.target = target;
133  mouseEventHandler = t -> {
134  if (t.getEventType() != MouseEvent.MOUSE_ENTERED_TARGET
135  && t.getEventType() != MouseEvent.MOUSE_EXITED_TARGET) {
136  lastMouseEvent = t;
137  }
138  if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
139  if (t.getButton() == mouseButton
140  && isModifierCorrect(t, modifier)) {
141  anchor = property.getValue();
142  dragAnchor = getCoord(t, orientation);
143  t.consume();
144  }
145  } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
146  if (t.getButton() == mouseButton
147  && isModifierCorrect(t, modifier)) {
148  property.setValue(anchor.doubleValue()
149  + (getCoord(t, orientation) - dragAnchor) * factor);
150  t.consume();
151  }
152  }
153  };
154  keyboardEventHandler = t -> {
155  if (t.getEventType() == KeyEvent.KEY_PRESSED) {
156  if (t.getCode() == modifier) {
157  anchor = property.getValue();
158  if (lastMouseEvent != null) {
159  dragAnchor = getCoord(lastMouseEvent, orientation);
160  }
161  t.consume();
162  }
163  } else if (t.getEventType() == KeyEvent.KEY_RELEASED) {
164  if (t.getCode() != modifier && isModifierCorrect(t, modifier)) {
165  anchor = property.getValue();
166  if (lastMouseEvent != null) {
167  dragAnchor = getCoord(lastMouseEvent, orientation);
168  }
169  t.consume();
170  }
171  }
172  };
173  target.addEventHandler(MouseEvent.ANY, mouseEventHandler);
174  target.addEventHandler(KeyEvent.ANY, keyboardEventHandler);
175  }
176 
184  private boolean isModifierCorrect(KeyEvent t, KeyCode keyCode) {
185  return (keyCode != KeyCode.ALT ^ t.isAltDown())
186  && (keyCode != KeyCode.CONTROL ^ t.isControlDown())
187  && (keyCode != KeyCode.SHIFT ^ t.isShiftDown())
188  && (keyCode != KeyCode.META ^ t.isMetaDown());
189  }
190 
198  private boolean isModifierCorrect(MouseEvent t, KeyCode keyCode) {
199  return (keyCode != KeyCode.ALT ^ t.isAltDown())
200  && (keyCode != KeyCode.CONTROL ^ t.isControlDown())
201  && (keyCode != KeyCode.SHIFT ^ t.isShiftDown())
202  && (keyCode != KeyCode.META ^ t.isMetaDown());
203  }
204 
212  private double getCoord(MouseEvent t, Orientation orientation) {
213  switch (orientation) {
214  case HORIZONTAL:
215  return t.getScreenX();
216  case VERTICAL:
217  return t.getScreenY();
218  default:
219  throw new IllegalArgumentException("This orientation is not supported: " + orientation);
220  }
221  }
222 
223 }
DragSupport(Scene target, final KeyCode modifier, MouseButton mouseButton, final Orientation orientation, final Property< Number > property)
double getCoord(MouseEvent t, Orientation orientation)
DragSupport(Scene target, final KeyCode modifier, final Orientation orientation, final Property< Number > property)
DragSupport(Scene target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property< Number > property, final double factor)
boolean isModifierCorrect(KeyEvent t, KeyCode keyCode)
DragSupport(Scene target, final KeyCode modifier, final Orientation orientation, final Property< Number > property, final double factor)
boolean isModifierCorrect(MouseEvent t, KeyCode keyCode)