cbb8ab7eab47e83be7a2e0ee0e63e423430d9208
[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.Collections;
5 import java.util.List;
6
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;
14
15 public class SimpleRectangularGUIGate extends GUIComponent
16 {
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;
21
22         private final String label;
23         protected final int logicWidth;
24         private final boolean isInverted;
25         private final double rectWidth;
26
27         private MovablePin outputPin;
28         private final List<Pin> inputPins;
29         private final List<Pin> inputPinsUnmodifiable;
30
31         protected SimpleRectangularGUIGate(ViewModelModifiable model, int logicWidth, String label, boolean isInverted)
32         {
33                 super(model);
34                 this.label = label;
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);
39                 addPin(outputPin);
40                 this.inputPins = new ArrayList<>();
41                 this.inputPinsUnmodifiable = Collections.unmodifiableList(inputPins);
42                 setInputCount(1);
43         }
44
45         protected void setInputCount(int inputCount)
46         {
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++)
54                         {
55                                 // TODO what for more than 26 input pins?
56                                 Pin pin = new Pin(this, "A" + i, logicWidth, 0, pinDistance / 2 + i * pinDistance);
57                                 inputPins.add(pin);
58                                 addPin(pin);
59                         }
60                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
61         }
62
63         public Pin getOutputPin()
64         {
65                 return outputPin;
66         }
67
68         public List<Pin> getInputPins()
69         {
70                 return inputPinsUnmodifiable;
71         }
72
73         @Override
74         public void render(GeneralGC gc, Rectangle visibleRegion)
75         {
76                 double posX = getBounds().x;
77                 double posY = getBounds().y;
78
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);
86                 gc.setFont(oldFont);
87                 if (isInverted)
88                         gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
89         }
90 }