checkpoint
[slipway.git] / src / edu / berkeley / slipway / gui / GuiCell.java
1 package edu.berkeley.slipway.gui;
2
3 import com.atmel.fpslic.*;
4 import static com.atmel.fpslic.FpslicConstants.*;
5
6 public class GuiCell {
7     
8     public static final int BORDER_COLOR      = 0x00BBBBBB;
9     public static final int BODY_COLOR        = 0x00555555;
10     public static final int GLOBAL_WIRE_COLOR = 0x00008000;
11     public static final int LOCAL_WIRE_COLOR  = 0x000FF000;
12     public static final int XGATE_COLOR       = 0x00800000;
13     public static final int YGATE_COLOR       = 0x00000080;
14     double MAIN_AREA = 0.75;
15
16     private final Fpslic.Cell fpslicCell;
17     private GuiGate xgate = null;
18     private GuiGate ygate = null;
19
20     public GuiCell(Fpslic.Cell fpslicCell) {
21         this.fpslicCell = fpslicCell;
22     }
23
24     /**
25      *  The graphics context argument must already be translated such
26      *  that the space allocated to this cell is from (-1,-1) to (1,1)
27      */
28     public void draw(G g, R r, boolean selected) {
29         if (selected) {
30             g.color(0x00555555);
31             r.fill(g);
32         }
33         drawGlobalRouting(g, r);
34         if (fpslicCell.relevant()) {
35             xgate = fpslicCell.xlut_relevant() ? new GuiGate() : null;
36             ygate = fpslicCell.ylut_relevant() ? new GuiGate() : null;
37             drawBody(g, r);
38         }
39     }
40
41     private void drawGlobalRouting(G g, R r) {
42         g.color(GLOBAL_WIRE_COLOR);
43         for(int i=1; i<6; i++) {
44             g.line(r.minx() + 2*i, r.miny(),
45                    r.minx() + 2*i, r.maxy());
46             g.line(r.minx(),       r.miny() + 2*i,
47                    r.maxx(),       r.miny() + 2*i);
48         }
49     }
50
51     private void drawBody(G g, R r) {
52         if (xgate == null && ygate == null) return;
53         R body = r;
54         g.color(BODY_COLOR);
55         R xgater = null;
56         R ygater = null;
57         int N = 7;
58         R gateArea = body.plus(12+N, 12+N, -(4+N), -(4+N));
59         if      (xgate==null) ygater = gateArea;
60         else if (ygate==null) xgater = gateArea;
61         else {
62             double factor = gateArea.width()/2;
63             xgater = gateArea.plus(0, 0, factor, factor);
64             ygater = gateArea.plus(factor, 0, 0, factor);
65         }
66
67         R xring = gateArea.plus(4, 4, -4, -4);
68         R yring = gateArea.plus(6, 6, -6, -6);
69
70         int rot = 0;
71         switch (fpslicCell.yi()) {
72             case NORTH: rot = 2; break;
73             case SOUTH: rot = 0; break;
74             case WEST:  rot = 3; break;
75             case EAST:  rot = 1; break;
76             default:    rot = 0;
77         }
78         if (xgate != null) {
79             xgate.rotation = rot;
80             xgate.gateArea = gateArea;
81         }
82         if (ygate != null) {
83             ygate.rotation = rot;
84             ygate.gateArea = gateArea;
85         }
86
87         int TSIZE   = 10;
88         double TSQR = TSIZE / Math.sqrt(2.0);
89         g.color(XGATE_COLOR);
90         P p = null;
91         switch (fpslicCell.xi()) {
92             case SW: p = new P(r.minx(), r.miny()); break;
93             case SE: p = new P(r.maxx(), r.miny()); break;
94             case NW: p = new P(r.minx(), r.maxy()); break;
95             case NE: p = new P(r.maxx(), r.maxy()); break;
96         }
97         if (p!=null) {
98             if (ygate != null) {
99                 g.route(p, xring, ygate.getInput(1, ygater));
100                 g.line(ygate.getInput(1, ygater), ygate.getInputDest(1, ygater));
101             }
102             if (xgate != null) {
103                 g.route(p, xring, xgate.getInput(0, xgater));
104                 g.line(xgate.getInput(0, xgater), xgate.getInputDest(0, xgater));
105             }
106         }
107
108         p = null;
109         g.color(YGATE_COLOR);
110         switch (fpslicCell.yi()) {
111             case NORTH: p = new P(r.cx(), r.maxy()); break;
112             case SOUTH: p = new P(r.cx(), r.miny()); break;
113             case WEST:  p = new P(r.minx(), r.cy()); break;
114             case EAST:  p = new P(r.maxx(), r.cy()); break;
115         }
116         if (p!=null) {
117             if (ygate != null) {
118                 g.route(p, yring, ygate.getInput(0, ygater));
119                 g.line(ygate.getInput(0, ygater), ygate.getInputDest(0, ygater));
120             }
121             if (xgate != null) {
122                 g.route(p, yring, xgate.getInput(1, xgater));
123                 g.line(xgate.getInput(1, xgater), xgate.getInputDest(1, xgater));
124             }
125         }
126
127         if (xgater != null) {
128             /*
129             if (fpslicCell.zi() != NONE) {
130                 g.color(LOCAL_WIRE_COLOR);
131                 int layer = fpslicCell.zi() - L0;
132                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
133                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
134                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
135                 g.route(p2, r2, xgate.getInput(3, xgater));
136             }
137             */
138             if (fpslicCell.wi() != NONE) {
139                 g.color(LOCAL_WIRE_COLOR);
140                 int layer = fpslicCell.wi() - L0;
141                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
142                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
143                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
144                 g.route(p2, r2, xgate.getInput(2, xgater));
145                 g.line(xgate.getInput(2, xgater), xgate.getInputDest(2, xgater));
146             }
147             xgate.draw(g, xgater, XGATE_COLOR);
148         }
149         if (ygater != null) {
150             /*
151             if (fpslicCell.zi() != NONE) {
152                 g.color(LOCAL_WIRE_COLOR);
153                 int layer = fpslicCell.zi() - L0;
154                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
155                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
156                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
157                 g.route(p2, r2, ygate.getInput(3, ygater));
158             }
159             */
160             if (fpslicCell.wi() != NONE) {
161                 g.color(LOCAL_WIRE_COLOR);
162                 int layer = fpslicCell.wi() - L0;
163                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
164                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
165                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
166                 g.route(p2, r2, ygate.getInput(2, ygater));
167                 g.line(ygate.getInput(2, ygater), ygate.getInputDest(2, ygater));
168             }
169             ygate.draw(g, ygater, YGATE_COLOR);
170         }
171     }
172 }