9058d2e0204470ea2ff7f9c905685a7c30bf9281
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / SimpleRectangularGUIGate.java
1 package net.mograsim.logic.ui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.ui.model.ViewModelModifiable;
11 import net.mograsim.logic.ui.model.wires.MovablePin;
12 import net.mograsim.logic.ui.model.wires.Pin;
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(ViewModelModifiable 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, "Y", 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.remove(inputCount).name);
49                 else if (oldInputCount < inputCount)
50                         for (int i = oldInputCount; i < inputCount; i++)
51                         {
52                                 // TODO what for more than 24 input pins?
53                                 Pin pin = new Pin(this, String.valueOf((char) ('A' + i)), logicWidth, 0, pinDistance / 2 + i * pinDistance);
54                                 inputPins.add(pin);
55                                 addPin(pin);
56                         }
57                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
58         }
59
60         @Override
61         public void render(GeneralGC gc, Rectangle visibleRegion)
62         {
63                 double posX = getBounds().x;
64                 double posY = getBounds().y;
65
66                 double height = (getPins().size() - 1) * pinDistance;
67                 gc.drawRectangle(posX, posY, rectWidth, height);
68                 Font oldFont = gc.getFont();
69                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
70                 gc.setFont(labelFont);
71                 Point textExtent = gc.textExtent(label);
72                 gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
73                 gc.setFont(oldFont);
74                 if (isInverted)
75                         gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
76         }
77 }