Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool TapeMeasureTool.java,1.13,1.14
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2006-04-21 15:08:28
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14183 Modified Files: TapeMeasureTool.java Log Message: rewrite to be a abstractpencil tool Index: TapeMeasureTool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/TapeMeasureTool.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** TapeMeasureTool.java 27 Mar 2006 14:43:48 -0000 1.13 --- TapeMeasureTool.java 21 Apr 2006 15:08:25 -0000 1.14 *************** *** 7,10 **** --- 7,13 ---- package net.sourceforge.bprocessor.gl.tool; + import java.util.LinkedList; + import java.util.Collection; + import java.awt.Cursor; import java.awt.event.MouseEvent; *************** *** 14,22 **** import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Vertex; - import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Plane; - import net.sourceforge.bprocessor.gl.view.Transformation; - import net.sourceforge.bprocessor.gl.view.View; import net.sourceforge.bprocessor.gl.GLView; --- 17,22 ---- *************** *** 26,36 **** * The TapeMeasureTool */ ! public class TapeMeasureTool extends AbstractTool { /** The logger */ private static Logger log = Logger.getLogger(TapeMeasureTool.class); - /** Angle mode on/off */ - private static boolean angleMode = false; - /** The x axis */ private Edge xAxis = new Edge(new Vertex(-50, 0, 0), new Vertex(50, 0, 0)); --- 26,33 ---- * The TapeMeasureTool */ ! public class TapeMeasureTool extends AbstractPencil { /** The logger */ private static Logger log = Logger.getLogger(TapeMeasureTool.class); /** The x axis */ private Edge xAxis = new Edge(new Vertex(-50, 0, 0), new Vertex(50, 0, 0)); *************** *** 48,75 **** private Edge currentConstruction; - /** The edge point */ - private Vertex edgePoint; - /** The construction edge point */ private Vertex constructionPoint; - /** The the edge between the current edge and the construction edge */ - private Edge edgeToConstruction; - - /** The plane we are moving in */ - private Plane movePlane; - - /** The edge normal */ - private Vertex edgeNormal; - - /** The current number in the length field */ - private String number; - - /** Tells if the tool is currently measuring or not */ - private boolean measuring; - - /** Tells if the tool is taking its angel or not */ - private boolean angling; - /** * Constructor --- 45,51 ---- *************** *** 79,84 **** public TapeMeasureTool(GLView glv, Cursor cursor) { super(glv, cursor); - measuring = false; - angling = false; } --- 55,58 ---- *************** *** 88,176 **** */ protected void moved(MouseEvent e) { ! if (currentEdge != null) { ! if (movePlane == null) { ! if (currentEdge.equals(xAxis)) { ! movePlane = new Plane(0, 0, 1, 0); ! edgeNormal = new Vertex(0, 1, 0); ! move(e); ! } else if (currentEdge.equals(yAxis)) { ! movePlane = new Plane(0, 0, 1, 0); ! edgeNormal = new Vertex(1, 0, 0); ! move(e); ! } else if (currentEdge.equals(zAxis)) { ! movePlane = new Plane(0, 1, 0, 0); ! edgeNormal = new Vertex(1, 0, 0); ! move(e); ! } else { ! findTarget(e); ! if (target instanceof Surface) { ! Surface targetSurface = (Surface)target; ! if (currentEdge.getSurfaces().contains(targetSurface)) { ! number = ""; ! glv.getView().makeTarget(target); ! movePlane = targetSurface.plane(); ! Vertex edgeDirection = currentEdge.getDirection(); ! Vertex surfaceNormal = targetSurface.normal(); ! edgeNormal = edgeDirection.cross(surfaceNormal); ! } ! } ! } ! } else if (angling) { ! findTarget(e); ! glv.getView().makeTarget(target); ! moveAngel(e); ! } else if (measuring) { ! findTarget(e); ! glv.getView().makeTarget(target); ! move(e); ! } ! } else { ! findTarget(e); ! glv.getView().makeTarget(target); } } /** - * Moves the angelsetting constructors. - * @param e the mouseevent - */ - private void moveAngel(MouseEvent e) { - Vertex moveTo = glv.getView().toPlaneCoords(new double[]{e.getX(), - e.getY()}, - movePlane); - Vertex delta = moveTo.minus(constructionPoint); - constructionPoint.move(delta.getX(), delta.getY(), delta.getZ()); - double degrees; - Vertex angelDir = edgeToConstruction.getDirection(); - Vertex edgeDir = currentEdge.getDirection(); - degrees = Math.acos((angelDir.dot(edgeDir) / (angelDir.length() * edgeDir.length()))); - degrees *= (180 / Math.PI); - glv.setLength(degrees / 1000); - } - - - /** * moves the construction line. - * @param e the mouseevent. */ ! private void move(MouseEvent e) { ! edgeNormal.scale(1 / edgeNormal.length()); ! Vertex moveVertex = edgeNormal.copy(); ! 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); ! Vertex intersect = movePlane.intersection(ray); ! Vertex delta = intersect.minus(constructionPoint); ! moveVertex.scale(moveVertex.dot(delta) / (moveVertex.length() * ! moveVertex.length())); ! currentConstruction.move(moveVertex.getX(), moveVertex.getY(), moveVertex.getZ()); ! constructionPoint.move(moveVertex.getX(), moveVertex.getY(), moveVertex.getZ()); ! number = ""; ! glv.setLength(edgeToConstruction.getLength()); } --- 62,92 ---- */ protected void moved(MouseEvent e) { ! current = findIntersection(e); ! makeTarget(current); ! if (current != null) { ! move(); } + updateConstructors(); } /** * moves the construction line. */ ! private void move() { ! if (start != null) { ! Vertex direction = currentEdge.getDirection(); ! Vertex origin = start.vertex(); ! Plane plane = new Plane (direction.getX(), direction.getY(), direction.getZ(), ! -(direction.getX() * origin.getX() + ! direction.getY() * origin.getY() + ! direction.getZ() * origin.getZ())); ! Vertex currentPos = plane.project(current.vertex()); ! direction.scale(origin.dot(direction) / (direction.length() * direction.length())); ! currentPos = currentPos.add(direction); ! Vertex move = currentPos.minus(constructionPoint); ! ! currentConstruction.move(move.getX(), move.getY(), move.getZ()); ! constructionPoint.move(move.getX(), move.getY(), move.getZ()); ! } } *************** *** 187,244 **** */ protected void pressed(MouseEvent e) { ! View v = glv.getView(); ! v.addTempEdge(xAxis); ! v.addTempEdge(yAxis); ! v.addTempEdge(zAxis); ! findTarget(e); ! if (!measuring && !angling) { ! if (target instanceof Edge) { ! currentEdge = (Edge)target; ! angling = false; ! measuring = true; Vertex minus = currentEdge.getDirection(); minus.scale(1 / minus.length()); ! Vertex constructionFrom = createVertex(new double[] {(currentEdge.getFrom().getX() + ! minus.getX() * -50), ! (currentEdge.getFrom().getY() + ! minus.getY() * -50), ! (currentEdge.getFrom().getZ() + ! minus.getZ() * -50)}); ! Vertex constructionTo = createVertex(new double[] {(currentEdge.getTo().getX() + ! minus.getX() * 50), ! (currentEdge.getTo().getY() + ! minus.getY() * 50), ! (currentEdge.getTo().getZ() + ! minus.getZ() * 50)}); ! currentConstruction = createEdge(constructionFrom, constructionTo); currentConstruction.setConstructor(true); ! ! double x = e.getX(); ! double y = View.getHeight() - e.getY(); ! 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); ! Edge intersection = currentEdge.intersection(ray); ! edgePoint = intersection.getFrom(); ! constructionPoint = edgePoint.copy(); ! edgeToConstruction = new Edge(edgePoint, constructionPoint); ! edgeToConstruction.setConstructor(true); ! v.addTempEdge(edgeToConstruction); ! v.addTempVertex(edgePoint); ! v.addTempVertex(constructionPoint); } - } else if (angling) { - endAngeling(); } else { endTapeMeasure(); } - v.removeTempEdge(xAxis); - v.removeTempEdge(yAxis); - v.removeTempEdge(zAxis); } /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent --- 103,153 ---- */ protected void pressed(MouseEvent e) { ! if (start == null) { ! if (current != null && current.object() instanceof Edge) { ! start = current; ! currentEdge = (Edge)current.object(); Vertex minus = currentEdge.getDirection(); minus.scale(1 / minus.length()); ! Vertex constructionFrom = new Vertex ((currentEdge.getFrom().getX() + ! minus.getX() * -50), ! (currentEdge.getFrom().getY() + ! minus.getY() * -50), ! (currentEdge.getFrom().getZ() + ! minus.getZ() * -50)); ! Vertex constructionTo = new Vertex((currentEdge.getTo().getX() + ! minus.getX() * 50), ! (currentEdge.getTo().getY() + ! minus.getY() * 50), ! (currentEdge.getTo().getZ() + ! minus.getZ() * 50)); ! currentConstruction = new Edge(constructionFrom, constructionTo); currentConstruction.setConstructor(true); ! constructionPoint = start.vertex().copy(); } } else { + start = null; endTapeMeasure(); } } /** + * Update constructors based on start, current and incident. + */ + protected void updateConstructors() { + if (start != null) { + Collection consts = new LinkedList(); + consts.add(currentConstruction); + Edge edge = new Edge(start.vertex(), constructionPoint); + edge.setConstructor(true); + glv.setLength(edge.getFrom().minus(edge.getTo()).length()); + consts.add(edge); + constructors(consts); + excluded(consts); + } else { + super.updateConstructors(); + } + } + + /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent *************** *** 252,322 **** */ public void keyPressed(KeyEvent e) { - /*a length can only be typed in if some surface is selected - for extrusion, and if the "number"-variable has been initialised - */ - super.keyPressed(e); - if (currentEdge != 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("")) { - if (measuring) { - double length = glv.getLength(); - double currentLength = edgeToConstruction.getLength(); - Vertex minus = edgeToConstruction.getTo().minus(edgeToConstruction.getFrom()); - edgeNormal.scale(1 / edgeNormal.length()); - minus.scale(1 / minus.length()); - double delta; - if (minus.dot(edgeNormal) > 0) { - delta = length - currentLength; - } else { - delta = -(length - currentLength); - } - constructionPoint.move(edgeNormal.getX() * delta, - edgeNormal.getY() * delta, - edgeNormal.getZ() * delta); - currentConstruction.move(edgeNormal.getX() * delta, - edgeNormal.getY() * delta, - edgeNormal.getZ() * delta); - endTapeMeasure(); - } - 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("")) { - glv.setLength(0); - } else { - try { - double d = Double.parseDouble(number); - glv.setLength(d / 1000); - } catch (NumberFormatException exp) { - log.warn(exp); - } - } - } super.keyPressed(e); } --- 161,166 ---- */ public void keyPressed(KeyEvent e) { super.keyPressed(e); + move(); } *************** *** 325,367 **** */ private void endTapeMeasure() { ! if (measuring) { ! View v = glv.getView(); ! v.removeTempEdge(edgeToConstruction); ! v.removeTempVertex(edgePoint); ! v.removeTempVertex(constructionPoint); currentEdge = null; ! currentConstruction = null; ! movePlane = null; ! edgeToConstruction = null; ! Project.getInstance().remove(edgePoint); ! Project.getInstance().remove(constructionPoint); ! measuring = false; Project.getInstance().checkpoint(); } } - /** - * Ends the angeling - */ - private void endAngeling() { - if (movePlane != null) { - Vertex angelDir = edgeToConstruction.getDirection(); - Vertex to = angelDir.copy(); - Vertex from = angelDir.copy(); - to.scale(50); - to = to.add(edgePoint); - from.scale(-50); - from = from.add(edgePoint); - currentConstruction.setTo(to); - currentConstruction.setFrom(from); - Vertex planeNormal = movePlane.normal(); - Vertex constructionDir = currentConstruction.getDirection(); - edgeNormal = constructionDir.cross(planeNormal); - - constructionPoint = edgePoint.copy(); - edgeToConstruction.setTo(constructionPoint); - glv.getView().makeTarget(constructionPoint); - } - angling = false; - measuring = true; - } } --- 169,180 ---- */ private void endTapeMeasure() { ! if (currentConstruction != null) { ! insertEdge(currentConstruction, false); ! createEdge(currentConstruction.getFrom(), currentConstruction.getFrom()); currentEdge = null; ! constructors(new LinkedList()); ! excluded(new LinkedList()); Project.getInstance().checkpoint(); } } } |