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