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