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