added mpardemo
[slipway.git] / src / edu / berkeley / slipway / gui / GuiGate.java
1 package edu.berkeley.slipway.gui;
2
3 import com.atmel.fpslic.*;
4 import java.awt.*;
5 import java.awt.geom.*;
6 import java.awt.event.*;
7 import java.awt.color.*;
8 import static com.atmel.fpslic.FpslicConstants.*;
9
10 public class GuiGate {
11
12     private final GuiCell cell;
13     private GeneralPath gp = new GeneralPath();
14
15     int rotation = 1;
16     R r;
17     boolean disabled = false;
18
19     public GuiGate(GuiCell cell) {
20         this.cell = cell;
21         gp.moveTo(29.141f, 36.301f);
22         gp.lineTo(29.141f, 36.301f-7.161f);
23         gp.curveTo(27.71f, 11.24f, 23.413f, 9.45f, 14.82f, 0.5f);
24         gp.curveTo(6.229f, 9.45f, 1.932f, 11.24f, 0.5f, 29.141f);
25         gp.lineTo(0.5f, 29.141f+7.161f);
26         float x = 0.5f;
27         float y = 29.141f+7.161f;
28         gp.curveTo(5.729f+x, -1.789f+y, 6.444f+x, -2.686f+y, 14.32f+x, -3.58f+y);
29         gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
30         double minx   = gp.getBounds2D().getMinX();
31         double miny   = gp.getBounds2D().getMinY();
32         gp.transform(AffineTransform.getTranslateInstance(-minx, -miny));
33         double width  = gp.getBounds2D().getWidth();
34         double height = gp.getBounds2D().getHeight();
35         double factor = Math.max(width, height);
36         gp.transform(AffineTransform.getTranslateInstance(-width/2, -height/2));
37         gp.transform(AffineTransform.getScaleInstance(1.0/factor, -1.0/factor));
38     }
39
40     public void draw(G g, int color, int fill) {
41         if (disabled) return;
42         g.pushTransform();
43         g.g.translate(cell.gateArea.cx(), cell.gateArea.cy());
44         g.g.rotate((2 * Math.PI * rotation)/4);
45         g.g.translate(-1 * cell.gateArea.cx(), -1 * cell.gateArea.cy());
46         AffineTransform at = AffineTransform.getTranslateInstance(r.cx(), r.cy());
47         at.scale(r.getWidth(), r.getHeight());
48         g.color(fill);
49         g.g.fill(gp.createTransformedShape(at));
50         g.color(color);
51         g.g.draw(gp.createTransformedShape(at));
52         g.popTransform();
53     }
54
55     public P getInput(int index) {
56         AffineTransform at = new AffineTransform();
57         at.translate(cell.gateArea.cx(), cell.gateArea.cy());
58         at.rotate((2 * Math.PI * rotation)/4);
59         at.translate(-1 * cell.gateArea.cx(), -1 * cell.gateArea.cy());
60         return new P(r.minx() + ((index + 1) * r.width()) / 4,
61                      r.miny() - 3).transform(at);
62     }
63
64     public P getInputDest(int index) {
65         AffineTransform at = new AffineTransform();
66         at.translate(cell.gateArea.cx(), cell.gateArea.cy());
67         at.rotate((2 * Math.PI * rotation)/4);
68         at.translate(-1 * cell.gateArea.cx(), -1 * cell.gateArea.cy());
69         return new P(r.minx() + ((index + 1) * r.width()) / 4,
70                      r.miny() + r.height()/2).transform(at);
71     }
72
73     public void rotation(int dir) {
74         switch (dir) {
75             case NORTH: rotation = 2; break;
76             case SOUTH: rotation = 0; break;
77             case WEST:  rotation = 3; break;
78             case EAST:  rotation = 1; break;
79             default:    rotation = 0;
80         }
81     }
82
83     public void route(G g, P p, R ring, int input, int color) {
84         if (disabled) return;
85         g.color(color);
86         g.route(p, ring, getInput(input));
87         g.line(getInput(input), getInputDest(input));
88     }
89
90 }