1 package net.mograsim.logic.ui.model.components.atomic;
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.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.components.GUIComponent;
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.logic.ui.serializing.IndirectGUIComponentCreator;
18 import net.mograsim.preferences.Preferences;
20 public class GUIBitDisplay extends GUIComponent
22 private static final double width = 20;
23 private static final double height = 15;
24 private static final double fontHeight = 5;
26 private final Pin inputPin;
28 private final LogicObserver logicObs;
29 private BitDisplay bitDisplay;
31 public GUIBitDisplay(ViewModelModifiable model)
34 logicObs = (i) -> requestRedraw();
36 setSize(width, height);
37 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
41 public void render(GeneralGC gc, Rectangle visibleRegion)
43 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
44 if (foreground != null)
45 gc.setForeground(foreground);
46 gc.drawRectangle(getBounds());
47 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
48 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
49 Font oldFont = gc.getFont();
50 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
51 gc.setFont(labelFont);
52 Point textExtent = gc.textExtent(label);
53 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
54 if (textColor != null)
55 gc.setForeground(textColor);
56 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
60 public void setLogicModelBinding(BitDisplay bitDisplay)
62 if (this.bitDisplay != null)
63 this.bitDisplay.deregisterObserver(logicObs);
64 this.bitDisplay = bitDisplay;
65 if (bitDisplay != null)
66 bitDisplay.registerObserver(logicObs);
69 public boolean hasLogicModelBinding()
71 return bitDisplay != null;
74 public BitDisplay getBitDisplay()
79 public Pin getInputPin()
86 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
87 IndirectGUIComponentCreator.setComponentProvider(GUIBitDisplay.class.getCanonicalName(), (m, p) -> new GUIBitDisplay(m));