1 package net.mograsim.logic.ui.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 java.util.TreeMap;
11 import org.eclipse.swt.graphics.Color;
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
16 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
17 import net.mograsim.logic.ui.model.ViewModelModifiable;
18 import net.mograsim.logic.ui.model.wires.MovablePin;
19 import net.mograsim.logic.ui.model.wires.Pin;
20 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
21 import net.mograsim.preferences.Preferences;
23 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
25 public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
27 private static final double width = 35;
28 private static final double pinDistance = 10;
29 private static final double pinNameMargin = .5;
30 private static final double labelFontHeight = 5;
31 private static final double pinNameFontHeight = 3.5;
33 private final String label;
34 protected final int logicWidth;
36 private final List<String> inputPinNames;
37 private final List<String> inputPinNamesUnmodifiable;
38 private final List<String> outputPinNames;
39 private final List<String> outputPinNamesUnmodifiable;
41 public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
45 this.logicWidth = logicWidth;
46 this.inputPinNames = new ArrayList<>();
47 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
48 this.outputPinNames = new ArrayList<>();
49 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
52 protected void setInputPins(String... pinNames)
54 setIOPins(0, inputPinNames, outputPinNames, pinNames);
57 protected void setOutputPins(String... pinNames)
59 setIOPins(width, outputPinNames, inputPinNames, pinNames);
62 private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
64 int inputCount = newPinNames.length;
65 List<String> newPinNamesList = Arrays.asList(newPinNames);
66 if (new HashSet<>(newPinNamesList).size() != inputCount)
67 throw new IllegalArgumentException("Pin names contain duplicates");
68 for (String pinName : newPinNamesList)
69 if (pinNamesListOtherSide.contains(pinName))
70 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
71 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
72 for (int i = 0; i < inputCount; i++)
74 String pinName = newPinNames[i];
75 int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
76 if (oldPinIndex == -1)
77 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
79 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
81 for (String pinName : pinNamesListThisSide)
82 if (!newPinNamesList.contains(pinName))
83 super.removeSubmodelInterface(pinName);
84 pinNamesListThisSide.clear();
85 pinNamesListThisSide.addAll(newPinNamesList);
88 public List<String> getInputPinNames()
90 return inputPinNamesUnmodifiable;
93 public List<String> getOutputPinNames()
95 return outputPinNamesUnmodifiable;
99 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
101 Font oldFont = gc.getFont();
102 gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
103 Point textExtent = gc.textExtent(label);
104 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
105 if (textColor != null)
106 gc.setForeground(textColor);
107 gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
108 gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
109 for (int i = 0; i < inputPinNames.size(); i++)
111 String pinName = inputPinNames.get(i);
112 textExtent = gc.textExtent(pinName);
113 gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
115 for (int i = 0; i < outputPinNames.size(); i++)
117 String pinName = outputPinNames.get(i);
118 textExtent = gc.textExtent(pinName);
119 gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
120 getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
126 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
128 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
129 if (foreground != null)
130 gc.setForeground(foreground);
131 gc.drawRectangle(getBounds());
135 public SubmodelComponentParams calculateParams()
137 SubmodelComponentParams ret = super.calculateParams();
138 ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
139 Map<String, Object> m = new TreeMap<>();
140 m.put(kLabel, label);
141 m.put(kInCount, inputPinNames.toArray());
142 m.put(kOutCount, outputPinNames.toArray());
143 m.put(kLogicWidth, logicWidth);
149 protected Pin addSubmodelInterface(MovablePin supermodelPin)
151 throw new UnsupportedOperationException(
152 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
156 protected void removeSubmodelInterface(String name)
158 throw new UnsupportedOperationException(
159 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
163 protected void setSize(double width, double height)
165 throw new UnsupportedOperationException(
166 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");