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.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.wires.MovablePin;
17 import net.mograsim.logic.ui.model.wires.Pin;
19 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
21 public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
23 private static final double width = 35;
24 private static final double pinDistance = 10;
25 private static final double pinNameMargin = .5;
26 private static final double labelFontHeight = 5;
27 private static final double pinNameFontHeight = 3.5;
29 private 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 protected SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
41 this.logicWidth = logicWidth;
42 this.inputPinNames = new ArrayList<>();
43 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
44 this.outputPinNames = new ArrayList<>();
45 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
48 protected void setInputPins(String... pinNames)
50 setIOPins(0, inputPinNames, outputPinNames, pinNames);
53 protected void setOutputPins(String... pinNames)
55 setIOPins(width, outputPinNames, inputPinNames, pinNames);
58 private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
60 int inputCount = newPinNames.length;
61 List<String> newPinNamesList = Arrays.asList(newPinNames);
62 if (new HashSet<>(newPinNamesList).size() != inputCount)
63 throw new IllegalArgumentException("Pin names contain duplicates");
64 for (String pinName : newPinNamesList)
65 if (pinNamesListOtherSide.contains(pinName))
66 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
67 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
68 for (int i = 0; i < inputCount; i++)
70 String pinName = newPinNames[i];
71 int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
72 if (oldPinIndex == -1)
73 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
75 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
77 for (String pinName : pinNamesListThisSide)
78 if (!newPinNamesList.contains(pinName))
79 super.removeSubmodelInterface(pinName);
80 pinNamesListThisSide.clear();
81 pinNamesListThisSide.addAll(newPinNamesList);
84 public List<String> getInputPinNames()
86 return inputPinNamesUnmodifiable;
89 public List<String> getOutputPinNames()
91 return outputPinNamesUnmodifiable;
95 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
97 Font oldFont = gc.getFont();
98 gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
99 Point textExtent = gc.textExtent(label);
100 gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
101 gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
102 for (int i = 0; i < inputPinNames.size(); i++)
104 String pinName = inputPinNames.get(i);
105 textExtent = gc.textExtent(pinName);
106 gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
108 for (int i = 0; i < outputPinNames.size(); i++)
110 String pinName = outputPinNames.get(i);
111 textExtent = gc.textExtent(pinName);
112 gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
113 getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
119 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
121 gc.drawRectangle(getBounds());
125 public SubmodelComponentParams calculateParams()
127 SubmodelComponentParams ret = super.calculateParams();
128 ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
129 Map<String, Object> m = new TreeMap<>();
130 m.put(kLabel, label);
131 m.put(kInCount, inputPinNames.size());
132 m.put(kOutCount, outputPinNames.size());
133 m.put(kLogicWidth, logicWidth);
139 protected Pin addSubmodelInterface(MovablePin supermodelPin)
141 throw new UnsupportedOperationException(
142 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
146 protected void removeSubmodelInterface(String name)
148 throw new UnsupportedOperationException(
149 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
153 protected void setSize(double width, double height)
155 throw new UnsupportedOperationException(
156 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");