c24ec1d37cd8bf2b300a66f92efb2ced5f1a23b2
[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 static java.awt.event.KeyEvent.*;
8 import edu.berkeley.slipway.*;
9 import java.awt.*;
10 import java.awt.geom.*;
11 import java.awt.event.*;
12 import java.awt.color.*;
13 import org.ibex.util.*;
14 import java.io.*;
15 import java.util.*;
16 import javax.swing.*;
17 import static edu.berkeley.slipway.gui.GuiConstants.*;
18
19 public class Gui3 extends Canvas implements MouseWheelListener, MouseMotionListener, KeyListener {
20
21     Fpslic at40k;
22     FtdiBoard drone;
23
24     private int width;
25     private int height;
26     private int magnify = 0;
27     public GuiCell[][] ca = new GuiCell[128][];
28     private FtdiBoard ftdiboard;
29     public Gui3(Fpslic at40k, FtdiBoard drone) {
30         this(at40k, drone, 24, 24);
31     }
32     public Gui3(Fpslic at40k, FtdiBoard drone, int width, int height) {
33         this.at40k = at40k;
34         this.drone = drone;
35         this.width = width;
36         this.height = height;
37         for(int i=0; i<ca.length; i++)
38             ca[i] = new GuiCell[128];
39         for(int x=0; x<width; x++)
40             for(int y=0; y<height; y++)
41                 ca[x][y] = new GuiCell(at40k.cell(x, y));
42         addMouseWheelListener(this);
43         addMouseMotionListener(this);
44         addKeyListener(this);
45     }
46
47     public void mouseWheelMoved(MouseWheelEvent e) {
48         magnify -= e.getWheelRotation();
49         repaint();
50     }
51
52     Fpslic.Cell selectedCell = null;
53     public void _paint(Graphics2D g_) {
54         int SIZE = 100;
55         //g_.setStroke(new BasicStroke((float)1.0/SIZE));
56         g_.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
57         g_.setRenderingHint(RenderingHints.KEY_RENDERING,    RenderingHints.VALUE_RENDER_QUALITY);
58         G g = new G(g_);
59         g.pushTransform();
60         AffineTransform at = new AffineTransform();
61         at.translate(getWidth()/2, getHeight()/2);
62         at.scale(1, -1);
63         double mag = magnify;
64         if (magnify > 0) {
65             mag -= 1;
66             mag /= 5;
67             mag += 1;
68             at.scale(mag, mag);
69         } else if (magnify < 0) {
70             mag += 1;
71             mag /= 5;
72             mag -= 1;
73             at.scale(-1.0/mag, -1.0/mag);
74         }
75         at.translate(-(width*SIZE)/4, -(height*SIZE)/4);
76         g.g.transform(at);
77         for(int x=0; x<ca.length; x++)
78             for(int y=0; y<ca[x].length; y++) {
79                 R r = new R(SIZE*x,     SIZE*y,
80                             SIZE*(x+1), SIZE*(y+1));
81                 if (ca[x][y] != null) {
82                     g.color(GuiCell.BORDER_COLOR);
83                     r.draw(g);
84                     repaint();
85                 }
86             }
87         P mouse = new P(mousex, mousey);
88         mouse = mouse.inverseTransform(at);
89         selectedCell = null;
90         for(int x=0; x<ca.length; x++)
91             for(int y=0; y<ca[x].length; y++) {
92                 R r = new R(SIZE*x,     SIZE*y,
93                             SIZE*(x+1), SIZE*(y+1));
94                 if (ca[x][y] != null) {
95                     if (r.contains(mouse)) selectedCell = ca[x][y].fpslicCell;
96                     ca[x][y].draw(g, r, r.contains(mouse));
97                 }
98             }
99         at = g.getTransform();
100         g.popTransform();
101
102         R statusArea = new R(0, getHeight() - 150, getWidth(), getHeight());
103         g.color(0x0);
104         statusArea.fill(g);
105
106         double keyboardRatio = ((double)keyboard1.getWidth(null)) / ((double)keyboard1.getHeight(null));
107         g.g.drawImage(keyboard1,
108                       (int)statusArea.minx(),
109                       (int)statusArea.miny(),
110                       (int)((keyboardRatio * 150)),
111                       (int)(150),
112                       null);
113
114         statusArea = statusArea.plus(keyboardRatio * 150 + 10, 0, 0, 0);
115         Inspector.draw(g, statusArea, selectedCell);
116
117         // map
118         R map = new R(getWidth() - 150, getHeight() - 150, getWidth(), getHeight());
119         map = map.plus(5, 5, -5, -5);
120         double mapw = map.width() / width;
121         double maph = map.height() / height;
122         P p1 = new P(0, 0).inverseTransform(at);
123         P p2 = new P(getWidth(), getHeight()-150).inverseTransform(at);
124         p1 = p1.scale(map.width() / (SIZE*width));
125         p2 = p2.scale(map.width() / (SIZE*width));
126         R rv = new R(map.minx() + p1.x,
127                      map.maxy() - p1.y,
128                      map.minx() + p2.x,
129                      map.maxy() - p2.y);
130         for(int x=0; x<width; x++)
131             for(int y=0; y<height; y++) {
132                 R rc = new R(map.minx() + x * mapw,
133                              map.miny() + y * maph,
134                              map.minx() + (x+1) * mapw - 1,
135                              map.miny() + (y+1) * maph - 1);
136                 if      (selectedCell != null && selectedCell.row==(height-y-1) && selectedCell.col==x) g.color(0xffff00);
137                 else if (rc.within(rv))  g.color(0x006600);
138                 else                     g.color(0x444444);
139                 rc.fill(g);
140             }
141         g.color(0x00ff00);
142         rv.draw(g);
143     }
144
145     public void paint(Graphics g) { _paint((Graphics2D)g); }
146     public void mouseDragged(MouseEvent e) { mouseMoved(e); }
147     public void mouseMoved(MouseEvent e) {
148         mousex = e.getX();
149         mousey = e.getY();
150     }
151     int mousex;
152     int mousey;
153
154     private static Image keyboard1 =
155         Toolkit.getDefaultToolkit().createImage("images/keyboard1.png");
156     private static Image keyboard2 =
157         Toolkit.getDefaultToolkit().createImage("images/keyboard2.png");
158     private static Image keyboard3 =
159         Toolkit.getDefaultToolkit().createImage("images/keyboard3.png");
160
161     private boolean[] keys = new boolean[1024];
162     public void keyTyped(KeyEvent k) { }
163     public void keyReleased(KeyEvent k) {
164         keys[k.getKeyCode()] = false;
165     }
166     public void keyPressed(KeyEvent k) {
167         synchronized(this) {
168         keys[k.getKeyCode()] = true;
169         switch(k.getKeyCode()) {
170             case VK_1: {
171                 if (selectedCell != null) {
172                     selectedCell.ylut(0xff);
173                 }
174                 break;
175             }
176             case VK_0: {
177                 if (selectedCell != null) {
178                     selectedCell.ylut(0x00);
179                 }
180                 break;
181             }
182             case VK_C: {
183                 if (selectedCell != null) {
184                     selectedCell.ylut((LUT_SELF & ~LUT_OTHER) |
185                                       (LUT_Z    & ~LUT_OTHER) |
186                                       (LUT_Z    &   LUT_SELF));
187                 }
188                 break;
189             }
190                 /*
191             case VK_0: case VK_1: case VK_2: case VK_3: case VK_4: {
192                 if (selectedCell != null) {
193                     int plane = k.getKeyCode() - VK_0;
194                     if      (keys[VK_X]) selectedCell.xi(plane+L0);
195                     else if (keys[VK_Y]) selectedCell.yi(plane+L0);
196                     else if (keys[VK_O]) selectedCell.out(plane+L0, !selectedCell.out(plane+L0));
197                 }
198                 break;
199             }
200                 */
201         }
202         }
203     }
204 }