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