]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Focus action 41/3441/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 1 Nov 2019 15:49:41 +0000 (17:49 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 1 Nov 2019 15:49:41 +0000 (17:49 +0200)
gitlab #45

Change-Id: Ic8439e3582c9759534a224d1aba6563707b2988b

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java [new file with mode: 0644]
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java

diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/FocusAction.java
new file mode 100644 (file)
index 0000000..97d5abf
--- /dev/null
@@ -0,0 +1,57 @@
+package org.simantics.g3d.vtk.swt;
+
+import java.util.Collection;
+
+import org.eclipse.jface.action.Action;
+import org.simantics.g3d.vtk.Activator;
+import org.simantics.g3d.vtk.common.VtkView;
+import org.simantics.utils.threads.AWTThread;
+
+import vtk.vtkProp;
+import vtk.vtkProp3D;
+
+public class FocusAction extends Action {
+       
+    VtkView view;
+    vtkCameraAndSelectorAction cameraAndSelectorAction;
+       Collection<vtkProp> props;
+       
+       public FocusAction(VtkView view, vtkCameraAndSelectorAction cameraAndSelectorAction) {
+               super("Focus");
+               setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/eye.png"));
+               this.view = view;
+               this.cameraAndSelectorAction = cameraAndSelectorAction;
+       }
+       
+       public void setProps(Collection<vtkProp> props) {
+               this.props = props;
+       }
+       
+       @Override
+       public void run() {
+               view.getThreadQueue().asyncExec(new Runnable() {
+                       
+                       @Override
+                       public void run() {
+                               int count = 0;
+                               double loc[] = new double[] {0,0,0};
+                               for (vtkProp prop : props) {
+                                       if (prop instanceof vtkProp3D) {
+                                               vtkProp3D prop3d = (vtkProp3D)prop;
+                                               double l[] = prop3d.GetCenter();
+                                               for (int i = 0; i < 3; i++)
+                                                       loc[i] = loc[i] + l[i]; 
+                                               count++;
+                                       }
+                               }
+                               if (count > 0) {
+                                       for (int i = 0; i < 3; i++)
+                                               loc[i] = loc[i] / count;
+                                       cameraAndSelectorAction.focus(loc[0],loc[1],loc[2]);
+                                       view.refresh();
+                               }
+                               
+                       }
+               });
+       }
+}
\ No newline at end of file
index 5775035a1e48cb5cb6368d02ad20fdde686c1c4f..44e6529287bfb3ba9fd875098b8af6a2ca121668 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.plant3d.editor;
 
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -29,9 +30,11 @@ import org.simantics.g3d.vtk.action.RemoveAction;
 import org.simantics.g3d.vtk.common.HoverHighlighter;
 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
 import org.simantics.g3d.vtk.common.SelectionHighlighter;
+import org.simantics.g3d.vtk.common.VTKSelectionItem;
 import org.simantics.g3d.vtk.common.VtkView;
 import org.simantics.g3d.vtk.shape.vtkShape;
 import org.simantics.g3d.vtk.swt.ContextMenuListener;
+import org.simantics.g3d.vtk.swt.FocusAction;
 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
 import org.simantics.g3d.vtk.swt.RotateAction;
 import org.simantics.g3d.vtk.swt.TranslateAction;
@@ -73,6 +76,7 @@ import vtk.vtkActor;
 import vtk.vtkCameraPass;
 import vtk.vtkDefaultPass;
 import vtk.vtkLightsPass;
+import vtk.vtkProp;
 import vtk.vtkRenderPassCollection;
 import vtk.vtkRenderer;
 import vtk.vtkSequencePass;
@@ -91,7 +95,9 @@ public class Plant3DEditor extends ResourceEditorPart {
        
        protected NodeSelectionProvider2<Resource,Object> selectionProvider;
        
+       
        protected vtkCameraAndSelectorAction cameraAction;
+       protected FocusAction focusAction;
        protected TranslateAction translateAction;
        protected TranslateInlineAction translateInlineAction;
        protected TranslateFreeVariableLengthAction translateFreeVariableLengthAction;
@@ -235,6 +241,7 @@ public class Plant3DEditor extends ResourceEditorPart {
        }
        
        protected void createActions() {
+           focusAction = new FocusAction(panel, cameraAction);
                translateAction = new TranslateAction(panel,nodeMap);
                translateInlineAction = new TranslateInlineAction(panel, nodeMap);
                translateFreeVariableLengthAction = new TranslateFreeVariableLengthAction(panel, getRootNode());
@@ -358,6 +365,19 @@ public class Plant3DEditor extends ResourceEditorPart {
        
        protected void createContextMenu(IMenuManager m) {
                List<INode> selected = selectionProvider.getSelectedNodes();
+               List<VTKSelectionItem<Resource>> selectedItems = selectionProvider.getSelectedItems();
+               if (selectedItems.size() > 0) {
+                   List<vtkProp> props = new ArrayList<>();
+                   for (VTKSelectionItem<Resource> i : selectedItems) {
+                       vtkProp p = (vtkProp)i.getAdapter(vtkProp.class);
+                       if (p != null)
+                           props.add(p);
+                   }
+                   if (props.size() > 0) {
+                       focusAction.setProps(props);
+                       m.add(focusAction);
+                   }
+               }
                try {
                        if (selected.size() == 0) {
                                for (Item eq : P3DUtil.getEquipments(getLibraryUri())) {