8ca57ee5faefe320a61e74b35ed04387df32bf07
[Mograsim.git] / LogicUI / src / net / mograsim / logic / ui / model / components / GUIManualSwitch.java
1 package net.mograsim.logic.ui.model.components;
2
3 import net.mograsim.logic.ui.model.ViewModel;
4 import net.mograsim.logic.ui.model.wires.Pin;
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.components.ManualSwitch;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
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                 double posX = getBounds().x;
35                 double posY = getBounds().y;
36
37                 gc.drawRectangle(posX, posY, width, height);
38                 String label = BitVectorFormatter.formatValueAsString(end);
39                 Font oldFont = gc.getFont();
40                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
41                 gc.setFont(labelFont);
42                 Point textExtent = gc.textExtent(label);
43                 gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
44                 gc.setFont(oldFont);
45         }
46
47         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
48         {
49                 this.logicSwitch = logicSwitch;
50                 this.end = end;
51                 // TODO when ManualSwitch supports it, add listeners
52                 end.addObserver((i, o) -> callComponentLookChangedListeners());
53         }
54
55         @Override
56         public boolean clicked(double x, double y)
57         {
58                 if (logicSwitch != null)
59                         logicSwitch.toggle();
60                 return true;
61         }
62
63         public Pin getOutputPin()
64         {
65                 return outputPin;
66         }
67 }