Started restructuring LogicUI
[Mograsim.git] / LogicUI / src / era / mi / gui / model / components / RectangularShapedGUIGate.java
1 package era.mi.gui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
13
14 public class RectangularShapedGUIGate extends GUIComponent
15 {
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;
20
21         private final String label;
22         private final boolean isInverted;
23         private final double rectWidth;
24
25         private MovablePin outputPin;
26         private final List<Pin> inputPins;
27
28         protected RectangularShapedGUIGate(ViewModel model, String label, boolean isInverted)
29         {
30                 super(model);
31                 this.label = label;
32                 this.isInverted = isInverted;
33                 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
34                 this.outputPin = new MovablePin(this, width, 0);
35                 addPin(outputPin);
36                 this.inputPins = new ArrayList<>();
37                 setInputCount(1);
38         }
39
40         protected void setInputCount(int inputCount)
41         {
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++)
49                         {
50                                 Pin pin = new Pin(this, 0, pinDistance / 2 + i * pinDistance);
51                                 inputPins.add(pin);
52                                 addPin(pin);
53                         }
54                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
55         }
56
57         @Override
58         public void render(GeneralGC gc, Rectangle visibleRegion)
59         {
60                 double posX = getBounds().x;
61                 double posY = getBounds().y;
62
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);
70                 gc.setFont(oldFont);
71                 if (isInverted)
72                         gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
73         }
74 }