Implemented GUIAndGate
[Mograsim.git] / LogicUI / src / era / mi / components / gui / GUIAndGate.java
1 package era.mi.components.gui;
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
14 public class GUIAndGate extends AndGate implements BasicGUIComponent
15 {
16         private static final String LABEL = "&";
17
18         private final int                               inputCount;
19         private final double                    height;
20         private final List<WireArray>   connectedWireArrays;
21         private final List<Point>               wireArrayConnectionPoints;
22
23         public GUIAndGate(int processTime, WireArray out, WireArray... in)
24         {
25                 super(processTime, out, in);
26
27                 List<WireArray> connectedWireArraysModifiable = new ArrayList<>();
28                 List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();
29
30                 this.inputCount = in.length;
31                 this.height = inputCount * 10;
32
33                 {
34                         connectedWireArraysModifiable.addAll(Arrays.asList(in));
35                         double inputHeight = 5;
36                         for(int i = 0; i < inputCount; i ++, inputHeight += 10)
37                                 wireArrayConnectionPointsModifiable.add(new Point(0, inputHeight));
38                 }
39
40                 connectedWireArraysModifiable.add(out);
41                 wireArrayConnectionPointsModifiable.add(new Point(20, height / 2));
42
43                 this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);
44                 this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);
45         }
46
47         @Override
48         public void render(GeneralGC gc)
49         {
50                 gc.drawRectangle(0, 0, 17, height);
51                 Font oldFont = gc.getFont();
52                 Font labelFont = new Font(oldFont.getName(), 5, oldFont.getStyle());
53                 gc.setFont(labelFont);
54                 Point textExtent = gc.textExtent(LABEL);
55                 gc.drawText(LABEL, 8.5 - textExtent.x / 2, (height - textExtent.y) / 2, true);
56                 gc.setFont(oldFont);
57                 gc.drawOval(17, height / 2 - 1.5, 3, 3);
58         }
59
60         @Override
61         public int getConnectedWireArraysCount()
62         {
63                 return connectedWireArrays.size();
64         }
65         @Override
66         public WireArray getConnectedWireArray(int connectionIndex)
67         {
68                 return connectedWireArrays.get(connectionIndex);
69         }
70         @Override
71         public Point getWireArrayConnectionPoint(int connectionI)
72         {
73                 return wireArrayConnectionPoints.get(connectionI);
74         }
75 }