JavaCAD
VFX3DUtil.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 package eu.mihosoft.vrl.v3d;
26 
27 import javafx.event.EventHandler;
28 import javafx.event.EventType;
29 import javafx.scene.Node;
30 import javafx.scene.Scene;
31 import javafx.scene.paint.Color;
32 import javafx.scene.shape.TriangleMesh;
33 import javafx.scene.shape.MeshView;
34 import javafx.scene.paint.PhongMaterial;
35 import javafx.scene.shape.DrawMode;
36 import javafx.scene.shape.CullFace;
37 import javafx.scene.input.MouseButton;
38 import javafx.scene.input.MouseEvent;
39 import javafx.scene.transform.Rotate;
40 
41 // TODO: Auto-generated Javadoc
47 public class VFX3DUtil {
48 
52  private VFX3DUtil() {
53  throw new AssertionError("don't instanciate me!");
54  }
55 
56 
57 
65  public static void addMouseBehavior(
66  Node n, Scene eventReceiver, MouseButton btn) {
67  eventReceiver.addEventHandler(MouseEvent.ANY,
68  new MouseBehaviorImpl1(n, btn));
69  }
70 
78  public static void addMouseBehavior(
79  Node n, Node eventReceiver, MouseButton btn) {
80  eventReceiver.addEventHandler(MouseEvent.ANY,
81  new MouseBehaviorImpl1(n, btn));
82  }
83 }
84 
85 // rotation behavior implementation
86 class MouseBehaviorImpl1 implements EventHandler<MouseEvent> {
87 
88  private double anchorAngleX;
89  private double anchorAngleY;
90  private double anchorX;
91  private double anchorY;
92  private final Rotate rotateX = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
93  private final Rotate rotateZ = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
94  private MouseButton btn;
95 
96  public MouseBehaviorImpl1(Node n, MouseButton btn) {
97  n.getTransforms().addAll(rotateX, rotateZ);
98  this.btn = btn;
99 
100  if (btn == null) {
101  this.btn = MouseButton.MIDDLE;
102  }
103  }
104 
105  @Override
106  public void handle(MouseEvent t) {
107  if (!btn.equals(t.getButton())) {
108  return;
109  }
110 
111  t.consume();
112 
113  if (MouseEvent.MOUSE_PRESSED.equals(t.getEventType())) {
114  anchorX = t.getSceneX();
115  anchorY = t.getSceneY();
116  anchorAngleX = rotateX.getAngle();
117  anchorAngleY = rotateZ.getAngle();
118  t.consume();
119  } else if (MouseEvent.MOUSE_DRAGGED.equals(t.getEventType())) {
120  rotateZ.setAngle(anchorAngleY + (anchorX - t.getSceneX()) * 0.7);
121  rotateX.setAngle(anchorAngleX - (anchorY - t.getSceneY()) * 0.7);
122 
123  }
124 
125  }
126 }
static void addMouseBehavior(Node n, Scene eventReceiver, MouseButton btn)
Definition: VFX3DUtil.java:65
static void addMouseBehavior(Node n, Node eventReceiver, MouseButton btn)
Definition: VFX3DUtil.java:78