b9c8a103a9ffe6e6fb509f60b7c6562b60fba4cf
[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 org.eclipse.swt.graphics.Color;
4
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.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.components.BitDisplay;
12 import net.mograsim.logic.core.types.BitVectorFormatter;
13 import net.mograsim.logic.ui.model.ViewModelModifiable;
14 import net.mograsim.logic.ui.model.wires.Pin;
15 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
16 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
17 import net.mograsim.preferences.Preferences;
18
19 public class GUIBitDisplay extends GUIComponent
20 {
21         private static final double width = 20;
22         private static final double height = 15;
23         private static final double fontHeight = 5;
24
25         private final Pin inputPin;
26
27         private final LogicObserver logicObs;
28         private BitDisplay bitDisplay;
29
30         public GUIBitDisplay(ViewModelModifiable model)
31         {
32                 super(model);
33                 logicObs = (i) -> requestRedraw();
34
35                 setSize(width, height);
36                 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
37         }
38
39         @Override
40         public void render(GeneralGC gc, Rectangle visibleRegion)
41         {
42                 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
43                 if (foreground != null)
44                         gc.setForeground(foreground);
45                 gc.drawRectangle(getBounds());
46                 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
47                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
48                 Font oldFont = gc.getFont();
49                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
50                 gc.setFont(labelFont);
51                 Point textExtent = gc.textExtent(label);
52                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
53                 if (textColor != null)
54                         gc.setForeground(textColor);
55                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
56                 gc.setFont(oldFont);
57         }
58
59         public void setLogicModelBinding(BitDisplay bitDisplay)
60         {
61                 deregisterLogicObs(this.bitDisplay);
62                 this.bitDisplay = bitDisplay;
63                 registerLogicObs(bitDisplay);
64         }
65
66         private void registerLogicObs(LogicObservable observable)
67         {
68                 if (observable != null)
69                         observable.registerObserver(logicObs);
70         }
71
72         private void deregisterLogicObs(LogicObservable observable)
73         {
74                 if (observable != null)
75                         observable.deregisterObserver(logicObs);
76         }
77
78         public BitDisplay getBitDisplay()
79         {
80                 return bitDisplay;
81         }
82
83         public Pin getInputPin()
84         {
85                 return inputPin;
86         }
87
88         static
89         {
90                 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
91         }
92 }