[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool MoveTool.java,1.48,1.49
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2006-03-27 14:40:20
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17068 Modified Files: MoveTool.java Log Message: refactoring movetool to use strategies Index: MoveTool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/MoveTool.java,v retrieving revision 1.48 retrieving revision 1.49 diff -C2 -d -r1.48 -r1.49 *** MoveTool.java 21 Mar 2006 22:33:36 -0000 1.48 --- MoveTool.java 27 Mar 2006 14:40:04 -0000 1.49 *************** *** 16,20 **** import net.sourceforge.bprocessor.model.Vertex; import net.sourceforge.bprocessor.model.Surface; - import net.sourceforge.bprocessor.model.Camera; import net.sourceforge.bprocessor.gl.view.Transformation; --- 16,19 ---- *************** *** 26,37 **** import java.util.Collection; - import java.util.HashMap; - import java.util.HashSet; import java.util.Iterator; import java.util.Set; ! import java.util.List; ! import java.util.LinkedList; ! import java.util.Timer; ! import java.util.TimerTask; import org.apache.log4j.Logger; --- 25,33 ---- import java.util.Collection; import java.util.Iterator; import java.util.Set; ! import java.util.HashSet; ! ! import org.apache.log4j.Logger; *************** *** 44,113 **** private static Logger log = Logger.getLogger(MoveTool.class); - /** The axis restricted movement mode */ - public static final int AXIS_RESTRICTED = 0; - - /** The free snap movement mode */ - public static final int FREE_SNAP = 1; - - /** The 3 clicks movement mode */ - public static final int THREE_CLICK = 2; - - /** The controlled movement mode */ - public static final int CONTROLLED = 3; - - /** The goemetry based movement mode */ - public static final int GEOMETRY = 4; - /** The initial movepoint */ ! private Vertex initial; /** The location to move from */ ! private Vertex from; - /** The plane to drag according to */ - private Plane dragPlane; - /** The set of vertices to move */ ! private Set vertices; ! ! /** constructors for aligning the movement */ ! private Set moveConstructors; ! ! /** Vector to restrict movement in one direction */ ! private Vertex restrictionVector; ! /** The last restriction vector used */ ! private Vertex lastRestriction; /** Number typed when using the length field */ ! private String number; ! ! /** The mode of moving */ ! private int moveMode; ! ! /** Number of clicks when in 3 click mode */ ! private int numberOfClicks; ! ! /** The Three click constructors edge */ ! private Edge threeClickConst; ! ! /** The entity that is being snapped to */ ! private Entity snapEntity; /** The Collection of entities being moved*/ ! private Collection moveEntities; ! ! /** ! * Mapping from each vertex being moved, ! * to a vertex defining the direction of the ! * edge the moving vertex slides on ! */ ! private HashMap slideMap; ! ! /** The entity being moved in a controlled way */ ! private Entity controlled; ! /** The aligning edge */ ! private Edge alignEdge; /** --- 40,63 ---- private static Logger log = Logger.getLogger(MoveTool.class); /** The initial movepoint */ ! protected Vertex initial; /** The location to move from */ ! protected Vertex from; /** The set of vertices to move */ ! protected Set vertices; ! /** The direction of the last move */ ! protected Vertex lastMoveDirection; /** Number typed when using the length field */ ! protected String number; /** The Collection of entities being moved*/ ! protected Collection moveEntities; ! /** The move strategy */ ! private MoveTool strategy; /** *************** *** 118,128 **** public MoveTool(GLView glv, Cursor cursor) { super(glv, cursor); ! moveConstructors = null; ! restrictionVector = null; ! snapEntity = null; ! alignEdge = null; ! slideMap = new HashMap(); ! numberOfClicks = 1; ! moveMode = AXIS_RESTRICTED; } --- 68,73 ---- public MoveTool(GLView glv, Cursor cursor) { super(glv, cursor); ! strategy = null; ! lastMoveDirection = null; } *************** *** 152,234 **** */ protected void pressed(MouseEvent e) { ! findTarget(e); ! { ! clearConstructors(moveConstructors); ! if (target != null) { ! moveEntities = new HashSet(); ! if (moveMode != THREE_CLICK) { ! initial = findInitial(target, e); ! from = initial.copy(); ! slideMap = new HashMap(); ! ! setupConstructors(); ! displayConstructors(moveConstructors); ! ! vertices = new HashSet(); ! ! if (selection.isEmpty()) { ! moveEntities.add(target); ! } else { ! moveEntities.addAll(selection); ! } ! ! if (moveMode == CONTROLLED) { ! if (!moveEntities.isEmpty()) { ! Iterator it = moveEntities.iterator(); ! controlled = (Entity)it.next(); ! } ! moveEntities.clear(); ! moveEntities.add(controlled); ! } ! collect(moveEntities, vertices); ! } else { ! threeClickMove(e); ! } } ! } ! } ! ! /** Taking care of the three-click mode ! * @param e the MouseEvent object ! */ ! private void threeClickMove(MouseEvent e) { ! vertices = new HashSet(); ! moveEntities.addAll(selection); ! collect(moveEntities, vertices); ! if (numberOfClicks == 1) { ! from = findInitial(target, e); ! threeClickConst = new Edge(from, from); ! threeClickConst.setConstructor(true); ! glv.getView().addTempEdge(threeClickConst); ! numberOfClicks = 2; ! } else if (numberOfClicks == 2) { ! Vertex movement = findInitial(target, e).minus(from); ! move(moveEntities, movement); ! glv.getView().removeTempEdge(threeClickConst); ! threeClickConst = null; ! if (alignEdge != null) { ! glv.getView().removeTempEdge(alignEdge); ! alignEdge = null; ! } ! update(); ! numberOfClicks = 1; ! } } /** - * Sets the move constructors appropriate for the - * movement mode. - */ - private void setupConstructors() { - if (moveMode == AXIS_RESTRICTED) { - moveConstructors = makeXYZConstructors(initial); - } else if (moveMode == FREE_SNAP) { - moveConstructors = makeXYZConerConstructors(initial); - } else if (moveMode == THREE_CLICK) { - moveConstructors = new HashSet(); - } - } - - /** * Finds the point clicked. * @param entity the entity clicked --- 97,114 ---- */ protected void pressed(MouseEvent e) { ! if (strategy != null) { ! strategy.pressed(e); ! } else { ! moveEntities = new HashSet(); ! findTarget(e); ! if (selection.isEmpty()) { ! moveEntities.add(target); ! } else { ! moveEntities.addAll(selection); } ! } } /** * Finds the point clicked. * @param entity the entity clicked *************** *** 271,281 **** */ protected void released(MouseEvent e) { ! findTarget(e); ! clearConstructors(moveConstructors); ! moveConstructors = null; ! restrictionVector = null; ! dragPlane = null; ! number = ""; ! Project.getInstance().checkpoint(); } --- 151,161 ---- */ protected void released(MouseEvent e) { ! if (strategy != null) { ! strategy.released(e); ! } else { ! findTarget(e); ! number = ""; ! Project.getInstance().checkpoint(); ! } } *************** *** 285,330 **** */ protected void moved(MouseEvent e) { ! boolean showAlign = false; ! if (moveMode == THREE_CLICK && threeClickConst != null) { ! moveConstructors = makeXYZConerConstructors(new Vertex(0, 0, 0)); ! displayConstructors(moveConstructors); ! glv.getView().removeTempEdge(threeClickConst); ! if (alignEdge != null) { ! glv.getView().addTempEdge(alignEdge); ! } ! ! findTarget(e); ! glv.getView().makeTarget(target); ! if (target != null) { ! Vertex to = findInitial(target, e); ! if (target instanceof Edge) { ! Edge targetEdge = (Edge)target; ! if (!targetEdge.equals(alignEdge)) { ! Timer alignTimer = new Timer(); ! TimerTask alignTask = new AlignTask(targetEdge); ! alignTimer.schedule(alignTask, 300); ! } ! if (alignEdge != null) { ! Vertex alignDir = alignEdge.getDirection(); ! Vertex targetDir = targetEdge.getDirection(); ! Vertex dirCross = alignDir.cross(targetDir); ! showAlign = dirCross.length() < 0.000001; ! } ! } ! ! if (to != null) { ! threeClickConst.setTo(to); ! } ! } else { ! threeClickConst.setTo(from); ! } ! ! if (threeClickConst != null) { ! glv.getView().addTempEdge(threeClickConst); ! } ! if (!showAlign) { ! glv.getView().removeTempEdge(alignEdge); ! } ! clearConstructors(moveConstructors); } else { findTarget(e); --- 165,170 ---- */ protected void moved(MouseEvent e) { ! if (strategy != null) { ! strategy.moved(e); } else { findTarget(e); *************** *** 333,336 **** --- 173,177 ---- } + /** * Invoked when the mouse is held pressed and moved *************** *** 338,373 **** */ protected void dragged(MouseEvent e) { ! findRestrictions(e); ! if (dragPlane != null && restrictionVector != null) { ! int x = e.getX(); ! int y = e.getY(); ! Vertex parentPos = glv.getView().toPlaneCoords(new double[] {x, y}, dragPlane); ! if (parentPos != null) { ! Vertex delta = parentPos.minus(from); ! ! //Restricting movement to fit movement mode. ! delta = restrict(delta, e); ! ! if (log.isDebugEnabled()) { ! log.debug("from " + from.getX() + ", " + from.getY() + ", " + from.getZ()); ! log.debug("delta " + delta.getX() + ", " + delta.getY() + ", " + delta.getZ()); ! } ! ! //delta = snapToInitial(delta); ! ! if (moveMode != CONTROLLED) { ! move(vertices, delta); ! } else { ! slideMove(vertices, delta); ! } ! from.move(delta.getX(), delta.getY(), delta.getZ()); ! glv.setLength(from.minus(initial).length()); ! update(); ! } else { ! log.warn("could not hit the dragplane"); ! } ! } } ! /** * Corrects a movement vector to make the --- 179,187 ---- */ protected void dragged(MouseEvent e) { ! if (strategy != null) { ! strategy.dragged(e); ! } } ! /** * Corrects a movement vector to make the *************** *** 377,381 **** * @return the corrected movement vector. */ ! private Vertex snapToInitial(Vertex delta) { Vertex parentPos = from.add(delta); Vertex v = initial.minus(parentPos); --- 191,195 ---- * @return the corrected movement vector. */ ! protected Vertex snapToInitial(Vertex delta) { Vertex parentPos = from.add(delta); Vertex v = initial.minus(parentPos); *************** *** 392,675 **** return delta; } - - /** - * Restricts the movement to fit the movement mode, - * given a desired movement vector. - * @param delta the movement vector. - * @param e the mouseEvent for this restriction - * @return the restricted movement vector. - */ - private Vertex restrict(Vertex delta, MouseEvent e) { - if (moveMode == AXIS_RESTRICTED) { - Vertex restrictCopy = restrictionVector.copy(); - restrictCopy.scale(1 / restrictCopy.length()); - restrictCopy.scale(delta.dot(restrictCopy)); - delta = restrictCopy; - } else if (moveMode == FREE_SNAP) { - Object o = glv.getView().getObjectAtPoint(e.getX(), e.getY(), moveEntities, - false, new Plane(0, 0, 1, 0)); - if (o != null) { - snapEntity = (Entity)o; - delta = findInitial(snapEntity, e).minus(from); - } - } else if (moveMode == CONTROLLED) { - Vertex restrictCopy = restrictionVector.copy(); - restrictCopy.scale(1 / restrictCopy.length()); - restrictCopy.scale(delta.dot(restrictCopy)); - delta = restrictCopy; - } - return delta; - } - - /** - * Set up the restriction depending on the - * movement mode. - * @param e a mouse event. - */ - private void findRestrictions(MouseEvent e) { - if (moveMode == AXIS_RESTRICTED) { - if (restrictionVector == null && moveConstructors != null) { - boolean found = findRestrictionVector(e); - if (found) { - findRestrictionPlane(); - } - } - } else if (moveMode == FREE_SNAP) { - if (dragPlane == null && moveConstructors != null) { - - Iterator iter = moveConstructors.iterator(); - Edge firstConst = (Edge)iter.next(); - Edge secondConst = (Edge)iter.next(); - Edge thirdConst = (Edge)iter.next(); - - Surface hitSurface1 = spannedSurface(firstConst, secondConst); - Surface hitSurface2 = spannedSurface(firstConst, thirdConst); - Surface hitSurface3 = spannedSurface(secondConst, thirdConst); - - glv.getView().addTempSurface(hitSurface1); - glv.getView().addTempSurface(hitSurface2); - glv.getView().addTempSurface(hitSurface3); - Object ob = glv.getView().getObjectAtPoint(e.getX(), e.getY()); - if (ob instanceof Surface) { - Surface surface = (Surface)ob; - if (surface.equals(hitSurface1) || - surface.equals(hitSurface2) || - surface.equals(hitSurface3)) { - dragPlane = surface.plane(); - restrictionVector = new Vertex(0, 0, 0); - } - } - glv.getView().removeTempSurface(hitSurface1); - glv.getView().removeTempSurface(hitSurface2); - glv.getView().removeTempSurface(hitSurface3); - clearConstructors(moveConstructors); - } - } else if (moveMode == CONTROLLED && controlled != null) { - if (slideMap.isEmpty()) { - findTarget(e); - if (controlled instanceof Edge) { - Edge controlledEdge = (Edge)controlled; - if (target instanceof Surface) { - Surface targetSurface = (Surface)target; - if (controlledEdge.getSurfaces().contains(targetSurface)) { - glv.getView().makeTarget(target); - dragPlane = targetSurface.plane(); - Vertex controlledDir = controlledEdge.getDirection(); - Vertex surfaceNormal = targetSurface.normal(); - restrictionVector = controlledDir.cross(surfaceNormal); - restrictionVector.scale(1 / restrictionVector.length()); - - List edges = targetSurface.getEdges(); - int index = edges.indexOf(controlledEdge); - Edge leftEdge; - Edge rightEdge; - if (index == 0) { - leftEdge = (Edge)edges.get(edges.size() - 1); - rightEdge = (Edge)edges.get(index + 1); - } else if (index == (edges.size() - 1)) { - leftEdge = (Edge)edges.get(index - 1); - rightEdge = (Edge)edges.get(0); - } else { - leftEdge = (Edge)edges.get(index - 1); - rightEdge = (Edge)edges.get(index + 1); - } - Edge toSlideEdge = null; - Edge fromSlideEdge = null; - if (controlledDir.cross(leftEdge.getDirection()).length() > 0.00001 && - controlledDir.cross(rightEdge.getDirection()).length() > 0.00001) { - Vertex to = controlledEdge.getTo(); - Vertex from = controlledEdge.getFrom(); - if (to.getEdges().contains(leftEdge) && - from.getEdges().contains(rightEdge)) { - toSlideEdge = leftEdge; - fromSlideEdge = rightEdge; - } else if (from.getEdges().contains(leftEdge) && - to.getEdges().contains(rightEdge)) { - fromSlideEdge = leftEdge; - toSlideEdge = rightEdge; - } - slideMap = new HashMap(); - slideMap.put(controlledEdge.getTo(), toSlideEdge.getDirection()); - slideMap.put(controlledEdge.getFrom(), fromSlideEdge.getDirection()); - } else { - dragPlane = null; - restrictionVector = null; - log.warn("This edge is bound"); - } - } - } - } else if (controlled instanceof Vertex) { - Vertex controlledVertex = (Vertex)controlled; - if (target instanceof Edge && - controlledVertex.getEdges().contains((Edge)target)) { - Edge targetEdge = (Edge)target; - slideMap = new HashMap(); - slideMap.put(controlledVertex, targetEdge.getDirection()); - restrictionVector = targetEdge.getDirection(); - findRestrictionPlane(); - } - } else if (controlled instanceof Surface) { - Surface controlledSurface = (Surface)controlled; - restrictionVector = controlledSurface.normal(); - findRestrictionPlane(); - slideMap = new HashMap(); - Collection vertices = controlledSurface.getVertices(); - Iterator it = vertices.iterator(); - boolean bound = false; - while (it.hasNext() && !bound) { - Vertex vertex = (Vertex)it.next(); - slideMap.put(vertex, restrictionVector); - Collection edges = vertex.getEdges(); - Iterator edgeIt = edges.iterator(); - int otherEdges = 0; - while (edgeIt.hasNext() && !bound) { - Edge edge = (Edge)edgeIt.next(); - if (!controlledSurface.getEdges().contains(edge)) { - otherEdges++; - slideMap.put(vertex, edge.getDirection()); - } - bound = otherEdges > 1; - } - } - if (bound) { - slideMap = new HashMap(); - restrictionVector = null; - dragPlane = null; - log.warn("this surface is bound!"); - } - } - } - } - } - - /** - * Creates the surface spanned by two edges connected - * at one point.Used in finding restrictions for the FREE_SNAP mode. - * @param edge1 the first edge. - * @param edge2 the second edge. - * @return the surface. - */ - private Surface spannedSurface(Edge edge1, Edge edge2) { - List edges = new LinkedList(); - - Vertex to1 = edge1.getTo().copy(); - Vertex to2 = edge2.getTo().copy(); - Vertex notConnected1; - Vertex notConnected2; - Vertex connected; - if (Math.abs(to1.getX() - to2.getX()) < 0.0001 && - Math.abs(to1.getY() - to2.getY()) < 0.0001 && - Math.abs(to1.getZ() - to2.getZ()) < 0.0001) { - connected = to1; - notConnected1 = edge1.getFrom().copy(); - notConnected2 = edge2.getFrom().copy(); - } else { - Vertex from2 = edge2.getFrom().copy(); - if (Math.abs(to1.getX() - from2.getX()) < 0.0001 && - Math.abs(to1.getY() - from2.getY()) < 0.0001 && - Math.abs(to1.getZ() - from2.getZ()) < 0.0001) { - connected = to1; - notConnected1 = edge1.getFrom().copy(); - notConnected2 = to2; - } else { - notConnected1 = to1; - connected = edge1.getFrom().copy(); - if (Math.abs(connected.getX() - from2.getX()) < 0.0001 && - Math.abs(connected.getY() - from2.getY()) < 0.0001 && - Math.abs(connected.getZ() - from2.getZ()) < 0.0001) { - notConnected2 = to2; - } else { - notConnected2 = from2; - } - } - } - - edges.add(new Edge(notConnected1, notConnected2)); - edges.add(new Edge(notConnected2, connected)); - edges.add(new Edge(connected, notConnected1)); - return new Surface(edges); - } - - /** - * Finds the current restriction vector. - * There must be some number of moveconstructors find a - * restriction vector. - * @param e the mouse event - * @return wherther or not a vector was found. - */ - private boolean findRestrictionVector(MouseEvent e) { - if (moveConstructors != null) { - List hitEdges = glv.getView().getEdgeAtPoint(e.getX(), e.getY()); - Iterator it = moveConstructors.iterator(); - int numberOfAxis = 0; - Edge axis = null; - while (it.hasNext()) { - Edge tempAxis = (Edge)it.next(); - if (hitEdges.contains(tempAxis)) { - numberOfAxis++; - axis = tempAxis; - } - } - //Only assign a restriction vector when exactly one - //moveconstructor is selected. - if (numberOfAxis == 1) { - restrictionVector = axis.getFrom().minus(axis.getTo()); - lastRestriction = restrictionVector.copy(); - //when a moveconstructor is selected it is the - //only one shown. - clearConstructors(moveConstructors); - moveConstructors = new HashSet(); - moveConstructors.add(axis); - displayConstructors(moveConstructors); - return true; - } - } - return false; - } - - /** - * Sets an appropriate dragPlane that aligned with the - * restrictionvector and containing a specified point to - * make reasonable movements. - */ - protected void findRestrictionPlane() { - Camera camera = Project.getInstance().getCurrentCamera(); - double[] centerPoint = camera.getCenter(); - double[] cameraPoint = camera.getCamera(); - - Vertex eyeVector = new Vertex(cameraPoint[0] - centerPoint[0], - cameraPoint[1] - centerPoint[1], - cameraPoint[2] - centerPoint[2]); - Vertex cross = restrictionVector.cross(eyeVector); - Vertex planeNormal; - if (cross.isZero()) { - double[] rollVector = camera.getRoll(); - planeNormal = new Vertex(rollVector[0], rollVector[1], rollVector[2]); - } else { - planeNormal = (restrictionVector.cross(eyeVector)).cross(restrictionVector); - } - dragPlane = new Plane(planeNormal.getX(), planeNormal.getY(), planeNormal.getZ(), - -planeNormal.dot(initial)); - } /** --- 206,209 ---- *************** *** 678,682 **** * @param delta The movement */ ! private void move(Collection sel, Vertex delta) { Iterator it = sel.iterator(); while (it.hasNext()) { --- 212,216 ---- * @param delta The movement */ ! protected void move(Collection sel, Vertex delta) { Iterator it = sel.iterator(); while (it.hasNext()) { *************** *** 693,731 **** /** - * Moves a set of vertices in a controlled way, so that they slide along - * vectors specified in the slideMap. - * @param vertices The vertices that should be moved. - * @param delta the movement. - */ - private void slideMove(Collection vertices, Vertex delta) { - Vertex deltaUnit = delta.copy(); - deltaUnit.scale(1 / delta.length()); - Iterator it = vertices.iterator(); - while (it.hasNext()) { - Vertex vertex = (Vertex)it.next(); - Vertex slideDir = (Vertex)slideMap.get(vertex); - double slideScale; - if (delta == null) { - log.info("delta null"); - } - if (slideDir == null) { - log.info("slideDir null"); - } - if (deltaUnit == null) { - log.info("deltaUnit null"); - } - if (delta.getX() != 0) { - slideScale = delta.getX() / (slideDir.dot(deltaUnit) * deltaUnit.getX()); - } else if (delta.getY() != 0) { - slideScale = delta.getY() / (slideDir.dot(deltaUnit) * deltaUnit.getY()); - } else { - slideScale = delta.getZ() / (slideDir.dot(deltaUnit) * deltaUnit.getZ()); - } - slideDir.scale(slideScale); - vertex.move(slideDir.getX(), slideDir.getY(), slideDir.getZ()); - } - } - - /** * 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 --- 227,230 ---- *************** *** 737,850 **** */ public void keyPressed(KeyEvent e) { ! if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { ! if (dragPlane != null) { ! dragPlane = null; move(vertices, initial.minus(from)); ! } ! } else if (lastRestriction != null && ! initial != null && ! from != null && ! vertices != null && ! number != null) { ! if (e.getKeyCode() == KeyEvent.VK_1) { ! number += "1"; ! } else if (e.getKeyCode() == KeyEvent.VK_2) { ! number += "2"; ! } else if (e.getKeyCode() == KeyEvent.VK_3) { ! number += "3"; ! } else if (e.getKeyCode() == KeyEvent.VK_4) { ! number += "4"; ! } else if (e.getKeyCode() == KeyEvent.VK_5) { ! number += "5"; ! } else if (e.getKeyCode() == KeyEvent.VK_6) { ! number += "6"; ! } else if (e.getKeyCode() == KeyEvent.VK_7) { ! number += "7"; ! } else if (e.getKeyCode() == KeyEvent.VK_8) { ! number += "8"; ! } else if (e.getKeyCode() == KeyEvent.VK_9) { ! number += "9"; ! } else if (e.getKeyCode() == KeyEvent.VK_0) { ! number += "0"; ! } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { ! if (!number.equals("")) { ! double length = glv.getLength(); ! Vertex delta = initial.minus(from); ! if (delta.length() > 0) { ! delta.scale((delta.length() - length) / delta.length()); ! } else { ! delta = lastRestriction.copy(); ! delta.scale(length / delta.length()); } - move(vertices, delta); - update(); - glv.repaint(); } ! } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { ! int length = number.length(); ! if (length > 0) { ! number = number.substring(0, length - 1); } - } - if (number.equals("") || number.equals("-")) { - glv.setLength(0); } else { ! try { ! double d = Double.parseDouble(number); ! glv.setLength(d / 1000); ! } catch (NumberFormatException exp) { ! System.out.println(exp); ! } } ! } else { ! super.keyPressed(e); } - glv.repaint(true); } ! ! /** ! * sets movemode ! * @param mode the mode ! */ ! public void setMoveMode(int mode) { ! moveMode = mode; ! } ! /** ! * Class for handeling the task of assigning an alignedge. */ ! private class AlignTask extends TimerTask { ! /** The edge candidate to become the alignedge */ ! private Edge candidateEdge; ! ! /** ! * Constructor ! * @param candidate the candidate to become alignedge ! */ ! public AlignTask(Edge candidate) { ! super(); ! candidateEdge = candidate; ! } ! ! /** ! * checks if the target is still the same edge, ! * thus making it the alignedge ! */ ! public void run() { ! if (target instanceof Edge) { ! candidateEdge.equals((Edge)target); ! ! Vertex direction = candidateEdge.getDirection(); ! Vertex alignTo = direction.copy(); ! Vertex alignFrom = direction.copy(); ! alignFrom.scale(-1000); ! alignTo.scale(1000); ! glv.getView().removeTempEdge(alignEdge); ! alignEdge = new Edge(alignFrom.add(from), alignTo.add(from)); ! alignEdge.setConstructor(true); ! glv.getView().addTempEdge(alignEdge); ! glv.repaint(true); ! } ! } } } --- 236,312 ---- */ public void keyPressed(KeyEvent e) { ! if (strategy != null) { ! strategy.keyPressed(e); ! } else { ! if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { move(vertices, initial.minus(from)); ! } else if (lastMoveDirection != null && ! initial != null && ! from != null && ! vertices != null && ! number != null) { ! if (e.getKeyCode() == KeyEvent.VK_1) { ! number += "1"; ! } else if (e.getKeyCode() == KeyEvent.VK_2) { ! number += "2"; ! } else if (e.getKeyCode() == KeyEvent.VK_3) { ! number += "3"; ! } else if (e.getKeyCode() == KeyEvent.VK_4) { ! number += "4"; ! } else if (e.getKeyCode() == KeyEvent.VK_5) { ! number += "5"; ! } else if (e.getKeyCode() == KeyEvent.VK_6) { ! number += "6"; ! } else if (e.getKeyCode() == KeyEvent.VK_7) { ! number += "7"; ! } else if (e.getKeyCode() == KeyEvent.VK_8) { ! number += "8"; ! } else if (e.getKeyCode() == KeyEvent.VK_9) { ! number += "9"; ! } else if (e.getKeyCode() == KeyEvent.VK_0) { ! number += "0"; ! } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { ! if (!number.equals("")) { ! double length = glv.getLength(); ! Vertex delta = initial.minus(from); ! if (delta.length() > 0) { ! delta.scale((delta.length() - length) / delta.length()); ! } else { ! delta = lastMoveDirection.copy(); ! delta.scale(length / delta.length()); ! } ! move(vertices, delta); ! update(); ! glv.repaint(); ! } ! } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { ! int length = number.length(); ! if (length > 0) { ! number = number.substring(0, length - 1); } } ! if (number.equals("") || number.equals("-")) { ! glv.setLength(0); ! } else { ! try { ! double d = Double.parseDouble(number); ! glv.setLength(d / 1000); ! } catch (NumberFormatException exp) { ! System.out.println(exp); ! } } } else { ! super.keyPressed(e); } ! glv.repaint(true); } } ! /** ! * Sets the strategy for the movetool ! * @param strategy the strategy */ ! public void setStrategy(MoveTool strategy) { ! this.strategy = strategy; } } |