Added View for micro instruction editor
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIBitDisplay.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.components.BitDisplay;
14 import net.mograsim.logic.core.types.BitVectorFormatter;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
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.ViewLogicModelAdapter;
20 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
21 import net.mograsim.logic.model.serializing.IdentifierGetter;
22 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
23 import net.mograsim.preferences.Preferences;
24
25 public class GUIBitDisplay extends GUIComponent
26 {
27         private static final double width = 20;
28         private static final double height = 15;
29         private static final double fontHeight = 5;
30
31         public final int logicWidth;
32         private final Pin inputPin;
33
34         private final LogicObserver logicObs;
35         private BitDisplay bitDisplay;
36
37         public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
38         {
39                 this(model, logicWidth, null);
40         }
41
42         public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
43         {
44                 super(model, name);
45                 this.logicWidth = logicWidth;
46                 logicObs = (i) -> model.requestRedraw();
47
48                 setSize(width, height);
49                 addPin(this.inputPin = new Pin(this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
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 = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
60                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
61                 Font oldFont = gc.getFont();
62                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
63                 gc.setFont(labelFont);
64                 Point textExtent = gc.textExtent(label);
65                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
66                 if (textColor != null)
67                         gc.setForeground(textColor);
68                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
69                 gc.setFont(oldFont);
70         }
71
72         public void setLogicModelBinding(BitDisplay bitDisplay)
73         {
74                 if (this.bitDisplay != null)
75                         this.bitDisplay.deregisterObserver(logicObs);
76                 this.bitDisplay = bitDisplay;
77                 if (bitDisplay != null)
78                         bitDisplay.registerObserver(logicObs);
79         }
80
81         public boolean hasLogicModelBinding()
82         {
83                 return bitDisplay != null;
84         }
85
86         public BitDisplay getBitDisplay()
87         {
88                 return bitDisplay;
89         }
90
91         public Pin getInputPin()
92         {
93                 return inputPin;
94         }
95
96         @Override
97         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
98         {
99                 return new JsonPrimitive(logicWidth);
100         }
101
102         static
103         {
104                 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
105                 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
106                                 (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));
107         }
108 }