--- /dev/null
+/*******************************************************************************\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.proconf.g3d.gizmo;\r
+\r
+import java.nio.FloatBuffer;\r
+\r
+import javax.vecmath.Color4f;\r
+import javax.vecmath.Point3f;\r
+import javax.vecmath.Vector3f;\r
+\r
+import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
+\r
+import com.jme.bounding.BoundingBox;\r
+import com.jme.renderer.ColorRGBA;\r
+import com.jme.renderer.Renderer;\r
+import com.jme.scene.TriMesh;\r
+import com.jme.scene.state.AlphaState;\r
+import com.jme.scene.state.MaterialState;\r
+import com.jme.scene.state.ZBufferState;\r
+import com.jme.util.geom.BufferUtils;\r
+\r
+\r
+public class TransformInlineGizmo extends AbstractGizmo {\r
+\r
+ TriMesh geom = new TriMesh();\r
+ ColorRGBA colors[] = new ColorRGBA[2];\r
+ \r
+ \r
+ public static String PICK_NAME = "translate";\r
+ public static String X_NAME = "ax";\r
+ \r
+ public static final int X = 0;\r
+ \r
+ public static final int SELECTED = 1;\r
+ public static final int UNSELECTED = 0;\r
+ \r
+ private boolean selected = false;\r
+ \r
+ \r
+ /* (non-Javadoc)\r
+ * @see fi.vtt.proconf.shapeeditor.common.Gizmo#getPickPrefix()\r
+ */\r
+ public String getPickPrefix() {\r
+ return PICK_NAME;\r
+ }\r
+ \r
+ public int getIndexForName(String name) {\r
+ if (!name.startsWith(PICK_NAME)) {\r
+ return -1;\r
+ }\r
+ name = name.substring(PICK_NAME.length());\r
+ if (name.startsWith(X_NAME))\r
+ return X;\r
+ return -1;\r
+ }\r
+ \r
+ private void updateColor(int sel) {\r
+ FloatBuffer buff = geom.getColorBuffer(0);\r
+ for (int i = 0; i < geom.getBatch(0).getVertexCount(); i++) {\r
+ BufferUtils.setInBuffer(colors[sel], buff, i);\r
+ }\r
+ }\r
+ \r
+ /* (non-Javadoc)\r
+ * @see fi.vtt.proconf.shapeeditor.common.Gizmo#changeSelected(java.lang.String)\r
+ */\r
+ public void setSelected(String name) {\r
+ \r
+ if (name == null) {\r
+ setChanged(true);\r
+ updateColor(UNSELECTED);\r
+ selected = false;\r
+ return;\r
+ }\r
+ int index = getIndexForName(name);\r
+ if (index == -1) {\r
+ selected = false;\r
+ return;\r
+ }\r
+\r
+ selected = true;\r
+ updateColor(SELECTED);\r
+ setChanged(true);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void setScale(Vector3f scale) {\r
+ /**\r
+ * A bug in JME (?)\r
+ * Negative scaling breaks picking (ray hits the bounding box, but ray doesn't intersect any triangles) \r
+ */\r
+ scale.x = Math.abs(scale.x);\r
+ scale.y = Math.abs(scale.y);\r
+ scale.z = Math.abs(scale.z);\r
+ \r
+ super.setScale(scale);\r
+ }\r
+ \r
+ \r
+ /*\r
+ public TransformInlineGizmo(){\r
+ */\r
+ public TransformInlineGizmo(Renderer renderer) {\r
+ super();\r
+ float size = 2.f;\r
+ float sizeD2 = 1.f;\r
+ float offset = 0.07f;\r
+ \r
+ Color4f colorx = new Color4f(0.5f,0.f,0.f,0.5f);\r
+ Color4f scolorx = new Color4f(1.f,0.f,0.f,0.7f);\r
+ \r
+\r
+ float coordinates[] = new float[18*3];\r
+ float cols[] = new float[18*4];\r
+ int[] indices = new int[]{0,2,1,\r
+ 0,3,2,\r
+ 0,4,3,\r
+ 0,1,4,\r
+ 1,2,6,\r
+ 1,6,5,\r
+ 2,3,7,\r
+ 2,7,6,\r
+ 3,4,8,\r
+ 3,8,7,\r
+ 4,1,5,\r
+ 4,5,8,\r
+ 5,6,7,\r
+ 5,7,8,\r
+ \r
+ 9,11,10,\r
+ 9,12,11,\r
+ 9,13,12,\r
+ 9,10,13,\r
+ 10,11,15,\r
+ 10,15,14,\r
+ 11,12,16,\r
+ 11,16,15,\r
+ 12,13,17,\r
+ 12,17,16,\r
+ 13,10,14,\r
+ 13,14,17,\r
+ 14,15,16,\r
+ 14,16,17\r
+ \r
+ };\r
+ for (int i = 0; i < 18; i++)\r
+ setColor(cols,i, colorx);\r
+\r
+ setCoordinate(coordinates,0, new Point3f(size, 0.f, 0.f));\r
+ setCoordinate(coordinates,1, new Point3f(size - offset*2, offset, offset));\r
+ setCoordinate(coordinates,2, new Point3f(size - offset*2, offset, -offset));\r
+ setCoordinate(coordinates,3, new Point3f(size - offset*2, -offset, -offset));\r
+ setCoordinate(coordinates,4, new Point3f(size - offset*2, -offset, offset));\r
+ setCoordinate(coordinates,5, new Point3f(sizeD2, offset, offset));\r
+ setCoordinate(coordinates,6, new Point3f(sizeD2, offset, -offset));\r
+ setCoordinate(coordinates,7, new Point3f(sizeD2, -offset, -offset));\r
+ setCoordinate(coordinates,8, new Point3f(sizeD2, -offset, offset));\r
+ \r
+ setCoordinate(coordinates,9, new Point3f(-size, 0.f, 0.f));\r
+ setCoordinate(coordinates,10, new Point3f(-size + offset*2, offset, offset));\r
+ setCoordinate(coordinates,11, new Point3f(-size + offset*2, offset, -offset));\r
+ setCoordinate(coordinates,12, new Point3f(-size + offset*2, -offset, -offset));\r
+ setCoordinate(coordinates,13, new Point3f(-size + offset*2, -offset, offset));\r
+ setCoordinate(coordinates,14, new Point3f(-sizeD2, offset, offset));\r
+ setCoordinate(coordinates,15, new Point3f(-sizeD2, offset, -offset));\r
+ setCoordinate(coordinates,16, new Point3f(-sizeD2, -offset, -offset));\r
+ setCoordinate(coordinates,17, new Point3f(-sizeD2, -offset, offset));\r
+\r
+ TriMesh linex = new TriMesh(PICK_NAME+X_NAME,BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ getGizmoNode().attachChild(linex);\r
+ getGizmoNode().setModelBound(new BoundingBox());\r
+ getGizmoNode().updateModelBound();\r
+\r
+ geom = linex;\r
+ geom.getBatch(0).setCastsShadows(false);\r
+\r
+ colors[0] = VecmathJmeTools.get(colorx);\r
+ colors[1] = VecmathJmeTools.get(scolorx);\r
+ \r
+ AlphaState as = renderer.createAlphaState();\r
+ as.setBlendEnabled(true);\r
+ as.setSrcFunction(AlphaState.DB_SRC_ALPHA);\r
+ as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);\r
+ as.setEnabled(true);\r
+ getGizmoNode().setRenderState(as);\r
+ MaterialState ms = renderer.createMaterialState();\r
+ ms.setColorMaterial(MaterialState.CM_AMBIENT_AND_DIFFUSE);\r
+ ms.setMaterialFace(MaterialState.MF_FRONT_AND_BACK);\r
+ getGizmoNode().setRenderState(ms);\r
+ ZBufferState zs = renderer.createZBufferState();\r
+ zs.setFunction(ZBufferState.CF_ALWAYS);\r
+ zs.setEnabled(true);\r
+ getGizmoNode().setRenderState(zs);\r
+ \r
+ }\r
+ \r
+\r
+ public boolean isSelected() {\r
+ return selected;\r
+ }\r
+\r
+ public void setSelected(boolean selected) {\r
+ this.selected = selected;\r
+ }\r
+}
\ No newline at end of file