b215cff86972c3843e6f1ea359288b704a2e7e4c
[Mograsim.git] / LogicUI / src / era / mi / gui / model / components / GUIManualSwitch.java
1 package era.mi.gui.model.components;
2
3 import era.mi.gui.model.ViewModel;
4 import era.mi.gui.model.wires.Pin;
5 import era.mi.logic.components.ManualSwitch;
6 import era.mi.logic.types.BitVectorFormatter;
7 import era.mi.logic.wires.Wire.ReadEnd;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12
13 public class GUIManualSwitch extends GUIComponent
14 {
15         private static final double width = 20;
16         private static final double height = 15;
17         private static final double fontHeight = 5;
18
19         private final Pin outputPin;
20
21         private ManualSwitch logicSwitch;
22         private ReadEnd end;
23
24         public GUIManualSwitch(ViewModel model)
25         {
26                 super(model);
27                 setSize(width, height);
28                 addPin(this.outputPin = new Pin(this, 1, width, height / 2));
29         }
30
31         @Override
32         public void render(GeneralGC gc, Rectangle visibleRegion)
33         {
34                 gc.drawRectangle(0, 0, width, height);
35                 String label = BitVectorFormatter.formatValueAsString(end);
36                 Font oldFont = gc.getFont();
37                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
38                 gc.setFont(labelFont);
39                 Point textExtent = gc.textExtent(label);
40                 gc.drawText(label, (width - textExtent.x) / 2, (height - textExtent.y) / 2, true);
41                 gc.setFont(oldFont);
42         }
43
44         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
45         {
46                 this.logicSwitch = logicSwitch;
47                 this.end = end;
48                 // TODO when ManualSwitch supports it, add listeners
49                 end.addObserver((i, o) -> callComponentLookChangedListeners());
50         }
51
52         @Override
53         public boolean clicked(double x, double y)
54         {
55                 logicSwitch.toggle();
56                 return true;
57         }
58
59         public Pin getOutputPin()
60         {
61                 return outputPin;
62         }
63 }