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
23     JFrame f;
24     GLCanvas glcanvas;
25     Point clickPoint = null;
26     Point clickClosest = null;
27
28     int mousex;
29     int mousey;
30     float tx = 0;
31     float ty = 0;
32     float tz = 0;
33     float anglex = 0;
34     float angley = 0;
35
36     boolean alt = false;
37     boolean shift = false;
38     boolean control = false;
39
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);
45     }
46
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);
50     }
51
52     public void mouseWheelMoved(MouseWheelEvent e) {
53         tz -= e.getWheelRotation();
54     }
55
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;
62         }
63     }
64
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;
70         }
71     }
72
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();
79     }
80
81     public void mouseReleased(MouseEvent e) {
82         clickPoint = null;
83         clickClosest = null;
84     }
85
86     public void mouseMoved(MouseEvent e) {
87         mousex = e.getX();
88         mousey = e.getY();
89     }
90
91     public void mouseDragged(MouseEvent e) {
92         if (shift) {
93             /*
94             tx += (mousex - e.getX())/(float)20;
95             ty += (mousey - e.getY())/(float)20;
96             */
97             if (closest != null && projection != null && clickClosest != null) {
98                 synchronized(this) {
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);
104                 }
105             }
106         } else {
107             anglex -= mousex - e.getX();
108             angley += mousey - e.getY();
109         }
110         mousex = e.getX();
111         mousey = e.getY();
112     }
113
114     public Viewer(JFrame f) {
115         this.f = 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);
123     }
124
125     public void repaint() {
126         glcanvas.repaint();
127     }
128 }