--- /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 javax.vecmath.Color4f;\r
+import javax.vecmath.Point3f;\r
+\r
+import com.jme.bounding.BoundingBox;\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 TransformGizmo extends MultiSelectionGizmo {\r
+\r
+ \r
+ public static String PICK_NAME = "translate";\r
+ public static String X_NAME = "ax";\r
+ public static String Y_NAME = "ay";\r
+ public static String Z_NAME = "az";\r
+ public static String XZ_NAME = "xz";\r
+ public static String XY_NAME = "xy";\r
+ public static String YZ_NAME = "yz";\r
+ public static String XYZ_NAME = "aa";\r
+ \r
+ public static final int X = 0;\r
+ public static final int Y = 1;\r
+ public static final int Z = 2;\r
+ public static final int XY = 3;\r
+ public static final int XZ = 4;\r
+ public static final int YZ = 5;\r
+ public static final int XYZ = 6;\r
+\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
+ if (name.startsWith(Y_NAME))\r
+ return Y;\r
+ if (name.startsWith(Z_NAME))\r
+ return Z;\r
+ if (name.startsWith(XY_NAME))\r
+ return XY;\r
+ if (name.startsWith(XZ_NAME))\r
+ return XZ;\r
+ if (name.startsWith(YZ_NAME))\r
+ return YZ;\r
+ if (name.startsWith(XYZ_NAME))\r
+ return XYZ;\r
+ return -1;\r
+ }\r
+ \r
+ @Override\r
+ public int getCount() {\r
+ return 7;\r
+ }\r
+\r
+ \r
+ /*\r
+ public TransformGizmo() {\r
+ */\r
+ public TransformGizmo(Renderer renderer) {\r
+ super();\r
+ float size = 2.f;\r
+ float sizeD2 = 1.f;\r
+ float offset = 0.2f;\r
+ \r
+ Color4f colorx = new Color4f(0.5f,0.f,0.f,0.5f);\r
+ Color4f colory = new Color4f(0.f,0.5f,0.f,0.5f);\r
+ Color4f colorz = new Color4f(0.f,0.f,0.5f,0.5f);\r
+ Color4f scolorx = new Color4f(1.f,0.f,0.f,0.7f);\r
+ Color4f scolory = new Color4f(0.f,1.f,0.f,0.7f);\r
+ Color4f scolorz = new Color4f(0.f,0.f,1.f,0.7f);\r
+ \r
+ Color4f colorxy = new Color4f();\r
+ colorxy.x = colorx.x + colory.x;\r
+ colorxy.y = colorx.y + colory.y;\r
+ colorxy.z = colorx.z + colory.z;\r
+ colorxy.w = 0.5f;\r
+ \r
+ Color4f colorxz = new Color4f();\r
+ colorxz.x = colorx.x + colorz.x;\r
+ colorxz.y = colorx.y + colorz.y;\r
+ colorxz.z = colorx.z + colorz.z;\r
+ colorxz.w = 0.5f;\r
+ \r
+ Color4f coloryz = new Color4f();\r
+ coloryz.x = colory.x + colorz.x;\r
+ coloryz.y = colory.y + colorz.y;\r
+ coloryz.z = colory.z + colorz.z;\r
+ coloryz.w = 0.5f;\r
+ \r
+ Color4f colorxyz = new Color4f();\r
+ colorxyz.x = colorx.x + colory.x + colorz.x;\r
+ colorxyz.y = colorx.y + colory.y + colorz.y;\r
+ colorxyz.z = colorx.z +colory.z + colorz.z;\r
+ colorxyz.w = 0.5f;\r
+ \r
+ Color4f scolorxy = new Color4f();\r
+ scolorxy.x = scolory.x + scolorx.x;\r
+ scolorxy.y = scolory.y + scolorx.y;\r
+ scolorxy.z = scolory.z + scolorx.z;\r
+ scolorxy.w = 0.5f;\r
+ \r
+ Color4f scolorxz = new Color4f();\r
+ scolorxz.x = scolorx.x + scolorz.x;\r
+ scolorxz.y = scolorx.y + scolorz.y;\r
+ scolorxz.z = scolorx.z + scolorz.z;\r
+ scolorxz.w = 0.5f;\r
+ \r
+ Color4f scoloryz = new Color4f();\r
+ scoloryz.x = scolory.x + scolorz.x;\r
+ scoloryz.y = scolory.y + scolorz.y;\r
+ scoloryz.z = scolory.z + scolorz.z;\r
+ scoloryz.w = 0.5f;\r
+ \r
+ Color4f scolorxyz = new Color4f();\r
+ scolorxyz.x = scolorx.x + scolory.x + scolorz.x;\r
+ scolorxyz.y = scolorx.y + scolory.y + scolorz.y;\r
+ scolorxyz.z = scolorx.z + scolory.z + scolorz.z;\r
+ scolorxyz.w = 0.5f;\r
+ \r
+ \r
+ float coordinates[] = new float[6*3];\r
+ float cols[] = new float[6*4];\r
+ int[] indices = new int[]{0,1,3,\r
+ 1,2,3,\r
+ 0,3,5,\r
+ 3,4,5};\r
+ for (int i = 0; i < 6; 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, offset, 0.f));\r
+ setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f));\r
+ setCoordinate(coordinates,3, new Point3f(sizeD2, 0.f, 0.f));\r
+ setCoordinate(coordinates,4, new Point3f(sizeD2 - offset, 0.f, offset));\r
+ setCoordinate(coordinates,5, new Point3f(size - offset, 0.f, offset));\r
+\r
+ TriMesh linex = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ for (int i = 0; i < 6; i++)\r
+ setColor(cols,i, colory);\r
+\r
+ setCoordinate(coordinates,0, new Point3f(0.f, size, 0.f));\r
+ setCoordinate(coordinates,1, new Point3f(offset, size - offset, 0.f));\r
+ setCoordinate(coordinates,2, new Point3f(offset, sizeD2 - offset, 0.f));\r
+ setCoordinate(coordinates,3, new Point3f(0.f, sizeD2, 0.f));\r
+ setCoordinate(coordinates,4, new Point3f(0.f, sizeD2 - offset, offset));\r
+ setCoordinate(coordinates,5, new Point3f(0.f, size - offset, offset));\r
+\r
+ TriMesh liney = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ for (int i = 0; i < 6; i++)\r
+ setColor(cols,i, colorz);\r
+\r
+ setCoordinate(coordinates,0, new Point3f(0.f, 0.f,size));\r
+ setCoordinate(coordinates,1, new Point3f(offset, 0.f, size - offset));\r
+ setCoordinate(coordinates,2, new Point3f(offset, 0.f, sizeD2 - offset));\r
+ setCoordinate(coordinates,3, new Point3f(0.f, 0.f, sizeD2));\r
+ setCoordinate(coordinates,4, new Point3f(0.f, offset, sizeD2 - offset));\r
+ setCoordinate(coordinates,5, new Point3f(0.f, offset, size - offset));\r
+\r
+ TriMesh linez = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ // TODO : picking did not work properly without chancing indices; this must be investigated \r
+ indices = new int[]{2,1,3,0,1,3};//{0,1,3,1,2,3};\r
+ coordinates = new float[4*3];\r
+ cols = new float[4*4];\r
+ \r
+ for (int i = 0; i < 4; i++)\r
+ setColor(cols,i, colorxz);\r
+ setCoordinate(coordinates,0, new Point3f(offset, 0.f, size-offset));\r
+ setCoordinate(coordinates,1, new Point3f(offset, 0.f, sizeD2 - offset));\r
+ setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, 0.f, offset));\r
+ setCoordinate(coordinates,3, new Point3f(size-offset, 0.f, offset));\r
+\r
+ TriMesh trianglexz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ for (int i = 0; i < 4; i++)\r
+ setColor(cols,i, colorxy);\r
+ setCoordinate(coordinates,0, new Point3f(offset, size-offset, 0.f));\r
+ setCoordinate(coordinates,1, new Point3f(offset, sizeD2 - offset, 0.f));\r
+ setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f));\r
+ setCoordinate(coordinates,3, new Point3f(size-offset, offset, 0.f));\r
+\r
+ TriMesh trianglexy = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ for (int i = 0; i < 4; i++)\r
+ setColor(cols,i, coloryz);\r
+ setCoordinate(coordinates,0, new Point3f( 0.f,offset, size-offset));\r
+ setCoordinate(coordinates,1, new Point3f( 0.f,offset, sizeD2 - offset));\r
+ setCoordinate(coordinates,2, new Point3f( 0.f,sizeD2 - offset, offset));\r
+ setCoordinate(coordinates,3, new Point3f( 0.f,size-offset, offset));\r
+\r
+ TriMesh triangleyz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ indices = new int[]{0,1,2,\r
+ 0,2,3,\r
+ 0,3,4,\r
+ 0,4,5,\r
+ 0,5,6,\r
+ 0,6,7,\r
+ 0,7,8,\r
+ 0,8,9,\r
+ 0,9,1};\r
+ coordinates = new float[10*3];\r
+ cols = new float[10*4];\r
+ \r
+ for (int i = 0; i < 10; i++)\r
+ setColor(cols,i, colorxyz);\r
+ setCoordinate(coordinates,0, new Point3f(0.f, 0.f, 0.f));\r
+ setCoordinate(coordinates,1, new Point3f(sizeD2, 0.f, 0.f));\r
+ setCoordinate(coordinates,2, new Point3f(sizeD2 - offset, offset, 0.f));\r
+ setCoordinate(coordinates,3, new Point3f(offset, sizeD2 - offset, 0.f));\r
+ setCoordinate(coordinates,4, new Point3f(0.f, sizeD2, 0.f));\r
+ setCoordinate(coordinates,5, new Point3f(0.f, sizeD2 - offset, offset));\r
+ setCoordinate(coordinates,6, new Point3f(0.f, offset, sizeD2-offset));\r
+ setCoordinate(coordinates,7, new Point3f(0.f, 0.f, sizeD2));\r
+ setCoordinate(coordinates,8, new Point3f(offset, 0.f, sizeD2-offset));\r
+ setCoordinate(coordinates,9, new Point3f(sizeD2-offset, 0.f, offset));\r
+ \r
+ TriMesh trianglexyz = new TriMesh("",BufferUtils.createFloatBuffer(coordinates),null,BufferUtils.createFloatBuffer(cols),null,BufferUtils.createIntBuffer(indices));\r
+ \r
+ linex.setName(PICK_NAME+X_NAME);\r
+ liney.setName(PICK_NAME+Y_NAME);\r
+ linez.setName(PICK_NAME+Z_NAME);\r
+ trianglexy.setName(PICK_NAME+XY_NAME);\r
+ trianglexz.setName(PICK_NAME+XZ_NAME);\r
+ triangleyz.setName(PICK_NAME+YZ_NAME);\r
+ trianglexyz.setName(PICK_NAME+XYZ_NAME);\r
+ \r
+ linex.getBatch(0).setCastsShadows(false);\r
+ liney.getBatch(0).setCastsShadows(false);\r
+ linez.getBatch(0).setCastsShadows(false);\r
+ trianglexy.getBatch(0).setCastsShadows(false);\r
+ trianglexz.getBatch(0).setCastsShadows(false);\r
+ triangleyz.getBatch(0).setCastsShadows(false);\r
+ trianglexyz.getBatch(0).setCastsShadows(false);\r
+\r
+ getGizmoNode().attachChild(linex);\r
+ getGizmoNode().attachChild(liney);\r
+ getGizmoNode().attachChild(linez);\r
+ getGizmoNode().attachChild(trianglexy);\r
+ getGizmoNode().attachChild(trianglexz);\r
+ getGizmoNode().attachChild(triangleyz);\r
+ getGizmoNode().attachChild(trianglexyz);\r
+ getGizmoNode().setModelBound(new BoundingBox());\r
+ getGizmoNode().updateModelBound();\r
+\r
+ setGeometry(0,linex);\r
+ setGeometry(1,liney);\r
+ setGeometry(2,linez);\r
+ setGeometry(3,trianglexy);\r
+ setGeometry(4,trianglexz);\r
+ setGeometry(5,triangleyz);\r
+ setGeometry(6,trianglexyz);\r
+ \r
+ \r
+ setColor(0,0,colorx);\r
+ setColor(0,1,scolorx);\r
+ setColor(1,0,colory);\r
+ setColor(1,1,scolory);\r
+ setColor(2,0,colorz);\r
+ setColor(2,1,scolorz);\r
+ setColor(3,0,colorxy);\r
+ setColor(3,1,scolorxy);\r
+ setColor(4,0,colorxz);\r
+ setColor(4,1,scolorxz);\r
+ setColor(5,0,coloryz);\r
+ setColor(5,1,scoloryz);\r
+ setColor(6,0,colorxyz);\r
+ setColor(6,1,scolorxyz);\r
+ \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
+ zs.setWritable(false);\r
+ getGizmoNode().setRenderState(zs);\r
+ }\r
+\r
+}
\ No newline at end of file