Merged SampleERCP into master
[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.WireArray;
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<WireArray> connectedWireArrays;
22         private final List<Point> wireArrayConnectionPoints;
23
24         public GUIAndGate(int processTime, WireArray out, WireArray... in)
25         {
26                 super(processTime, out, in);
27
28                 List<WireArray> connectedWireArraysModifiable = new ArrayList<>();
29                 List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();
30
31                 this.inputCount = in.length;
32                 this.height = inputCount * 10;
33
34                 {
35                         connectedWireArraysModifiable.addAll(Arrays.asList(in));
36                         double inputHeight = 5;
37                         for (int i = 0; i < inputCount; i++, inputHeight += 10)
38                                 wireArrayConnectionPointsModifiable.add(new Point(0, inputHeight));
39                 }
40
41                 connectedWireArraysModifiable.add(out);
42                 wireArrayConnectionPointsModifiable.add(new Point(20, height / 2));
43
44                 this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);
45                 this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);
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 getConnectedWireArraysCount()
68         {
69                 return connectedWireArrays.size();
70         }
71
72         @Override
73         public WireArray getConnectedWireArray(int connectionIndex)
74         {
75                 return connectedWireArrays.get(connectionIndex);
76         }
77
78         @Override
79         public Point getWireArrayConnectionPoint(int connectionI)
80         {
81                 return wireArrayConnectionPoints.get(connectionI);
82         }
83 }