Added concept of wire width to GUIComponents and GUIWires
[Mograsim.git] / LogicUI / src / era / mi / gui / model / components / SimpleRectangularGUIGate.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 SimpleRectangularGUIGate 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         protected final int logicWidth;
23         private final boolean isInverted;
24         private final double rectWidth;
25
26         private MovablePin outputPin;
27         private final List<Pin> inputPins;
28
29         protected SimpleRectangularGUIGate(ViewModel model, int logicWidth, String label, boolean isInverted)
30         {
31                 super(model);
32                 this.label = label;
33                 this.logicWidth = logicWidth;
34                 this.isInverted = isInverted;
35                 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
36                 this.outputPin = new MovablePin(this, logicWidth, width, 0);
37                 addPin(outputPin);
38                 this.inputPins = new ArrayList<>();
39                 setInputCount(1);
40         }
41
42         protected void setInputCount(int inputCount)
43         {
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++)
51                         {
52                                 Pin pin = new Pin(this, logicWidth, 0, pinDistance / 2 + i * pinDistance);
53                                 inputPins.add(pin);
54                                 addPin(pin);
55                         }
56                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
57         }
58
59         @Override
60         public void render(GeneralGC gc, Rectangle visibleRegion)
61         {
62                 double posX = getBounds().x;
63                 double posY = getBounds().y;
64
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);
72                 gc.setFont(oldFont);
73                 if (isInverted)
74                         gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
75         }
76 }