]> gerrit.simantics Code Review - simantics/3d.git/blob
42b93f0a9ad1240c03512b75ac38ca6e04df10be
[simantics/3d.git] /
1 package fi.vtt.simantics.processeditor.tools;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.List;\r
6 import java.util.Stack;\r
7 \r
8 import org.eclipse.jface.action.Action;\r
9 import org.eclipse.jface.action.IMenuManager;\r
10 import org.eclipse.jface.action.IToolBarManager;\r
11 import org.eclipse.swt.layout.FillLayout;\r
12 import org.eclipse.swt.widgets.Composite;\r
13 import org.simantics.db.Graph;\r
14 import org.simantics.db.GraphRequestAdapter;\r
15 import org.simantics.db.GraphRequestStatus;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.proconf.g3d.actions.ContextAction;\r
18 import org.simantics.proconf.g3d.actions.FocusAction;\r
19 import org.simantics.proconf.g3d.animation.Animatable;\r
20 import org.simantics.proconf.g3d.animation.AnimationController;\r
21 import org.simantics.proconf.g3d.animation.AnimationSystem;\r
22 import org.simantics.proconf.g3d.animation.TestAnimationController;\r
23 import org.simantics.proconf.g3d.animation.ui.AnimationControlCreator;\r
24 import org.simantics.proconf.g3d.base.EditorContribution;\r
25 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
26 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
27 import org.simantics.proconf.g3d.stubs.G3DNode;\r
28 import org.simantics.utils.ui.jface.MenuTools;\r
29 \r
30 import fi.vtt.simantics.processeditor.Activator;\r
31 import fi.vtt.simantics.processeditor.ProcessResource;\r
32 import fi.vtt.simantics.processeditor.animations.PipeAnimationController;\r
33 import fi.vtt.simantics.processeditor.dialogs.ConfigureAnimationDialog;\r
34 import fi.vtt.simantics.processeditor.dialogs.ConfigureMonitorDialog;\r
35 import fi.vtt.simantics.processeditor.dialogs.ConfigurePipelineAnimationDialog;\r
36 import fi.vtt.simantics.processeditor.monitors.Monitor;\r
37 import fi.vtt.simantics.processeditor.monitors.ResourcePathPropertyProvider;\r
38 import fi.vtt.simantics.processeditor.monitors.TextMonitor;\r
39 import fi.vtt.simantics.processeditor.stubs.PipeRun;\r
40 import fi.vtt.simantics.processeditor.views.ProcessEditor;\r
41 \r
42 public class PlantVisualizationContribution implements EditorContribution {\r
43         private List<ContextAction> actions = new ArrayList<ContextAction>();\r
44         private ProcessEditor parent;\r
45         \r
46     private AnimationSystem animationSystem = null;\r
47     private Action animatePipesAction = null;\r
48     private Action animateAction = null;\r
49     private Action showMonitorsAction = null;\r
50     private Action configureAnimationAction = null;\r
51     private Action configurePipelineAnimationAction = null;\r
52     private Action configureMonitorAction = null;\r
53     \r
54     private Monitor monitor;\r
55         \r
56         public PlantVisualizationContribution(ProcessEditor parent) {\r
57                 this.parent = parent;\r
58         }\r
59         \r
60         @Override\r
61         public void createControl(Composite parent) {\r
62                 parent.setLayout(new FillLayout());\r
63                 this.parent.getRenderingComposite().setLayoutData(null);\r
64         }\r
65         \r
66         @Override\r
67         public void disposeControl() {\r
68                 \r
69         }\r
70         \r
71         @Override\r
72         public void dispose() {\r
73                 \r
74         }\r
75         \r
76         @Override\r
77         public void fillContextMenu(Graph graph, IMenuManager manager,\r
78                         StructuredResourceSelection selection) {\r
79                 \r
80         }\r
81         \r
82         @Override\r
83         public void fillLocalPullDown(IMenuManager menuManager) {\r
84                 MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(showMonitorsAction);\r
85         MenuTools.getOrCreate(parent.getMenuID(),"Monitors", menuManager).add(configureMonitorAction);\r
86         IMenuManager animationMenu = MenuTools.getOrCreate(parent.getMenuID(),"Animations",menuManager);\r
87         animationMenu.add(configureAnimationAction);\r
88         animationMenu.add(configurePipelineAnimationAction);\r
89         MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animateAction);\r
90         MenuTools.getOrCreate(parent.getMenuID(),"Test", animationMenu).add(animatePipesAction);\r
91                 \r
92         }\r
93         \r
94 //      @Override\r
95 //      public void fillMainMenu(List<IContributionItem> list) {\r
96 //              MenuTools.getOrCreate("Monitors", list).add(showMonitorsAction);\r
97 //      MenuTools.getOrCreate("Monitors", list).add(configureMonitorAction);\r
98 //      IMenuManager animationMenu = MenuTools.getOrCreate("Animations",list);\r
99 //      animationMenu.add(configureAnimationAction);\r
100 //      animationMenu.add(configurePipelineAnimationAction);\r
101 //      MenuTools.getOrCreate("Test", animationMenu).add(animateAction);\r
102 //      MenuTools.getOrCreate("Test", animationMenu).add(animatePipesAction);\r
103 //              \r
104 //      }\r
105         \r
106         @Override\r
107         public void fillLocalToolBar(IToolBarManager manager) {\r
108                 AnimationControlCreator c = new AnimationControlCreator(getAnimationSystem());\r
109         manager.add(c.createStopAction());\r
110         manager.add(c.createPauseAction());\r
111         manager.add(c.createPlayAction());\r
112                 manager.add(showMonitorsAction);\r
113         }\r
114         \r
115         @Override\r
116         public Collection<ContextAction> getActions() {\r
117                 return actions;\r
118         }\r
119         \r
120         @Override\r
121         public String getName() {\r
122                 return "Plant Visualization";\r
123         }\r
124         \r
125         @Override\r
126         public void run() {\r
127                 parent.getSession().syncRead(new GraphRequestAdapter() {\r
128                         @Override\r
129                         public GraphRequestStatus perform(Graph g) throws Exception {\r
130                                 getAnimationSystem().run(g,0.01);\r
131                                 updateMonitor(g);\r
132                                 return GraphRequestStatus.transactionComplete();\r
133                         }\r
134                 });\r
135                 \r
136                 \r
137         }\r
138         \r
139         @Override\r
140         public void initialize(Graph graph) {\r
141                  animateAction = new Action("", Action.AS_CHECK_BOX) {\r
142                     public void run() {\r
143                         if (this.isChecked()) {\r
144                                 parent.getSession().asyncRead(new GraphRequestAdapter() {\r
145                                         @Override\r
146                                         public GraphRequestStatus perform(Graph g)\r
147                                                         throws Exception {\r
148                                                 AnimationController c = new TestAnimationController();\r
149                                                         Collection<IGraphicsNode> nodes = parent.getScenegraphAdapter().getNodes();\r
150                                                         for (IGraphicsNode node : nodes) {\r
151                                                                 if (node instanceof Animatable) {\r
152                                                                         Animatable a = (Animatable) node;\r
153                                                                         if (a.setRandomAnimation(g))\r
154                                                                                 c.addAnimatable(a);\r
155                                                                 }\r
156                                                         }\r
157                                                         animationSystem.add(c);\r
158                                                         return GraphRequestStatus.transactionComplete();\r
159                                         }\r
160                                 });\r
161                                 \r
162 //                                              showMessage("Activated " + animatables.size()\r
163 //                                                              + " animations");\r
164                                         } else {\r
165                                                 animationSystem.stop();\r
166                                         }\r
167                     }\r
168                 };\r
169                 \r
170                 animateAction.setText("Random animations");\r
171                 \r
172                 animatePipesAction = new AnimatePipesAction("Pipe animations");\r
173                 \r
174                 showMonitorsAction = new Action("Interactive Monitor", Action.AS_CHECK_BOX) {\r
175                         public void run() {\r
176                                 showMonitors(this.isChecked());\r
177                         }\r
178                 };\r
179                 showMonitorsAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor.png"));\r
180                 \r
181                 \r
182                 \r
183                 configureAnimationAction = new Action() {\r
184                         \r
185                         public void run() {\r
186                                 parent.getSession().syncRead(new GraphRequestAdapter(){\r
187                                         List <Resource> list = new ArrayList<Resource>();\r
188                                         @Override\r
189                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
190                                                 Stack<G3DNode> stack = new Stack<G3DNode>();\r
191                                                 stack.add(parent.getPlant(g).toG3DNode());\r
192                                                 while (!stack.isEmpty()) {\r
193                                                         G3DNode n = stack.pop();\r
194                                                         list.add(n.getResource());\r
195                                                         for (G3DNode no : n.getChild())\r
196                                                                 stack.push(no);\r
197                                                 }\r
198                                                 \r
199                                                 if (list.size() == 0)\r
200                                                         return GraphRequestStatus.transactionComplete();\r
201                                                 \r
202                                                 ConfigureAnimationDialog dialog = new ConfigureAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getScenegraphAdapter(),getAnimationSystem());\r
203                                                 dialog.open();\r
204                                                 return GraphRequestStatus.transactionComplete();\r
205                                         }\r
206                                         \r
207                                 });\r
208 \r
209                         }\r
210                         \r
211                         \r
212                 };\r
213                 configureAnimationAction.setText("Configure animations");\r
214                 configureAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));\r
215                 \r
216                 configurePipelineAnimationAction = new Action() {\r
217                         \r
218                         public void run() {\r
219                                 parent.getSession().syncRead(new GraphRequestAdapter(){\r
220                                         List <Resource> list = new ArrayList<Resource>();\r
221                                         @Override\r
222                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
223                                                 Stack<G3DNode> stack = new Stack<G3DNode>();\r
224                                                 stack.add(parent.getPlant(g).toG3DNode());\r
225                                                 while (!stack.isEmpty()) {\r
226                                                         G3DNode n = stack.pop();\r
227                                                         if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))\r
228                                                                 list.add(n.getResource());\r
229                                                         else //piperun will not contain other piperuns\r
230                                                                 for (G3DNode no : n.getChild())\r
231                                                                         stack.push(no);\r
232                                                 }\r
233                                                 \r
234                                                 if (list.size() == 0)\r
235                                                         return GraphRequestStatus.transactionComplete();\r
236                                                 \r
237                                                 ConfigurePipelineAnimationDialog dialog = new ConfigurePipelineAnimationDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list,parent.getRenderingComponent(),getAnimationSystem());\r
238                                                 dialog.open();\r
239                                                 return GraphRequestStatus.transactionComplete();\r
240                                         }\r
241                                         \r
242                                 });\r
243                                 \r
244                         }\r
245                         \r
246                         \r
247                 };\r
248                 configurePipelineAnimationAction.setText("Configure pipeline animations");\r
249                 configurePipelineAnimationAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/film_edit.png"));\r
250                 configureMonitorAction = new Action() {\r
251                         \r
252                         public void run() {\r
253                                 parent.getSession().syncRead(new GraphRequestAdapter(){\r
254                                         List <Resource> list = new ArrayList<Resource>();\r
255                                         @Override\r
256                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
257                                                 Stack<G3DNode> stack = new Stack<G3DNode>();\r
258                                                 stack.add(parent.getPlant(g).toG3DNode());\r
259                                                 while (!stack.isEmpty()) {\r
260                                                         G3DNode n = stack.pop();\r
261                                                         list.add(n.getResource());\r
262                                                         for (G3DNode no : n.getChild())\r
263                                                                 stack.push(no);\r
264                                                 }\r
265                                                 \r
266                                                 if (list.size() == 0)\r
267                                                         return GraphRequestStatus.transactionComplete();\r
268                                                 \r
269                                                 ConfigureMonitorDialog dialog = new ConfigureMonitorDialog(parent.getRenderingComposite().getShell(),parent.getSession(),list);\r
270                                                 dialog.open();\r
271                                                 return GraphRequestStatus.transactionComplete();\r
272                                         }\r
273                                         \r
274                                 });\r
275                                 \r
276                         }\r
277                 };\r
278                 configureMonitorAction.setText("Configure monitor");\r
279                 configureMonitorAction.setImageDescriptor(Activator.imageDescriptorFromPlugin("fi.vtt.proconf.ode", "icons/silk/monitor_edit.png"));\r
280                 \r
281                 actions.add(new FocusAction(parent));\r
282         }\r
283         \r
284          private AnimationSystem getAnimationSystem() {\r
285                 if (animationSystem == null) {\r
286                         animationSystem = new AnimationSystem(parent.getScenegraphAdapter());\r
287                 }\r
288                 return animationSystem;\r
289          }\r
290          \r
291          protected void updateMonitor(Graph graph) {\r
292                 if (showMonitorsAction.isChecked()) {\r
293                 \r
294                         List<IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();\r
295                                 if (nodes.size() > 0) {\r
296                                         IGraphicsNode selected = nodes.get(0);\r
297                                         if (monitor.acceptNode(graph,selected))\r
298                                                 monitor.setNode(graph,selected);\r
299                                 }\r
300                                 monitor.update();\r
301                 }\r
302             }\r
303             \r
304             protected Monitor createMonitor() {\r
305                 Monitor m = new TextMonitor(parent);\r
306                 m.setTextProvider(new ResourcePathPropertyProvider());\r
307                 return m;\r
308             }\r
309             \r
310             private  void showMonitors(boolean show) {\r
311                 if (show) {\r
312                         if (monitor == null) {\r
313                                 monitor = createMonitor();\r
314                         }\r
315                         final List <IGraphicsNode> nodes = parent.getSelectionAdapter().getInteractiveSelectedObjects();\r
316                         if (nodes.size() > 0) {\r
317                                 parent.getSession().asyncRead(new GraphRequestAdapter() {\r
318                                         @Override\r
319                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
320                                                 monitor.setNode(g,nodes.get(0));\r
321                                                 return GraphRequestStatus.transactionComplete();\r
322                                         }\r
323                                 });\r
324                                 \r
325                         }\r
326                         \r
327                 } else if (!show && monitor != null) {\r
328                         monitor.remove();\r
329                         parent.setViewChanged(true);\r
330                 }\r
331             }\r
332          \r
333          private class AnimatePipesAction extends Action {\r
334 \r
335                 public AnimatePipesAction(String text) {\r
336                         super(text, Action.AS_CHECK_BOX);\r
337 \r
338                 }\r
339 \r
340                 public void run() {\r
341                         if (this.isChecked()) {\r
342                                 parent.getSession().syncRead(new GraphRequestAdapter() {\r
343                                         List<PipeRun> list = new ArrayList<PipeRun>();\r
344 \r
345                                         @Override\r
346                                         public GraphRequestStatus perform(Graph g) throws Exception {\r
347                                                 Stack<G3DNode> stack = new Stack<G3DNode>();\r
348                                                 stack.add(parent.getPlant(g).toG3DNode());\r
349                                                 while (!stack.isEmpty()) {\r
350                                                         G3DNode n = stack.pop();\r
351                                                         if (n.isInstanceOf(ProcessResource.plant3Dresource.PipeRun))\r
352                                                                 list.add(new PipeRun(n));\r
353                                                         else\r
354                                                                 //piperun will not contain other piperuns\r
355                                                                 for (G3DNode no : n.getChild())\r
356                                                                         stack.push(no);\r
357                                                 }\r
358 \r
359                                                 for (PipeRun n : list) {\r
360                                                         PipeAnimationController c = new PipeAnimationController(parent.getRenderingComponent(), n);\r
361                                                         //animationSystem.add(c);\r
362                                                         getAnimationSystem().add(c);\r
363                                                 }\r
364                                                 return GraphRequestStatus.transactionComplete();\r
365                                         }\r
366 \r
367                                 });\r
368 \r
369                         } else {\r
370                                 //stopAnimations();\r
371                                 getAnimationSystem().stop();\r
372                         }\r
373                 }\r
374 \r
375         }\r
376 }\r