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