checkpoint
[anneal.git] / src / edu / berkeley / qfat / Viewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public abstract class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
15
16     Mesh.Vertex closest = null;
17     Matrix projection = null;
18
19     public Mesh tile = new Mesh(false);
20     public Mesh goal = new Mesh(false);
21
22     JFrame f;
23     GLCanvas glcanvas;
24     Point clickPoint = null;
25     Point clickClosest = null;
26
27     int mousex;
28     int mousey;
29     float tx = 0;
30     float ty = 0;
31     float tz = 0;
32     float anglex = 0;
33     float angley = 0;
34
35     boolean alt = false;
36     boolean shift = false;
37     boolean control = false;
38
39     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
40     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
41
42     public abstract void _display(GLAutoDrawable drawable, GL gl);
43     public void display(GLAutoDrawable drawable) {
44         GL gl = drawable.getGL();
45         _display(drawable, gl);
46         this.projection = Matrix.getProjectionMatrix(gl);
47     }
48
49     /** return the position of the mouse as a point in window-space */
50     public Point getMouse() {
51         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
52     }
53
54     public void mouseWheelMoved(MouseWheelEvent e) {
55         tz -= e.getWheelRotation();
56     }
57
58     public void keyTyped(KeyEvent e)  { }
59     public void keyPressed(KeyEvent e)  {
60         switch(e.getKeyCode()) {
61             case KeyEvent.VK_CONTROL: control = true; break;
62             case KeyEvent.VK_ALT: alt = true; break;
63             case KeyEvent.VK_SHIFT: shift = true; break;
64         }
65     }
66
67     public void keyReleased(KeyEvent e) {
68         switch(e.getKeyCode()) {
69             case KeyEvent.VK_CONTROL: control = false; break;
70             case KeyEvent.VK_ALT: alt = false; break;
71             case KeyEvent.VK_SHIFT: shift = false; break;
72         }
73     }
74
75     public void mouseClicked(MouseEvent e) { }
76     public void mouseEntered(MouseEvent e) { }
77     public void mouseExited(MouseEvent e) { }
78     public void mousePressed(MouseEvent e) {
79         clickPoint = getMouse();
80         clickClosest = closest == null ? null : closest.getPoint();
81     }
82
83     public void mouseReleased(MouseEvent e) {
84         clickPoint = null;
85         clickClosest = null;
86     }
87
88     public void mouseMoved(MouseEvent e) {
89         mousex = e.getX();
90         mousey = e.getY();
91     }
92
93     public void mouseDragged(MouseEvent e) {
94         if (shift) {
95             /*
96             tx += (mousex - e.getX())/(float)20;
97             ty += (mousey - e.getY())/(float)20;
98             */
99             if (closest != null && projection != null && clickClosest != null) {
100                 synchronized(this) {
101                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
102                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
103                     //System.out.println(delta + " " + closest.getPoint());
104                     System.out.println(getMouse().minus(clickPoint));
105                     closest.move(Matrix.translate(delta), true);
106                 }
107             }
108         } else {
109             anglex -= mousex - e.getX();
110             angley += mousey - e.getY();
111         }
112         mousex = e.getX();
113         mousey = e.getY();
114     }
115
116     public Viewer(JFrame f) {
117         this.f = f;
118         glcanvas = new GLCanvas();
119         glcanvas.addGLEventListener(this);
120         f.add(glcanvas, BorderLayout.CENTER);
121         glcanvas.addMouseListener(this);
122         glcanvas.addMouseMotionListener(this);
123         glcanvas.addMouseWheelListener(this);
124         glcanvas.addKeyListener(this);
125     }
126
127     public void repaint() {
128         glcanvas.repaint();
129     }
130 }