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.ViewModelModifiable;
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(ViewModelModifiable model, int logicWidth, String label)
35 this(model, logicWidth, label, null);
38 public SimpleRectangularSubmodelComponent(ViewModelModifiable 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(new MovablePin(this, pinName, logicWidth, usage, relX, pinDistance / 2 + i * pinDistance));
86 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
88 for (String pinName : pinNamesListThisSide)
89 if (!newPinNamesList.contains(pinName))
90 super.removeSubmodelInterface(pinName);
91 pinNamesListThisSide.clear();
92 pinNamesListThisSide.addAll(newPinNamesList);
95 public List<String> getInputPinNames()
97 return inputPinNamesUnmodifiable;
100 public List<String> getOutputPinNames()
102 return outputPinNamesUnmodifiable;
106 protected Pin addSubmodelInterface(MovablePin supermodelPin)
108 throw new UnsupportedOperationException(
109 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
113 protected void removeSubmodelInterface(String name)
115 throw new UnsupportedOperationException(
116 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
120 protected void setSize(double width, double height)
122 throw new UnsupportedOperationException(
123 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");