checkpoint
[slipway.git] / src / edu / berkeley / slipway / gui / Gui3.java
1 package edu.berkeley.slipway.gui;
2
3 import com.atmel.fpslic.*;
4 import edu.berkeley.slipway.*;
5 import static com.atmel.fpslic.FpslicConstants.*;
6 import static com.atmel.fpslic.FpslicUtil.*;
7 import edu.berkeley.slipway.*;
8 import java.awt.*;
9 import java.awt.geom.*;
10 import java.awt.event.*;
11 import java.awt.color.*;
12 import org.ibex.util.*;
13 import java.io.*;
14 import java.util.*;
15 import javax.swing.*;
16 import static edu.berkeley.slipway.gui.GuiConstants.*;
17
18 public class Gui3 extends Canvas implements MouseWheelListener, MouseMotionListener {
19
20     Fpslic at40k;
21     FtdiBoard drone;
22
23     private int width;
24     private int height;
25     private int magnify = 0;
26     private GuiCell[][] ca = new GuiCell[128][];
27     private FtdiBoard ftdiboard;
28     public Gui3(Fpslic at40k, FtdiBoard drone) {
29         this(at40k, drone, 24, 24);
30     }
31     public Gui3(Fpslic at40k, FtdiBoard drone, int width, int height) {
32         this.at40k = at40k;
33         this.drone = drone;
34         this.width = width;
35         this.height = height;
36         for(int i=0; i<ca.length; i++)
37             ca[i] = new GuiCell[128];
38         for(int x=0; x<width; x++)
39             for(int y=0; y<height; y++)
40                 ca[x][y] = new GuiCell(at40k.cell(x, y));
41         addMouseWheelListener(this);
42         addMouseMotionListener(this);
43     }
44
45     public void mouseWheelMoved(MouseWheelEvent e) {
46         magnify -= e.getWheelRotation();
47         repaint();
48     }
49
50     public void _paint(Graphics2D g_) {
51         int SIZE = 100;
52         //g_.setStroke(new BasicStroke((float)1.0/SIZE));
53         g_.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
54         g_.setRenderingHint(RenderingHints.KEY_RENDERING,    RenderingHints.VALUE_RENDER_QUALITY);
55         G g = new G(g_);
56         AffineTransform at = new AffineTransform();
57         at.translate(getWidth()/2, getHeight()/2);
58         at.scale(1, -1);
59         double mag = magnify;
60         if (magnify > 0) {
61             mag -= 1;
62             mag /= 5;
63             mag += 1;
64             at.scale(mag, mag);
65         } else if (magnify < 0) {
66             mag += 1;
67             mag /= 5;
68             mag -= 1;
69             at.scale(-1.0/mag, -1.0/mag);
70         }
71         at.translate(-(width*SIZE)/4, -(height*SIZE)/4);
72         g.g.transform(at);
73         for(int x=0; x<ca.length; x++)
74             for(int y=0; y<ca[x].length; y++) {
75                 R r = new R(SIZE*x,     SIZE*y,
76                             SIZE*(x+1), SIZE*(y+1));
77                 if (ca[x][y] != null) {
78                     g.color(GuiCell.BORDER_COLOR);
79                     r.draw(g);
80                     repaint();
81                 }
82             }
83         P mouse = new P(mousex, mousey);
84         mouse = mouse.inverseTransform(at);
85         for(int x=0; x<ca.length; x++)
86             for(int y=0; y<ca[x].length; y++) {
87                 R r = new R(SIZE*x,     SIZE*y,
88                             SIZE*(x+1), SIZE*(y+1));
89                 if (ca[x][y] != null)
90                     ca[x][y].draw(g, r, r.contains(mouse));
91             }
92     }
93
94     public void paint(Graphics g) { _paint((Graphics2D)g); }
95     public void mouseDragged(MouseEvent e) { mouseMoved(e); }
96     public void mouseMoved(MouseEvent e) {
97         mousex = e.getX();
98         mousey = e.getY();
99     }
100     int mousex;
101     int mousey;
102 }