Restructured the Preferences system
[Mograsim.git] / plugins / 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 static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR;
5
6 import java.util.Objects;
7
8 import org.eclipse.swt.graphics.Color;
9
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;
26
27 public class ModelFixedOutput extends ModelComponent
28 {
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;
33
34         public final BitVector bits;
35
36         public ModelFixedOutput(LogicModelModifiable model, BitVector bits, String name)
37         {
38                 super(model, name, false);
39                 this.bits = bits;
40                 setSize(width, height);
41                 addPin(new Pin(model, this, "out", bits.length(), PinUsage.OUTPUT, width, height / 2));
42
43                 init();
44         }
45
46         @Override
47         public String getIDForSerializing(IdentifyParams idParams)
48         {
49                 return "FixedOutput";
50         }
51
52         @Override
53         public Object getParamsForSerializing(IdentifyParams idParams)
54         {
55                 return bits;
56         }
57
58         @Override
59         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
60         {
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);
73                 gc.setFont(oldFont);
74         }
75
76         static
77         {
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));
81         }
82 }