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 private final Pin inputPin;
32 private final LogicObserver logicObs;
33 private BitDisplay bitDisplay;
35 public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
37 this(model, logicWidth, null);
40 public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
43 logicObs = (i) -> model.requestRedraw();
45 setSize(width, height);
46 addPin(this.inputPin = new Pin(this, "", logicWidth, 0, height / 2));
50 public void render(GeneralGC gc, Rectangle visibleRegion)
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);
69 public void setLogicModelBinding(BitDisplay bitDisplay)
71 if (this.bitDisplay != null)
72 this.bitDisplay.deregisterObserver(logicObs);
73 this.bitDisplay = bitDisplay;
74 if (bitDisplay != null)
75 bitDisplay.registerObserver(logicObs);
78 public boolean hasLogicModelBinding()
80 return bitDisplay != null;
83 public BitDisplay getBitDisplay()
88 public Pin getInputPin()
94 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
96 return new JsonPrimitive(inputPin.logicWidth);
101 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
102 IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
103 (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));