ac92ecbac0127b99908213e79e0e7b9bf314daf4
[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 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.BitVectorFormatter;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.model.wires.PinUsage;
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
18 import net.mograsim.logic.model.serializing.IdentifyParams;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.preferences.Preferences;
21
22 public class GUIBitDisplay extends GUIComponent
23 {
24         private static final double width = 20;
25         private static final double height = 15;
26         private static final double fontHeight = 5;
27
28         public final int logicWidth;
29         private final Pin inputPin;
30
31         private final LogicObserver logicObs;
32         private CoreBitDisplay bitDisplay;
33
34         public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
35         {
36                 this(model, logicWidth, null);
37         }
38
39         public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
40         {
41                 super(model, name);
42                 this.logicWidth = logicWidth;
43                 logicObs = (i) -> model.requestRedraw();
44
45                 setSize(width, height);
46                 addPin(this.inputPin = new Pin(this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
47         }
48
49         @Override
50         public void render(GeneralGC gc, Rectangle visibleRegion)
51         {
52                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
53                 if (foreground != null)
54                         gc.setForeground(foreground);
55                 gc.drawRectangle(getBounds());
56                 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
57                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
58                 Font oldFont = gc.getFont();
59                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
60                 gc.setFont(labelFont);
61                 Point textExtent = gc.textExtent(label);
62                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
63                 if (textColor != null)
64                         gc.setForeground(textColor);
65                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
66                 gc.setFont(oldFont);
67         }
68
69         public void setLogicModelBinding(CoreBitDisplay bitDisplay)
70         {
71                 if (this.bitDisplay != null)
72                         this.bitDisplay.deregisterObserver(logicObs);
73                 this.bitDisplay = bitDisplay;
74                 if (bitDisplay != null)
75                         bitDisplay.registerObserver(logicObs);
76         }
77
78         public boolean hasLogicModelBinding()
79         {
80                 return bitDisplay != null;
81         }
82
83         public CoreBitDisplay getBitDisplay()
84         {
85                 return bitDisplay;
86         }
87
88         public Pin getInputPin()
89         {
90                 return inputPin;
91         }
92
93         @Override
94         public String getIDForSerializing(IdentifyParams idParams)
95         {
96                 return "GUIBitDisplay";
97         }
98
99         @Override
100         public Integer getParamsForSerializing(IdentifyParams idParams)
101         {
102                 return logicWidth;
103         }
104
105         static
106         {
107                 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
108                 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
109                                 (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));
110         }
111 }