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 org.eclipse.swt.graphics.Color;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.model.model.ViewModelModifiable;
14 import net.mograsim.logic.model.model.wires.MovablePin;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.snippets.Renderer;
17 import net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer;
18 import net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer.SimpleRectangularLikeParams;
19 import net.mograsim.preferences.Preferences;
21 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
23 public static final double width = 35;
24 public static final double pinDistance = 10;
25 public static final double pinNameMargin = .5;
26 public static final double labelFontHeight = 5;
27 public static final double pinNameFontHeight = 3.5;
29 public final String label;
30 protected final int logicWidth;
32 private final List<String> inputPinNames;
33 private final List<String> inputPinNamesUnmodifiable;
34 private final List<String> outputPinNames;
35 private final List<String> outputPinNamesUnmodifiable;
37 private Renderer symbolRenderer;
39 public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
41 this(model, logicWidth, label, null);
44 public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label, String name)
48 this.logicWidth = logicWidth;
49 this.inputPinNames = new ArrayList<>();
50 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
51 this.outputPinNames = new ArrayList<>();
52 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
54 SimpleRectangularLikeParams rendererParams = new SimpleRectangularLikeParams();
55 rendererParams.centerText = label;
56 rendererParams.centerTextHeight = labelFontHeight;
57 rendererParams.horizontalComponentCenter = getWidth() / 2;
58 rendererParams.pinLabelHeight = pinNameFontHeight;
59 rendererParams.pinLabelMargin = pinNameMargin;
60 symbolRenderer = new SimpleRectangularLikeSymbolRenderer(this, rendererParams);
63 protected void setInputPins(String... pinNames)
65 setIOPins(0, inputPinNames, outputPinNames, pinNames);
68 protected void setOutputPins(String... pinNames)
70 setIOPins(width, outputPinNames, inputPinNames, pinNames);
73 private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
75 int inputCount = newPinNames.length;
76 List<String> newPinNamesList = Arrays.asList(newPinNames);
77 if (new HashSet<>(newPinNamesList).size() != inputCount)
78 throw new IllegalArgumentException("Pin names contain duplicates");
79 for (String pinName : newPinNamesList)
80 if (pinNamesListOtherSide.contains(pinName))
81 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
82 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
83 for (int i = 0; i < inputCount; i++)
85 String pinName = newPinNames[i];
86 int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
87 if (oldPinIndex == -1)
88 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
90 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
92 for (String pinName : pinNamesListThisSide)
93 if (!newPinNamesList.contains(pinName))
94 super.removeSubmodelInterface(pinName);
95 pinNamesListThisSide.clear();
96 pinNamesListThisSide.addAll(newPinNamesList);
99 public List<String> getInputPinNames()
101 return inputPinNamesUnmodifiable;
104 public List<String> getOutputPinNames()
106 return outputPinNamesUnmodifiable;
110 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
112 symbolRenderer.render(gc, visibleRegion);
116 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
118 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
119 if (foreground != null)
120 gc.setForeground(foreground);
121 gc.drawRectangle(getBounds());
125 protected Pin addSubmodelInterface(MovablePin supermodelPin)
127 throw new UnsupportedOperationException(
128 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
132 protected void removeSubmodelInterface(String name)
134 throw new UnsupportedOperationException(
135 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
139 protected void setSize(double width, double height)
141 throw new UnsupportedOperationException(
142 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");