Use the DragEventTool
From JReality Wiki
JavaDoc: DragEventTool
The DragEventTool is a tool which is designed to be customized.
- It frees the user from directly interacting with the pick system
- It notifies its listeners when picking events generate events in the scene graph where the tool is positioned.
- There are separate listeners for point, line, and polygon drag events.
Simple example
Source file: DragEventTool01
This example takes advantage of the difference between the picked point and the closest actual vertex of the geometric data. The listener replaces the actual vertex with the picked point. This has the effect of dragging the actual vertex in the plane parallel to the viewing plane.
... public static void main(String[] args) { SceneGraphComponent cmp = new SceneGraphComponent(); cmp.setGeometry(Primitives.icosahedron()); Appearance ap = new Appearance(); cmp.setAppearance(ap); setupAppearance(ap); DragEventTool t = new DragEventTool(); t.addPointDragListener(new PointDragListener() { public void pointDragStart(PointDragEvent e) { System.out.println("drag start of vertex no "+e.getIndex()); } public void pointDragged(PointDragEvent e) { PointSet pointSet = e.getPointSet(); double[][] points=new double[pointSet.getNumPoints()][]; pointSet.getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(points); points[e.getIndex()]=e.getPosition(); pointSet.setVertexAttributes(Attribute.COORDINATES,StorageModel.DOUBLE_ARRAY.array(3).createReadOnly(points)); } public void pointDragEnd(PointDragEvent e) { } }); cmp.addTool(t); JRViewer.display(cmp); }
Advanced example
Source file: DragEventTool02
This example registers as listeners for point, line and face drag events, and moves the corresponding element rigidly. We only list the new methods required to handle lines and polygons.
Play with it to see how it works!
t.addLineDragListener(new LineDragListener() { private IndexedLineSet lineSet; private double[][] points; public void lineDragStart(LineDragEvent e) { System.out.println("start dragging line "+e.getIndex()); lineSet = e.getIndexedLineSet(); points=new double[lineSet.getNumPoints()][]; lineSet.getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(points); } public void lineDragged(LineDragEvent e) { double[][] newPoints=(double[][])points.clone(); Matrix trafo=new Matrix(); MatrixBuilder.euclidean().translate(e.getTranslation()).assignTo(trafo); int[] lineIndices=e.getLineIndices(); for(int i=0;i<lineIndices.length;i++){ newPoints[lineIndices[i]]=trafo.multiplyVector(points[lineIndices[i]]); } lineSet.setVertexAttributes(Attribute.COORDINATES,StorageModel.DOUBLE_ARRAY.array(3).createReadOnly(newPoints)); } public void lineDragEnd(LineDragEvent e) { } }); t.addFaceDragListener(new FaceDragListener() { private IndexedFaceSet faceSet; private double[][] points; public void faceDragStart(FaceDragEvent e) { faceSet = e.getIndexedFaceSet(); points=new double[faceSet.getNumPoints()][]; points = faceSet.getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(null); } public void faceDragged(FaceDragEvent e) { double[][] newPoints=(double[][])points.clone(); Matrix trafo=new Matrix(); MatrixBuilder.euclidean().translate(e.getTranslation()).assignTo(trafo); int[] faceIndices=e.getFaceIndices(); for(int i=0;i<faceIndices.length;i++){ newPoints[faceIndices[i]]=trafo.multiplyVector(points[faceIndices[i]]); } faceSet.setVertexAttributes(Attribute.COORDINATES,StorageModel.DOUBLE_ARRAY.array(3).createReadOnly(newPoints)); } public void faceDragEnd(FaceDragEvent e) { } });
|