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