1 package net.mograsim.logic.model.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.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
17 import net.mograsim.logic.model.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)
36 public GUIBitDisplay(ViewModelModifiable model, String name)
39 logicObs = (i) -> requestRedraw();
41 setSize(width, height);
42 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
46 public void render(GeneralGC gc, Rectangle visibleRegion)
48 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
49 if (foreground != null)
50 gc.setForeground(foreground);
51 gc.drawRectangle(getBounds());
52 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
53 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
54 Font oldFont = gc.getFont();
55 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
56 gc.setFont(labelFont);
57 Point textExtent = gc.textExtent(label);
58 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
59 if (textColor != null)
60 gc.setForeground(textColor);
61 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
65 public void setLogicModelBinding(BitDisplay bitDisplay)
67 if (this.bitDisplay != null)
68 this.bitDisplay.deregisterObserver(logicObs);
69 this.bitDisplay = bitDisplay;
70 if (bitDisplay != null)
71 bitDisplay.registerObserver(logicObs);
74 public boolean hasLogicModelBinding()
76 return bitDisplay != null;
79 public BitDisplay getBitDisplay()
84 public Pin getInputPin()
91 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
92 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(), (m, p, n) -> new GUIBitDisplay(m, n));