1d3bb1eea4671f9ccbbe4ac8fd81454d9cc2d9de
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelFixedOutput.java
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.Point;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
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.FixedOutputAdapter;
19 import net.mograsim.logic.model.serializing.IdentifyParams;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.logic.model.util.JsonHandler;
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
30         public final BitVector bits;
31
32         public ModelFixedOutput(LogicModelModifiable model, BitVector bits, String name)
33         {
34                 super(model, name, false);
35                 this.bits = bits;
36                 setSize(width, height);
37                 addPin(new Pin(model, this, "out", bits.length(), PinUsage.OUTPUT, width, height / 2));
38
39                 init();
40         }
41
42         @Override
43         public String getIDForSerializing(IdentifyParams idParams)
44         {
45                 return "FixedOutput";
46         }
47
48         @Override
49         public Object getParamsForSerializing(IdentifyParams idParams)
50         {
51                 return bits;
52         }
53
54         @Override
55         public void render(GeneralGC gc, Rectangle visibleRegion)
56         {
57                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
58                 if (foreground != null)
59                         gc.setForeground(foreground);
60                 gc.drawRectangle(getBounds());
61                 String label = BitVectorFormatter.formatAsString(bits);
62                 Font oldFont = gc.getFont();
63                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
64                 gc.setFont(labelFont);
65                 Point textExtent = gc.textExtent(label);
66                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
67                 if (textColor != null)
68                         gc.setForeground(textColor);
69                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, 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 }