1 package edu.berkeley.qfat;
5 import java.awt.event.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
14 public abstract class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
16 Mesh.Vertex closest = null;
17 Matrix projection = null;
19 public Mesh tile = new Mesh(false);
20 public Mesh goal = new Mesh(false);
25 Point clickPoint = null;
26 Point clickClosest = null;
37 boolean shift = false;
38 boolean control = false;
40 public abstract void _display(GLAutoDrawable drawable, GL gl);
41 public void display(GLAutoDrawable drawable) {
42 GL gl = drawable.getGL();
43 _display(drawable, gl);
44 this.projection = Matrix.getProjectionMatrix(gl);
47 /** return the position of the mouse as a point in window-space */
48 public Point getMouse() {
49 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
52 public void mouseWheelMoved(MouseWheelEvent e) {
53 tz -= e.getWheelRotation();
56 public void keyTyped(KeyEvent e) { }
57 public void keyPressed(KeyEvent e) {
58 switch(e.getKeyCode()) {
59 case KeyEvent.VK_CONTROL: control = true; break;
60 case KeyEvent.VK_ALT: alt = true; break;
61 case KeyEvent.VK_SHIFT: shift = true; break;
65 public void keyReleased(KeyEvent e) {
66 switch(e.getKeyCode()) {
67 case KeyEvent.VK_CONTROL: control = false; break;
68 case KeyEvent.VK_ALT: alt = false; break;
69 case KeyEvent.VK_SHIFT: shift = false; break;
73 public void mouseClicked(MouseEvent e) { }
74 public void mouseEntered(MouseEvent e) { }
75 public void mouseExited(MouseEvent e) { }
76 public void mousePressed(MouseEvent e) {
77 clickPoint = getMouse();
78 clickClosest = closest == null ? null : closest.getPoint();
81 public void mouseReleased(MouseEvent e) {
86 public void mouseMoved(MouseEvent e) {
91 public void mouseDragged(MouseEvent e) {
94 tx += (mousex - e.getX())/(float)20;
95 ty += (mousey - e.getY())/(float)20;
97 if (closest != null && projection != null && clickClosest != null) {
99 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
100 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
101 //System.out.println(delta + " " + closest.getPoint());
102 System.out.println(getMouse().minus(clickPoint));
103 closest.move(Matrix.translate(delta), true);
107 anglex -= mousex - e.getX();
108 angley += mousey - e.getY();
114 public Viewer(JFrame f) {
116 glcanvas = new GLCanvas();
117 glcanvas.addGLEventListener(this);
118 f.add(glcanvas, BorderLayout.CENTER);
119 glcanvas.addMouseListener(this);
120 glcanvas.addMouseMotionListener(this);
121 glcanvas.addMouseWheelListener(this);
122 glcanvas.addKeyListener(this);
125 public void repaint() {