Merge branch 'fusebug' into development
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / submodels / SimpleRectangularSubmodelComponent.java
1 package net.mograsim.logic.model.model.components.submodels;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8
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;
16
17 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
18 {
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;
24
25         public final String label;
26         protected final int logicWidth;
27
28         private final List<String> inputPinNames;
29         private final List<String> inputPinNamesUnmodifiable;
30         private final List<String> outputPinNames;
31         private final List<String> outputPinNamesUnmodifiable;
32
33         public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
34         {
35                 this(model, logicWidth, label, null);
36         }
37
38         public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label, String name)
39         {
40                 super(model, name);
41                 this.label = label;
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);
47
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));
56         }
57
58         protected void setInputPins(String... pinNames)
59         {
60                 setIOPins(0, inputPinNames, outputPinNames, PinUsage.INPUT, pinNames);
61         }
62
63         protected void setOutputPins(String... pinNames)
64         {
65                 setIOPins(width, outputPinNames, inputPinNames, PinUsage.OUTPUT, pinNames);
66         }
67
68         private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, PinUsage usage,
69                         String... newPinNames)
70         {
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++)
80                 {
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));
85                         else
86                                 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
87                 }
88                 for (String pinName : pinNamesListThisSide)
89                         if (!newPinNamesList.contains(pinName))
90                                 super.removeSubmodelInterface(pinName);
91                 pinNamesListThisSide.clear();
92                 pinNamesListThisSide.addAll(newPinNamesList);
93         }
94
95         public List<String> getInputPinNames()
96         {
97                 return inputPinNamesUnmodifiable;
98         }
99
100         public List<String> getOutputPinNames()
101         {
102                 return outputPinNamesUnmodifiable;
103         }
104
105         @Override
106         protected Pin addSubmodelInterface(MovablePin supermodelPin)
107         {
108                 throw new UnsupportedOperationException(
109                                 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
110         }
111
112         @Override
113         protected void removeSubmodelInterface(String name)
114         {
115                 throw new UnsupportedOperationException(
116                                 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
117         }
118
119         @Override
120         protected void setSize(double width, double height)
121         {
122                 throw new UnsupportedOperationException(
123                                 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
124         }
125 }