1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
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.modeladapter.ViewLogicModelAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
20 import net.mograsim.logic.model.serializing.IdentifierGetter;
21 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
22 import net.mograsim.preferences.Preferences;
24 public class GUIBitDisplay extends GUIComponent
26 private static final double width = 20;
27 private static final double height = 15;
28 private static final double fontHeight = 5;
30 public final int logicWidth;
31 private final Pin inputPin;
33 private final LogicObserver logicObs;
34 private BitDisplay bitDisplay;
36 public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
38 this(model, logicWidth, null);
41 public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
44 this.logicWidth = logicWidth;
45 logicObs = (i) -> model.requestRedraw();
47 setSize(width, height);
48 addPin(this.inputPin = new Pin(this, "", logicWidth, 0, height / 2));
52 public void render(GeneralGC gc, Rectangle visibleRegion)
54 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
55 if (foreground != null)
56 gc.setForeground(foreground);
57 gc.drawRectangle(getBounds());
58 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
59 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
60 Font oldFont = gc.getFont();
61 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62 gc.setFont(labelFont);
63 Point textExtent = gc.textExtent(label);
64 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65 if (textColor != null)
66 gc.setForeground(textColor);
67 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
71 public void setLogicModelBinding(BitDisplay bitDisplay)
73 if (this.bitDisplay != null)
74 this.bitDisplay.deregisterObserver(logicObs);
75 this.bitDisplay = bitDisplay;
76 if (bitDisplay != null)
77 bitDisplay.registerObserver(logicObs);
80 public boolean hasLogicModelBinding()
82 return bitDisplay != null;
85 public BitDisplay getBitDisplay()
90 public Pin getInputPin()
96 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
98 return new JsonPrimitive(logicWidth);
103 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
104 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
105 (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));