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