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.CoreBitDisplay;
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.logic.core.types.BitVectorFormatter;
13 import net.mograsim.logic.model.model.LogicModelModifiable;
14 import net.mograsim.logic.model.model.components.ModelComponent;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.model.wires.PinUsage;
17 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
18 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
19 import net.mograsim.logic.model.serializing.IdentifyParams;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.preferences.Preferences;
23 public class ModelBitDisplay extends ModelComponent
25 private static final double width = 20;
26 private static final double height = 15;
27 private static final double fontHeight = 5;
29 public final int logicWidth;
30 private final Pin inputPin;
32 private final LogicObserver logicObs;
33 private CoreBitDisplay bitDisplay;
35 public ModelBitDisplay(LogicModelModifiable model, int logicWidth)
37 this(model, logicWidth, null);
40 public ModelBitDisplay(LogicModelModifiable model, int logicWidth, String name)
42 super(model, name, false);
43 this.logicWidth = logicWidth;
44 logicObs = (i) -> model.requestRedraw();
46 setSize(width, height);
47 addPin(this.inputPin = new Pin(model, this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
53 public void render(GeneralGC gc, Rectangle visibleRegion)
55 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
56 if (foreground != null)
57 gc.setForeground(foreground);
58 gc.drawRectangle(getBounds());
60 if (bitDisplay == null)
61 label = BitVectorFormatter.formatAsString(null);
64 BitVector toDisplay = bitDisplay.getDisplayedValue();
65 label = toDisplay != null && toDisplay.isHighImpedance() ? "-"
66 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
68 Font oldFont = gc.getFont();
69 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
70 gc.setFont(labelFont);
71 Point textExtent = gc.textExtent(label);
72 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
73 if (textColor != null)
74 gc.setForeground(textColor);
75 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
79 public void setCoreModelBinding(CoreBitDisplay bitDisplay)
81 if (this.bitDisplay != null)
82 this.bitDisplay.deregisterObserver(logicObs);
83 this.bitDisplay = bitDisplay;
84 if (bitDisplay != null)
85 bitDisplay.registerObserver(logicObs);
88 public boolean hasCoreModelBinding()
90 return bitDisplay != null;
93 public CoreBitDisplay getBitDisplay()
98 public Pin getInputPin()
104 public String getIDForSerializing(IdentifyParams idParams)
110 public Integer getParamsForSerializing(IdentifyParams idParams)
117 LogicCoreAdapter.addComponentAdapter(new BitDisplayAdapter());
118 IndirectModelComponentCreator.setComponentSupplier(ModelBitDisplay.class.getCanonicalName(),
119 (m, p, n) -> new ModelBitDisplay(m, p.getAsInt(), n));