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.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
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.IdentifierGetter;
20 import net.mograsim.preferences.Preferences;
22 public class SimpleRectangularGUIGate extends GUIComponent
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 label;
30 private final boolean isInverted;
31 protected final int logicWidth;
32 private final double rectWidth;
34 private MovablePin outputPin;
35 private final List<Pin> inputPins;
37 protected SimpleRectangularGUIGate(ViewModelModifiable model, String label, boolean isInverted, int logicWidth, String name)
41 this.logicWidth = logicWidth;
42 this.isInverted = isInverted;
43 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
44 this.outputPin = new MovablePin(this, "Y", logicWidth, PinUsage.OUTPUT, width, 0);
46 this.inputPins = new ArrayList<>();
50 protected void setInputCount(int inputCount)
52 int oldInputCount = inputPins.size();
53 setSize(width, inputCount * pinDistance);
54 if (oldInputCount > inputCount)
55 while (inputPins.size() > inputCount)
56 removePin(inputPins.remove(inputCount).name);
57 else if (oldInputCount < inputCount)
58 for (int i = oldInputCount; i < inputCount; i++)
60 // TODO what for more than 24 input pins?
61 Pin pin = new Pin(this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0, pinDistance / 2 + i * pinDistance);
65 outputPin.setRelPos(width, inputCount * pinDistance / 2);
69 public void render(GeneralGC gc, Rectangle visibleRegion)
71 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
72 if (foreground != null)
73 gc.setForeground(foreground);
74 double height = (getPins().size() - 1) * pinDistance;
75 gc.drawRectangle(getPosX(), getPosY(), rectWidth, height);
77 gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
78 Font oldFont = gc.getFont();
79 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
80 gc.setFont(labelFont);
81 Point textExtent = gc.textExtent(label);
82 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
83 if (textColor != null)
84 gc.setForeground(textColor);
85 gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
92 * {@link SimpleRectangularGUIGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
95 * @see GUIComponent#getParamsForSerializing()
98 public Integer getParamsForSerializing(IdentifierGetter idGetter)