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