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