]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
aad92f987a20271e8790e59eb4ae6403b401a8ea
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / ParallelPerspectiveHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.vtk.handlers;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.eclipse.core.commands.AbstractHandler;\r
20 import org.eclipse.core.commands.Command;\r
21 import org.eclipse.core.commands.ExecutionEvent;\r
22 import org.eclipse.core.commands.ExecutionException;\r
23 import org.eclipse.ui.IWorkbenchPart;\r
24 import org.eclipse.ui.handlers.HandlerUtil;\r
25 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;\r
26 import org.simantics.utils.threads.AWTThread;\r
27 import org.simantics.utils.threads.ThreadUtils;\r
28 \r
29 public class ParallelPerspectiveHandler extends AbstractHandler {\r
30 \r
31         \r
32         private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();\r
33         \r
34         @Override\r
35         public Object execute(ExecutionEvent event) throws ExecutionException {\r
36                  Command command = event.getCommand();\r
37              boolean oldValue = HandlerUtil.toggleCommandState(command);\r
38              final boolean activate = !oldValue;\r
39              \r
40              final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);\r
41              final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);\r
42              \r
43              ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
44                         \r
45                         @Override\r
46                         public void run() {\r
47                                 if (activate){\r
48                                         Vector3d focal = new Vector3d(panel.GetRenderer().GetActiveCamera().GetFocalPoint());\r
49                                         Vector3d pos = new Vector3d(panel.GetRenderer().GetActiveCamera().GetPosition());\r
50                                         cameraPos.put(panel, pos);\r
51                                         Vector3d dir = new Vector3d(pos);\r
52                                         dir.sub(focal);\r
53                                         dir.normalize();\r
54                                         dir.scale(100);\r
55                                         dir.add(focal);\r
56                                         panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);\r
57                                         \r
58                                         \r
59                                         panel.GetRenderer().GetActiveCamera().SetParallelProjection(1);\r
60                                         panel.GetRenderer().ResetCameraClippingRange();\r
61                                 } else {\r
62                                         panel.GetRenderer().GetActiveCamera().SetParallelProjection(0);\r
63                                         Vector3d pos = cameraPos.get(panel);\r
64                                         if (pos != null) {\r
65                                                 panel.GetRenderer().GetActiveCamera().SetPosition(pos.x, pos.y, pos.z);\r
66                                         }\r
67                                         panel.GetRenderer().ResetCameraClippingRange();\r
68                                         \r
69                                 }\r
70                                 panel.UpdateLight();\r
71                                 panel.repaint();\r
72 \r
73                         }\r
74                  });\r
75             \r
76              return null;\r
77              \r
78         }\r
79         \r
80         \r
81         \r
82 }\r