1 package net.mograsim.logic.model.model.components.atomic;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR;
6 import org.eclipse.swt.graphics.Color;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.core.LogicObserver;
12 import net.mograsim.logic.core.components.CoreBitDisplay;
13 import net.mograsim.logic.model.BitVectorFormatter;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
20 import net.mograsim.logic.model.preferences.RenderPreferences;
21 import net.mograsim.logic.model.serializing.IdentifyParams;
22 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
23 import net.mograsim.logic.model.util.TextRenderingHelper;
25 public class ModelBitDisplay extends ModelComponent
27 private static final double width = 20;
28 private static final double height = 10;
29 private static final double fontHeight = 5;
30 private static final double textMargin = 0.5;
32 public final int logicWidth;
33 private final Pin inputPin;
35 private final LogicObserver logicObs;
36 private CoreBitDisplay bitDisplay;
38 public ModelBitDisplay(LogicModelModifiable model, int logicWidth)
40 this(model, logicWidth, null);
43 public ModelBitDisplay(LogicModelModifiable model, int logicWidth, String name)
45 super(model, name, false);
46 this.logicWidth = logicWidth;
47 logicObs = (i) -> model.requestRedraw();
49 setSize(width, height);
50 addPin(this.inputPin = new Pin(model, this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
56 public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
58 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
59 if (foreground != null)
60 gc.setForeground(foreground);
61 gc.drawRectangle(getBounds());
62 String label = BitVectorFormatter.formatAsString(bitDisplay == null ? null : bitDisplay.getDisplayedValue(), true);
63 Font oldFont = gc.getFont();
64 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
65 gc.setFont(labelFont);
66 Color textColor = renderPrefs.getColor(TEXT_COLOR);
67 if (textColor != null)
68 gc.setForeground(textColor);
69 TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true);
73 public void setCoreModelBinding(CoreBitDisplay bitDisplay)
75 if (this.bitDisplay != null)
76 this.bitDisplay.deregisterObserver(logicObs);
77 this.bitDisplay = bitDisplay;
78 if (bitDisplay != null)
79 bitDisplay.registerObserver(logicObs);
82 public boolean hasCoreModelBinding()
84 return bitDisplay != null;
87 public CoreBitDisplay getBitDisplay()
92 public Pin getInputPin()
98 public String getIDForSerializing(IdentifyParams idParams)
104 public Integer getParamsForSerializing(IdentifyParams idParams)
111 LogicCoreAdapter.addComponentAdapter(new BitDisplayAdapter());
112 IndirectModelComponentCreator.setComponentSupplier(ModelBitDisplay.class.getCanonicalName(),
113 (m, p, n) -> new ModelBitDisplay(m, p.getAsInt(), n));