1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.wires.MovablePin;
13 import net.mograsim.logic.ui.model.wires.Pin;
15 public class SimpleRectangularGUIGate extends GUIComponent
17 private static final double width = 20;
18 private static final double pinDistance = 10;
19 private static final double fontHeight = 5;
20 private static final double invertedCircleDiam = 3.5;
22 private final String label;
23 protected final int logicWidth;
24 private final boolean isInverted;
25 private final double rectWidth;
27 private MovablePin outputPin;
28 private final List<Pin> inputPins;
29 private final List<Pin> inputPinsUnmodifiable;
31 protected SimpleRectangularGUIGate(ViewModelModifiable model, int logicWidth, String label, boolean isInverted)
35 this.logicWidth = logicWidth;
36 this.isInverted = isInverted;
37 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
38 this.outputPin = new MovablePin(this, "Y", logicWidth, width, 0);
40 this.inputPins = new ArrayList<>();
41 this.inputPinsUnmodifiable = Collections.unmodifiableList(inputPins);
45 protected void setInputCount(int inputCount)
47 int oldInputCount = inputPins.size();
48 setSize(width, inputCount * pinDistance);
49 if (oldInputCount > inputCount)
50 while (inputPins.size() > inputCount)
51 removePin(inputPins.remove(inputCount));
52 else if (oldInputCount < inputCount)
53 for (int i = oldInputCount; i < inputCount; i++)
55 // TODO what for more than 26 input pins?
56 Pin pin = new Pin(this, "A" + i, logicWidth, 0, pinDistance / 2 + i * pinDistance);
60 outputPin.setRelPos(width, inputCount * pinDistance / 2);
63 public Pin getOutputPin()
68 public List<Pin> getInputPins()
70 return inputPinsUnmodifiable;
74 public void render(GeneralGC gc, Rectangle visibleRegion)
76 double posX = getBounds().x;
77 double posY = getBounds().y;
79 double height = inputPins.size() * pinDistance;
80 gc.drawRectangle(posX, posY, rectWidth, height);
81 Font oldFont = gc.getFont();
82 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
83 gc.setFont(labelFont);
84 Point textExtent = gc.textExtent(label);
85 gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
88 gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);