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