[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool DrawTool.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-26 12:35:55
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13420 Added Files: DrawTool.java Log Message: Implements tools for surface drawing --- NEW FILE: DrawTool.java --- //--------------------------------------------------------------------------------- // $Id: DrawTool.java,v 1.1 2005/07/26 12:35:44 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.model.Edge; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; import net.sourceforge.bprocessor.model.VertexFacade; import net.sourceforge.bprocessor.gl.view.View; import net.sourceforge.bprocessor.gl.GLView; import java.awt.event.MouseEvent; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.log4j.Logger; /** * The abstracttool */ public class DrawTool extends AbstractTool { /** The logger */ private static Logger log = Logger.getLogger(DrawTool.class); /** The current Edge list */ private Set edges; /** * KeyListener for the GL Canvas * @param glv The 3D canvas */ public DrawTool(GLView glv) { super(glv); edges = new HashSet(); } /** * Invoked when the mouse cursor has been moved * @param e The MouseEvent object */ protected void moved(MouseEvent e) { int x = e.getX(); int y = e.getY(); View v = glv.getView(); double[] coord = v.toCanvasCoords(new double[] {x, y}); coord = snapToGrid(coord); // snap to vertexes coord = aligned(coord); //check for snap point change checkVertexSnap(coord); //change the activeEdge according to mouse position changeTo(coord); } /** * Invoked when the mouse is held pressed and moved * @param e The MouseEvent object */ protected void dragged(MouseEvent e) { } /** * Invoked when a mouse button has been pressed on a component. * @param e The MouseEvent object */ protected void pressed(MouseEvent e) { int currentButton = e.getButton(); View v = glv.getView(); double[] coords = v.toCanvasCoords(new double[]{pressPos[0], pressPos[1]}); // snap to grid coords = snapToGrid(coords); // snap to vertex coords = aligned(coords); if (log.isDebugEnabled()) { log.debug("[mousePressed] " + currentButton + "coords(" + coords[0] + ", " + coords[1] + ", " + coords[2] + ")"); } if (e.getClickCount() >= 2) { if (currentButton == MouseEvent.BUTTON1) { // double click on Button1 => surface end endSurface(coords); } } else { // we did not double click if (currentButton == MouseEvent.BUTTON1) { addEdge(coords); } } removeVertexSnap(); } /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent */ protected void released(MouseEvent e) { } /** * Add the last edge to the edgelist => surface * @param coord The end coordinate */ protected void endSurface(double[] coord) { Edge activeEdge = glv.getView().getActiveEdge(); if (activeEdge != null) { Vertex to = vertexCollide(coord); if (to != null) { // we have to change excisting surface removeVertex(activeEdge.getTo()); activeEdge.setTo(to); updateEdge(activeEdge); } else { to = activeEdge.getTo(); updateVertex(to, coord); } Surface s = createSurface(edges); glv.getView().setAlignVertex(null); glv.getView().setActiveEdge(null); edges = new HashSet(); } } /** * Add an edge to the parent edgelist * @param coord The to point */ protected void addEdge(double[] coord) { if (edges.isEmpty()) { Vertex from = createVertex(coord); Vertex to = createVertex(coord); Edge activeEdge = createEdge(to, from); glv.getView().setActiveEdge(activeEdge); edges.add(activeEdge); glv.getView().setAlignVertex(null); } else { if (vertexCollide(coord) == null) { // we did not end an edgelist Edge activeEdge = glv.getView().getActiveEdge(); Vertex from = activeEdge.getTo(); Vertex to = createVertex(coord); activeEdge = createEdge(to, from); glv.getView().setActiveEdge(activeEdge); edges.add(activeEdge); glv.getView().setAlignVertex(null); } else { // we did end a edgelist endSurface(coord); } } } /** * Change the to vertex coordinates for the active edge * @param coord The new coordinate */ protected void changeTo(double[] coord) { Edge activeEdge = glv.getView().getActiveEdge(); if (activeEdge != null && !edges.isEmpty()) { Vertex v = activeEdge.getTo(); updateVertex(v, coord); } } /** * Check if this cordinate collide with another vertex * @param coord The cordinate to check for collision at * @return The excisting vertex at coord, null if there ain't any */ protected Vertex vertexCollide(double[] coord) { Set vertexes = VertexFacade.getInstance().findAll(); Iterator it = vertexes.iterator(); Edge e = glv.getView().getActiveEdge(); while (it.hasNext()) { Vertex v = (Vertex)it.next(); if (collide(v.getX(), coord[0]) && collide(v.getY(), coord[1]) && collide(v.getZ(), coord[2]) && e != null && v != e.getTo() && v != e.getFrom()) { return v; } } return null; } /** * Check if a and b is as close as EPSILON * @param a The target * @param b The value * @return true if |a - b| < EPSILON false otherwise */ protected boolean collide(double a, double b) { return Math.abs(a - b) < EPSILON; } /** * Check if the current point is a vertex if so, it registers the vertex as * snapVertex and alignVertex in the AbstractListener. * @param coord the cordinate to check for snap (x, y, z) */ protected void checkVertexSnap(double[] coord) { Vertex v = vertexCollide(coord); View view = glv.getView(); Edge e = view.getActiveEdge(); if (v != null && v != view.getActiveEdge().getTo() && v != view.getActiveEdge().getFrom()) { view.setAlignVertex(v); view.setSnapVertex(v); } else { removeVertexSnap(); } } /** * Set the snapVertex variable to null */ protected void removeVertexSnap() { glv.getView().setSnapVertex(null); } /** * Check if v is aligned horizontally or vertically with alignVertex * @param coord The coordinate to check for alignment with * @return The alignmentpoint, if there ain't any the original point is returned */ protected double[] aligned(double[] coord) { double[] res = new double[] {coord[0], coord[1], coord[2]}; Vertex v = glv.getView().getAlignVertex(); if (v != null) { if (Math.abs(coord[0] - v.getX()) < EPSILON) { res[0] = v.getX(); } if (Math.abs(coord[1] - v.getY()) < EPSILON) { res[1] = v.getY(); } if (Math.abs(coord[2] - v.getZ()) < EPSILON) { res[2] = v.getZ(); } if (res[0] != coord[0] && res[1] != coord[1] && res[2] != coord[2]) { glv.getView().setAlignPoint(null); return coord; } else { glv.getView().setAlignPoint(res); return res; } } glv.getView().setAlignPoint(null); return coord; } } |