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