5be3e22de69b9c7aae2f6fd6265c27b8c19ea112
[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.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.LogicObserver;
10 import net.mograsim.logic.core.components.CoreBitDisplay;
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.BitDisplayAdapter;
19 import net.mograsim.logic.model.serializing.IdentifyParams;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.preferences.Preferences;
22
23 public class ModelBitDisplay extends ModelComponent
24 {
25         private static final double width = 20;
26         private static final double height = 15;
27         private static final double fontHeight = 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;
60                 if (bitDisplay == null)
61                         label = BitVectorFormatter.formatAsString(null);
62                 else
63                 {
64                         BitVector toDisplay = bitDisplay.getDisplayedValue();
65                         label = toDisplay != null && toDisplay.isHighImpedance() ? "-"
66                                         : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
67                 }
68                 Font oldFont = gc.getFont();
69                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
70                 gc.setFont(labelFont);
71                 Point textExtent = gc.textExtent(label);
72                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
73                 if (textColor != null)
74                         gc.setForeground(textColor);
75                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
76                 gc.setFont(oldFont);
77         }
78
79         public void setCoreModelBinding(CoreBitDisplay bitDisplay)
80         {
81                 if (this.bitDisplay != null)
82                         this.bitDisplay.deregisterObserver(logicObs);
83                 this.bitDisplay = bitDisplay;
84                 if (bitDisplay != null)
85                         bitDisplay.registerObserver(logicObs);
86         }
87
88         public boolean hasCoreModelBinding()
89         {
90                 return bitDisplay != null;
91         }
92
93         public CoreBitDisplay getBitDisplay()
94         {
95                 return bitDisplay;
96         }
97
98         public Pin getInputPin()
99         {
100                 return inputPin;
101         }
102
103         @Override
104         public String getIDForSerializing(IdentifyParams idParams)
105         {
106                 return "BitDisplay";
107         }
108
109         @Override
110         public Integer getParamsForSerializing(IdentifyParams idParams)
111         {
112                 return logicWidth;
113         }
114
115         static
116         {
117                 LogicCoreAdapter.addComponentAdapter(new BitDisplayAdapter());
118                 IndirectModelComponentCreator.setComponentSupplier(ModelBitDisplay.class.getCanonicalName(),
119                                 (m, p, n) -> new ModelBitDisplay(m, p.getAsInt(), n));
120         }
121 }