package net.mograsim.logic.ui.model.components;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
+import org.eclipse.swt.graphics.Color;
+
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.ui.model.ViewModelModifiable;
+import net.mograsim.logic.ui.model.wires.MovablePin;
import net.mograsim.logic.ui.model.wires.Pin;
+import net.mograsim.preferences.Preferences;
public class SimpleRectangularSubmodelComponent extends SubmodelComponent
{
private static final double width = 35;
private static final double pinDistance = 10;
- private static final double fontHeight = 5;
+ private static final double pinNameMargin = .5;
+ private static final double labelFontHeight = 5;
+ private static final double pinNameFontHeight = 3.5;
private final String label;
protected final int logicWidth;
- private final List<Pin> inputSupermodelPins;
- private final List<Pin> inputSupermodelPinsUnmodifiable;
- private final List<Pin> outputSupermodelPins;
- private final List<Pin> outputSupermodelPinsUnmodifiable;
- private final List<Pin> inputSubmodelPins;
- private final List<Pin> inputSubmodelPinsUnmodifiable;
- private final List<Pin> outputSubmodelPins;
- private final List<Pin> outputSubmodelPinsUnmodifiable;
+ private final List<String> inputPinNames;
+ private final List<String> inputPinNamesUnmodifiable;
+ private final List<String> outputPinNames;
+ private final List<String> outputPinNamesUnmodifiable;
protected SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
{
super(model);
this.label = label;
this.logicWidth = logicWidth;
- this.inputSupermodelPins = new ArrayList<>();
- this.inputSupermodelPinsUnmodifiable = Collections.unmodifiableList(inputSupermodelPins);
- this.outputSupermodelPins = new ArrayList<>();
- this.outputSupermodelPinsUnmodifiable = Collections.unmodifiableList(outputSupermodelPins);
- this.inputSubmodelPins = new ArrayList<>();
- this.inputSubmodelPinsUnmodifiable = Collections.unmodifiableList(inputSubmodelPins);
- this.outputSubmodelPins = new ArrayList<>();
- this.outputSubmodelPinsUnmodifiable = Collections.unmodifiableList(outputSubmodelPins);
+ this.inputPinNames = new ArrayList<>();
+ this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
+ this.outputPinNames = new ArrayList<>();
+ this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
}
- protected void setInputCount(int inputCount)
+ protected void setInputPins(String... pinNames)
{
- int oldInputCount = inputSupermodelPins.size();
- double height = Math.max(inputCount, outputSupermodelPins.size()) * pinDistance;
- setSize(width, height);
- if (oldInputCount > inputCount)
- while (inputSupermodelPins.size() > inputCount)
- {
- inputSubmodelPins.remove(inputCount);
- removePin(inputSupermodelPins.remove(inputCount));
- }
- else if (oldInputCount < inputCount)
- for (int i = oldInputCount; i < inputCount; i++)
- {
- // TODO pin names
- Pin submodelPin = addSubmodelInterface("Input pin #" + i, logicWidth, 0, pinDistance / 2 + i * pinDistance);
- inputSubmodelPins.add(submodelPin);
- inputSupermodelPins.add(getSupermodelPin(submodelPin));
- }
+ setIOPins(0, inputPinNames, outputPinNames, pinNames);
}
- protected void setOutputCount(int outputCount)
+ protected void setOutputPins(String... pinNames)
{
- int oldOutputCount = outputSupermodelPins.size();
- setSize(width, Math.max(inputSupermodelPins.size(), outputCount) * pinDistance);
- if (oldOutputCount > outputCount)
- while (outputSupermodelPins.size() > outputCount)
- {
- outputSubmodelPins.remove(outputCount);
- removePin(outputSupermodelPins.get(outputCount));
- }
- else if (oldOutputCount < outputCount)
- for (int i = oldOutputCount; i < outputCount; i++)
- {
- // TODO pin names
- Pin submodelPin = addSubmodelInterface("Output pin #" + i, logicWidth, width, pinDistance / 2 + i * pinDistance);
- outputSubmodelPins.add(submodelPin);
- outputSupermodelPins.add(getSupermodelPin(submodelPin));
- }
+ setIOPins(width, outputPinNames, inputPinNames, pinNames);
}
- public List<Pin> getInputPins()
+ private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
{
- return inputSupermodelPinsUnmodifiable;
+ int inputCount = newPinNames.length;
+ List<String> newPinNamesList = Arrays.asList(newPinNames);
+ if (new HashSet<>(newPinNamesList).size() != inputCount)
+ throw new IllegalArgumentException("Pin names contain duplicates");
+ for (String pinName : newPinNamesList)
+ if (pinNamesListOtherSide.contains(pinName))
+ throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
+ super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
+ for (int i = 0; i < inputCount; i++)
+ {
+ String pinName = newPinNames[i];
+ int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
+ if (oldPinIndex == -1)
+ super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
+ else
+ getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
+ }
+ for (String pinName : pinNamesListThisSide)
+ if (!newPinNamesList.contains(pinName))
+ super.removeSubmodelInterface(pinName);
+ pinNamesListThisSide.clear();
+ pinNamesListThisSide.addAll(newPinNamesList);
}
- public List<Pin> getOutputPins()
+ public List<String> getInputPinNames()
{
- return outputSupermodelPinsUnmodifiable;
+ return inputPinNamesUnmodifiable;
}
- protected List<Pin> getInputSubmodelPins()
+ public List<String> getOutputPinNames()
{
- return inputSubmodelPinsUnmodifiable;
- }
-
- protected List<Pin> getOutputSubmodelPins()
- {
- return outputSubmodelPinsUnmodifiable;
+ return outputPinNamesUnmodifiable;
}
@Override
protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
{
- double posX = getBounds().x;
- double posY = getBounds().y;
-
Font oldFont = gc.getFont();
- Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
- gc.setFont(labelFont);
+ gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
Point textExtent = gc.textExtent(label);
- gc.drawText(label, posX + (getBounds().width - textExtent.x) / 2, posY + (getBounds().height - textExtent.y) / 2, true);
+ Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
+ if (textColor != null)
+ gc.setForeground(textColor);
+ gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
+ gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
+ for (int i = 0; i < inputPinNames.size(); i++)
+ {
+ String pinName = inputPinNames.get(i);
+ textExtent = gc.textExtent(pinName);
+ gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
+ }
+ for (int i = 0; i < outputPinNames.size(); i++)
+ {
+ String pinName = outputPinNames.get(i);
+ textExtent = gc.textExtent(pinName);
+ gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
+ getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
+ }
gc.setFont(oldFont);
- // TODO draw pin names
}
@Override
protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
{
+ Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
+ if (foreground != null)
+ gc.setForeground(foreground);
gc.drawRectangle(getBounds());
}
ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
Map<String, Object> m = new TreeMap<>();
m.put(kLabel, label);
- m.put(kInCount, inputSupermodelPins.size());
- m.put(kOutCount, outputSupermodelPins.size());
+ m.put(kInCount, inputPinNames.toArray());
+ m.put(kOutCount, outputPinNames.toArray());
m.put(kLogicWidth, logicWidth);
ret.specialized = m;
return ret;
}
+
+ @Override
+ protected Pin addSubmodelInterface(MovablePin supermodelPin)
+ {
+ throw new UnsupportedOperationException(
+ "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
+ }
+
+ @Override
+ protected void removeSubmodelInterface(String name)
+ {
+ throw new UnsupportedOperationException(
+ "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
+ }
+
+ @Override
+ protected void setSize(double width, double height)
+ {
+ throw new UnsupportedOperationException(
+ "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
+ }
}
\ No newline at end of file