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 java.util.Objects;
8 import org.eclipse.swt.graphics.Color;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.model.BitVectorFormatter;
15 import net.mograsim.logic.model.model.LogicModelModifiable;
16 import net.mograsim.logic.model.model.components.ModelComponent;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.model.wires.PinUsage;
19 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
20 import net.mograsim.logic.model.modeladapter.componentadapters.FixedOutputAdapter;
21 import net.mograsim.logic.model.preferences.RenderPreferences;
22 import net.mograsim.logic.model.serializing.IdentifyParams;
23 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
24 import net.mograsim.logic.model.util.JsonHandler;
25 import net.mograsim.logic.model.util.TextRenderingHelper;
27 public class ModelFixedOutput extends ModelComponent
29 private static final double width = 10;
30 private static final double height = 10;
31 private static final double fontHeight = 5;
32 private static final double textMargin = 0.5;
34 public final BitVector bits;
36 public ModelFixedOutput(LogicModelModifiable model, BitVector bits, String name)
38 super(model, name, false);
40 setSize(width, height);
41 addPin(new Pin(model, this, "out", bits.length(), PinUsage.OUTPUT, width, height / 2));
47 public String getIDForSerializing(IdentifyParams idParams)
53 public Object getParamsForSerializing(IdentifyParams idParams)
59 public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
61 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
62 if (foreground != null)
63 gc.setForeground(foreground);
64 gc.drawRectangle(getBounds());
65 String label = BitVectorFormatter.formatAsString(bits, false);
66 Font oldFont = gc.getFont();
67 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
68 gc.setFont(labelFont);
69 Color textColor = renderPrefs.getColor(TEXT_COLOR);
70 if (textColor != null)
71 gc.setForeground(textColor);
72 TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true);
78 LogicCoreAdapter.addComponentAdapter(new FixedOutputAdapter());
79 IndirectModelComponentCreator.setComponentSupplier(ModelFixedOutput.class.getCanonicalName(),
80 (m, p, n) -> new ModelFixedOutput(m, Objects.requireNonNull(JsonHandler.fromJsonTree(p, BitVector.class)), n));