1 package net.mograsim.logic.model.model.components.atomic;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR;
6 import java.util.ArrayList;
9 import org.eclipse.swt.graphics.Color;
11 import com.google.gson.JsonPrimitive;
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
16 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
17 import net.mograsim.logic.model.model.LogicModelModifiable;
18 import net.mograsim.logic.model.model.components.ModelComponent;
19 import net.mograsim.logic.model.model.wires.MovablePin;
20 import net.mograsim.logic.model.model.wires.Pin;
21 import net.mograsim.logic.model.model.wires.PinUsage;
22 import net.mograsim.logic.model.preferences.RenderPreferences;
23 import net.mograsim.logic.model.serializing.IdentifyParams;
25 public class SimpleRectangularModelGate extends ModelComponent
27 private static final double width = 20;
28 private static final double pinDistance = 10;
29 private static final double fontHeight = 5;
30 private static final double invertedCircleDiam = 3.5;
32 private final String id;
34 private final String label;
35 private final boolean isInverted;
36 protected final int logicWidth;
37 private final double rectWidth;
39 private MovablePin outputPin;
40 private final List<Pin> inputPins;
42 protected SimpleRectangularModelGate(LogicModelModifiable model, String id, String label, boolean isInverted, int logicWidth,
45 this(model, id, label, isInverted, logicWidth, name, true);
48 protected SimpleRectangularModelGate(LogicModelModifiable model, String id, String label, boolean isInverted, int logicWidth,
49 String name, boolean callInit)
51 super(model, name, false);
54 this.logicWidth = logicWidth;
55 this.isInverted = isInverted;
56 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
57 this.outputPin = new MovablePin(model, this, "Y", logicWidth, PinUsage.OUTPUT, width, 0);
59 this.inputPins = new ArrayList<>();
66 protected void setInputCount(int inputCount)
68 int oldInputCount = inputPins.size();
69 setSize(width, inputCount * pinDistance);
70 if (oldInputCount > inputCount)
71 while (inputPins.size() > inputCount)
72 removePin(inputPins.remove(inputCount).name);
73 else if (oldInputCount < inputCount)
74 for (int i = oldInputCount; i < inputCount; i++)
76 // TODO what for more than 24 input pins?
77 Pin pin = new Pin(model, this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0,
78 pinDistance / 2 + i * pinDistance);
82 outputPin.setRelPos(width, inputCount * pinDistance / 2);
86 public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
88 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
89 if (foreground != null)
90 gc.setForeground(foreground);
91 double height = (getPins().size() - 1) * pinDistance;
92 gc.drawRectangle(getPosX(), getPosY(), rectWidth, height);
94 gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
95 Font oldFont = gc.getFont();
96 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
97 gc.setFont(labelFont);
98 Point textExtent = gc.textExtent(label);
99 Color textColor = renderPrefs.getColor(TEXT_COLOR);
100 if (textColor != null)
101 gc.setForeground(textColor);
102 gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
109 public String getIDForSerializing(IdentifyParams idParams)
115 * {@link SimpleRectangularModelGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
118 * @see ModelComponent#getParamsForSerializing()
121 public Integer getParamsForSerializing(IdentifyParams idParams)