[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool AbstractTool.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-26 12:33:48
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13069 Added Files: AbstractTool.java Log Message: Implements common methods for all tools --- NEW FILE: AbstractTool.java --- //--------------------------------------------------------------------------------- // $Id: AbstractTool.java,v 1.1 2005/07/26 12:33:39 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.kernel.notification.Notifier; import net.sourceforge.bprocessor.kernel.notification.Notification; 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 net.sourceforge.bprocessor.gl.view.View; import net.sourceforge.bprocessor.gl.GLView; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.util.Set; import javax.swing.Timer; import org.apache.log4j.Logger; /** * The abstracttool */ public abstract class AbstractTool implements Tool { /** The logger */ private static Logger log = Logger.getLogger(AbstractTool.class); /** The mouse position last time the mouse was pressed initializes to (0,0) */ protected int[] pressPos = new int[2]; /** The mouse movement in x axis since press */ protected int dx = 0; /** The mouse movement in y axis since press */ protected int dy = 0; /** 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 */ protected static final double EPSILON = 0.4; /** The zoom action listener */ private ActionListener zoomAction; /** The move action listener */ private ActionListener moveAction; /** The carmera move timer */ private Timer timer; /** The 3DView */ protected GLView glv = null; /** * KeyListener for the GL Canvas * @param glv The 3D canvas */ public AbstractTool(GLView glv) { this.glv = glv; timer = new Timer(10, null); timer.start(); moveAction = new CameraMoveTimer(this, glv); zoomAction = new CameraZoomTimer(this, glv); } /** * Invoked when a key has been pressed. * @param e The KeyEvent */ public void keyPressed(KeyEvent e) { View v = glv.getView(); if (e.getKeyCode() == KeyEvent.VK_UP) { v.translateCenter(new double[] {0.0, 1.0, 0.0}); } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { v.translateCenter(new double[] {0.0, -1.0, 0.0}); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { v.translateCenter(new double[] {-1.0, 0.0, 0.0}); } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { v.translateCenter(new double[] {1.0, 0.0, 0.0}); } else if (e.getKeyChar() == KeyEvent.VK_COMMA) { v.zoom(1.1); } else if (e.getKeyChar() == KeyEvent.VK_PERIOD) { v.zoom(0.9); } else if (e.getKeyCode() == KeyEvent.VK_Q) { glv.changeTool(Tool.SELECT_TOOL); } else if (e.getKeyCode() == KeyEvent.VK_W) { glv.changeTool(Tool.DRAW_TOOL); } glv.repaint(); } /** * Invoked when a key has been released. * @param e The KeyEvent */ public void keyReleased(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_1) { glv.changeView(View.VIEW_3D); } else if (e.getKeyChar() == KeyEvent.VK_2) { glv.changeView(View.VIEW_XZ); } else if (e.getKeyChar() == KeyEvent.VK_3) { glv.changeView(View.VIEW_XY); } else if (e.getKeyChar() == KeyEvent.VK_4) { glv.changeView(View.VIEW_YZ); } } /** * Invoked when a key has been typed. * @param e The KeyEvent */ public void keyTyped(KeyEvent e) { } /** * Invoked when a mouse button is held down while moving the mouse * @param e The MouseEvent object */ public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (e.getButton() == MouseEvent.BUTTON3) { this.dx = x - pressPos[0]; this.dy = y - pressPos[1]; } dragged(e); glv.repaint(); } /** * Invoked when the mouse cursor has been moved * @param e The MouseEvent object */ public void mouseMoved(MouseEvent e) { moved(e); glv.repaint(); } /** * Invoked when the mouse button has been clicked (pressed and released) on a component. * @param e The MouseEvent object */ public void mouseClicked(MouseEvent e) { } /** * Invoked when the mouse enters a component. * @param e The MouseEvent object */ public void mouseEntered(MouseEvent e) { } /** * Invoked when the mouse exits a component. * @param e The MouseEvent object */ public void mouseExited(MouseEvent e) { } /** * Invoked when a mouse button has been pressed on a component. * @param e The MouseEvent object */ public void mousePressed(MouseEvent e) { int currentButton = e.getButton(); pressPos[0] = e.getX(); pressPos[1] = e.getY(); if (currentButton == MouseEvent.BUTTON2) { timer.addActionListener(zoomAction); } else if (currentButton == MouseEvent.BUTTON3) { timer.addActionListener(moveAction); } pressed(e); glv.repaint(); } /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent */ public void mouseReleased(MouseEvent e) { int currentButton = e.getButton(); if (currentButton == MouseEvent.BUTTON3) { timer.removeActionListener(moveAction); this.dx = 0; this.dy = 0; } else if (currentButton == MouseEvent.BUTTON2) { timer.removeActionListener(zoomAction); this.dx = 0; this.dy = 0; } released(e); glv.repaint(); } /** * @return The x distance the mouse has moved since press */ protected int getdx() { return dx; } /** * @return The y distance the mouse has moved since press */ protected int getdy() { return dy; } /** * Take a coordinate and check if it should snap to grid * @param coords The coordinates to check for snapping * @return the snapped coordinates */ protected 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 * @param coord The coordinates to create with * @return The new Vertex */ protected Vertex createVertex(double[] coord) { if (coord.length == 3) { Vertex v = new Vertex("V" + vertexNum); vertexNum++; VertexFacade.getInstance().add(v); v.setX(coord[0]); v.setY(coord[1]); v.setZ(coord[2]); Notification n = new Notification(Notification.VERTEX_CREATED, v.getId()); Notifier.getInstance().sendNotification(n); return v; } else { log.error("[createVertex] wrong argument length was: " + coord.length); return null; } } /** * Update the coordinates of the vertex v * @param v The vertex to change coordinates on * @param coord The new coordinates */ protected void updateVertex(Vertex v, double[] coord) { if (coord.length == 3) { v.setX(coord[0]); v.setY(coord[1]); v.setZ(coord[2]); Notification n = new Notification(Notification.VERTEX_MODIFIED, v.getId()); Notifier.getInstance().sendNotification(n); } else { log.error("[updateVertex] wrong argument length was: " + coord.length); } } /** * Remove a vertex for good * @param v Vertex to remove */ protected 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 * @param to the to vertex * @param from the from vertex * @return The new Edge */ protected Edge createEdge(Vertex to, Vertex from) { Edge e = new Edge("E" + edgeNum); edgeNum++; EdgeFacade.getInstance().add(e); e.setTo(to); e.setFrom(from); Notification n = new Notification(Notification.EDGE_CREATED, e.getId()); Notifier.getInstance().sendNotification(n); return e; } /** * Updates the edge * @param e The edge to update */ protected void updateEdge(Edge e) { Notification n = new Notification(Notification.EDGE_MODIFIED, e.getId()); Notifier.getInstance().sendNotification(n); } /** * Make and register a new Surface * @param set The set of edges in the surface * @return The new Surface */ protected Surface createSurface(Set set) { Surface s = new Surface("S" + surfaceNum); surfaceNum++; SurfaceFacade.getInstance().add(s); s.setEdges(set); Notification n = new Notification(Notification.SURFACE_CREATED, s.getId()); Notifier.getInstance().sendNotification(n); return s; } /** * Invoked when the mouse cursor has been moved * @param e The MouseEvent object */ protected abstract void moved(MouseEvent e); /** * Invoked when the mouse is held pressed and moved * @param e The MouseEvent object */ protected abstract void dragged(MouseEvent e); /** * Invoked when a mouse button has been pressed on a component. * @param e The MouseEvent object */ protected abstract void pressed(MouseEvent e); /** * Invoked when a mouse button has been released on a component. * @param e The MouseEvent */ protected abstract void released(MouseEvent e); /** * Carama move timer class */ private class CameraMoveTimer implements ActionListener { /** The calling mouselistener */ private AbstractTool at; /** The current GLView */ private GLView glv; /** * The Constructor * @param at the abstract tool * @param glv the GLView */ public CameraMoveTimer(AbstractTool at, GLView glv) { this.at = at; this.glv = glv; } /** * executed every time camaraMove is sceduled * @param e The ActionEvent */ public void actionPerformed(ActionEvent e) { View v = glv.getView(); v.translateRotationX(at.getdy()); v.translateRotationY(at.getdx()); glv.repaint(); } } /** * Camara zoom timer */ private class CameraZoomTimer implements ActionListener { /** The calling mouselistener */ private AbstractTool at; /** The current GLView */ private GLView glv; /** * The Constructor * @param at the abstract tool * @param glv the GLView */ public CameraZoomTimer(AbstractTool at, GLView glv) { this.at = at; this.glv = glv; } /** * executed every time camaraMove is sceduled * @param e The ActionEvent */ public void actionPerformed(ActionEvent e) { View v = glv.getView(); if (at.getdy() > 0) { v.zoom(0.9); } else { v.zoom(1.0); } glv.repaint(); } } } |