Pins now have a PinUsage
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / SimpleRectangularGUIGate.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.swt.graphics.Color;
7
8 import com.google.gson.JsonElement;
9 import com.google.gson.JsonPrimitive;
10
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.model.wires.MovablePin;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.serializing.IdentifierGetter;
21 import net.mograsim.preferences.Preferences;
22
23 public class SimpleRectangularGUIGate extends GUIComponent
24 {
25         private static final double width = 20;
26         private static final double pinDistance = 10;
27         private static final double fontHeight = 5;
28         private static final double invertedCircleDiam = 3.5;
29
30         private final String label;
31         private final boolean isInverted;
32         protected final int logicWidth;
33         private final double rectWidth;
34
35         private MovablePin outputPin;
36         private final List<Pin> inputPins;
37
38         protected SimpleRectangularGUIGate(ViewModelModifiable model, String label, boolean isInverted, int logicWidth, String name)
39         {
40                 super(model, name);
41                 this.label = label;
42                 this.logicWidth = logicWidth;
43                 this.isInverted = isInverted;
44                 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
45                 this.outputPin = new MovablePin(this, "Y", logicWidth, PinUsage.OUTPUT, width, 0);
46                 addPin(outputPin);
47                 this.inputPins = new ArrayList<>();
48                 setInputCount(1);
49         }
50
51         protected void setInputCount(int inputCount)
52         {
53                 int oldInputCount = inputPins.size();
54                 setSize(width, inputCount * pinDistance);
55                 if (oldInputCount > inputCount)
56                         while (inputPins.size() > inputCount)
57                                 removePin(inputPins.remove(inputCount).name);
58                 else if (oldInputCount < inputCount)
59                         for (int i = oldInputCount; i < inputCount; i++)
60                         {
61                                 // TODO what for more than 24 input pins?
62                                 Pin pin = new Pin(this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0, pinDistance / 2 + i * pinDistance);
63                                 inputPins.add(pin);
64                                 addPin(pin);
65                         }
66                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
67         }
68
69         @Override
70         public void render(GeneralGC gc, Rectangle visibleRegion)
71         {
72                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
73                 if (foreground != null)
74                         gc.setForeground(foreground);
75                 double height = (getPins().size() - 1) * pinDistance;
76                 gc.drawRectangle(getPosX(), getPosY(), rectWidth, height);
77                 if (isInverted)
78                         gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
79                 Font oldFont = gc.getFont();
80                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
81                 gc.setFont(labelFont);
82                 Point textExtent = gc.textExtent(label);
83                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
84                 if (textColor != null)
85                         gc.setForeground(textColor);
86                 gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
87                 gc.setFont(oldFont);
88         }
89
90         // serializing
91
92         /**
93          * {@link SimpleRectangularGUIGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
94          * this component.
95          * 
96          * @see GUIComponent#getParamsForSerializing()
97          */
98         @Override
99         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
100         {
101                 return new JsonPrimitive(logicWidth);
102         }
103 }