]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java
Allow multiple radii for turns.
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / common / SelectionHighlighter.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.common;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.simantics.g3d.scenegraph.IG3DNode;
22 import org.simantics.g3d.scenegraph.NodeHighlighter;
23 import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightEventType;
24 import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightObjectType;
25 import org.simantics.g3d.scenegraph.base.INode;
26 import org.simantics.g3d.tools.AdaptationUtils;
27 import org.simantics.utils.threads.AWTThread;
28 import org.simantics.utils.threads.ThreadUtils;
29
30 import vtk.vtkActor;
31 import vtk.vtkAlgorithm;
32 import vtk.vtkAlgorithmOutput;
33 import vtk.vtkFeatureEdges;
34 import vtk.vtkMapper;
35 import vtk.vtkProp;
36 import vtk.vtkProperty;
37
38 public class SelectionHighlighter<DBObject, E extends INode> implements ISelectionChangedListener{
39
40         
41         
42         protected VtkView panel;
43         protected VTKNodeMap<DBObject,E> nodeMap;
44         
45         protected List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>();
46         protected List<vtkActor> selectedActors = new ArrayList<vtkActor>();
47         
48         protected HighlightObjectType type = HighlightObjectType.Node;
49         
50         public SelectionHighlighter(VtkView panel, VTKNodeMap<DBObject,E> nodeMap) {
51                 this.panel = panel;
52                 this.nodeMap = nodeMap;
53         }
54         
55         @Override
56         public void selectionChanged(SelectionChangedEvent event) {
57                 final ISelection s = event.getSelection();
58                 
59                 if (Thread.currentThread().equals(panel.getThreadQueue().getThread()))
60                         highlight(s);
61                 else {
62                         ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
63                                 @Override
64                                 public void run() {
65                                         highlight(s);
66                                         //System.out.println(this.getClass().getName() + " highlight ");
67                                         //panel.Render();
68                                         panel.refresh();
69                                 }
70                         });
71                 }
72                         
73         }
74         
75         protected void hilight(E node, HighlightEventType type) {
76                 if (node instanceof NodeHighlighter) {
77                         ((NodeHighlighter)node).highlight(type);
78                         return;
79                 }
80                 if (type == HighlightEventType.Selection) {
81                         setSelectedColor(node); 
82                 } else if (type == HighlightEventType.ClearSelection) {
83                         setDefaultColor(node);
84                 }
85         }
86         
87         protected void hilight(vtkActor actor, HighlightEventType type) {
88                 if (type == HighlightEventType.Selection) {
89                         setColor(actor,false,new double[]{1,0,0});
90                         setColor(actor,true,new double[]{1,0,1});
91                 } else if (type == HighlightEventType.ClearSelection) {
92                         setColor(actor,false,new double[]{1,1,0});
93                         setColor(actor,true,new double[]{0,0,0});
94                 }
95         }
96         
97         protected void highlight(ISelection s) {
98                 highlight(s, HighlightEventType.Selection, HighlightEventType.ClearSelection);
99         }
100         
101         protected void highlight(ISelection s, HighlightEventType apply, HighlightEventType clear) {
102         
103                 boolean changed = false;
104                 if (type == HighlightObjectType.Node) {
105                         Collection<IG3DNode> currentSelectedNodes = AdaptationUtils.adaptToCollection(s,IG3DNode.class);//getSelectedNodes(currentSelectedActors);
106                         if (currentSelectedNodes.size() == 0) {
107                                 Collection<vtkProp> currentSelectedActors = AdaptationUtils.adaptToCollection(s, vtkProp.class);
108                                 currentSelectedNodes = getSelectedNodes(currentSelectedActors);
109                         }
110                         for (IG3DNode node : selectedNodes) {
111                                 if (!currentSelectedNodes.contains(node)) {
112                                         hilight((E)node, clear);
113                                         changed = true;
114                                 }
115                         }
116                         for (IG3DNode node : currentSelectedNodes) {
117                                 if (!selectedNodes.contains(node)) {
118                                         hilight((E)node, apply);
119                                         changed = true;
120                                 }
121                         }
122                         selectedNodes.clear();
123                         selectedNodes.addAll(currentSelectedNodes);
124                         //selectedNodes = currentSelectedNodes;
125                         
126                 } else {
127                         
128                         Collection<vtkActor> currentSelectedActors = AdaptationUtils.adaptToCollection(s, vtkActor.class);
129                         
130                         for (vtkActor act : selectedActors) {
131                                 if (!currentSelectedActors.contains(act)) {
132                                         hilight(act,clear);
133                                         changed = true;
134                                 }
135                         }
136                         for (vtkActor act : currentSelectedActors) {
137                                 if (!selectedActors.contains(act)) {
138                                         hilight(act,apply);
139                                         changed = true;
140                                 }
141                         }
142                         selectedActors.clear();
143                         selectedActors.addAll(currentSelectedActors);
144                 }
145                 if (changed) {
146                         panel.refresh();
147                 }
148         }
149         
150         protected List<IG3DNode> getSelectedNodes(Collection<vtkProp> selectedActors) {
151                 List<IG3DNode> currentSelectedNodes = new ArrayList<IG3DNode>();
152                 
153                 for (vtkProp a : selectedActors) {
154                         INode node = nodeMap.getNode((vtkProp)a);
155                         if (node == null || !(node instanceof IG3DNode))
156                                 continue;
157                         if (!currentSelectedNodes.contains(node))
158                                 currentSelectedNodes.add((IG3DNode)node);
159                 }
160                 return currentSelectedNodes;
161         }
162         
163         protected void setDefaultColor(E node) {
164                 double color[] = new double[]{1,1,0};
165                 setColor(node, false, color);
166         }
167         
168         protected void setSelectedColor(E node) {
169                 double color[] = new double[]{1,0,0};
170                 setColor(node, false, color);
171         }
172         
173         
174         public void setColor(E node, boolean edge, double color[]) {
175                 for (vtkProp prop : nodeMap.getRenderObjects(node)) {
176                         if (prop instanceof vtkActor) {
177                                 vtkActor act = (vtkActor)prop;
178                                 setColor(act, edge, color);
179                         }
180                 }
181         }
182         
183         protected void setColor(vtkActor act, boolean edge, double color[]) {
184                 
185                 vtkMapper mapper = act.GetMapper();
186                 vtkAlgorithmOutput out = mapper.GetInputConnection(0, 0);
187                 vtkAlgorithm producer = out.GetProducer();
188                 boolean isEdge = (producer instanceof vtkFeatureEdges);
189                 producer.Delete();
190                 if (isEdge == edge) {
191                         vtkProperty property = act.GetProperty();
192                         property.SetColor(color);
193                         property.Delete();
194                 }
195                 out.Delete();
196                 mapper.Delete();
197         }
198 }