1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.swt;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseWheelEvent;
17 import java.math.BigDecimal;
19 import javax.vecmath.AxisAngle4d;
20 import javax.vecmath.Point3d;
21 import javax.vecmath.Quat4d;
22 import javax.vecmath.Vector3d;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Cursor;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Label;
32 import org.simantics.g3d.math.MathTools;
33 import org.simantics.g3d.math.Ray;
34 import org.simantics.g3d.scenegraph.IG3DNode;
35 import org.simantics.g3d.scenegraph.base.INode;
36 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
37 import org.simantics.g3d.toolbar.ToolComposite;
38 import org.simantics.g3d.vtk.Activator;
39 import org.simantics.g3d.vtk.common.VTKNodeMap;
40 import org.simantics.g3d.vtk.gizmo.TranslateAxisGizmo;
41 import org.simantics.g3d.vtk.utils.vtkUtil;
42 import org.simantics.utils.threads.ThreadUtils;
46 public class TranslateAction extends vtkSwtAction{
48 public static final int X = 0;
49 public static final int Y = 1;
50 public static final int Z = 2;
51 public static final int XY = 3;
52 public static final int XZ = 4;
53 public static final int YZ = 5;
54 public static final int P = 6;
56 private VTKNodeMap<?, ? extends INode> nodeMap;
57 //private TranslateGizmo gizmo = new TranslateGizmo();
58 private TranslateAxisGizmo gizmo = new TranslateAxisGizmo();
59 protected IG3DNode node;
63 private Cursor activeCursor;// = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
64 private Cursor dragCursor;// = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
66 protected ToolComposite toolComposite;
67 protected Combo axisCombo;
69 public void setNode(IG3DNode node) {
71 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
78 public IG3DNode getNode() {
82 public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
84 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_out.png"));
86 this.nodeMap = nodeMap;
88 activeCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND);
89 dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL);
90 this.toolComposite = toolComposite;
93 protected void createTools(ToolComposite toolComposite) {
94 Label label = new Label(toolComposite, SWT.READ_ONLY);
95 label.setText("Translate direction:");
96 axisCombo = new Combo(toolComposite, SWT.READ_ONLY);
103 axisCombo.add("Camera");
104 axisCombo.addSelectionListener(new SelectionAdapter() {
106 public void widgetSelected(SelectionEvent e) {
107 Combo c = (Combo)e.getSource();
108 index = c.getSelectionIndex();
110 panel.getComponent().setFocus();
113 axisCombo.select(index);
114 Button close = new Button(toolComposite, SWT.PUSH);
115 close.setText("Close");
116 close.addSelectionListener(new SelectionAdapter() {
117 public void widgetSelected(SelectionEvent e) {
118 panel.useDefaultAction();
121 toolComposite.relayout();
124 public void attach() {
127 if (toolComposite != null) {
128 createTools(toolComposite);
132 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
140 public void deattach() {
141 if (toolComposite != null) {
142 toolComposite.clear();
147 nodeMap.commit("Translate");
153 private void attachUI() {
154 panel.getComponent().setCursor(activeCursor);
158 private void deattachUI() {
159 panel.getComponent().setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW));
164 public boolean keyPressed(KeyEvent e) {
165 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
166 panel.useDefaultAction();
169 if (e.getKeyCode() == KeyEvent.VK_X) {
175 if (e.getKeyCode() == KeyEvent.VK_Y) {
181 if (e.getKeyCode() == KeyEvent.VK_Z) {
187 if (e.getKeyCode() == KeyEvent.VK_G) {
188 worldCoord = !worldCoord;
197 private void updateLock() {
198 gizmo.setType(index);
199 if (axisCombo != null)
200 axisCombo.select(index);
206 public boolean mouseClicked(MouseEvent e) {
207 if (e.getClickCount() > 1) {
211 panel.useDefaultAction();
213 //if(!gizmo.isPartOf(actor))
214 // panel.useDefaultAction();
220 private boolean isOverNode(MouseEvent e) {
221 vtkProp picked[] = panel.pick(e.getX(), e.getY());
223 for (int i = 0; i < picked.length; i++) {
224 if (node.equals(nodeMap.getNode(picked[i])))
233 protected boolean valid = false;
234 private boolean worldCoord = true;
235 private AxisAngle4d aa = null;
236 private Quat4d q = null;
239 public void setWorldCoord(boolean b) {
248 protected void update() {
252 gizmo.setRotation(new AxisAngle4d());
256 aa = new AxisAngle4d();
257 aa.set(((IG3DNode)node.getParent()).getWorldOrientation());
258 gizmo.setRotation(aa);
260 MathTools.getQuat(aa, q);
263 Vector3d nodePos = node.getWorldPosition();
264 //System.out.println(nodePos);
265 gizmo.setPosition(nodePos);
268 Point3d camPos = new Point3d(panel.getRenderer().GetActiveCamera().GetPosition());
269 Vector3d p = new Vector3d(nodePos);
273 Quat4d qi = new Quat4d(q);
275 MathTools.rotate(q, p, p);
277 if (panel.getRenderer().GetActiveCamera().GetParallelProjection() == 0) {
278 double distance = p.length();
280 double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
281 float s = (float) (Math.sin(fov) * distance * 0.1);
283 Vector3d scale = new Vector3d(1., 1., 1.);
292 gizmo.setScale(scale);
295 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
296 double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
304 gizmo.setScale(scale);
311 protected Vector3d prevTranslate = null;
314 public boolean mousePressed(MouseEvent e) {
315 if (e.getButton() == MouseEvent.BUTTON1) {
318 prevTranslate = getTranslate(e.getX(), e.getY());
320 panel.getComponent().setCursor(dragCursor);
323 getDefaultAction().mousePressed(e);
324 panel.getComponent().setCursor(activeCursor);
327 getDefaultAction().mousePressed(e);
330 //index = gizmo.getTranslateAxis(actor);
333 // panel.getDefaultAction().mousePressed(e);
337 //prevTranslate = getTranslate(e.getX(), e.getY());
338 //System.out.println("start translate " + prevTranslate);
344 public boolean mouseReleased(MouseEvent e) {
345 if (e.getButton() == MouseEvent.BUTTON1) {
347 prevTranslate = null;
348 panel.getComponent().setCursor(activeCursor);
350 getDefaultAction().mouseReleased(e);
356 public boolean mouseDragged(MouseEvent e) {
357 //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
358 if (e.getButton() == MouseEvent.BUTTON1 && valid) {
360 Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
361 //System.out.println("translate " + translate);
362 if (translate == null)
364 boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
366 Vector3d pos = new Vector3d(node.getWorldPosition());
368 pos = constaints(pos, step);
371 Vector3d pos = new Vector3d(node.getPosition());
373 pos = constaints(pos, step);
376 //mapping.rangeModified(node);
378 //nodeMap.modified(node);
381 getDefaultAction().mouseDragged(e);
388 public boolean mouseWheelMoved(MouseWheelEvent e) {
389 return getDefaultAction().mouseWheelMoved(e);
392 protected void setPos(Vector3d pos) {
393 node.setPosition(pos);
396 protected void setWorldPos(Vector3d pos) {
397 node.setWorldPosition(pos);
400 private double istep = 10.0;
401 private int decimals = 2;
403 protected Vector3d constaints(Vector3d p, boolean step) {
408 p.x = Math.round(istep * p.x) / istep;
409 BigDecimal bx = new BigDecimal(p.x);
410 bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
411 p.x = bx.doubleValue();
414 p.y = Math.round(istep * p.y) / istep;
415 BigDecimal by = new BigDecimal(p.y);
416 by.setScale(decimals, BigDecimal.ROUND_HALF_UP);
417 p.y = by.doubleValue();
421 p.z = Math.round(istep * p.z) / istep;
422 BigDecimal bz = new BigDecimal(p.z);
423 bz.setScale(decimals, BigDecimal.ROUND_HALF_UP);
424 p.z = bz.doubleValue();
431 public boolean mouseMoved(MouseEvent e) {
432 getDefaultAction().mouseMoved(e);
436 protected Vector3d getTranslate(double x, double y) {
437 return getTranslate(x, y, new Vector3d());
440 protected Vector3d getTranslate(double x, double y, Vector3d offset) {
441 Vector3d translate = new Vector3d();
443 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
445 Vector3d p = node.getWorldPosition();
450 Vector3d normal = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
452 MathTools.rotate(q, normal, normal);
455 double s[] = new double[1];
456 Vector3d r = new Vector3d();
457 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
466 dir = new Vector3d(1.0,0.0,0.0);
468 MathTools.rotate(q, dir, dir);
469 Vector3d i1 = new Vector3d();
470 Vector3d i2 = new Vector3d();
472 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
477 dir = new Vector3d(0.0,1.0,0.0);
479 MathTools.rotate(q, dir, dir);
483 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
487 dir = new Vector3d(0.0,0.0,1.0);
489 MathTools.rotate(q, dir, dir);
493 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
497 normal = new Vector3d(0.0,0.0,1.0);
499 MathTools.rotate(q, normal, normal);
501 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
508 normal = new Vector3d(0.0,1.0,0.0);
510 MathTools.rotate(q, normal, normal);
512 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
519 normal = new Vector3d(1.0,0.0,0.0);
521 MathTools.rotate(q, normal, normal);
523 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
533 translate.sub(offset);