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 = new GuiGate();
18     private GuiGate ygate = new GuiGate();
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.disabled = !fpslicCell.xlut_relevant();
36             ygate.disabled = !fpslicCell.ylut_relevant();
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
54         int N = 7;
55         int TSIZE   = 10;
56         double TSQR = TSIZE / Math.sqrt(2.0);
57
58         R body = r;
59         g.color(BODY_COLOR);
60
61         R gateArea = body.plus(12+N, 12+N, -(4+N), -(4+N));
62         double factor = gateArea.width()/2;
63         R ygater = xgate.disabled || ygate.disabled ? gateArea : gateArea.plus(0, 0, -factor, -factor);
64         R xgater = xgate.disabled || ygate.disabled ? gateArea : gateArea.plus(factor, 0, 0, -factor);
65
66         R xring = gateArea.plus(-4, -4, 4, 4);
67         R yring = gateArea.plus(-6, -6, 6, 6);
68         P xip = r.corner(fpslicCell.xi());
69         P yip = r.corner(fpslicCell.yi());
70
71         xgate.rotation(fpslicCell.yi());
72         xgate.gateArea = gateArea;
73         xgate.r = xgater;
74         if (xip != null) xgate.route(g, xip, xring, 0, XGATE_COLOR);
75         if (yip != null) xgate.route(g, yip, yring, 2, YGATE_COLOR);
76
77         ygate.rotation(fpslicCell.yi());
78         ygate.gateArea = gateArea;
79         ygate.r = ygater;
80         if (xip != null) ygate.route(g, xip, xring, 2, XGATE_COLOR);
81         if (yip != null) ygate.route(g, yip, yring, 0, YGATE_COLOR);
82
83         int layer = fpslicCell.wi() - L0;
84         P p2 = r.corner(SW).translate(2*(layer+1), 2*(layer+1));
85         R r2 = r.plus(2*(layer+1), 2*(layer+1), -2*(layer+1), -2*(layer+1));
86         ygate.route(g, p2, r2, 1, LOCAL_WIRE_COLOR);
87         xgate.route(g, p2, r2, 1, LOCAL_WIRE_COLOR);
88
89         xgate.draw(g, XGATE_COLOR);
90         ygate.draw(g, YGATE_COLOR);
91     }
92 }