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