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