4a75f541b5d3c14836a543c4645c84a35b8b3d7d
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIBitDisplay.java
1 package net.mograsim.logic.ui.model.components;
2
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
5 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.core.LogicObservable;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.components.BitDisplay;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.ui.model.ViewModel;
12 import net.mograsim.logic.ui.model.wires.Pin;
13
14 public class GUIBitDisplay extends GUIComponent
15 {
16         private static final double width = 20;
17         private static final double height = 15;
18         private static final double fontHeight = 5;
19
20         private final Pin inputPin;
21
22         private final LogicObserver logicObs;
23         private BitDisplay bitDisplay;
24
25         public GUIBitDisplay(ViewModel model)
26         {
27                 super(model);
28                 logicObs = (i) -> callComponentLookChangedListeners();
29
30                 setSize(width, height);
31                 addPin(this.inputPin = new Pin(this, 1, 0, height / 2));
32         }
33
34         @Override
35         public void render(GeneralGC gc, Rectangle visibleRegion)
36         {
37                 double posX = getBounds().x;
38                 double posY = getBounds().y;
39
40                 // TODO maybe draw switch state too?
41                 gc.drawRectangle(posX, posY, width, height);
42                 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
43                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
44                 Font oldFont = gc.getFont();
45                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
46                 gc.setFont(labelFont);
47                 Point textExtent = gc.textExtent(label);
48                 gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
49                 gc.setFont(oldFont);
50         }
51
52         public void setLogicModelBinding(BitDisplay bitDisplay)
53         {
54                 deregisterLogicObs(this.bitDisplay);
55                 this.bitDisplay = bitDisplay;
56                 registerLogicObs(bitDisplay);
57         }
58
59         private void registerLogicObs(LogicObservable observable)
60         {
61                 if (observable != null)
62                         observable.registerObserver(logicObs);
63         }
64
65         private void deregisterLogicObs(LogicObservable observable)
66         {
67                 if (observable != null)
68                         observable.deregisterObserver(logicObs);
69         }
70
71         public Pin getInputPin()
72         {
73                 return inputPin;
74         }
75 }