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