[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool RotationTool.java,1.3,1.4 Tool.java,1.
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2006-01-27 11:43:46
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23906/src/net/sourceforge/bprocessor/gl/tool Modified Files: Tool.java ToolFactory.java Added Files: RotationTool.java Log Message: Added first version of Rotation tool for objects, can't rotate yet but do almost all initial stuff for it Index: ToolFactory.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/ToolFactory.java,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** ToolFactory.java 19 Jan 2006 10:58:12 -0000 1.24 --- ToolFactory.java 27 Jan 2006 11:43:37 -0000 1.25 *************** *** 38,42 **** /** move tool */ private MoveTool move; ! /** extrusion tool */ private SelectTool extrusion; --- 38,45 ---- /** move tool */ private MoveTool move; ! ! /** rotation tool */ ! private RotationTool rotation; ! /** extrusion tool */ private SelectTool extrusion; *************** *** 49,53 **** /** rotation tool */ ! private CameraTool rotation; /** Camera fly tool */ --- 52,56 ---- /** rotation tool */ ! private CameraTool camera; /** Camera fly tool */ *************** *** 98,101 **** --- 101,106 ---- but = tb.registerAction(new MoveAction(glv)); but.setToolTipText("Move"); + but = tb.registerAction(new RotationAction(glv)); + but.setToolTipText("Rotate"); but = tb.registerAction(new ExtrudeAction(glv)); but.setToolTipText("Extrude"); *************** *** 105,110 **** but.setToolTipText("Tape Measure"); tb.addSeparator(); ! but = tb.registerAction(new RotationAction(glv)); ! but.setToolTipText("Rotation"); but = tb.registerAction(new CameraFlyAction(glv)); but.setToolTipText("Fly mode"); --- 110,115 ---- but.setToolTipText("Tape Measure"); tb.addSeparator(); ! but = tb.registerAction(new CameraAction(glv)); ! but.setToolTipText("Camera rotation"); but = tb.registerAction(new CameraFlyAction(glv)); but.setToolTipText("Fly mode"); *************** *** 116,123 **** pencil = new PencilTool(glv, pencilcursor); move = new MoveTool(glv, pencilcursor); extrusion = new ExtrudeTool(glv, pencilcursor); clipplane = new ClipplaneTool(glv, pencilcursor); tapeMeasure = new TapeMeasureTool(glv, pencilcursor); ! rotation = new CameraTool(glv, rotationCursor); fly = new CameraFlyTool(glv, flyCursor); walk = new CameraWalkTool(glv, walkCursor); --- 121,129 ---- pencil = new PencilTool(glv, pencilcursor); move = new MoveTool(glv, pencilcursor); + rotation = new RotationTool(glv, pencilcursor); extrusion = new ExtrudeTool(glv, pencilcursor); clipplane = new ClipplaneTool(glv, pencilcursor); tapeMeasure = new TapeMeasureTool(glv, pencilcursor); ! camera = new CameraTool(glv, rotationCursor); fly = new CameraFlyTool(glv, flyCursor); walk = new CameraWalkTool(glv, walkCursor); *************** *** 166,169 **** --- 172,177 ---- } else if (i == Tool.MOVE_TOOL) { currentTool = move; + } else if (i == Tool.ROTATION_TOOL) { + currentTool = rotation; } else if (i == Tool.EXTRUSION_TOOL) { currentTool = extrusion; *************** *** 172,177 **** } else if (i == Tool.CLIP_TOOL) { currentTool = clipplane; ! } else if (i == Tool.ROTATION_TOOL) { ! currentTool = rotation; } else if (i == Tool.FLY_TOOL) { currentTool = fly; --- 180,185 ---- } else if (i == Tool.CLIP_TOOL) { currentTool = clipplane; ! } else if (i == Tool.CAMERA_TOOL) { ! currentTool = camera; } else if (i == Tool.FLY_TOOL) { currentTool = fly; *************** *** 223,227 **** * The rotation action inner class */ ! class RotationAction extends AbstractAction { /** The GLView */ private GLView glv = null; --- 231,235 ---- * The rotation action inner class */ ! class CameraAction extends AbstractAction { /** The GLView */ private GLView glv = null; *************** *** 231,235 **** * @param glv TheGLView */ ! RotationAction(GLView glv) { this.glv = glv; ClassLoader cl = Thread.currentThread().getContextClassLoader(); --- 239,243 ---- * @param glv TheGLView */ ! CameraAction(GLView glv) { this.glv = glv; ClassLoader cl = Thread.currentThread().getContextClassLoader(); *************** *** 244,248 **** */ public void actionPerformed(ActionEvent e) { ! glv.changeTool(Tool.ROTATION_TOOL); } } --- 252,256 ---- */ public void actionPerformed(ActionEvent e) { ! glv.changeTool(Tool.CAMERA_TOOL); } } *************** *** 333,336 **** --- 341,372 ---- /** + * The rotaion action inner class + */ + class RotationAction extends AbstractAction { + /** The GLView */ + private GLView glv = null; + + /** + * Constructor + * @param glv TheGLView + */ + RotationAction(GLView glv) { + this.glv = glv; + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL url = cl.getResource("Biconrotobj.gif"); + ImageIcon im = new ImageIcon(url); + putValue(Action.SMALL_ICON, im); + } + + /** + * Called when the button is pressed + * @param e The ActionEvent + */ + public void actionPerformed(ActionEvent e) { + glv.changeTool(Tool.ROTATION_TOOL); + } + } + + /** * The extrude action inner class */ Index: Tool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/Tool.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** Tool.java 15 Dec 2005 12:37:00 -0000 1.16 --- Tool.java 27 Jan 2006 11:43:37 -0000 1.17 *************** *** 30,35 **** /** The Clipping tool */ public static final int CLIP_TOOL = 4; ! /** The Rotation tool */ ! public static final int ROTATION_TOOL = 5; /** The Tape Measure tool */ public static final int TAPE_MEASURE_TOOL = 6; --- 30,35 ---- /** The Clipping tool */ public static final int CLIP_TOOL = 4; ! /** The Camera tool */ ! public static final int CAMERA_TOOL = 5; /** The Tape Measure tool */ public static final int TAPE_MEASURE_TOOL = 6; *************** *** 38,41 **** --- 38,43 ---- /** The walk tool */ public static final int WALK_TOOL = 8; + /** The Rotation tool */ + public static final int ROTATION_TOOL = 9; /** --- NEW FILE: RotationTool.java --- //--------------------------------------------------------------------------------- // $Id: RotationTool.java,v 1.4 2006/01/27 11:43:37 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.tool; import net.sourceforge.bprocessor.gl.GLView; import net.sourceforge.bprocessor.gl.model.ClippingPlane; import net.sourceforge.bprocessor.model.Constraints; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Plane; import net.sourceforge.bprocessor.model.Selection; import net.sourceforge.bprocessor.model.Vertex; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.gl.view.Transformation; import net.sourceforge.bprocessor.gl.view.View; import java.awt.Cursor; import java.awt.event.MouseEvent; import java.awt.event.KeyEvent; import java.util.HashSet; import java.util.Iterator; import org.apache.log4j.Logger; /** * The rotation tool */ public class RotationTool extends SelectTool { /** */ private static final int X_AXIS = 1; /** */ private static final int Y_AXIS = 2; /** */ private static final int Z_AXIS = 3; /** The chosen axis */ private int axis; /** The logger */ private static Logger log = Logger.getLogger(RotationTool.class); /** The initial vertex */ private Vertex initial; /** The vertex at drag start */ private Vertex from; /** The previous vertex when dragging */ private Vertex prev; /** The vertex at drag start */ private Vertex center; /** The maximum distance in x y and z */ private double dist = 0; /** The circle in the xy plane */ private HashSet zCircle = new HashSet(); /** The circle in the zy plane */ private HashSet xCircle = new HashSet(); /** The circle in the xz plane */ private HashSet yCircle = new HashSet(); /** The set containing the guide line */ private HashSet guide = new HashSet(); /** * The Constructor * @param glv The 3D canvas * @param cursor The cursor */ public RotationTool(GLView glv, Cursor cursor) { super(glv, cursor); } /** * Update selection * */ protected void update() { Iterator iter = selection.iterator(); while (iter.hasNext()) { Object o = iter.next(); if (o instanceof Entity) { Constraints.getInstance().update((Entity) o); } } } /** * Invoked when a mouse button has been pressed on a component. * @param e The MouseEvent object */ protected void pressed(MouseEvent e) { super.pressed(e); if (target != null && target instanceof Entity) { // If one edge in a rotate circle are selected all should be if (xCircle.contains(target)) { axis = X_AXIS; Selection.primary().addAll(xCircle); clearConstructors(zCircle); clearConstructors(yCircle); } else if (yCircle.contains(target)) { axis = Y_AXIS; Selection.primary().addAll(yCircle); clearConstructors(xCircle); clearConstructors(zCircle); } else if (zCircle.contains(target)) { axis = Z_AXIS; Selection.primary().addAll(zCircle); clearConstructors(xCircle); clearConstructors(yCircle); } else { // ther were no rotation selection axis = 0; } if (axis != 0) { // do the rotation preparation and wait for drag events initial = findInitial(target, e); from = initial.copy(); prev = initial; Edge edge = new Edge(center, initial); edge.setConstructor(true); guide.clear(); guide.add(edge); displayConstructors(guide); } } else { clearConstructors(xCircle); xCircle.clear(); clearConstructors(yCircle); yCircle.clear(); clearConstructors(zCircle); zCircle.clear(); } } /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent */ protected void released(MouseEvent e) { super.released(e); if (axis != 0) { // We were rotation so show all constructor line again displayConstructors(xCircle); displayConstructors(yCircle); displayConstructors(zCircle); Selection.primary().clear(); // remove guideline clearConstructors(guide); guide.clear(); } // make selection if (target != null && target instanceof Entity) { if (target instanceof Edge && ((Edge)target).getConstructor()) { // If it is a constructor it should not be rotatable return; } clearConstructors(xCircle); clearConstructors(yCircle); clearConstructors(zCircle); xCircle.clear(); yCircle.clear(); zCircle.clear(); HashSet vertices = new HashSet(); collect(selection, vertices); // find the size of the handles center = ((Entity)target).center(); dist = 0; gatherInformation(center, vertices); drawCircles(((Entity)target).center(), dist); } else { if (axis == 0) { dist = 0; initial = null; from = null; } } } /** * Invoked when the mouse is held pressed and moved * @param e The MouseEvent object */ protected void dragged(MouseEvent e) { if (target != null && (xCircle.contains(target) || yCircle.contains(target) || zCircle.contains(target))) { initial = findInitial(target, e); // TODO rotate the object from prev to initial prev = initial; } } /** * Invoked when a key has been pressed. Lets user control the mode of movement. * After movement in one direction a length can be typed in to adjust the movement * to a specific length. * ESCAPE - cancel movement. * 0-9 - type length in length field. * ENTER - perform move of the specified length. * @param e The KeyEvent */ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { //TODO rotate object from initial back to from } } /** * Remove tool specific things at tool change * */ public void cleanUp() { clearConstructors(xCircle); clearConstructors(yCircle); clearConstructors(zCircle); xCircle.clear(); yCircle.clear(); zCircle.clear(); } /** * Draw the three circles * @param center The center of the circles * @param radius The radius of the circles */ private void drawCircles(Vertex center, double radius) { zCircle.clear(); double xr = radius; double yr = 0; int segments = 20; double cos = Math.cos(Math.PI * 2.0 / segments); double sin = Math.sin(Math.PI * 2.0 / segments); // DRAW THE XY CIRCLE Vertex first = new Vertex(xr + center.getX(), yr + center.getY(), center.getZ()); Vertex parent = first.copy(); for (int i = 0; i < segments; ++i) { double temp = xr * cos + yr * sin; yr = yr * cos - xr * sin; xr = temp; Vertex prev = parent; parent = new Vertex(xr + center.getX(), yr + center.getY(), center.getZ()); Edge e = new Edge(prev, parent); e.setConstructor(true); zCircle.add(e); } Edge e = new Edge(parent, first); e.setConstructor(true); zCircle.add(e); displayConstructors(zCircle); // DRAW THE YZ CIRCLE xCircle.clear(); yr = radius; double zr = 0; first = new Vertex(center.getX(), yr + center.getY(), zr + center.getZ()); parent = first.copy(); for (int i = 0; i < segments; ++i) { double temp = yr * cos + zr * sin; zr = zr * cos - yr * sin; yr = temp; Vertex prev = parent; parent = new Vertex(center.getX(), yr + center.getY(), zr + center.getZ()); e = new Edge(prev, parent); e.setConstructor(true); xCircle.add(e); } e = new Edge(parent, first); e.setConstructor(true); xCircle.add(e); displayConstructors(xCircle); // DRAW THE XZ CIRCLE yCircle.clear(); xr = radius; zr = 0; first = new Vertex(xr + center.getX(), center.getY(), zr + center.getZ()); parent = first.copy(); for (int i = 0; i < segments; ++i) { double temp = xr * cos + zr * sin; zr = zr * cos - xr * sin; xr = temp; Vertex prev = parent; parent = new Vertex(xr + center.getX(), center.getY(), zr + center.getZ()); e = new Edge(prev, parent); e.setConstructor(true); yCircle.add(e); } e = new Edge(parent, first); e.setConstructor(true); yCircle.add(e); displayConstructors(yCircle); } /** * Gather information about size of bound * @param vertices The vertices to rotate * @param center The center of the element */ private void gatherInformation(Vertex center, HashSet vertices) { Iterator iter = vertices.iterator(); while (iter.hasNext()) { Vertex cur = (Vertex)iter.next(); Vertex temp = cur.minus(center); if (temp.length() > dist) { dist = temp.length(); } } dist *= 1.1; } /** * Finds the initial point clicked for movement. * @param entity the entity initialy clicked * @param e the mouse event associated with the click. * @return the initial point of movement. */ protected Vertex findInitial(Object entity, MouseEvent e) { double x = e.getX(); double y = View.getHeight() - e.getY(); View v = glv.getView(); Transformation transformation = v.transformation(); Vertex near = new Vertex(x, y, 0.0); Vertex far = new Vertex(x, y, 1.0); Edge ray = new Edge(near, far); ray = transformation.unProject(ray); if (entity instanceof Vertex) { return ((Vertex)entity).copy(); } else if (entity instanceof Surface) { Plane plane = ((Surface)entity).plane(); return plane.intersection(ray); } else if (entity instanceof Edge) { Edge edge = (Edge) entity; Edge intersection = edge.intersection(ray); return intersection.getFrom(); } else if (entity instanceof ClippingPlane) { Plane plane = ((ClippingPlane) entity).getPlane(); return plane.intersection(ray); } log.warn("The clicked object is not an Entity"); return null; } } |