1 package era.mi.gui.model.components;
3 import java.util.ArrayList;
6 import era.mi.gui.model.ViewModel;
7 import era.mi.gui.model.wires.MovablePin;
8 import era.mi.gui.model.wires.Pin;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14 public class SimpleRectangularGUIGate extends GUIComponent
16 private static final double width = 20;
17 private static final double pinDistance = 10;
18 private static final double fontHeight = 5;
19 private static final double invertedCircleDiam = 3.5;
21 private final String label;
22 protected final int logicWidth;
23 private final boolean isInverted;
24 private final double rectWidth;
26 private MovablePin outputPin;
27 private final List<Pin> inputPins;
29 protected SimpleRectangularGUIGate(ViewModel model, int logicWidth, String label, boolean isInverted)
33 this.logicWidth = logicWidth;
34 this.isInverted = isInverted;
35 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
36 this.outputPin = new MovablePin(this, logicWidth, width, 0);
38 this.inputPins = new ArrayList<>();
42 protected void setInputCount(int inputCount)
44 int oldInputCount = inputPins.size();
45 setSize(width, inputCount * pinDistance);
46 if (oldInputCount > inputCount)
47 while (inputPins.size() > inputCount)
48 removePin(inputPins.get(inputCount));
49 else if (oldInputCount < inputCount)
50 for (int i = oldInputCount; i < inputCount; i++)
52 Pin pin = new Pin(this, logicWidth, 0, pinDistance / 2 + i * pinDistance);
56 outputPin.setRelPos(width, inputCount * pinDistance / 2);
60 public void render(GeneralGC gc, Rectangle visibleRegion)
62 double posX = getBounds().x;
63 double posY = getBounds().y;
65 double height = inputPins.size() * pinDistance;
66 gc.drawRectangle(posX, posY, rectWidth, height);
67 Font oldFont = gc.getFont();
68 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
69 gc.setFont(labelFont);
70 Point textExtent = gc.textExtent(label);
71 gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
74 gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);