1 package net.mograsim.logic.ui.model.components;
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.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.wires.Pin;
14 public class GUIBitDisplay extends GUIComponent
16 private static final double width = 20;
17 private static final double height = 15;
18 private static final double fontHeight = 5;
20 private final Pin inputPin;
22 private final LogicObserver logicObs;
23 private BitDisplay bitDisplay;
25 public GUIBitDisplay(ViewModelModifiable model)
28 logicObs = (i) -> requestRedraw();
30 setSize(width, height);
31 addPin(this.inputPin = new Pin(this, 1, 0, height / 2));
35 public void render(GeneralGC gc, Rectangle visibleRegion)
37 double posX = getBounds().x;
38 double posY = getBounds().y;
40 // TODO maybe draw switch state too?
41 gc.drawRectangle(getBounds());
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);
52 public void setLogicModelBinding(BitDisplay bitDisplay)
54 deregisterLogicObs(this.bitDisplay);
55 this.bitDisplay = bitDisplay;
56 registerLogicObs(bitDisplay);
59 private void registerLogicObs(LogicObservable observable)
61 if (observable != null)
62 observable.registerObserver(logicObs);
65 private void deregisterLogicObs(LogicObservable observable)
67 if (observable != null)
68 observable.deregisterObserver(logicObs);
71 public Pin getInputPin()