Adjusted LogicUI to new Wire / WireEnd concept
[Mograsim.git] / LogicUI / src / era / mi / gui / components / GUIAndGate.java
1 package era.mi.gui.components;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import era.mi.logic.components.gates.AndGate;
9 import era.mi.logic.wires.Wire.WireEnd;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14
15 public class GUIAndGate extends AndGate implements BasicGUIComponent
16 {
17         private static final String LABEL = "&";
18
19         private final int inputCount;
20         private final double height;
21         private final List<WireEnd> connectedWireEnds;
22         private final List<Point> wireEndConnectionPoints;
23
24         public GUIAndGate(int processTime, WireEnd out, WireEnd... in)
25         {
26                 super(processTime, out, in);
27
28                 List<WireEnd> connectedWireEndsModifiable = new ArrayList<>();
29                 List<Point> wireEndConnectionPointsModifiable = new ArrayList<>();
30
31                 this.inputCount = in.length;
32                 this.height = inputCount * 10;
33
34                 {
35                         connectedWireEndsModifiable.addAll(Arrays.asList(in));
36                         double inputHeight = 5;
37                         for (int i = 0; i < inputCount; i++, inputHeight += 10)
38                                 wireEndConnectionPointsModifiable.add(new Point(0, inputHeight));
39                 }
40
41                 connectedWireEndsModifiable.add(out);
42                 wireEndConnectionPointsModifiable.add(new Point(20, height / 2));
43
44                 this.connectedWireEnds = Collections.unmodifiableList(connectedWireEndsModifiable);
45                 this.wireEndConnectionPoints = Collections.unmodifiableList(wireEndConnectionPointsModifiable);
46         }
47
48         @Override
49         public Rectangle getBounds()
50         {
51                 return new Rectangle(0, 0, 20, height);
52         }
53
54         @Override
55         public void render(GeneralGC gc)
56         {
57                 gc.drawRectangle(0, 0, 20, height);
58                 Font oldFont = gc.getFont();
59                 Font labelFont = new Font(oldFont.getName(), 5, oldFont.getStyle());
60                 gc.setFont(labelFont);
61                 Point textExtent = gc.textExtent(LABEL);
62                 gc.drawText(LABEL, 10 - textExtent.x / 2, (height - textExtent.y) / 2, true);
63                 gc.setFont(oldFont);
64         }
65
66         @Override
67         public int getConnectedWireEndsCount()
68         {
69                 return connectedWireEnds.size();
70         }
71
72         @Override
73         public WireEnd getConnectedWireEnd(int connectionIndex)
74         {
75                 return connectedWireEnds.get(connectionIndex);
76         }
77
78         @Override
79         public Point getWireEndConnectionPoint(int connectionI)
80         {
81                 return wireEndConnectionPoints.get(connectionI);
82         }
83 }