checkpoint
[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 GeneralPath gp = new GeneralPath();
13
14     public GuiGate() {
15         gp.moveTo(29.141f, 36.301f);
16         gp.lineTo(29.141f, 36.301f-7.161f);
17         gp.curveTo(27.71f, 11.24f, 23.413f, 9.45f, 14.82f, 0.5f);
18         gp.curveTo(6.229f, 9.45f, 1.932f, 11.24f, 0.5f, 29.141f);
19         gp.lineTo(0.5f, 29.141f+7.161f);
20         float x = 0.5f;
21         float y = 29.141f+7.161f;
22         gp.curveTo(5.729f+x, -1.789f+y, 6.444f+x, -2.686f+y, 14.32f+x, -3.58f+y);
23         gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
24         double minx   = gp.getBounds2D().getMinX();
25         double miny   = gp.getBounds2D().getMinY();
26         gp.transform(AffineTransform.getTranslateInstance(-minx, -miny));
27         double width  = gp.getBounds2D().getWidth();
28         double height = gp.getBounds2D().getHeight();
29         double factor = Math.max(width, height);
30         gp.transform(AffineTransform.getTranslateInstance(-width/2, -height/2));
31         gp.transform(AffineTransform.getScaleInstance(1.0/factor, -1.0/factor));
32     }
33
34     int rotation = 1;
35     R gateArea;
36     R r;
37
38     public void draw(G g, int color) {
39         g.pushTransform();
40         g.g.translate(gateArea.cx(), gateArea.cy());
41         g.g.rotate((2 * Math.PI * rotation)/4);
42         g.g.translate(-1 * gateArea.cx(), -1 * gateArea.cy());
43         AffineTransform at = AffineTransform.getTranslateInstance(r.cx(), r.cy());
44         at.scale(r.getWidth(), r.getHeight());
45         g.color(0xffffffff);
46         g.g.fill(gp.createTransformedShape(at));
47         g.color(color);
48         g.g.draw(gp.createTransformedShape(at));
49         g.popTransform();
50     }
51
52     public P getInput(int index) {
53         AffineTransform at = new AffineTransform();
54         at.translate(gateArea.cx(), gateArea.cy());
55         at.rotate((2 * Math.PI * rotation)/4);
56         at.translate(-1 * gateArea.cx(), -1 * gateArea.cy());
57         return new P(r.minx() + ((index + 1) * r.width()) / 4,
58                      r.miny() - 3).transform(at);
59     }
60
61     public P getInputDest(int index) {
62         AffineTransform at = new AffineTransform();
63         at.translate(gateArea.cx(), gateArea.cy());
64         at.rotate((2 * Math.PI * rotation)/4);
65         at.translate(-1 * gateArea.cx(), -1 * gateArea.cy());
66         return new P(r.minx() + ((index + 1) * r.width()) / 4,
67                      r.miny() + r.height()/2).transform(at);
68     }
69
70     public void rotation(int dir) {
71         switch (dir) {
72             case NORTH: rotation = 2; break;
73             case SOUTH: rotation = 0; break;
74             case WEST:  rotation = 3; break;
75             case EAST:  rotation = 1; break;
76             default:    rotation = 0;
77         }
78     }
79
80 }