1 package net.mograsim.logic.ui.model.components;
3 import org.eclipse.swt.graphics.Color;
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;
19 public class GUIBitDisplay extends GUIComponent
21 private static final double width = 20;
22 private static final double height = 15;
23 private static final double fontHeight = 5;
25 private final Pin inputPin;
27 private final LogicObserver logicObs;
28 private BitDisplay bitDisplay;
30 public GUIBitDisplay(ViewModelModifiable model)
33 logicObs = (i) -> requestRedraw();
35 setSize(width, height);
36 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
40 public void render(GeneralGC gc, Rectangle visibleRegion)
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);
59 public void setLogicModelBinding(BitDisplay bitDisplay)
61 deregisterLogicObs(this.bitDisplay);
62 this.bitDisplay = bitDisplay;
63 registerLogicObs(bitDisplay);
66 private void registerLogicObs(LogicObservable observable)
68 if (observable != null)
69 observable.registerObserver(logicObs);
72 private void deregisterLogicObs(LogicObservable observable)
74 if (observable != null)
75 observable.deregisterObserver(logicObs);
78 public BitDisplay getBitDisplay()
83 public Pin getInputPin()
90 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());