1 package net.mograsim.logic.model.model.components.submodels;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
9 import net.mograsim.logic.model.model.LogicModelModifiable;
10 import net.mograsim.logic.model.model.wires.MovablePin;
11 import net.mograsim.logic.model.model.wires.Pin;
12 import net.mograsim.logic.model.model.wires.PinUsage;
13 import net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer;
14 import net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer;
15 import net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer.SimpleRectangularLikeParams;
17 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
19 public static final double width = 35;
20 public static final double pinDistance = 10;
21 public static final double pinNameMargin = .5;
22 public static final double labelFontHeight = 5;
23 public static final double pinNameFontHeight = 3.5;
25 public final String label;
26 protected final int logicWidth;
28 private final List<String> inputPinNames;
29 private final List<String> inputPinNamesUnmodifiable;
30 private final List<String> outputPinNames;
31 private final List<String> outputPinNamesUnmodifiable;
33 public SimpleRectangularSubmodelComponent(LogicModelModifiable model, int logicWidth, String label)
35 this(model, logicWidth, label, null);
38 public SimpleRectangularSubmodelComponent(LogicModelModifiable model, int logicWidth, String label, String name)
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);
48 SimpleRectangularLikeParams rendererParams = new SimpleRectangularLikeParams();
49 rendererParams.centerText = label;
50 rendererParams.centerTextHeight = labelFontHeight;
51 rendererParams.horizontalComponentCenter = width / 2;
52 rendererParams.pinLabelHeight = pinNameFontHeight;
53 rendererParams.pinLabelMargin = pinNameMargin;
54 setSymbolRenderer(new SimpleRectangularLikeSymbolRenderer(this, rendererParams));
55 setOutlineRenderer(new DefaultOutlineRenderer(this));
58 protected void setInputPins(String... pinNames)
60 setIOPins(0, inputPinNames, outputPinNames, PinUsage.INPUT, pinNames);
63 protected void setOutputPins(String... pinNames)
65 setIOPins(width, outputPinNames, inputPinNames, PinUsage.OUTPUT, pinNames);
68 private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, PinUsage usage,
69 String... newPinNames)
71 int inputCount = newPinNames.length;
72 List<String> newPinNamesList = Arrays.asList(newPinNames);
73 if (new HashSet<>(newPinNamesList).size() != inputCount)
74 throw new IllegalArgumentException("Pin names contain duplicates");
75 for (String pinName : newPinNamesList)
76 if (pinNamesListOtherSide.contains(pinName))
77 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
78 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
79 for (int i = 0; i < inputCount; i++)
81 String pinName = newPinNames[i];
82 int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
83 if (oldPinIndex == -1)
84 super.addSubmodelInterface(
85 new MovablePin(model, this, pinName, logicWidth, usage, relX, pinDistance / 2 + i * pinDistance));
87 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
89 for (String pinName : pinNamesListThisSide)
90 if (!newPinNamesList.contains(pinName))
91 super.removeSubmodelInterface(pinName);
92 pinNamesListThisSide.clear();
93 pinNamesListThisSide.addAll(newPinNamesList);
96 public List<String> getInputPinNames()
98 return inputPinNamesUnmodifiable;
101 public List<String> getOutputPinNames()
103 return outputPinNamesUnmodifiable;
107 protected Pin addSubmodelInterface(MovablePin supermodelPin)
109 throw new UnsupportedOperationException(
110 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
114 protected void removeSubmodelInterface(String name)
116 throw new UnsupportedOperationException(
117 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
121 protected void setSize(double width, double height)
123 throw new UnsupportedOperationException(
124 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");