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.ModelVisitor;
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;
17 public class GUIBitDisplay extends GUIComponent
19 private static final double width = 20;
20 private static final double height = 15;
21 private static final double fontHeight = 5;
23 private final Pin inputPin;
25 private final LogicObserver logicObs;
26 private BitDisplay bitDisplay;
28 public GUIBitDisplay(ViewModelModifiable model)
31 logicObs = (i) -> requestRedraw();
33 setSize(width, height);
34 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
38 public void render(GeneralGC gc, Rectangle visibleRegion)
40 gc.drawRectangle(getBounds());
41 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
42 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
43 Font oldFont = gc.getFont();
44 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
45 gc.setFont(labelFont);
46 Point textExtent = gc.textExtent(label);
47 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
51 public void setLogicModelBinding(BitDisplay bitDisplay)
53 deregisterLogicObs(this.bitDisplay);
54 this.bitDisplay = bitDisplay;
55 registerLogicObs(bitDisplay);
58 private void registerLogicObs(LogicObservable observable)
60 if (observable != null)
61 observable.registerObserver(logicObs);
64 private void deregisterLogicObs(LogicObservable observable)
66 if (observable != null)
67 observable.deregisterObserver(logicObs);
70 public BitDisplay getBitDisplay()
75 public Pin getInputPin()
81 public void accept(ModelVisitor mv)
88 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());