Merge branch 'development' of
[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 ManualSwitch logicSwitch;
20         private ReadEnd end;
21
22         public GUIManualSwitch(ViewModel model)
23         {
24                 super(model);
25                 setSize(width, height);
26                 addPin(new Pin(this, width, height / 2));
27         }
28
29         @Override
30         public void render(GeneralGC gc, Rectangle visibleRegion)
31         {
32                 gc.drawRectangle(0, 0, width, height);
33                 String label = BitVectorFormatter.formatValueAsString(end);
34                 Font oldFont = gc.getFont();
35                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
36                 gc.setFont(labelFont);
37                 Point textExtent = gc.textExtent(label);
38                 gc.drawText(label, (width - textExtent.x) / 2, (height - textExtent.y) / 2, true);
39                 gc.setFont(oldFont);
40         }
41
42         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
43         {
44                 this.logicSwitch = logicSwitch;
45                 this.end = end;
46                 // TODO when ManualSwitch supports it, add listeners
47                 end.addObserver((i, o) -> callComponentChangedListeners());
48         }
49
50         @Override
51         public boolean clicked(double x, double y)
52         {
53                 logicSwitch.toggle();
54                 return true;
55         }
56 }