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.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;
25 public class GUIBitDisplay extends GUIComponent
27 private static final double width = 20;
28 private static final double height = 15;
29 private static final double fontHeight = 5;
31 public final int logicWidth;
32 private final Pin inputPin;
34 private final LogicObserver logicObs;
35 private BitDisplay bitDisplay;
37 public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
39 this(model, logicWidth, null);
42 public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
45 this.logicWidth = logicWidth;
46 logicObs = (i) -> model.requestRedraw();
48 setSize(width, height);
49 addPin(this.inputPin = new Pin(this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
53 public void render(GeneralGC gc, Rectangle visibleRegion)
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);
72 public void setLogicModelBinding(BitDisplay bitDisplay)
74 if (this.bitDisplay != null)
75 this.bitDisplay.deregisterObserver(logicObs);
76 this.bitDisplay = bitDisplay;
77 if (bitDisplay != null)
78 bitDisplay.registerObserver(logicObs);
81 public boolean hasLogicModelBinding()
83 return bitDisplay != null;
86 public BitDisplay getBitDisplay()
91 public Pin getInputPin()
97 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
99 return new JsonPrimitive(logicWidth);
104 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
105 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
106 (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));