Restructured the Preferences system
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelBitDisplay.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 org.eclipse.swt.graphics.Color;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.core.LogicObserver;
12 import net.mograsim.logic.core.components.CoreBitDisplay;
13 import net.mograsim.logic.model.BitVectorFormatter;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
20 import net.mograsim.logic.model.preferences.RenderPreferences;
21 import net.mograsim.logic.model.serializing.IdentifyParams;
22 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
23 import net.mograsim.logic.model.util.TextRenderingHelper;
24
25 public class ModelBitDisplay extends ModelComponent
26 {
27         private static final double width = 20;
28         private static final double height = 10;
29         private static final double fontHeight = 5;
30         private static final double textMargin = 0.5;
31
32         public final int logicWidth;
33         private final Pin inputPin;
34
35         private final LogicObserver logicObs;
36         private CoreBitDisplay bitDisplay;
37
38         public ModelBitDisplay(LogicModelModifiable model, int logicWidth)
39         {
40                 this(model, logicWidth, null);
41         }
42
43         public ModelBitDisplay(LogicModelModifiable model, int logicWidth, String name)
44         {
45                 super(model, name, false);
46                 this.logicWidth = logicWidth;
47                 logicObs = (i) -> model.requestRedraw();
48
49                 setSize(width, height);
50                 addPin(this.inputPin = new Pin(model, this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
51
52                 init();
53         }
54
55         @Override
56         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
57         {
58                 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
59                 if (foreground != null)
60                         gc.setForeground(foreground);
61                 gc.drawRectangle(getBounds());
62                 String label = BitVectorFormatter.formatAsString(bitDisplay == null ? null : bitDisplay.getDisplayedValue(), true);
63                 Font oldFont = gc.getFont();
64                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
65                 gc.setFont(labelFont);
66                 Color textColor = renderPrefs.getColor(TEXT_COLOR);
67                 if (textColor != null)
68                         gc.setForeground(textColor);
69                 TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true);
70                 gc.setFont(oldFont);
71         }
72
73         public void setCoreModelBinding(CoreBitDisplay bitDisplay)
74         {
75                 if (this.bitDisplay != null)
76                         this.bitDisplay.deregisterObserver(logicObs);
77                 this.bitDisplay = bitDisplay;
78                 if (bitDisplay != null)
79                         bitDisplay.registerObserver(logicObs);
80         }
81
82         public boolean hasCoreModelBinding()
83         {
84                 return bitDisplay != null;
85         }
86
87         public CoreBitDisplay getBitDisplay()
88         {
89                 return bitDisplay;
90         }
91
92         public Pin getInputPin()
93         {
94                 return inputPin;
95         }
96
97         @Override
98         public String getIDForSerializing(IdentifyParams idParams)
99         {
100                 return "BitDisplay";
101         }
102
103         @Override
104         public Integer getParamsForSerializing(IdentifyParams idParams)
105         {
106                 return logicWidth;
107         }
108
109         static
110         {
111                 LogicCoreAdapter.addComponentAdapter(new BitDisplayAdapter());
112                 IndirectModelComponentCreator.setComponentSupplier(ModelBitDisplay.class.getCanonicalName(),
113                                 (m, p, n) -> new ModelBitDisplay(m, p.getAsInt(), n));
114         }
115 }