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