GUIManualSwitch now shows the actual value of the output WireArray
[Mograsim.git] / LogicUI / src / era / mi / gui / components / GUIManualSwitch.java
1 package era.mi.gui.components;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collections;\r
5 import java.util.HashMap;\r
6 import java.util.List;\r
7 import java.util.Map;\r
8 \r
9 import era.mi.logic.Bit;\r
10 import era.mi.logic.components.ManualSwitch;\r
11 import era.mi.logic.wires.WireArray;\r
12 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;\r
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
16 \r
17 public class GUIManualSwitch extends ManualSwitch implements BasicGUIComponent\r
18 {\r
19         private static final Map<Bit, String> bitNames;\r
20         static\r
21         {\r
22                 Map<Bit, String> bitNamesModifiable = new HashMap<>();\r
23                 bitNamesModifiable.put(Bit.ONE, "1");\r
24                 bitNamesModifiable.put(Bit.ZERO, "0");\r
25                 bitNamesModifiable.put(Bit.Z, "Z");\r
26                 bitNamesModifiable.put(Bit.U, "U");\r
27                 bitNamesModifiable.put(Bit.X, "X");\r
28                 bitNames = Collections.unmodifiableMap(bitNamesModifiable);\r
29         }\r
30 \r
31         private final WireArray                 wa;\r
32         private final List<WireArray>   connectedWireArrays;\r
33         private final List<Point>               wireArrayConnectionPoints;\r
34 \r
35         public GUIManualSwitch(WireArray output)\r
36         {\r
37                 super(output);\r
38 \r
39                 this.wa = output;\r
40 \r
41                 List<WireArray> connectedWireArraysModifiable = new ArrayList<>();\r
42                 List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();\r
43 \r
44                 connectedWireArraysModifiable.add(output);\r
45                 wireArrayConnectionPointsModifiable.add(new Point(20, 7.5));\r
46 \r
47                 this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);\r
48                 this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);\r
49         }\r
50 \r
51         @Override\r
52         public Rectangle getBounds()\r
53         {\r
54                 return new Rectangle(0, 0, 20, 15);\r
55         }\r
56         @Override\r
57         public void render(GeneralGC gc)\r
58         {\r
59                 gc.drawRectangle(0, 0, 20, 15);\r
60                 String label = bitNames.get(wa.getValue());\r
61                 Font oldFont = gc.getFont();\r
62                 Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle());\r
63                 gc.setFont(labelFont);\r
64                 Point textExtent = gc.textExtent(label);\r
65                 gc.drawText(label, 10 - textExtent.x / 2, 7.5 - textExtent.y / 2, true);\r
66                 gc.setFont(oldFont);\r
67         }\r
68         @Override\r
69         public boolean clicked(double x, double y)\r
70         {\r
71                 toggle();\r
72                 return true;\r
73         }\r
74 \r
75         @Override\r
76         public int getConnectedWireArraysCount()\r
77         {\r
78                 return connectedWireArrays.size();\r
79         }\r
80         @Override\r
81         public WireArray getConnectedWireArray(int connectionIndex)\r
82         {\r
83                 return connectedWireArrays.get(connectionIndex);\r
84         }\r
85         @Override\r
86         public Point getWireArrayConnectionPoint(int connectionI)\r
87         {\r
88                 return wireArrayConnectionPoints.get(connectionI);\r
89         }\r
90 }