1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
9 import java.util.TreeMap;
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.ui.model.ModelVisitor;
16 import net.mograsim.logic.ui.model.ViewModelModifiable;
17 import net.mograsim.logic.ui.model.wires.MovablePin;
18 import net.mograsim.logic.ui.model.wires.Pin;
20 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
22 public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
24 private static final double width = 35;
25 private static final double pinDistance = 10;
26 private static final double pinNameMargin = .5;
27 private static final double labelFontHeight = 5;
28 private static final double pinNameFontHeight = 3.5;
30 private final String label;
31 protected final int logicWidth;
33 private final List<String> inputPinNames;
34 private final List<String> inputPinNamesUnmodifiable;
35 private final List<String> outputPinNames;
36 private final List<String> outputPinNamesUnmodifiable;
38 protected SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
42 this.logicWidth = logicWidth;
43 this.inputPinNames = new ArrayList<>();
44 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
45 this.outputPinNames = new ArrayList<>();
46 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
49 protected void setInputPins(String... pinNames)
51 setIOPins(0, inputPinNames, outputPinNames, pinNames);
54 protected void setOutputPins(String... pinNames)
56 setIOPins(width, outputPinNames, inputPinNames, pinNames);
59 private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
61 int inputCount = newPinNames.length;
62 List<String> newPinNamesList = Arrays.asList(newPinNames);
63 if (new HashSet<>(newPinNamesList).size() != inputCount)
64 throw new IllegalArgumentException("Pin names contain duplicates");
65 for (String pinName : newPinNamesList)
66 if (pinNamesListOtherSide.contains(pinName))
67 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
68 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
69 for (int i = 0; i < inputCount; i++)
71 String pinName = newPinNames[i];
72 int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
73 if (oldPinIndex == -1)
74 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
76 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
78 for (String pinName : pinNamesListThisSide)
79 if (!newPinNamesList.contains(pinName))
80 super.removeSubmodelInterface(pinName);
81 pinNamesListThisSide.clear();
82 pinNamesListThisSide.addAll(newPinNamesList);
85 public List<String> getInputPinNames()
87 return inputPinNamesUnmodifiable;
90 public List<String> getOutputPinNames()
92 return outputPinNamesUnmodifiable;
96 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
98 Font oldFont = gc.getFont();
99 gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
100 Point textExtent = gc.textExtent(label);
101 gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
102 gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
103 for (int i = 0; i < inputPinNames.size(); i++)
105 String pinName = inputPinNames.get(i);
106 textExtent = gc.textExtent(pinName);
107 gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
109 for (int i = 0; i < outputPinNames.size(); i++)
111 String pinName = outputPinNames.get(i);
112 textExtent = gc.textExtent(pinName);
113 gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
114 getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
120 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
122 gc.drawRectangle(getBounds());
126 public SubmodelComponentParams calculateParams()
128 SubmodelComponentParams ret = super.calculateParams();
129 ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
130 Map<String, Object> m = new TreeMap<>();
131 m.put(kLabel, label);
132 m.put(kInCount, inputPinNames.toArray());
133 m.put(kOutCount, outputPinNames.toArray());
134 m.put(kLogicWidth, logicWidth);
140 protected Pin addSubmodelInterface(MovablePin supermodelPin)
142 throw new UnsupportedOperationException(
143 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
147 protected void removeSubmodelInterface(String name)
149 throw new UnsupportedOperationException(
150 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
154 protected void setSize(double width, double height)
156 throw new UnsupportedOperationException(
157 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
161 public void accept(ModelVisitor mv)