]> gerrit.simantics Code Review - simantics/3d.git/blob
41c60a6b73d42c203284a9f145d1e81120a217a9
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\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 fi.vtt.simantics.processeditor.monitors;\r
12 \r
13 import java.net.URL;\r
14 import java.util.ArrayList;\r
15 \r
16 import javax.vecmath.Point3d;\r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.eclipse.core.runtime.FileLocator;\r
20 import org.eclipse.core.runtime.Path;\r
21 import org.eclipse.swt.graphics.Rectangle;\r
22 import org.simantics.db.Graph;\r
23 import org.simantics.proconf.g3d.base.G3DTools;\r
24 import org.simantics.proconf.g3d.base.MathTools;\r
25 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
26 import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
27 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
28 import org.simantics.proconf.g3d.stubs.G3DNode;\r
29 \r
30 import com.jme.image.Texture;\r
31 import com.jme.math.Vector2f;\r
32 import com.jme.math.Vector3f;\r
33 import com.jme.renderer.ColorRGBA;\r
34 import com.jme.renderer.Renderer;\r
35 import com.jme.scene.Node;\r
36 import com.jme.scene.Text;\r
37 import com.jme.scene.shape.Quad;\r
38 import com.jme.scene.state.AlphaState;\r
39 import com.jme.scene.state.MaterialState;\r
40 import com.jme.scene.state.RenderState;\r
41 import com.jme.scene.state.TextureState;\r
42 import com.jme.util.TextureManager;\r
43 \r
44 import fi.vtt.simantics.processeditor.ProcessResource;\r
45 import fi.vtt.simantics.processeditor.stubs.InlineComponent;\r
46 \r
47 /**\r
48  * A monitor implemetation that uses JME's text component to show it's information. \r
49  * \r
50  * @author Marko Luukkainen\r
51  *\r
52  */\r
53 public class TextMonitor implements Monitor {\r
54         private enum Type{POSITION,PIPE};\r
55         private Type type;\r
56         \r
57         public static String fontLocation = "data/textures/tahoma512.png";\r
58     \r
59         \r
60         private ThreeDimensionalEditorBase editor;\r
61         private ArrayList<Text> texts = new ArrayList<Text>();\r
62         private Node monitorNode = new Node("");\r
63         Node textNode;\r
64         private IGraphicsNode node = null;\r
65         \r
66     Point3d start;\r
67     Point3d end;\r
68     Point3d middle;\r
69     \r
70     float width = 0.f;\r
71     float height = 0.f;\r
72     float textHeight = 0.f;\r
73     \r
74     Quad background;\r
75     \r
76     private MonitorTextProvider provider = new ObjectPropertyProvider();\r
77         \r
78         public TextMonitor(ThreeDimensionalEditorBase editor) {\r
79                 this.editor = editor;\r
80                 textNode = new Node("");\r
81                 \r
82                 \r
83                 \r
84                 AlphaState as1 = editor.getRenderingComponent().getDisplaySystem().getRenderer().createAlphaState();\r
85         as1.setBlendEnabled(true);\r
86         as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);\r
87         as1.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);\r
88         as1.setTestEnabled(true);\r
89         as1.setTestFunction(AlphaState.TF_GREATER);\r
90         as1.setEnabled(true);\r
91         \r
92         MaterialState ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();\r
93         ms.setEmissive(new ColorRGBA(0.f,1.f,0.f,0.f));\r
94         \r
95         TextureState font = editor.getRenderingComponent().getDisplaySystem().getRenderer().createTextureState();\r
96         /** The texture is loaded from fontLocation */\r
97         font.setTexture(loadFontTexture());\r
98         font.setEnabled(true);\r
99         textNode.setRenderState(font);\r
100         textNode.setRenderState(as1);\r
101         textNode.setRenderState(ms);\r
102         background = new Quad("",100.f,100.f);\r
103         \r
104         MaterialState ms2 = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();\r
105         ms2.setEmissive(new ColorRGBA(0.f,0.f,0.f,0.8f));\r
106         ms2.setDiffuse(new ColorRGBA(0.f,0.f,0.f,0.8f));\r
107 \r
108         AlphaState as2 = editor.getRenderingComponent().getDisplaySystem().getRenderer().createAlphaState();\r
109         as2.setBlendEnabled(true);\r
110         as2.setSrcFunction(AlphaState.SB_SRC_ALPHA);\r
111         as2.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);\r
112         as2.setTestEnabled(true);\r
113         as2.setTestFunction(AlphaState.TF_GREATER);\r
114         as2.setEnabled(true);\r
115         monitorNode.setRenderState(ms2);\r
116         monitorNode.setRenderState(as2);\r
117         \r
118         background.setRenderQueueMode(Renderer.QUEUE_ORTHO);\r
119         textNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);\r
120         \r
121         monitorNode.attachChild(background);\r
122         monitorNode.attachChild(textNode);\r
123        // editor.getRenderingComponent().getOrthoNode().attachChild(monitorNode);\r
124         \r
125                 \r
126         }\r
127         \r
128         public boolean acceptNode(Graph graph,IGraphicsNode node) {\r
129                 G3DNode n = node.getG3DNode(graph);\r
130                 if (this.node == null || !node.equals(this.node)) {\r
131                         if (n.getLocalPosition() != null) {\r
132                                 return true;\r
133                         } else if (n.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {\r
134                                 return true;\r
135                         }\r
136                 }\r
137 \r
138                 return false;\r
139 \r
140         }\r
141         \r
142         public void setNode(Graph graph, IGraphicsNode node) {\r
143                 G3DNode n = node.getG3DNode(graph);\r
144                 if (n.getLocalPosition() != null) {\r
145                         type = Type.POSITION;\r
146                 } else if (n.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {\r
147                         type = Type.PIPE;\r
148                         InlineComponent ic = new InlineComponent(n);\r
149                         start = G3DTools.getPoint(ic.getControlPoint().getPrevious().getWorldPosition());\r
150                         end = G3DTools.getPoint(ic.getControlPoint().getNext().getWorldPosition());\r
151                         Vector3d dir = new Vector3d(end);\r
152                         dir.sub(start);\r
153                         dir.scale(0.5);\r
154                         middle = new Point3d(start);\r
155                         middle.add(dir);\r
156                 } else {\r
157                         return;\r
158                 }\r
159                 \r
160                 this.node = node;\r
161                 provider.setSource(n);\r
162                 ArrayList<String> titles = provider.getTexts(graph);\r
163                 \r
164                 for (int i = titles.size() - 1; i < texts.size(); i++) {\r
165                         texts.get(i).removeFromParent();\r
166                 }\r
167                 while (texts.size() < titles.size()) {\r
168                         Text text = new Text("", "");\r
169                         texts.add(text);\r
170                         textNode.attachChild(text);\r
171                 }\r
172                 width = 0.f;\r
173                 height = 0.f;\r
174                 for (int i = 0; i < titles.size(); i++) {\r
175                         Text text = texts.get(i);\r
176                         text.print(titles.get(i));\r
177                         width = Math.max(width,text.getWidth());\r
178                         textHeight = text.getHeight();\r
179                         height += textHeight;\r
180                         textNode.attachChild(text);\r
181                 }\r
182                 background.resize(width+20.f, height+10.f);\r
183                 if (monitorNode.getParent() == null)\r
184                         editor.getScenegraphAdapter().getRoot().attachChild(monitorNode);\r
185                 \r
186         }\r
187         \r
188         private void updateText(Graph graph) {\r
189                 ArrayList<String> titles = provider.getTexts(graph);\r
190                 float newWidth = 0.f;\r
191                 for (int i = 0; i < titles.size(); i++) {\r
192                         Text text = texts.get(i);\r
193                         text.print(titles.get(i));\r
194                         newWidth = Math.max(newWidth,text.getWidth());\r
195                 }\r
196                 if (newWidth != width) {\r
197                         width = newWidth;\r
198                         background.resize(width+20.f, height+10.f);\r
199                 }\r
200                         \r
201         }\r
202         \r
203         public void update() {\r
204                 if (node == null)\r
205                         return;\r
206                 Vector2f v;\r
207                         \r
208                 if (type == Type.POSITION) {\r
209                         v = editor.getScreenCoord(VecmathJmeTools.getD(node.getGroup().getWorldTranslation()));\r
210                 } else {\r
211                         float mx = editor.getRenderingComponent().getDisplaySystem().getWidth();\r
212                         float my = editor.getRenderingComponent().getDisplaySystem().getHeight();\r
213                         Vector2f s = editor.getScreenCoord(start);\r
214                         Vector2f e = editor.getScreenCoord(end);\r
215                         Vector2f rs = new Vector2f();\r
216                         Vector2f re = new Vector2f();\r
217                         boolean in = MathTools.clipLineRectangle(s, e, new Vector2f(0.f,0.f), new Vector2f(mx,my), rs, re);\r
218                         if (in) {\r
219                                 re.subtractLocal(rs);\r
220                                 re.multLocal(0.5f);\r
221                                 rs.addLocal(re);\r
222                                 v = rs;\r
223                         } else {\r
224                                 // just a hack to move monitor out of the view\r
225                                 v = new Vector2f (mx+width,my+height);\r
226                         }\r
227                         \r
228                         \r
229                 }\r
230                 background.setLocalTranslation(new Vector3f(v.x,v.y,0.f));\r
231                 float y = v.y + (height * 0.5f) - textHeight;\r
232                 v.x -= width * 0.5f;\r
233                 \r
234                 for (Text text : texts) {\r
235                         text.setLocalTranslation(new Vector3f(v.x,y,0.f));\r
236                         y -= textHeight;\r
237                 }\r
238                 \r
239                 \r
240                 \r
241         }\r
242         \r
243         @Override\r
244         public void update(Graph graph) {\r
245                 updateText(graph);\r
246                 update();\r
247         }\r
248         \r
249         public IGraphicsNode getNode() {\r
250                 return node;\r
251         }\r
252         \r
253         public void remove() {\r
254                 monitorNode.removeFromParent();\r
255                 node = null;\r
256         }\r
257         \r
258         protected Texture loadFontTexture() {\r
259                 URL url = FileLocator.find(com.jme.eclipse.Activator.getDefault().getBundle(),new Path(fontLocation),null);\r
260         return TextureManager.loadTexture(url, Texture.MM_LINEAR,\r
261                 Texture.FM_LINEAR);\r
262         }\r
263         \r
264         public void setTextProvider(MonitorTextProvider provider) {\r
265                 this.provider = provider;\r
266         }\r
267         \r
268         \r
269 \r
270         \r
271         \r
272 }\r