Restructured packages
[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.List;\r
6 \r
7 import era.mi.logic.components.ManualSwitch;\r
8 import era.mi.logic.wires.WireArray;\r
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;\r
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
13 \r
14 public class GUIManualSwitch extends ManualSwitch implements BasicGUIComponent\r
15 {\r
16         private final List<WireArray>   connectedWireArrays;\r
17         private final List<Point>               wireArrayConnectionPoints;\r
18 \r
19         public GUIManualSwitch(WireArray output)\r
20         {\r
21                 super(output);\r
22 \r
23                 List<WireArray> connectedWireArraysModifiable = new ArrayList<>();\r
24                 List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();\r
25 \r
26                 connectedWireArraysModifiable.add(output);\r
27                 wireArrayConnectionPointsModifiable.add(new Point(20, 7.5));\r
28 \r
29                 this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);\r
30                 this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);\r
31         }\r
32 \r
33         @Override\r
34         public Rectangle getBounds()\r
35         {\r
36                 return new Rectangle(0, 0, 20, 15);\r
37         }\r
38         @Override\r
39         public void render(GeneralGC gc)\r
40         {\r
41                 gc.drawRectangle(0, 0, 20, 15);\r
42                 String label = isOn() ? "ON" : "OFF";\r
43                 Font oldFont = gc.getFont();\r
44                 Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle());\r
45                 gc.setFont(labelFont);\r
46                 Point textExtent = gc.textExtent(label);\r
47                 gc.drawText(label, 10 - textExtent.x / 2, 7.5 - textExtent.y / 2, true);\r
48                 gc.setFont(oldFont);\r
49         }\r
50         @Override\r
51         public boolean clicked(double x, double y)\r
52         {\r
53                 toggle();\r
54                 return true;\r
55         }\r
56 \r
57         @Override\r
58         public int getConnectedWireArraysCount()\r
59         {\r
60                 return connectedWireArrays.size();\r
61         }\r
62         @Override\r
63         public WireArray getConnectedWireArray(int connectionIndex)\r
64         {\r
65                 return connectedWireArrays.get(connectionIndex);\r
66         }\r
67         @Override\r
68         public Point getWireArrayConnectionPoint(int connectionI)\r
69         {\r
70                 return wireArrayConnectionPoints.get(connectionI);\r
71         }\r
72 }