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