Renamed ViewModel to LogicModel
[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);
35                 this.bits = bits;
36                 setSize(width, height);
37                 addPin(new Pin(this, "out", bits.length(), PinUsage.OUTPUT, width, height / 2));
38         }
39
40         @Override
41         public String getIDForSerializing(IdentifyParams idParams)
42         {
43                 return "FixedOutput";
44         }
45
46         @Override
47         public Object getParamsForSerializing(IdentifyParams idParams)
48         {
49                 return bits;
50         }
51
52         @Override
53         public void render(GeneralGC gc, Rectangle visibleRegion)
54         {
55                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
56                 if (foreground != null)
57                         gc.setForeground(foreground);
58                 gc.drawRectangle(getBounds());
59                 String label = BitVectorFormatter.formatAsString(bits);
60                 Font oldFont = gc.getFont();
61                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62                 gc.setFont(labelFont);
63                 Point textExtent = gc.textExtent(label);
64                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65                 if (textColor != null)
66                         gc.setForeground(textColor);
67                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
68                 gc.setFont(oldFont);
69         }
70
71         static
72         {
73                 LogicCoreAdapter.addComponentAdapter(new FixedOutputAdapter());
74                 IndirectModelComponentCreator.setComponentSupplier(ModelFixedOutput.class.getCanonicalName(),
75                                 (m, p, n) -> new ModelFixedOutput(m, Objects.requireNonNull(JsonHandler.fromJsonTree(p, BitVector.class)), n));
76         }
77 }