-/*******************************************************************************\r
- * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.processeditor.monitors;\r
-\r
-import java.awt.Color;\r
-import java.util.ArrayList;\r
-\r
-import javax.vecmath.Point3d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.db.Graph;\r
-import org.simantics.processeditor.ProcessResource;\r
-import org.simantics.processeditor.stubs.InlineComponent;\r
-import org.simantics.proconf.g3d.base.G3DTools;\r
-import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
-import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
-import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
-import org.simantics.proconf.g3d.stubs.G3DNode;\r
-\r
-import com.jme.image.Texture;\r
-import com.jme.math.Vector3f;\r
-import com.jme.renderer.ColorRGBA;\r
-import com.jme.renderer.Renderer;\r
-import com.jme.scene.BillboardNode;\r
-import com.jme.scene.Node;\r
-import com.jme.scene.shape.Quad;\r
-import com.jme.scene.state.AlphaState;\r
-import com.jme.scene.state.MaterialState;\r
-import com.jme.scene.state.RenderState;\r
-import com.jme.scene.state.TextureState;\r
-import com.jme.scene.state.ZBufferState;\r
-import com.jmex.awt.swingui.ImageGraphics;\r
-\r
-\r
-\r
-\r
-\r
-/**\r
- * A monitor that uses billboard to show it's information. \r
- * \r
- * @author Marko Luukkainen\r
- *\r
- */\r
-//TODO : should it be variable length / fixed length inline component instead of Straight\r
-\r
-public class BillboardMonitor implements Monitor{\r
- private enum Type{POSITION,PIPE};\r
- private Type type;\r
- private ThreeDimensionalEditorBase editor; \r
- private BillboardNode monitorNode = null;\r
- private int monitorSize = 128;\r
- private ImageGraphics graphics = ImageGraphics.createInstance(monitorSize, monitorSize, 0);\r
- private Node currentParent = null;\r
- private IGraphicsNode node = null;\r
- private boolean useDistanceResize = true;\r
- \r
- Point3d start;\r
- Point3d end;\r
- Point3d middle;\r
- \r
- private MonitorTextProvider provider = new ObjectPropertyProvider();\r
- \r
- \r
- public BillboardMonitor(ThreeDimensionalEditorBase editor) {\r
- this.editor = editor;\r
- monitorNode = new BillboardNode("");\r
- monitorNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);\r
- ZBufferState zs = editor.getRenderingComponent()\r
- .getDisplaySystem().getRenderer().createZBufferState();\r
- zs.setEnabled(true);\r
- zs.setFunction(ZBufferState.CF_ALWAYS);\r
- monitorNode.setRenderState(zs);\r
- Quad quad = new Quad("");\r
- quad.initialize(3.f, 3.f);\r
- monitorNode.attachChild(quad);\r
- // SWTImageGraphics graphics =\r
- // SWTImageGraphics.createInstance(64,64, 0);\r
-\r
- graphics.clearRect(0, 0, monitorSize, monitorSize);\r
-\r
- // graphics.setFont(new Font("Arial",Font.PLAIN,30));\r
- // graphics.setColor(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));\r
- graphics.setColor(new Color(0, 255, 0, 255));\r
- graphics.drawString("Monitor", 0, 32);\r
- \r
- TextureState ts = editor.getRenderingComponent().getDisplaySystem().getRenderer().createTextureState();\r
- Texture texture = new Texture();\r
- texture.setApply(Texture.AM_MODULATE);\r
- // texture.setBlendColor(new ColorRGBA(1, 1, 1, 1));\r
- texture.setFilter(Texture.MM_LINEAR);\r
- texture.setMipmapState(Texture.FM_LINEAR);\r
- texture.setImage(graphics.getImage());\r
- graphics.update();\r
- AlphaState as = editor.getRenderingComponent().getDisplaySystem()\r
- .getRenderer().createAlphaState();\r
- as.setBlendEnabled(true);\r
- as.setSrcFunction(AlphaState.SB_SRC_ALPHA);\r
- as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);\r
- as.setTestEnabled(true);\r
- as.setTestFunction(AlphaState.TF_GREATER);\r
- ts.setTexture(texture);\r
- MaterialState ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();\r
- ms.setEmissive(new ColorRGBA(1.f, 1.f, 1.f, 0.f));\r
- monitorNode.setRenderState(ts);\r
- monitorNode.setRenderState(as);\r
- monitorNode.setRenderState(ms);\r
- graphics.drawLine(0, 33, monitorSize-1, 33);\r
- graphics.update();\r
- }\r
- \r
- public boolean acceptNode(Graph graph, IGraphicsNode node) {\r
- Node selectedParent = node.getGroup();\r
- if (currentParent == null || !selectedParent.equals(currentParent)) {\r
- if (node.getG3DNode(graph).getLocalPosition() != null) {\r
- return true;\r
- } else if (node.getG3DNode(graph).isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {\r
- \r
- return true;\r
- }\r
- }\r
-\r
- return false;\r
- }\r
- \r
- public void setNode(Graph graph, IGraphicsNode node) {\r
- G3DNode n = node.getG3DNode(graph);\r
- if (n.getLocalPosition() != null) {\r
- type = Type.POSITION;\r
- monitorNode.setLocalTranslation(new Vector3f(0.f,0.f,0.f));\r
- } else if (n.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {\r
- type = Type.PIPE;\r
- InlineComponent ic = new InlineComponent(n);\r
- start = G3DTools.getPoint(ic.getControlPoint().getPrevious().getLocalPosition());\r
- end = G3DTools.getPoint(ic.getControlPoint().getNext().getLocalPosition());\r
- Vector3d dir = new Vector3d(end);\r
- dir.sub(start);\r
- dir.scale(0.5);\r
- middle = new Point3d(start);\r
- middle.add(dir);\r
- monitorNode.setLocalTranslation(VecmathJmeTools.get(middle));\r
- } else {\r
- return;\r
- }\r
- Node selectedParent = node.getGroup();\r
- this.node = node;\r
- currentParent = selectedParent;\r
- monitorNode.removeFromParent();\r
- currentParent.attachChild(monitorNode);\r
-\r
- provider.setSource(n);\r
- update(graph);\r
- }\r
- \r
- public IGraphicsNode getNode() {\r
- return node;\r
- }\r
- \r
- public void update() {\r
- if (useDistanceResize) {\r
- Vector3f v = VecmathJmeTools.get(editor.getCamera().getCameraPos());\r
- v.subtractLocal(monitorNode.getWorldTranslation());\r
- float length = v.length();\r
- monitorNode.setLocalScale(length * 0.06f);\r
- }\r
- }\r
- \r
- \r
- public void update(Graph graph) {\r
- ArrayList<String> titles = provider.getTexts(graph);\r
- graphics.clearRect(0, 0, monitorSize, monitorSize);\r
- int y = 16;\r
- for (String s : titles) {\r
- graphics.drawString(s, 0, y);\r
- y += 16;\r
- }\r
-\r
- TextureState ts = (TextureState) monitorNode\r
- .getRenderState(RenderState.RS_TEXTURE);\r
- ts.deleteAll(); // FIXME : texture won't be updated without this\r
- graphics.update();\r
- update();\r
- }\r
- \r
- public void remove() {\r
- monitorNode.removeFromParent();\r
- node = null;\r
- }\r
-\r
- /**\r
- * if true, monitors size is independent of distance to camera. Else monitor's size changes when camera is moved.\r
- * @return\r
- */\r
- public boolean isUseDistanceResize() {\r
- return useDistanceResize;\r
- }\r
-\r
- public void setUseDistanceResize(boolean useDistanceResize) {\r
- this.useDistanceResize = useDistanceResize;\r
- if (!useDistanceResize) {\r
- monitorNode.setLocalScale(1.f);\r
- }\r
- }\r
- \r
- public void setTextProvider(MonitorTextProvider provider) {\r
- this.provider = provider;\r
- }\r
-\r
-}\r