Restructured serializing classes
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / submodels / SimpleRectangularSubmodelComponent.java
1 package net.mograsim.logic.ui.model.components.submodels;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.TreeMap;
10
11 import org.eclipse.swt.graphics.Color;
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.ui.model.ViewModelModifiable;
18 import net.mograsim.logic.ui.model.wires.MovablePin;
19 import net.mograsim.logic.ui.model.wires.Pin;
20 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
21 import net.mograsim.preferences.Preferences;
22
23 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
24 {
25         public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
26
27         private static final double width = 35;
28         private static final double pinDistance = 10;
29         private static final double pinNameMargin = .5;
30         private static final double labelFontHeight = 5;
31         private static final double pinNameFontHeight = 3.5;
32
33         private final String label;
34         protected final int logicWidth;
35
36         private final List<String> inputPinNames;
37         private final List<String> inputPinNamesUnmodifiable;
38         private final List<String> outputPinNames;
39         private final List<String> outputPinNamesUnmodifiable;
40
41         public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
42         {
43                 super(model);
44                 this.label = label;
45                 this.logicWidth = logicWidth;
46                 this.inputPinNames = new ArrayList<>();
47                 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
48                 this.outputPinNames = new ArrayList<>();
49                 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
50         }
51
52         protected void setInputPins(String... pinNames)
53         {
54                 setIOPins(0, inputPinNames, outputPinNames, pinNames);
55         }
56
57         protected void setOutputPins(String... pinNames)
58         {
59                 setIOPins(width, outputPinNames, inputPinNames, pinNames);
60         }
61
62         private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
63         {
64                 int inputCount = newPinNames.length;
65                 List<String> newPinNamesList = Arrays.asList(newPinNames);
66                 if (new HashSet<>(newPinNamesList).size() != inputCount)
67                         throw new IllegalArgumentException("Pin names contain duplicates");
68                 for (String pinName : newPinNamesList)
69                         if (pinNamesListOtherSide.contains(pinName))
70                                 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
71                 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
72                 for (int i = 0; i < inputCount; i++)
73                 {
74                         String pinName = newPinNames[i];
75                         int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
76                         if (oldPinIndex == -1)
77                                 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
78                         else
79                                 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
80                 }
81                 for (String pinName : pinNamesListThisSide)
82                         if (!newPinNamesList.contains(pinName))
83                                 super.removeSubmodelInterface(pinName);
84                 pinNamesListThisSide.clear();
85                 pinNamesListThisSide.addAll(newPinNamesList);
86         }
87
88         public List<String> getInputPinNames()
89         {
90                 return inputPinNamesUnmodifiable;
91         }
92
93         public List<String> getOutputPinNames()
94         {
95                 return outputPinNamesUnmodifiable;
96         }
97
98         @Override
99         protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
100         {
101                 Font oldFont = gc.getFont();
102                 gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
103                 Point textExtent = gc.textExtent(label);
104                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
105                 if (textColor != null)
106                         gc.setForeground(textColor);
107                 gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
108                 gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
109                 for (int i = 0; i < inputPinNames.size(); i++)
110                 {
111                         String pinName = inputPinNames.get(i);
112                         textExtent = gc.textExtent(pinName);
113                         gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
114                 }
115                 for (int i = 0; i < outputPinNames.size(); i++)
116                 {
117                         String pinName = outputPinNames.get(i);
118                         textExtent = gc.textExtent(pinName);
119                         gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
120                                         getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
121                 }
122                 gc.setFont(oldFont);
123         }
124
125         @Override
126         protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
127         {
128                 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
129                 if (foreground != null)
130                         gc.setForeground(foreground);
131                 gc.drawRectangle(getBounds());
132         }
133
134         @Override
135         public SubmodelComponentParams calculateParams()
136         {
137                 SubmodelComponentParams ret = super.calculateParams();
138                 ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
139                 Map<String, Object> m = new TreeMap<>();
140                 m.put(kLabel, label);
141                 m.put(kInCount, inputPinNames.toArray());
142                 m.put(kOutCount, outputPinNames.toArray());
143                 m.put(kLogicWidth, logicWidth);
144                 ret.specialized = m;
145                 return ret;
146         }
147
148         @Override
149         protected Pin addSubmodelInterface(MovablePin supermodelPin)
150         {
151                 throw new UnsupportedOperationException(
152                                 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
153         }
154
155         @Override
156         protected void removeSubmodelInterface(String name)
157         {
158                 throw new UnsupportedOperationException(
159                                 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
160         }
161
162         @Override
163         protected void setSize(double width, double height)
164         {
165                 throw new UnsupportedOperationException(
166                                 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
167         }
168 }