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 RectangularShapedGUIGate 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 private final boolean isInverted;
23 private final double rectWidth;
25 private MovablePin outputPin;
26 private final List<Pin> inputPins;
28 protected RectangularShapedGUIGate(ViewModel model, String label, boolean isInverted)
32 this.isInverted = isInverted;
33 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
34 this.outputPin = new MovablePin(this, width, 0);
36 this.inputPins = new ArrayList<>();
40 protected void setInputCount(int inputCount)
42 int oldInputCount = inputPins.size();
43 setSize(width, inputCount * pinDistance);
44 if (oldInputCount > inputCount)
45 while (inputPins.size() > inputCount)
46 removePin(inputPins.get(inputCount));
47 else if (oldInputCount < inputCount)
48 for (int i = oldInputCount; i < inputCount; i++)
50 Pin pin = new Pin(this, 0, pinDistance / 2 + i * pinDistance);
54 outputPin.setRelPos(width, inputCount * pinDistance / 2);
58 public void render(GeneralGC gc, Rectangle visibleRegion)
60 double posX = getBounds().x;
61 double posY = getBounds().y;
63 double height = inputPins.size() * pinDistance;
64 gc.drawRectangle(posX, posY, rectWidth, height);
65 Font oldFont = gc.getFont();
66 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
67 gc.setFont(labelFont);
68 Point textExtent = gc.textExtent(label);
69 gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
72 gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);