[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl GLView.java,1.3,1.4
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-26 12:48:32
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16916 Modified Files: GLView.java Log Message: funtionality refactored to tools. Now it only binds the GL together Index: GLView.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/GLView.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** GLView.java 22 Jul 2005 11:25:57 -0000 1.3 --- GLView.java 26 Jul 2005 12:48:23 -0000 1.4 *************** *** 7,27 **** package net.sourceforge.bprocessor.gl; ! import net.sourceforge.bprocessor.kernel.notification.Notification; ! import net.sourceforge.bprocessor.kernel.notification.Notifier; import net.sourceforge.bprocessor.gui.GUI; - import net.sourceforge.bprocessor.model.Edge; - import net.sourceforge.bprocessor.model.EdgeFacade; - import net.sourceforge.bprocessor.model.Surface; - import net.sourceforge.bprocessor.model.SurfaceFacade; - import net.sourceforge.bprocessor.model.Vertex; - import net.sourceforge.bprocessor.model.VertexFacade; - import java.util.EventListener; - import java.util.HashSet; - import java.util.Iterator; - import java.util.Set; - import java.awt.event.MouseListener; - import java.awt.event.MouseMotionListener; - import java.awt.event.MouseWheelListener; import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; --- 7,16 ---- package net.sourceforge.bprocessor.gl; ! import net.sourceforge.bprocessor.gl.tool.Tool; ! import net.sourceforge.bprocessor.gl.tool.ToolFactory; ! import net.sourceforge.bprocessor.gl.view.View; ! import net.sourceforge.bprocessor.gl.view.ViewFactory; import net.sourceforge.bprocessor.gui.GUI; import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; *************** *** 37,84 **** private static Logger log = Logger.getLogger(GLView.class); - /** An vertex counter */ - private static long vertexNum = 0; - - /** An edge counter */ - private static long edgeNum = 0; - - /** An surface counter */ - private static long surfaceNum = 0; - - /** The snap variable */ - public static final double EPSILON = 0.4; - - /** The 3D view mode */ - protected static final int MODE_3D = 0; - - /** The x z plane view */ - protected static final int MODE_XZ = 1; - - /** The x y plane view */ - protected static final int MODE_XY = 2; - - /** The y z plane view */ - protected static final int MODE_YZ = 3; - - /** The 3D listener*/ - private static AbstractViewListener view3D; - - /**The XY listener */ - private static AbstractViewListener viewXY; - - /** The XZ listener */ - private static AbstractViewListener viewXZ; - - /** The */ - private static AbstractViewListener viewYZ; - /** GL canvas */ private GLCanvas glc; /** current event listener */ ! private AbstractViewListener currentListener; ! /** The current Edge list */ ! private Set edges; /** --- 26,37 ---- private static Logger log = Logger.getLogger(GLView.class); /** GL canvas */ private GLCanvas glc; /** current event listener */ ! private View view; ! /** current working tool */ ! private Tool tool; /** *************** *** 89,107 **** glc = GLDrawableFactory.getFactory().createGLCanvas(glCap); ! currentListener = new ViewYZListener(); ! glc.addGLEventListener(currentListener); ! ! view3D = new View3DListener(); ! viewXY = new ViewXYListener(); ! viewXZ = new ViewXZListener(); ! viewYZ = new ViewYZListener(); ! ! EventListener mil = new GLMouseListener(this); ! glc.addMouseListener((MouseListener)mil); ! glc.addMouseMotionListener((MouseMotionListener)mil); ! glc.addMouseWheelListener((MouseWheelListener)mil); ! glc.addKeyListener(new GLKeyListener(this)); ! edges = new HashSet(); GUI.getInstance().registerPanel(glc, GUI.SPLIT_MIDDLE); --- 42,52 ---- glc = GLDrawableFactory.getFactory().createGLCanvas(glCap); ! view = ViewFactory.getFactory().getDefault(); ! glc.addGLEventListener(view); ! tool = ToolFactory.getFactory(this).getDefault(); ! glc.addMouseListener(tool); ! glc.addMouseMotionListener(tool); ! glc.addKeyListener(tool); GUI.getInstance().registerPanel(glc, GUI.SPLIT_MIDDLE); *************** *** 109,352 **** /** ! * Change the view ! * @param mode The view mode ! */ ! public void changeView(int mode) { ! glc.removeGLEventListener(currentListener); ! ! if (mode == MODE_3D) { ! currentListener = view3D; ! } else if (mode == MODE_XZ) { ! currentListener = viewXZ; ! } else if (mode == MODE_XY) { ! currentListener = viewXY; ! } else if (mode == MODE_YZ) { ! currentListener = viewYZ; ! } else { ! log.error("[changeView] mode non-existing"); ! } ! glc.addGLEventListener(currentListener); ! this.repaint(); ! } ! ! /** ! * Repaint the canvas ! */ ! public void repaint() { ! glc.getGL().glFlush(); ! glc.repaint(); ! } ! ! /** ! * Add the last edge to the edgelist => surface ! * @param coord The end coordinate ! */ ! protected void endSurface(double[] coord) { ! Edge activeEdge = currentListener.getActiveEdge(); ! Vertex to = vertexCollide(coord); ! if (to != null) { ! // we have to change excisting surface ! removeVertex(activeEdge.getTo()); ! activeEdge.setTo(to); ! } else { ! activeEdge.getTo().setX(coord[0]); ! activeEdge.getTo().setY(coord[1]); ! activeEdge.getTo().setZ(coord[2]); ! } ! Surface s = surface(); ! s.setEdges(edges); ! currentListener.setAlignVertex(null); ! edges = new HashSet(); ! currentListener.setActiveEdge(null); ! } ! ! /** ! * Add an edge to the parent edgelist ! * @param coord The to point */ ! protected void addEdge(double[] coord) { ! if (edges.isEmpty()) { ! Edge activeEdge = edge(); ! currentListener.setActiveEdge(activeEdge); ! Vertex from = vertex(); ! from.setX(coord[0]); ! from.setY(coord[1]); ! from.setZ(coord[2]); ! activeEdge.setFrom(from); ! Vertex to = vertex(); ! to.setX(coord[0]); ! to.setY(coord[1]); ! to.setZ(coord[2]); ! activeEdge.setTo(to); ! edges.add(activeEdge); ! currentListener.setAlignVertex(null); } else { ! if (vertexCollide(coord) == null) { ! // we did not end an edgelist ! Edge activeEdge = currentListener.getActiveEdge(); ! Vertex from = activeEdge.getTo(); ! activeEdge = edge(); ! currentListener.setActiveEdge(activeEdge); ! activeEdge.setFrom(from); ! Vertex to = vertex(); ! to.setX(coord[0]); ! to.setY(coord[1]); ! to.setZ(coord[2]); ! activeEdge.setTo(to); ! edges.add(activeEdge); ! currentListener.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 = currentListener.getActiveEdge(); ! if (activeEdge != null && !edges.isEmpty()) { ! activeEdge.getTo().setX(coord[0]); ! activeEdge.getTo().setY(coord[1]); ! activeEdge.getTo().setZ(coord[2]); ! } ! } ! ! /** ! * 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); ! if (v != null) { ! currentListener.setAlignVertex(v); ! currentListener.setSnapVertex(v); } else { ! removeVertexSnap(); ! } ! } ! ! /** ! * Set the snapVertex variable to null ! */ ! protected void removeVertexSnap() { ! currentListener.setSnapVertex(null); ! } ! ! /** ! * 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 ! */ ! private Vertex vertexCollide(double[] coord) { ! Set vertexes = VertexFacade.getInstance().findAll(); ! Iterator it = vertexes.iterator(); ! while (it.hasNext()) { ! Vertex v = (Vertex)it.next(); ! if (collide(v.getX(), coord[0]) && ! collide(v.getY(), coord[1]) && ! collide(v.getZ(), coord[2]) && ! currentListener.getActiveEdge() != null && ! currentListener.getActiveEdge().getTo() != v && ! currentListener.getActiveEdge().getFrom() != v) { ! 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 ! */ ! private boolean collide(double a, double b) { ! return Math.abs(a - b) < EPSILON; ! } ! ! /** ! * Take a coordinate and check if it should snap to grid ! * @param coords The coordinates to check for snapping ! * @return the snapped coordinates ! */ ! public double[] snapToGrid(double[] coords) { ! if (Math.abs(coords[0] - Math.round(coords[0])) < EPSILON) { ! coords[0] = Math.round(coords[0]); ! } ! if (Math.abs(coords[1] - Math.round(coords[1])) < EPSILON) { ! coords[1] = Math.round(coords[1]); ! } ! if (Math.abs(coords[2] - Math.round(coords[2])) < EPSILON) { ! coords[2] = Math.round(coords[2]); } ! return coords; ! } ! ! /** ! * Make and register a new vertex ! * @return The new Vertex ! */ ! private Vertex vertex() { ! Vertex v = new Vertex("V" + vertexNum); ! vertexNum++; ! VertexFacade.getInstance().add(v); ! ! Notification n = new Notification(Notification.VERTEX_CREATED, v.getId()); ! Notifier.getInstance().sendNotification(n); ! ! return v; ! } ! ! /** ! * Remove a vertex for good ! * @param v Vertex to remove ! */ ! private void removeVertex(Vertex v) { ! VertexFacade.getInstance().remove(v); ! ! Notification n = new Notification(Notification.VERTEX_DELETED, v.getId()); ! Notifier.getInstance().sendNotification(n); } /** ! * Make and register a new edge ! * @return The new Edge */ ! private Edge edge() { ! Edge e = new Edge("E" + edgeNum); ! edgeNum++; ! EdgeFacade.getInstance().add(e); ! ! Notification n = new Notification(Notification.EDGE_CREATED, e.getId()); ! Notifier.getInstance().sendNotification(n); ! ! return e; } /** ! * Make and register a new Surface ! * @return The new Surface */ ! private Surface surface() { ! Surface s = new Surface("S" + surfaceNum); ! surfaceNum++; ! SurfaceFacade.getInstance().add(s); ! ! Notification n = new Notification(Notification.SURFACE_CREATED, s.getId()); ! Notifier.getInstance().sendNotification(n); ! return s; } /** ! * Return the current used GLEventListener ! * @return The current GLEventListener */ ! public AbstractViewListener getEventListener() { ! return currentListener; } } --- 54,112 ---- /** ! * Change the tool ! * @param mode The new tool */ ! public void changeTool(int mode) { ! glc.removeMouseListener(tool); ! glc.removeMouseMotionListener(tool); ! glc.removeKeyListener(tool); ! tool = ToolFactory.getFactory(this).get(mode); ! if (tool != null) { ! glc.addMouseListener(tool); ! glc.addMouseMotionListener(tool); ! glc.addKeyListener(tool); } else { ! log.error("[changeMode] tool was null"); } } /** ! * Change the view ! * @param v The view */ ! public void changeView(int v) { ! glc.removeGLEventListener(view); ! view = ViewFactory.getFactory().get(v); ! if (view != null) { ! glc.addGLEventListener(view); } else { ! log.error("[changeView] View was null shifts to default"); ! view = ViewFactory.getFactory().getDefault(); ! glc.addGLEventListener(view); } ! this.repaint(); } /** ! * Repaint the canvas */ ! public void repaint() { ! glc.repaint(); } /** ! * Return the current used view ! * @return The current view */ ! public View getView() { ! return view; } /** ! * return the current tool in use ! * @return the tool */ ! public Tool getTool() { ! return tool; } } |