9a62cadbf34a4afdcd0a663f3fa261bb58947d85
[Mograsim.git] /
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import java.util.Objects;
4
5 import org.eclipse.swt.graphics.Color;
6
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.core.types.BitVector;
11 import net.mograsim.logic.core.types.BitVectorFormatter;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.model.wires.PinUsage;
16 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.FixedOutputAdapter;
18 import net.mograsim.logic.model.serializing.IdentifyParams;
19 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
20 import net.mograsim.logic.model.util.JsonHandler;
21 import net.mograsim.logic.model.util.TextRenderingHelper;
22 import net.mograsim.preferences.Preferences;
23
24 public class ModelFixedOutput extends ModelComponent
25 {
26         private static final double width = 20;
27         private static final double height = 20;
28         private static final double fontHeight = 5;
29         private static final double textMargin = 0.5;
30
31         public final BitVector bits;
32
33         public ModelFixedOutput(LogicModelModifiable model, BitVector bits, String name)
34         {
35                 super(model, name, false);
36                 this.bits = bits;
37                 setSize(width, height);
38                 addPin(new Pin(model, this, "out", bits.length(), PinUsage.OUTPUT, width, height / 2));
39
40                 init();
41         }
42
43         @Override
44         public String getIDForSerializing(IdentifyParams idParams)
45         {
46                 return "FixedOutput";
47         }
48
49         @Override
50         public Object getParamsForSerializing(IdentifyParams idParams)
51         {
52                 return bits;
53         }
54
55         @Override
56         public void render(GeneralGC gc, Rectangle visibleRegion)
57         {
58                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
59                 if (foreground != null)
60                         gc.setForeground(foreground);
61                 gc.drawRectangle(getBounds());
62                 String label = BitVectorFormatter.formatAsString(bits, false);
63                 Font oldFont = gc.getFont();
64                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
65                 gc.setFont(labelFont);
66                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
67                 if (textColor != null)
68                         gc.setForeground(textColor);
69                 TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true);
70                 gc.setFont(oldFont);
71         }
72
73         static
74         {
75                 LogicCoreAdapter.addComponentAdapter(new FixedOutputAdapter());
76                 IndirectModelComponentCreator.setComponentSupplier(ModelFixedOutput.class.getCanonicalName(),
77                                 (m, p, n) -> new ModelFixedOutput(m, Objects.requireNonNull(JsonHandler.fromJsonTree(p, BitVector.class)), n));
78         }
79 }