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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package fi.vtt.simantics.processeditor.monitors;
\r
13 import java.net.URL;
\r
14 import java.util.ArrayList;
\r
16 import javax.vecmath.Point3d;
\r
17 import javax.vecmath.Vector3d;
\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
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
44 import fi.vtt.simantics.processeditor.ProcessResource;
\r
45 import fi.vtt.simantics.processeditor.stubs.InlineComponent;
\r
48 * A monitor implemetation that uses JME's text component to show it's information.
\r
50 * @author Marko Luukkainen
\r
53 public class TextMonitor implements Monitor {
\r
54 private enum Type{POSITION,PIPE};
\r
57 public static String fontLocation = "data/textures/tahoma512.png";
\r
60 private ThreeDimensionalEditorBase editor;
\r
61 private ArrayList<Text> texts = new ArrayList<Text>();
\r
62 private Node monitorNode = new Node("");
\r
64 private IGraphicsNode node = null;
\r
72 float textHeight = 0.f;
\r
76 private MonitorTextProvider provider = new ObjectPropertyProvider();
\r
78 public TextMonitor(ThreeDimensionalEditorBase editor) {
\r
79 this.editor = editor;
\r
80 textNode = new Node("");
\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
92 MaterialState ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();
\r
93 ms.setEmissive(new ColorRGBA(0.f,1.f,0.f,0.f));
\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
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
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
118 background.setRenderQueueMode(Renderer.QUEUE_ORTHO);
\r
119 textNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
\r
121 monitorNode.attachChild(background);
\r
122 monitorNode.attachChild(textNode);
\r
123 // editor.getRenderingComponent().getOrthoNode().attachChild(monitorNode);
\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
133 } else if (n.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {
\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
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
154 middle = new Point3d(start);
\r
161 provider.setSource(n);
\r
162 ArrayList<String> titles = provider.getTexts(graph);
\r
164 for (int i = titles.size() - 1; i < texts.size(); i++) {
\r
165 texts.get(i).removeFromParent();
\r
167 while (texts.size() < titles.size()) {
\r
168 Text text = new Text("", "");
\r
170 textNode.attachChild(text);
\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
182 background.resize(width+20.f, height+10.f);
\r
183 if (monitorNode.getParent() == null)
\r
184 editor.getScenegraphAdapter().getRoot().attachChild(monitorNode);
\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
196 if (newWidth != width) {
\r
198 background.resize(width+20.f, height+10.f);
\r
203 public void update() {
\r
208 if (type == Type.POSITION) {
\r
209 v = editor.getScreenCoord(VecmathJmeTools.getD(node.getGroup().getWorldTranslation()));
\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
219 re.subtractLocal(rs);
\r
220 re.multLocal(0.5f);
\r
224 // just a hack to move monitor out of the view
\r
225 v = new Vector2f (mx+width,my+height);
\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
234 for (Text text : texts) {
\r
235 text.setLocalTranslation(new Vector3f(v.x,y,0.f));
\r
244 public void update(Graph graph) {
\r
249 public IGraphicsNode getNode() {
\r
253 public void remove() {
\r
254 monitorNode.removeFromParent();
\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
264 public void setTextProvider(MonitorTextProvider provider) {
\r
265 this.provider = provider;
\r